-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnullable_test.go
83 lines (68 loc) · 2.5 KB
/
nullable_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package nullable_test
import (
"encoding/json"
"testing"
"time"
"github.com/ovya/nullable"
"github.com/stretchr/testify/assert"
)
type Test[T any] struct {
ID int64 `json:"id"`
Name *nullable.Of[string] `json:"name"`
DateTo *nullable.Of[time.Time] `json:"dateTo"`
Data *nullable.Of[T] `json:"data"`
}
type testedType = struct {
String string `json:"string"`
Bool *nullable.Of[bool] `json:"bool"`
Int int64 `json:"int"`
JSON *nullable.Of[nullable.JSON] `json:"json"`
}
func TestMarshalUnmarshal(t *testing.T) {
data1 := testedType{
String: "a string",
Bool: nullable.FromValue(true),
Int: 768,
}
data1.JSON = nullable.FromValue[nullable.JSON](data1)
name := "PLOP"
now := time.Now()
obj1 := Test[testedType]{
Name: nullable.FromValue(name),
DateTo: nullable.FromValue(now),
Data: nullable.FromValue(data1),
}
obj2 := Test[testedType]{
Name: nullable.Null[string](), // Null value
DateTo: nullable.Null[time.Time](), // Null value
Data: nullable.Null[testedType](), // Null value
}
obj := []Test[testedType]{obj1, obj2}
b, err := json.Marshal(obj)
if !assert.Nil(t, err, "Marshaling Nullable data failed") {
panic(err)
}
toObj := []Test[testedType]{{}, {}}
err = json.Unmarshal(b, &toObj)
if !assert.Nil(t, err, "Unmarshaling into Nullable data failed") {
panic(err)
}
for i := 0; i < 2; i++ {
assert.Equal(t, obj[i].Name.GetValue(), toObj[i].Name.GetValue(), "Marshaling and Unmarshaling does not return the same value")
dte := toObj[i].DateTo.GetValue()
if dte == nil {
assert.Nil(t, obj[i].DateTo.GetValue(), "Marshaling and Unmarshaling nil value mismatch.")
} else {
assert.Equal(t, time.Duration(0), dte.Sub(now), "Marshaling and Unmarshaling does not return the same value")
}
data := obj[i].Data.GetValue()
if data == nil {
assert.Nil(t, toObj[i].Data.GetValue(), "Marshaling and Unmarshaling nil value mismatch")
} else {
assert.Equal(t, data.Bool.GetValue(), toObj[i].Data.GetValue().Bool.GetValue(), "Marshaling and Unmarshaling does not return the same value")
assert.Equal(t, data.Int, toObj[i].Data.GetValue().Int, "Marshaling and Unmarshaling does not return the same value")
assert.Equal(t, data.String, toObj[i].Data.GetValue().String, "Marshaling and Unmarshaling does not return the same value")
assert.Equal(t, data.String, toObj[i].Data.GetValue().String, "Marshaling and Unmarshaling does not return the same value")
}
}
}