-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_test.go
149 lines (116 loc) · 3.23 KB
/
load_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
package grocery
import (
"testing"
"time"
)
type StringAlias string
type LoadTestModel struct {
Base
StringVal string
IntVal int
UInt16Val uint16
BoolVal bool
Float32Val float32
Float64Val float64
StringAliasVal StringAlias
MapVal *Map
SetVal *Set
}
func TestLoad(t *testing.T) {
mapData := map[string]string{"a": "b", "c": "d"}
setData := []string{"a", "b"}
model := &LoadTestModel{
StringVal: "hello world",
IntVal: 4,
UInt16Val: 3,
BoolVal: true,
Float32Val: 3.5,
Float64Val: 3.9,
StringAliasVal: "asdf",
MapVal: NewMap(mapData),
SetVal: NewSet(setData),
}
id, err := Store(model)
storeTime := time.Now()
if err != nil {
t.Error(err)
}
loadedModel := new(LoadTestModel)
err = Load(id, loadedModel)
if err != nil {
t.Error(err)
}
if loadedModel.ID != id {
t.Errorf("id FAILED, expected %s but got %s", id, model.ID)
}
if time.Since(loadedModel.CreatedAt) < time.Since(storeTime) {
t.Errorf("createdAt FAILED")
}
if time.Since(loadedModel.UpdatedAt) < time.Since(storeTime) {
t.Errorf("updatedAt FAILED")
}
if model.StringVal != loadedModel.StringVal {
t.Errorf("string FAILED, expected %s but got %s", model.StringVal, loadedModel.StringVal)
}
if model.IntVal != loadedModel.IntVal {
t.Errorf("int FAILED, expected %d but got %d", model.IntVal, loadedModel.IntVal)
}
if model.UInt16Val != loadedModel.UInt16Val {
t.Errorf("uint16 FAILED, expected %d but got %d", model.UInt16Val, loadedModel.UInt16Val)
}
if model.BoolVal != loadedModel.BoolVal {
t.Errorf("bool FAILED, expected %t but got %t", model.BoolVal, loadedModel.BoolVal)
}
if model.Float32Val != loadedModel.Float32Val {
t.Errorf("float32 FAILED, expected %f but got %f", model.Float32Val, loadedModel.Float32Val)
}
if model.Float64Val != loadedModel.Float64Val {
t.Errorf("float64 FAILED, expected %f but got %f", model.Float32Val, loadedModel.Float32Val)
}
if model.StringAliasVal != loadedModel.StringAliasVal {
t.Errorf("string alias FAILED, expected %s but got %s", model.StringAliasVal, loadedModel.StringAliasVal)
}
for k, v := range mapData {
loadedValue, ok := loadedModel.MapVal.Load(k)
if !ok {
t.Errorf("map value FAILED, key %s does not exist", k)
continue
}
if v != loadedValue.(string) {
t.Errorf("map value FAILED, expected m[%s]=%s but got m[%s]=%s", k, v, k, loadedValue.(string))
}
}
for _, k := range setData {
if !loadedModel.SetVal.Contains(k) {
t.Errorf("set value FAILED, key %s does not exist", k)
}
}
}
func TestLoadMissing(t *testing.T) {
model := new(LoadTestModel)
err := Load("asdf", model)
if err == nil {
t.Error("Should throw error for missing models but did not")
}
}
func TestLoadAll(t *testing.T) {
model := &LoadTestModel{
StringVal: "hello world",
IntVal: 4,
BoolVal: true,
StringAliasVal: "asdf",
MapVal: NewMap(map[string]string{"a": "b", "c": "d"}),
}
id, err := Store(model)
if err != nil {
t.Error(err)
}
loadedModels := make([]LoadTestModel, 1)
err = LoadAll([]string{id}, &loadedModels)
if err != nil {
t.Error(err)
}
if loadedModels[0].ID != id {
t.Errorf("id FAILED, expected %s but got %s", id, model.ID)
}
}