-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.coffee
122 lines (86 loc) · 2.96 KB
/
tests.coffee
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
Tinytest.add 'directcollection - basic', (test) ->
testCollection = new DirectCollection 'direct'
# First cleanup
testCollection.remove {}
test.equal testCollection.count({}), 0
document1 =
foo: 'bar1'
faa: 'zar'
document1Id = testCollection.insert document1
# insert should not modify document1 directly
test.isFalse document1._id
document1._id = document1Id
test.isTrue _.isString document1._id
test.equal testCollection.findOne(_id: document1._id), document1
test.equal testCollection.findOne(document1), document1
document2 =
_id: 'test'
foo: 'bar2'
faa: 'zar'
document2Id = testCollection.insert document2
test.equal document2._id, 'test'
test.equal document2Id, 'test'
test.equal testCollection.findOne(_id: document2._id), document2
test.equal testCollection.findOne(document2), document2
test.equal testCollection.count({}), 2
test.equal testCollection.count(foo: 'bar2'), 1
test.equal testCollection.count(faa: 'zar'), 2
test.equal testCollection.findToArray({}), [document1, document2]
correctOrder = 0
testCollection.findEach {}, (document) ->
Meteor._nodeCodeMustBeInFiber()
Meteor._sleepForMs 50
correctOrder++
if correctOrder is 1
test.equal document1, document
else if correctOrder is 2
test.equal document2, document
else
test.fail
type: 'fail'
message: "Invalid correctOrder: " + correctOrder
test.equal correctOrder, 2
test.throws ->
testCollection.findEach {}, (document) ->
throw new Error 'test'
, /test/
updates = testCollection.update {},
$set:
foo: 'bar1a'
document1.foo = 'bar1a'
test.equal updates, 1
test.equal testCollection.findToArray({}), [document1, document2]
updates = testCollection.update {},
$set:
faa: 'zar2'
,
multi: true
document1.faa = 'zar2'
document2.faa = 'zar2'
test.equal updates, 2
test.equal testCollection.findToArray({}), [document1, document2]
removed = testCollection.remove foo: 'bar2'
test.equal removed, 1
test.equal testCollection.count({}), 1
removed = testCollection.remove {}
test.equal removed, 1
test.equal testCollection.count({}), 0
Tinytest.add 'directcollection - external db', (test) ->
# Obtain current URL
mongoUrl = null
AppConfig.configurePackage 'mongo-livedata', (config) ->
mongoUrl = "#{ config.url }_external"
testCollection = new DirectCollection 'foo', null, mongoUrl
# First cleanup
testCollection.remove {}
test.equal testCollection.count({}), 0
document1 =
foo: 'bar1'
faa: 'zar'
document1._id = testCollection.insert document1
test.equal testCollection.findOne(_id: document1._id), document1
test.equal testCollection.findOne(document1), document1
test.equal DirectCollection.command({getLastError: 1}, null, mongoUrl)?.ok, 1
# Ensure that insert went to the right database
testCollection = new DirectCollection 'foo'
test.isFalse testCollection.findOne _id: document1._id