/
schema_spec.cr
312 lines (266 loc) · 9.83 KB
/
schema_spec.cr
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
require "./spec_helper"
describe Crecto do
describe "Schema" do
describe "#schema and #field" do
it "should set default values" do
d = DefaultValue.new
d.default_string.should eq("should set default")
d.default_int.should eq(64)
d.default_float.should eq(3.14)
d.default_bool.should eq(false)
d.default_time.should_not be_nil
end
it "should set the table name" do
User.table_name.should eq("users")
end
it "should set the primary key" do
User.primary_key_field.should eq("id")
end
it "should set the changeset fields" do
User.changeset_fields.should eq([:name, :things, :smallnum, :nope, :yep, :some_date, :pageviews, :unique_field])
end
it "should set properties for the fields" do
u = User.new
u.name = "fridge"
u.things = 123
u.stuff = 543
u.nope = 3.343
u.yep = false
u.pageviews = 123451651234
u.name.should eq("fridge")
u.things.should eq(123)
u.stuff.should eq(543)
u.nope.should eq(3.343)
u.yep.should eq(false)
u.pageviews.should eq(123451651234)
u.name!.should eq("fridge")
typeof(u.name!).should eq(String)
typeof(u.name).should eq((Nil | String))
end
it "should work with empty blocks" do
ThingWithoutFields.new
end
describe "changing default values" do
it "should set properties for the values" do
u = UserDifferentDefaults.new
now = Time.local
u.xyz = now
u.to_query_hash.should eq({:name => nil, :xyz => now})
UserDifferentDefaults.primary_key_field.should eq("user_id")
end
it "should set the created at field" do
UserDifferentDefaults.created_at_field.should eq("xyz")
end
end
end
describe "#enum_field" do
it "should define a field for the enum" do
v = Vehicle.new
v.state = Vehicle::State::RUNNING
v.state.should eq(Vehicle::State::RUNNING)
end
it "should define a column field for the enum" do
v = Vehicle.new
v.state = Vehicle::State::RUNNING
v.state_string.should be_a(String)
end
it "should accept a user-specified column field name" do
v = Vehicle.new
v.make = Vehicle::Make::SEDAN
v.vehicle_type.should eq(Vehicle::Make::SEDAN.value)
end
it "should accept a type-override for the column field" do
v = Vehicle.new
v.make = Vehicle::Make::SEDAN
v.vehicle_type.should be_a(Int32)
end
it "should update the column field when the enum field is set" do
v = Vehicle.new
v.state = Vehicle::State::RUNNING
v.state_string.should eq("RUNNING")
v.state = Vehicle::State::OFF
v.state_string.should eq("OFF")
end
it "should save the column field to the database" do
v = Vehicle.new
v.state = Vehicle::State::RUNNING
v.make = Vehicle::Make::SEDAN
id = Repo.insert(v).instance.id
vehicle = Repo.get!(Vehicle, id)
vehicle.state_string.should eq("RUNNING")
vehicle.vehicle_type.should eq(Vehicle::Make::SEDAN.value)
end
it "should parse the enum field value from the column name" do
v = Vehicle.new
v.state = Vehicle::State::RUNNING
v.make = Vehicle::Make::SEDAN
id = Repo.insert(v).instance.id
vehicle = Repo.get!(Vehicle, id)
vehicle.state.should eq(Vehicle::State::RUNNING)
vehicle.make.should eq(Vehicle::Make::SEDAN)
end
end
describe "#belongs_to" do
it "should define a nilable accessor for the association" do
post = Post.new
post.user?.should eq(nil)
end
it "should define a non-nilable accessor for the association" do
post = Post.new
user = User.new
post.user = user
post.user.should eq(user)
typeof(post.user).should eq(User)
end
it "allows the foreign_id to be a String" do
user = UserUUIDCustom.new
thing = ThingThatBelongsToUserUUIDCustom.new
thing.user_uuid_custom = user
thing.user_uuid_custom.should eq(user)
typeof(thing.user_uuid_custom_id).should eq(PkeyValue)
end
end
describe "#has_one" do
it "should define a nilable accessor for the association" do
user = User.new
user.post?.should eq(nil)
end
it "should define a non-nilable accessor for the association" do
user = User.new
post = Post.new
user.post = post
user.post.should eq(post)
typeof(user.post).should eq(Post)
end
end
describe "#has_many" do
it "should define a nilable accessor for the association" do
user = User.new
user.posts?.should eq(nil)
end
it "should define a non-nilable accessor for the association" do
user = User.new
post = Post.new
user.posts = [post]
user.posts.should eq([post])
typeof(user.posts).should eq(Array(Post))
end
end
describe "#to_query_hash" do
it "should build the correct hash from the object" do
u = User.new
u.name = "tester"
u.things = 6644
u.stuff = 2343 # virtual, shouldn't be in query hash
u.nope = 34.9900
u.pageviews = 1234567890
u.to_query_hash.should eq({:name => "tester", :things => 6644, :smallnum => nil, :nope => 34.99, :yep => nil, :some_date => nil, :pageviews => 1234567890, :unique_field => nil, :created_at => nil, :updated_at => nil})
end
it "should return virtual attributes if `include_virtual` is passed as true" do
u = User.new
u.name = "tester"
u.things = 6644
u.stuff = 2343
u.nope = 34.9900
u.pageviews = 1234567890
u.to_query_hash(true).should eq({:name => "tester", :things => 6644, :stuff => 2343, :smallnum => nil, :nope => 34.99, :yep => nil, :some_date => nil, :pageviews => 1234567890, :unique_field => nil, :created_at => nil, :updated_at => nil})
end
end
describe "#pkey_value" do
it "should return the value of the primary key" do
user = UserDifferentDefaults.new
user.user_id = 8858
user.pkey_value.as(Int32).should eq(user.user_id)
end
# TODO: fixme
# it "allows the primary key to be a string" do
# user = UserUUIDCustom.new
# user.name = "whatever"
# # Need to set this because of MySQL and SQLite
# # MySQL actually inserts the uuid because of the trigger,
# # but the `instance` method below seems to return the object before the trigger is fired.
# # SQLite doesn't seem to have native UUID out of the box support
# user.id = UUID.random.to_s
# user = Repo.insert(user).instance
# user.pkey_value.as(String).should eq(user.id)
# end
end
describe "#update_primary_key" do
it "should update the value of the primary key" do
user = UserDifferentDefaults.new
user.update_primary_key(9899)
user.user_id.should eq(9899)
end
it "should update the value of the primary key for bigger sizes" do
user = UserLargeDefaults.new
user.update_primary_key(9899)
user.id.should eq(9899)
end
end
describe "#updated_at_value" do
it "should return the updated at value" do
now = Time.local
u = User.new
u.updated_at = now
u.updated_at_value.should eq(now)
end
end
describe "#created_at_value" do
it "should return the created at value" do
now = Time.local
u = UserDifferentDefaults.new
u.xyz = now
u.created_at_value.should eq(now)
end
end
describe "#updated_at_to_now" do
it "should set the updated at value to now" do
u = User.new
u.updated_at_to_now
u.updated_at.as(Time).to_unix_ms.should be_close(Time.local.to_unix_ms, 100)
end
end
describe "#created_at_to_now" do
it "should set the created at value to now" do
u = UserDifferentDefaults.new
u.created_at_to_now
u.xyz.as(Time).to_unix_ms.should be_close(Time.local.to_unix_ms, 2000)
end
end
describe "#klass_for_association" do
it "should return the correct class" do
User.klass_for_association(:posts).should eq(Post)
User.klass_for_association(:addresses).should eq(Address)
UserDifferentDefaults.klass_for_association(:things).should eq(Thing)
Address.klass_for_association(:user).should eq(User)
Post.klass_for_association(:user).should eq(User)
end
end
describe "#foreign_key_for_association" do
it "should return the correct foreign key symbol" do
User.foreign_key_for_association(:posts).should eq(:user_id)
User.foreign_key_for_association(:addresses).should eq(:user_id)
Post.foreign_key_for_association(:user).should eq(:user_id)
end
end
describe "#foreign_key_value_for_association" do
it "should return the correct foreign key value for associations" do
user = User.new
user.name = "tester"
user = Repo.insert(user).instance
post = Post.new
post.user_id = user.id
post = Repo.insert(post).instance
User.foreign_key_value_for_association(:posts, post).should eq(post.user_id)
Post.foreign_key_value_for_association(:user, post).should eq(user.id)
end
end
describe "#association_type_for_association" do
it "should return the association type symbol" do
User.association_type_for_association(:posts).should eq(:has_many)
User.association_type_for_association(:addresses).should eq(:has_many)
Post.association_type_for_association(:user).should eq(:belongs_to)
end
end
end
end