Skip to content

Commit 5996204

Browse files
committed
add test for unmarshal
Signed-off-by: Ashutosh Kumar <sonasingh46@gmail.com>
1 parent 1bbece8 commit 5996204

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

types/nullable_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,58 @@ func TestNullable_MarshalJSON(t *testing.T) {
405405
})
406406
}
407407
}
408+
409+
func TestNullable_UnmarshalJSON(t *testing.T) {
410+
type testCase struct {
411+
name string
412+
json []byte
413+
assert func(obj SimpleInt, t *testing.T)
414+
}
415+
tests := []testCase{
416+
{
417+
name: "when not set",
418+
json: []byte(`{}`),
419+
assert: func(obj SimpleInt, t *testing.T) {
420+
assert.Equalf(t, false, obj.ReplicaCount.Set, "replica count should not be set")
421+
assert.Equalf(t, false, obj.ReplicaCount.Null, "replica count should not be null")
422+
},
423+
},
424+
425+
{
426+
name: "when explicity set to zero value",
427+
json: []byte(`{"replicaCount":0}`),
428+
assert: func(obj SimpleInt, t *testing.T) {
429+
assert.Equalf(t, true, obj.ReplicaCount.Set, "replica count should be set")
430+
assert.Equalf(t, false, obj.ReplicaCount.Null, "replica count should not be null")
431+
assert.Equalf(t, 0, obj.ReplicaCount.Value, "replica count value should be 0l")
432+
},
433+
},
434+
435+
{
436+
name: "when explicity set to null value",
437+
json: []byte(`{"replicaCount":null}`),
438+
assert: func(obj SimpleInt, t *testing.T) {
439+
assert.Equalf(t, true, obj.ReplicaCount.Set, "replica count should be set")
440+
assert.Equalf(t, true, obj.ReplicaCount.Null, "replica count should be null")
441+
},
442+
},
443+
444+
{
445+
name: "when explicity set to a specific value",
446+
json: []byte(`{"replicaCount":5}`),
447+
assert: func(obj SimpleInt, t *testing.T) {
448+
assert.Equalf(t, true, obj.ReplicaCount.Set, "replica count should be set")
449+
assert.Equalf(t, false, obj.ReplicaCount.Null, "replica count should not be null")
450+
assert.Equalf(t, 5, obj.ReplicaCount.Value, "replica count value should be 0l")
451+
},
452+
},
453+
}
454+
for _, tt := range tests {
455+
t.Run(tt.name, func(t *testing.T) {
456+
var obj SimpleInt
457+
err := json.Unmarshal(tt.json, &obj)
458+
require.NoError(t, err)
459+
tt.assert(obj, t)
460+
})
461+
}
462+
}

0 commit comments

Comments
 (0)