-
Notifications
You must be signed in to change notification settings - Fork 31
/
meddler_test.go
153 lines (135 loc) · 3.73 KB
/
meddler_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package meddler
import (
"testing"
)
type ItemJson struct {
ID int64 `meddler:"id,pk"`
Stuff map[string]bool `meddler:"stuff,json"`
StuffZ map[string]bool `meddler:"stuffz,jsongzip"`
}
type ItemGob struct {
ID int64 `meddler:"id,pk"`
Stuff map[string]bool `meddler:"stuff,gob"`
StuffZ map[string]bool `meddler:"stuffz,gobgzip"`
}
type ItemZeroes struct {
ID int64 `meddler:"id,pk"`
Int int `meddler:"nullint,zeroisnull"`
Float float64 `meddler:"nullfloat,zeroisnull"`
Complex complex128 `meddler:"nullcomplex,zeroisnull"`
String string `meddler:"nullstring,zeroisnull"`
Bool bool `meddler:"nullbool,zeroisnull"`
}
func TestZeroIsNullMeddler(t *testing.T) {
once.Do(setup)
before := &ItemZeroes{}
if err := Save(db, "null_item", before); err != nil {
t.Errorf("Save error: %v", err)
}
id := before.ID
after := new(ItemZeroes)
if err := Load(db, "null_item", after, id); err != nil {
t.Errorf("Load error: %v", err)
}
if before.String != after.String {
t.Errorf("before.String: expected %s, got %s", before.String, after.String)
}
if before.Int != after.Int {
t.Errorf("before.Int: expected %d, got %d", before.Int, after.Int)
}
if before.Float != after.Float {
t.Errorf("before.Float: expected %#v, got %#v", before.Float, after.Float)
}
if before.Bool != after.Bool {
t.Errorf("before.Bool: expected %#v, got %#v", before.Bool, after.Bool)
}
if before.Complex != after.Complex {
t.Errorf("before.Complex: expected %#v, got %#v", before.Complex, after.Complex)
}
}
func TestJsonMeddler(t *testing.T) {
once.Do(setup)
// save a value
elt := &ItemJson{
ID: 0,
Stuff: map[string]bool{
"hello": true,
"world": true,
},
StuffZ: map[string]bool{
"goodbye": true,
"cruel": true,
"world": true,
},
}
if err := Save(db, "item", elt); err != nil {
t.Errorf("Save error: %v", err)
}
id := elt.ID
// load it again
elt = new(ItemJson)
if err := Load(db, "item", elt, id); err != nil {
t.Errorf("Load error: %v", err)
}
if elt.ID != id {
t.Errorf("expected id of %d, found %d", id, elt.ID)
}
if len(elt.Stuff) != 2 {
t.Errorf("expected %d items in Stuff, found %d", 2, len(elt.Stuff))
}
if !elt.Stuff["hello"] || !elt.Stuff["world"] {
t.Errorf("contents of stuff wrong: %v", elt.Stuff)
}
if len(elt.StuffZ) != 3 {
t.Errorf("expected %d items in StuffZ, found %d", 3, len(elt.StuffZ))
}
if !elt.StuffZ["goodbye"] || !elt.StuffZ["cruel"] || !elt.StuffZ["world"] {
t.Errorf("contents of stuffz wrong: %v", elt.StuffZ)
}
if _, err := db.Exec("delete from `item`"); err != nil {
t.Errorf("error wiping item table: %v", err)
}
}
func TestGobMeddler(t *testing.T) {
once.Do(setup)
// save a value
elt := &ItemGob{
ID: 0,
Stuff: map[string]bool{
"hello": true,
"world": true,
},
StuffZ: map[string]bool{
"goodbye": true,
"cruel": true,
"world": true,
},
}
if err := Save(db, "item", elt); err != nil {
t.Errorf("Save error: %v", err)
}
id := elt.ID
// load it again
elt = new(ItemGob)
if err := Load(db, "item", elt, id); err != nil {
t.Errorf("Load error: %v", err)
}
if elt.ID != id {
t.Errorf("expected id of %d, found %d", id, elt.ID)
}
if len(elt.Stuff) != 2 {
t.Errorf("expected %d items in Stuff, found %d", 2, len(elt.Stuff))
}
if !elt.Stuff["hello"] || !elt.Stuff["world"] {
t.Errorf("contents of stuff wrong: %v", elt.Stuff)
}
if len(elt.StuffZ) != 3 {
t.Errorf("expected %d items in StuffZ, found %d", 3, len(elt.StuffZ))
}
if !elt.StuffZ["goodbye"] || !elt.StuffZ["cruel"] || !elt.StuffZ["world"] {
t.Errorf("contents of stuffz wrong: %v", elt.StuffZ)
}
if _, err := db.Exec("delete from `item`"); err != nil {
t.Errorf("error wiping item table: %v", err)
}
}