This repository has been archived by the owner on Mar 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
realm_spec.lua
427 lines (405 loc) · 15.2 KB
/
realm_spec.lua
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
---Disable undefined globals to account for globals
---provided by the testing library
---@diagnostic disable: undefined-global
---This forces the Lua language server to override
---the built-in definition of assert.
assert = assert
local uv = require "luv"
local Realm = require "realm"
require "realm.scheduler.libuv"
local LuaFileSystem = require "lfs"
local inspect = require "inspect"
assert:register("assertion", "is_realm_handle", function(_, arguments)
return getmetatable(arguments[1]).__name == "_realm_handle"
end)
local function makeTempPath()
local path = LuaFileSystem.currentdir()
return path .. "/test.realm"
end
---@param milliseconds integer
local function timeout(milliseconds)
local timer = uv.new_timer()
timer:start(milliseconds, 0, function ()
error("timeout")
timer:stop()
timer:close()
end)
return timer
end
local function post(cb)
local async
async = uv.new_async(function ()
cb()
async:close()
end)
async:send()
return async
end
---Helper function for cleaning up created objects
---@param realm Realm
---@param objects Realm.Object
local function _delete(realm, objects)
realm:write(function()
for _, object in ipairs(objects) do
realm:delete(object)
end
end)
end
---@class Pet:Realm.Object
---@field name string
---@field category string
---@class Person:Realm.Object
---@field name string
---@field age integer
---@field pet Pet
---@type Realm.Schema.ClassDefinition[]
local schema = {
{
name = "Person",
properties = {
name = "string",
age = "int",
pet = "Pet?",
pets = "Pet[]",
ints = "int[]",
petDictionary = "Pet{}?",
intDictionary = "int{}",
petSet = "Pet<>",
stringSet = "string<>"
}
},
{
name = "Pet",
properties = {
name = "string",
category = "string",
}
},
{
name = "PersonWithPK",
properties = {
name = "string",
age = "int"
},
primaryKey = "name"
}
}
describe("Realm Lua tests", function()
---@type Realm
local realm
local path
---@type Person
local testPerson
setup(function()
path = makeTempPath()
realm = Realm.open({ path = path, schemaVersion = 0, schema = schema})
realm:write(function()
testPerson = realm:create("Person", { name = "Peter", age = 3 })
end)
end)
teardown(function()
realm:write(function()
realm:delete(testPerson)
end)
realm:close()
os.remove(path)
end)
describe("Creating and modifying objects", function()
it("should set proper Realm handle at Realm open", function()
assert.is_realm_handle(realm._handle)
end)
it("should set correct properties at creation", function()
assert.is_realm_handle(testPerson._handle)
assert.are.equal(testPerson.name, "Peter")
assert.are.equal(testPerson.age, 3)
assert.are.equal(testPerson.pet, nil)
end)
it("should correctly change properties", function()
realm:write(function()
testPerson.name = "Aram"
testPerson.age = 100
end)
assert.are.equal(testPerson.name, "Aram")
assert.are.equal(testPerson.age, 100)
assert.are.equal(testPerson.pet, nil)
end)
describe("with references fields", function()
local testPet
setup(function()
realm:write(function()
testPet = realm:create("Pet", { name = "Oreo", category = "Cat" })
testPerson.pet = testPet
end)
end)
teardown(function() _delete(realm, {testPet}) end)
it("should set them correctly", function()
assert.is_realm_handle(testPerson.pet._handle)
assert.are.equal(testPerson.pet.name, testPet.name)
assert.are.equal(testPerson.pet.category, testPet.category)
end)
it("should update in sync with references", function()
realm:write(function()
testPet.category = "Dog"
testPet.name = "Thor"
end)
assert.are.equal(testPerson.pet.name, testPet.name)
assert.are.equal(testPerson.pet.category, testPet.category)
end)
end)
describe("with classes that have a primary key", function()
local testPersonPK
setup(function()
realm:write(function()
testPersonPK = realm:create("PersonWithPK", { name = "Unique", age = 25 })
end)
end)
teardown(function() _delete(realm, {testPersonPK}) end)
it("should succesfuly create and set properties", function()
assert.is_realm_handle(testPersonPK._handle)
end)
it("should not let user create objects without a primary key", function()
realm:write(function()
local badCreateA = function() realm:create("PersonWithPK") end
local badCreateB = function() realm:create("PersonWithPK", { age = 10 }) end
assert.has_error(badCreateA, "Primary key not set at declaration")
assert.has_error(badCreateB, "Primary key not set at declaration")
end)
end)
it("should not let user create duplicate objects with the same primary key", function()
realm:write(function()
local badCreate = function() realm:create("PersonWithPK", { name = "Unique" }) end
assert.has_error(badCreate, "Object with this primary key already exists")
end)
end)
end)
describe("with notifications", function()
local notificationReceived = false;
local people = realm:objects("Person")
local notificationToken = people:addListener(function (collection, changes)
if #changes.insertions == 1 then
notificationReceived = true;
uv.stop()
end
end)
assert.is_realm_handle(notificationToken)
post(function ()
local otherRealm <close> = Realm.open({path = path, schemaVersion = 0, schema = schema, _cached = false})
otherRealm:write(function()
otherRealm:create("Person")
end)
end)
timeout(1000)
uv.run()
assert.True(notificationReceived)
end)
end)
describe("with lists", function()
local testPetA
local testPetB
setup(function()
realm:write(function()
testPetA = realm:create("Pet", { name = "TurtleA"})
testPetB = realm:create("Pet", { name = "TurtleB"})
table.insert(testPerson.pets, testPetA)
table.insert(testPerson.pets, testPetB)
table.insert(testPerson.ints, 1)
table.insert(testPerson.ints, 2)
end)
end)
teardown(function() _delete(realm, {testPetA, testPetB}) end)
it("inserts objects", function()
local petList = testPerson.pets
assert.is.equal(#petList, 2)
assert.is.equal(petList[1].name, "TurtleA")
end)
it("inserts primitives", function()
local intList = testPerson.ints
assert.is.equal(#intList, 2)
assert.is.equal(intList[2], 2)
end)
it("removes objects by assigning to nil", function()
local petList = testPerson.pets
local currentLength
local testPetC
realm:write(function()
testPetC = realm:create("Pet", { name = "TurtleC"})
table.insert(petList, testPetC)
currentLength = #petList
petList[currentLength] = nil
end)
assert.is.equal(#petList, currentLength-1)
-- check that object still exists.
assert.True(realm:isValid(testPetC))
end)
it("removes primitives by assigning to nil", function()
local intList = testPerson.ints
local currentLength
realm:write(function()
table.insert(intList, 1)
currentLength = #intList
intList[currentLength] = nil
end)
assert.is.equal(#intList, currentLength-1)
end)
it("removes objects with built in remove", function()
local petList = testPerson.pets
local currentLength
local testPetC
realm:write(function()
testPetC = realm:create("Pet", { name = "TurtleC"})
table.insert(petList, testPetC)
currentLength = #petList
table.remove(petList, currentLength)
end)
assert.is.equal(#petList, currentLength-1)
-- check that object still exists.
assert.True(realm:isValid(testPetC))
end)
it("removes primitives by assigning to nil", function()
local intList = testPerson.ints
local currentLength
realm:write(function()
table.insert(intList, 1)
currentLength = #intList
table.remove(intList, currentLength)
end)
assert.is.equal(#intList, currentLength-1)
end)
end)
describe("with dictionaries", function()
local testPetA
local testPetB
setup(function()
realm:write(function()
testPetA = realm:create("Pet", { name = "TurtleA"})
testPetB = realm:create("Pet", { name = "TurtleB"})
testPerson.petDictionary["pet1"] = testPetA
testPerson.petDictionary["pet2"] = testPetB
testPerson.intDictionary["1"] = 1
testPerson.intDictionary["2"] = 2
end)
end)
teardown(function() _delete(realm, {testPetA, testPetB}) end)
it("inserts objects", function()
local petDictionary = testPerson.petDictionary
assert.is.equal(#petDictionary, 2)
assert.is.equal(petDictionary["pet1"].name, "TurtleA")
end)
it("inserts primitives", function()
local intDictionary = testPerson.intDictionary
assert.is.equal(#intDictionary, 2)
assert.is.equal(intDictionary["2"], 2)
end)
it("removes objects", function()
local petDictionary = testPerson.petDictionary
local testPetC
realm:write(function()
testPetC = realm:create("Pet", { name = "TurtleC"})
petDictionary["pet3"] = testPetC
end)
local currentLength = #petDictionary
realm:write(function()
petDictionary["pet3"] = nil
end)
assert.is.equal(#petDictionary, currentLength-1)
end)
it("removes primitives", function()
local intDictionary = testPerson.intDictionary
realm:write(function()
intDictionary["3"] = 3
end)
local currentLength = #intDictionary
realm:write(function()
intDictionary["3"] = nil
end)
assert.is.equal(#intDictionary, currentLength-1)
end)
it("writing to same key overwrites objects", function()
local petDictionary = testPerson.petDictionary
local currentLength = #petDictionary
local testPetC
realm:write(function()
testPetC = realm:create("Pet", { name = "TurtleC"})
-- should write over TurtleA
petDictionary["pet1"] = testPetC
end)
-- length of dictionary should remain the same
assert.is.equal(#petDictionary, currentLength)
assert.is.equal(petDictionary["pet1"].name, "TurtleC")
end)
it("writing to same key overwrites primitives", function()
local intDictionary = testPerson.intDictionary
local currentLength = #intDictionary
realm:write(function()
-- should write over value 1
intDictionary["1"] = 2
end)
-- length of dictionary should remain the same
assert.is.equal(#intDictionary, currentLength)
assert.is.equal(intDictionary["1"], 2)
end)
end)
describe("with sets", function()
local testPetA
local testPetB
local testPetC
local testPerson
setup(function()
realm:write(function()
testPetA = realm:create("Pet", { name = "TurtleA" })
testPetB = realm:create("Pet", { name = "TurtleB" })
testPetC = realm:create("Pet", { name = "TurtleC" })
testPerson = realm:create("Person", { name = "Peter", age = 3 })
testPerson.petSet[testPetA] = true
testPerson.petSet[testPetB] = true
testPerson.stringSet["foo"] = true
testPerson.stringSet["bar"] = true
end)
end)
teardown(function() _delete(realm, { testPetA, testPetB, testPetC }) end)
it("length of set with objects is correct", function()
local petSet = testPerson.petSet
assert.is.equal(#petSet, 2)
end)
it("length of set with primitive values is correct", function()
local stringSet = testPerson.stringSet
assert.is.equal(#stringSet, 2)
end)
it("returns true on object lookup", function()
local petSet = testPerson.petSet
assert.True(petSet[testPetA])
assert.True(petSet[testPetB])
assert.False(petSet[testPetC])
end)
it("returns true on string lookup", function()
local stringSet = testPerson.stringSet
assert.True(stringSet["foo"])
assert.True(stringSet["bar"])
assert.False(stringSet["nonExistingValue"])
end)
it("insert same object entry again does not increase size", function()
local petSet = testPerson.petSet
assert.is.equal(#petSet, 2)
realm:write(function()
testPerson.petSet[testPetA] = true
end)
assert.is.equal(#petSet, 2)
end)
it("insert same string entry again does not increase size", function()
local stringSet = testPerson.stringSet
assert.is.equal(#stringSet, 2)
realm:write(function()
testPerson.stringSet["foo"] = true
end)
assert.is.equal(#stringSet, 2)
end)
it("remove element", function()
local petSet = testPerson.petSet
assert.is.equal(#petSet, 2)
realm:write(function()
petSet[testPetA] = nil
end)
assert.is.equal(#petSet, 1)
end)
end)
end)