-
Notifications
You must be signed in to change notification settings - Fork 62
/
types_test.go
99 lines (77 loc) · 1.95 KB
/
types_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package dat
import (
"encoding/json"
"testing"
"time"
"github.com/lib/pq"
"gopkg.in/stretchr/testify.v1/assert"
)
func TestNullStringFrom(t *testing.T) {
v := "foo"
n := NullStringFrom(v)
assert.True(t, n.Valid)
assert.Equal(t, n.String, v)
}
func TestNullFloat64From(t *testing.T) {
v := 42.2
n := NullFloat64From(v)
assert.True(t, n.Valid)
assert.Equal(t, n.Float64, v)
}
func TestNullInt64From(t *testing.T) {
v := int64(400)
n := NullInt64From(v)
assert.True(t, n.Valid)
assert.Equal(t, n.Int64, v)
}
func TestNullTimeFrom(t *testing.T) {
v := time.Now()
n := NullTimeFrom(v)
assert.True(t, n.Valid)
assert.Equal(t, n.Time, v)
}
func TestNullBoolFrom(t *testing.T) {
v := false
n := NullBoolFrom(v)
assert.True(t, n.Valid)
assert.Equal(t, n.Bool, v)
}
func TestInvalidNullTime(t *testing.T) {
n := NullTime{pq.NullTime{Valid: false}}
assert.False(t, n.Valid)
var when time.Time
assert.Equal(t, n.Time, when)
}
func TestJSONFromString(t *testing.T) {
type foo struct {
Jason JSON `json:"jason"`
NoValue JSON `json:"noValue"`
}
j := foo{Jason: JSONFromString(`{"fruit":"apple"}`)}
b, err := json.Marshal(j)
if err != nil {
t.Error("Expected struct with JSON string fields to marshal", err)
}
if string(b) != `{"jason":{"fruit":"apple"},"noValue":null}` {
t.Error("Expected JSON to defaul to null", string(b))
}
}
func TestNullMarshalling(t *testing.T) {
type nully struct {
Int NullInt64 `json:"int"`
Intp *NullInt64 `json:"intp"`
Intv NullInt64 `json:"intv"`
Time NullTime `json:"time"`
Timep *NullTime `json:"timep"`
Jason JSON `json:"jason"`
Jasonp *JSON `json:"jasonp"`
}
a := nully{Intv: NullInt64From(100)}
b, err := json.Marshal(a)
if err != nil {
t.Error("Expected struct with null fields to marshal", err)
}
if string(b) != `{"int":null,"intp":null,"intv":100,"time":null,"timep":null,"jason":null,"jasonp":null}` {
t.Error("Expected nulltypes to defaul to null", string(b), err)
}
}