Files
bookhoard/internal/handlers/series_test.go
T
John O'Keefe 188ae076ab refactor(utils): one PgTextString for the four pgtype.Text unwrappers
handlers textToString, router textToString, router getText, and utils
normText were four copies of the same NULL-to-empty unwrap (99 call
sites). One exported utils.PgTextString replaces them all; the private
normText (comparison normalizer) stays but is now the odd one out.

Harness fix found on the way: setupTestServer built a full wiring
literal for route registration and then RETURNED a second, sparser
literal that never copied AnnotationService — setup.AnnotationService
was always nil on the struct every test receives. Nobody noticed until
cross_library_move_test became the first caller (it previously used
the now-retired plain CreateMediaHighlight query); it now exercises
the real SaveHighlight path through the wired service.
2026-10-17 16:36:58 -04:00

48 lines
1.0 KiB
Go

package handlers
import (
"testing"
"bookhoard/internal/utils"
"github.com/jackc/pgx/v5/pgtype"
"github.com/stretchr/testify/assert"
)
func TestNewSeriesHandler_NilDB(t *testing.T) {
handler := NewSeriesHandler(nil)
assert.NotNil(t, handler, "Handler should not be nil even with nil DB")
assert.NotNil(t, handler.seriesService, "Internal service should be initialized")
}
func TestSeriesHandler_TextToStringConversion(t *testing.T) {
tests := []struct {
name string
input pgtype.Text
expected string
}{
{
name: "valid author text",
input: pgtype.Text{String: "Brandon Sanderson", Valid: true},
expected: "Brandon Sanderson",
},
{
name: "empty valid text",
input: pgtype.Text{String: "", Valid: true},
expected: "",
},
{
name: "null text returns empty",
input: pgtype.Text{String: "ignored", Valid: false},
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := utils.PgTextString(tt.input)
assert.Equal(t, tt.expected, result)
})
}
}