forked from russross/meddler
-
Notifications
You must be signed in to change notification settings - Fork 2
/
meddler_test.go
114 lines (101 loc) · 2.63 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
package meddler
import (
"testing"
"context"
)
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"`
}
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(context.Background(), db, "item", elt); err != nil {
t.Errorf("Save error: %v", err)
}
id := elt.ID
// load it again
elt = new(ItemJson)
if err := Load(context.Background(),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(context.Background(),db, "item", elt); err != nil {
t.Errorf("Save error: %v", err)
}
id := elt.ID
// load it again
elt = new(ItemGob)
if err := Load(context.Background(),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)
}
}