-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel_test.go
217 lines (201 loc) · 6.04 KB
/
model_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package apifaker
import (
. "github.com/smartystreets/goconvey/convey"
"os"
"testing"
)
var testRouter = &Router{apiFaker: &ApiFaker{}}
func validModel(name string) *Model {
faker, _ := NewWithApiDir(testDir)
return faker.Routers[name].Model
}
func validUserModel() *Model {
return validModel("users")
}
func validBookModel() *Model {
return validModel("books")
}
func TestModel(t *testing.T) {
model, err := NewModelWithPath(testDir+"/users.json", testRouter)
Describ("NewModelWithPath", t, func() {
It("allocates and returns a new Model", func() {
Expect(err, ShouldBeNil)
Expect(model.currentId, ShouldEqual, 3)
Expect(model.dataChanged, ShouldBeFalse)
})
})
Describ("InsertRelatedData", t, func() {
model := validUserModel()
li, _ := model.Get(float64(1))
newLi := li.InsertRelatedData(model)
_, hasbooks := newLi.Get("books")
_, hasAvatar := newLi.Get("avatar")
It("inserts books and avatar columns into li", func() {
Expect(newLi.Len(), ShouldEqual, 6)
Expect(hasbooks, ShouldBeTrue)
Expect(hasAvatar, ShouldBeTrue)
})
})
Describ("Uniqueness", t, func() {
Context("when check a user name already exists", func() {
model := validBookModel()
li, _ := model.Get(float64(1))
titleColumn := model.Columns[1]
name, _ := li.Get("title")
It("returns error", func() {
Expect(titleColumn.CheckValue(name, model), ShouldNotBeNil)
})
})
Context("when check a title which has been edited", func() {
model := validBookModel()
li, _ := model.Get(float64(1))
titleColumn := model.Columns[1]
oldName, _ := li.Get("title")
newLi := NewLineItemWithMap(li.ToMap())
newLi.Set("title", "XXX")
model.Update(float64(1), &newLi)
It("returns nil error", func() {
Expect(titleColumn.CheckValue(oldName, model), ShouldBeNil)
})
})
})
Describ("CheckRelationshipsMeta", t, func() {
Context("when has_many has repeated elements", func() {
model := validUserModel()
model.HasMany = append(model.HasMany, model.HasMany...)
It("returns error ", func() {
Expect(model.CheckRelationshipsMeta(), ShouldNotBeNil)
})
})
Context("when has_one has repeated elements", func() {
model := validUserModel()
model.HasOne = append(model.HasOne, model.HasMany...)
It("returns error ", func() {
Expect(model.CheckRelationshipsMeta(), ShouldNotBeNil)
})
})
})
Describ("CheckRelationships", t, func() {
Context("when has_one or has_many has unknown resources", func() {
model := validUserModel()
model.HasMany = append(model.HasMany, "foo")
It("returns error ", func() {
Expect(model.CheckRelationships(), ShouldNotBeNil)
})
})
Context("when has_one or has_one has unknown resources", func() {
model := validUserModel()
model.HasOne = append(model.HasOne, "foo")
It("returns error ", func() {
Expect(model.CheckRelationships(), ShouldNotBeNil)
})
})
})
Describ("CheckColumnsMeta", t, func() {
Context("when first item is not id", func() {
It("returns error", func() {
model := validUserModel()
model.Columns[0] = &Column{Name: "idea"}
Expect(model.CheckColumnsMeta(), ShouldNotBeNil)
})
})
Context("when id's type is nil", func() {
It("returns error", func() {
model := validUserModel()
model.Columns[0] = &Column{Name: "id"}
Expect(model.CheckColumnsMeta(), ShouldNotBeNil)
})
})
Context("when name is nil", func() {
It("returns error", func() {
model := validUserModel()
model.Columns[1].Name = ""
Expect(model.CheckColumnsMeta(), ShouldNotBeNil)
})
})
Context("when type is nil", func() {
It("returns error", func() {
model := validUserModel()
model.Columns[1].Type = ""
Expect(model.CheckColumnsMeta(), ShouldNotBeNil)
})
})
Context("when type is not in jsonTypes", func() {
It("returns error", func() {
model := validUserModel()
model.Columns[1].Type = "xxx"
Expect(model.CheckColumnsMeta(), ShouldNotBeNil)
})
})
Context("when regexp format is wrong", func() {
It("returns error", func() {
model := validUserModel()
model.Columns[0] = &Column{Name: "id", Type: "number", RegexpPattern: "(?=)"}
Expect(model.CheckColumnsMeta(), ShouldNotBeNil)
})
})
})
Describ("ValidateSeedsValue", t, func() {
Context("when has wrong columns count", func() {
It("returns error", func() {
model := validUserModel()
delete(model.Seeds[0], "id")
Expect(model.ValidateSeedsValue(), ShouldNotBeNil)
})
})
Context("when has wrong colum", func() {
It("returns error", func() {
model := validUserModel()
delete(model.Seeds[0], "id")
model.Seeds[0]["xxx"] = true
Expect(model.ValidateSeedsValue(), ShouldNotBeNil)
})
})
Context("when type is wrong", func() {
It("returns error", func() {
model := validUserModel()
model.Seeds[0]["name"] = 1010
Expect(model.ValidateSeedsValue(), ShouldNotBeNil)
})
})
Context("when doesn't much regexp pattern", func() {
It("returns error", func() {
model := validUserModel()
model.Seeds[0]["name"] = "~.~"
Expect(model.ValidateSeedsValue(), ShouldNotBeNil)
})
})
Context("when column value has been existed", func() {
It("returns error", func() {
model := validUserModel()
model.Seeds[0]["name"] = model.Seeds[1]["name"]
Expect(model.ValidateSeedsValue(), ShouldNotBeNil)
})
})
Context("when column has a inexistent foreign key", func() {
It("returns error", func() {
model := validBookModel()
model.Seeds[0]["title"] = "On the way"
model.Seeds[0]["user_id"] = float64(100)
Expect(model.Validate(model.Seeds[0]), ShouldNotBeNil)
})
})
})
Describ("SaveToFile", t, func() {
model := validUserModel()
err := model.Add(LineItem{map[string]interface{}{
"id": float64(4),
"name": "Monica",
"phone": "12332132132",
"age": float64(21),
}})
It("set dataChanged to be true", func() {
Expect(model.dataChanged, ShouldBeTrue)
})
err = model.SaveToFile(testDir + "/users_temp.json.test")
defer os.Remove(testDir + "/users_temp.json.test")
It("saves to file", func() {
Expect(err, ShouldBeNil)
})
})
}