Skip to content

Commit 7016e79

Browse files
committed
test(null): expand test coverage for NullUUID
1 parent 6d9254e commit 7016e79

File tree

1 file changed

+78
-20
lines changed

1 file changed

+78
-20
lines changed

null_test.go

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,33 +103,47 @@ func TestNullUUIDMarshalText(t *testing.T) {
103103

104104
func TestNullUUIDUnmarshalText(t *testing.T) {
105105
tests := []struct {
106-
nullUUID NullUUID
106+
name string
107+
data []byte
108+
expected NullUUID
109+
expectedErr bool
107110
}{
108111
{
109-
nullUUID: NullUUID{},
112+
name: "null text",
113+
data: []byte("null"),
114+
expected: NullUUID{Valid: false},
115+
expectedErr: true, // "null" is not a valid UUID format
110116
},
111117
{
112-
nullUUID: NullUUID{
113-
UUID: MustParse("12345678-abcd-1234-abcd-0123456789ab"),
114-
Valid: true,
115-
},
118+
name: "valid UUID",
119+
data: []byte("12345678-abcd-1234-abcd-0123456789ab"),
120+
expected: NullUUID{UUID: MustParse("12345678-abcd-1234-abcd-0123456789ab"), Valid: true},
121+
expectedErr: false,
122+
},
123+
{
124+
name: "invalid text",
125+
data: []byte("invalid"),
126+
expected: NullUUID{Valid: false},
127+
expectedErr: true,
116128
},
117129
}
130+
118131
for _, test := range tests {
119-
var uText []byte
120-
var uErr error
121-
nuText, nuErr := test.nullUUID.MarshalText()
122-
if test.nullUUID.Valid {
123-
uText, uErr = test.nullUUID.UUID.MarshalText()
124-
} else {
125-
uText = []byte("null")
126-
}
127-
if nuErr != uErr {
128-
t.Errorf("expected error %e, got %e", nuErr, uErr)
129-
}
130-
if !bytes.Equal(nuText, uText) {
131-
t.Errorf("expected text data %s, got %s", string(nuText), string(uText))
132-
}
132+
t.Run(test.name, func(t *testing.T) {
133+
var nu NullUUID
134+
err := nu.UnmarshalText(test.data)
135+
if (err != nil) != test.expectedErr {
136+
t.Errorf("expected error %v, got %v", test.expectedErr, err)
137+
}
138+
if !test.expectedErr {
139+
if nu.Valid != test.expected.Valid {
140+
t.Errorf("expected Valid %v, got %v", test.expected.Valid, nu.Valid)
141+
}
142+
if nu.Valid && nu.UUID != test.expected.UUID {
143+
t.Errorf("expected UUID %v, got %v", test.expected.UUID, nu.UUID)
144+
}
145+
}
146+
})
133147
}
134148
}
135149

@@ -165,6 +179,50 @@ func TestNullUUIDMarshalBinary(t *testing.T) {
165179
}
166180
}
167181

182+
func TestNullUUIDUnmarshalBinary(t *testing.T) {
183+
validUUID := MustParse("12345678-abcd-1234-abcd-0123456789ab")
184+
validData := validUUID[:]
185+
invalidData := []byte{1, 2, 3}
186+
187+
tests := []struct {
188+
name string
189+
data []byte
190+
expected NullUUID
191+
expectedErr bool
192+
}{
193+
{
194+
name: "valid data",
195+
data: validData,
196+
expected: NullUUID{UUID: validUUID, Valid: true},
197+
expectedErr: false,
198+
},
199+
{
200+
name: "invalid length",
201+
data: invalidData,
202+
expected: NullUUID{},
203+
expectedErr: true,
204+
},
205+
}
206+
207+
for _, test := range tests {
208+
t.Run(test.name, func(t *testing.T) {
209+
var nu NullUUID
210+
err := nu.UnmarshalBinary(test.data)
211+
if (err != nil) != test.expectedErr {
212+
t.Errorf("expected error %v, got %v", test.expectedErr, err)
213+
}
214+
if !test.expectedErr {
215+
if nu.Valid != test.expected.Valid {
216+
t.Errorf("expected Valid %v, got %v", test.expected.Valid, nu.Valid)
217+
}
218+
if nu.Valid && nu.UUID != test.expected.UUID {
219+
t.Errorf("expected UUID %v, got %v", test.expected.UUID, nu.UUID)
220+
}
221+
}
222+
})
223+
}
224+
}
225+
168226
func TestNullUUIDMarshalJSON(t *testing.T) {
169227
jsonNull, _ := json.Marshal(nil)
170228
jsonUUID, _ := json.Marshal(MustParse("12345678-abcd-1234-abcd-0123456789ab"))

0 commit comments

Comments
 (0)