-
Notifications
You must be signed in to change notification settings - Fork 4
/
bool_test.go
71 lines (53 loc) · 1.51 KB
/
bool_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
package null
import (
"database/sql"
"encoding/json"
"testing"
)
type testBool struct {
Value Bool `json:"value"`
}
func TestNewBool(t *testing.T) {
value := NewBool(true, true)
if !value.Bool || !value.Valid {
t.Fatal("New Bool must have value and be valid")
}
}
func TestMarshalBool(t *testing.T) {
value := Bool{sql.NullBool{Bool: true, Valid: true}}
if data, err := json.Marshal(value); err != nil || string(data) != "true" {
t.Fatalf("Bool must be marshalled to value, but was %v %v", err, string(data))
}
value.Valid = false
if data, err := json.Marshal(value); err != nil || string(data) != "null" {
t.Fatalf("Bool must be marshalled to null, but was %v %v", err, string(data))
}
}
func TestUnmarshalBool(t *testing.T) {
str := `{"value": true}`
var value testBool
if err := json.Unmarshal([]byte(str), &value); err != nil {
t.Fatalf("Bool must be unmarshalled to value, but was %v", err)
}
if !value.Value.Valid || !value.Value.Bool {
t.Fatalf("Unmarshalled null bool must be valid, but was %v", value.Value)
}
str = `{"value": null}`
if err := json.Unmarshal([]byte(str), &value); err != nil {
t.Fatalf("Bool must be unmarshalled to null, but was %v", err)
}
if value.Value.Valid {
t.Fatal("Unmarshalled null bool must be invalid")
}
}
func TestGettersSettersBool(t *testing.T) {
value := NewBool(true, true)
value.SetNil()
if value.Bool || value.Valid {
t.Fatal("Bool must be nil")
}
value.SetValid(true)
if !value.Bool || !value.Valid {
t.Fatal("Bool must be valid")
}
}