forked from Meteor-Community-Packages/meteor-simple-schema
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongo-object-tests.js
234 lines (197 loc) · 7.23 KB
/
mongo-object-tests.js
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
var MongoObject = require("./mongo-object");
var Tinytest = require("./tinytest-shim");
var _ = require("lodash");
/* global MongoObject */
var flat = function(doc, opts) {
var mDoc = new MongoObject(doc);
return mDoc.getFlatObject(opts);
};
var passthru = function(doc) {
var mDoc = new MongoObject(doc);
return mDoc.getObject();
};
Tinytest.add("MongoObject - Round Trip", function(test) {
// Helper Function
function rt(o) {
var po = passthru(o);
var jo = JSON.stringify(o);
var jpo = JSON.stringify(po);
test.equal(jpo, jo, "After round trip, object was " + jpo + " but should have been " + jo);
}
// Round Trip Tests
rt({});
rt({a: 1});
rt({a: "Test"});
rt({a: new Date()});
rt({a: []});
rt({a: {}});
rt({a: [1, 2]});
rt({a: ["Test1", "Test2"]});
rt({a: [new Date(), new Date()]});
rt({a: {b: 1}});
rt({a: {b: "Test"}});
rt({a: {b: new Date()}});
rt({a: {b: []}});
rt({a: {b: {}}});
rt({a: {b: [1, 2]}});
rt({a: {b: ["Test1", "Test2"]}});
rt({a: {b: [new Date(), new Date()]}});
rt({a: {b: [{c: 1}, {c: 2}]}});
rt({a: {b: [{c: "Test1"}, {c: "Test2"}]}});
rt({a: {b: [{c: new Date()}, {c: new Date()}]}});
});
Tinytest.add("MongoObject - TypedArrays", function (test) {
var mo = new MongoObject({foo: new Uint8Array(10)});
test.isUndefined(mo._affectedKeys["foo.0"]);
});
Tinytest.add("MongoObject - Flat", function(test) {
// Helper Function
function testFlat(o, exp, opts) {
var fo = flat(o, opts);
var jo = JSON.stringify(o);
var jfo = JSON.stringify(fo);
var jexp = JSON.stringify(exp);
test.equal(jfo, jexp, "Object " + jo + " was flattened to " + jfo + " but should have been " + jexp);
}
// Flatten Tests
var testDate = new Date();
testFlat({}, {});
testFlat({a: 1}, {a: 1});
testFlat({a: "Test"}, {a: "Test"});
testFlat({a: testDate}, {a: testDate});
testFlat({a: []}, {a: []});
testFlat({a: {}}, {a: {}});
testFlat({a: [1, 2]}, {"a.0": 1, "a.1": 2});
testFlat({a: [1, 2]}, {a: [1, 2]}, {keepArrays: true});
testFlat({a: ["Test1", "Test2"]}, {"a.0": "Test1", "a.1": "Test2"});
testFlat({a: ["Test1", "Test2"]}, {a: ["Test1", "Test2"]}, {keepArrays: true});
testFlat({a: [testDate, testDate]}, {"a.0": testDate, "a.1": testDate});
testFlat({a: [testDate, testDate]}, {a: [testDate, testDate]}, {keepArrays: true});
testFlat({a: {b: 1}}, {"a.b": 1});
testFlat({a: {b: "Test"}}, {"a.b": "Test"});
testFlat({a: {b: testDate}}, {"a.b": testDate});
testFlat({a: {b: []}}, {"a.b": []});
testFlat({a: {b: {}}}, {"a.b": {}});
testFlat({a: {b: [1, 2]}}, {"a.b.0": 1, "a.b.1": 2});
testFlat({a: {b: [1, 2]}}, {"a.b": [1, 2]}, {keepArrays: true});
testFlat({a: {b: ["Test1", "Test2"]}}, {"a.b.0": "Test1", "a.b.1": "Test2"});
testFlat({a: {b: ["Test1", "Test2"]}}, {"a.b": ["Test1", "Test2"]}, {keepArrays: true});
testFlat({a: {b: [testDate, testDate]}}, {"a.b.0": testDate, "a.b.1": testDate});
testFlat({a: {b: [testDate, testDate]}}, {"a.b": [testDate, testDate]}, {keepArrays: true});
testFlat({a: {b: [{c: 1}, {c: 2}]}}, {"a.b.0.c": 1, "a.b.1.c": 2});
testFlat({a: {b: [{c: 1}, {c: 2}]}}, {"a.b": [{c: 1}, {c: 2}]}, {keepArrays: true});
testFlat({a: {b: [{c: "Test1"}, {c: "Test2"}]}}, {"a.b.0.c": "Test1", "a.b.1.c": "Test2"});
testFlat({a: {b: [{c: "Test1"}, {c: "Test2"}]}}, {"a.b": [{c: "Test1"}, {c: "Test2"}]}, {keepArrays: true});
testFlat({a: {b: [{c: testDate}, {c: testDate}]}}, {"a.b.0.c": testDate, "a.b.1.c": testDate});
testFlat({a: {b: [{c: testDate}, {c: testDate}]}}, {"a.b": [{c: testDate}, {c: testDate}]}, {keepArrays: true});
});
Tinytest.add("MongoObject - removeValueForPosition", function(test) {
// Helper Function
function testRemove(o, exp, pos) {
var mDoc = new MongoObject(o);
mDoc.removeValueForPosition(pos);
var jo = JSON.stringify(o);
var jno = JSON.stringify(mDoc.getObject());
var jexp = JSON.stringify(exp);
test.equal(jno, jexp, "After round trip, object " + jo + " was " + jno + " but should have been " + jexp);
}
// correctly removed
testRemove({
foo: "bar"
}, {}, 'foo');
// correctly not removed
testRemove({
foo: "bar"
}, {
foo: "bar"
}, 'fooBar');
// all descendents are removed, too
testRemove({
foo: {
bar: "foobar"
}
}, {}, 'foo');
// but not siblings
testRemove({
foo: {
bar: "foobar",
foobar: 1
}
}, {
foo: {
bar: "foobar"
}
}, 'foo[foobar]');
});
Tinytest.add("MongoObject - getValueForPosition", function(test) {
// Helper Function
function testGetVal(o, pos, exp) {
var mDoc = new MongoObject(o);
var val = mDoc.getValueForPosition(pos);
var jo = JSON.stringify(o);
var jval = JSON.stringify(val);
var jexp = JSON.stringify(exp);
test.equal(jval, jexp, "Wrong value returned for position " + pos + " in object " + jo);
}
testGetVal({$pull: {foo: "bar"}}, '$pull', {foo: "bar"});
testGetVal({$pull: {foo: "bar"}}, '$pull[foo]', 'bar');
testGetVal({foo: ['bar']}, 'foo', ['bar']);
testGetVal({foo: ['bar']}, 'foo[0]', 'bar');
testGetVal({foo: [{a: 1}, {a: 2}]}, 'foo', [{a: 1}, {a: 2}]);
testGetVal({foo: [{a: 1}, {a: 2}]}, 'foo[1]', {a: 2});
testGetVal({foo: [{a: 1}, {a: 2}]}, 'foo[1][a]', 2);
});
Tinytest.add("MongoObject - getInfoForKey", function(test) {
// Helper Function
function testGetInfo(o, key, exp) {
var mDoc = new MongoObject(o);
var info = mDoc.getInfoForKey(key);
var jo = JSON.stringify(o);
var jinfo = JSON.stringify(info);
var jexp = JSON.stringify(exp);
test.equal(jinfo, jexp, "Wrong info returned for object " + jo);
}
testGetInfo({$set: {foo: "bar"}}, 'foo', {value: 'bar', operator: '$set'});
testGetInfo({$set: {'foo.bar': 1}}, 'foo.bar', {value: 1, operator: '$set'});
testGetInfo({$set: {'foo.bar': 1}}, '$set', undefined); //not valid
testGetInfo({$set: {'foo.bar.0': 1}}, 'foo.bar.0', {value: 1, operator: '$set'});
testGetInfo({$pull: {foo: "bar"}}, 'foo', {value: 'bar', operator: '$pull'});
testGetInfo({foo: ['bar']}, 'foo', {value: ['bar'], operator: null});
testGetInfo({foo: ['bar']}, 'foo.0', {value: 'bar', operator: null});
testGetInfo({foo: [{a: 1}, {a: 2}]}, 'foo.1.a', {value: 2, operator: null});
testGetInfo({foo: [{a: 1}, {a: 2}]}, 'foo.1', {value: {a: 2}, operator: null});
});
Tinytest.add("MongoObject - _keyToPosition", function(test) {
// Helper Function
function convert(key, wrapAll, exp) {
var pos = MongoObject._keyToPosition(key, wrapAll);
var jpos = JSON.stringify(pos);
var jexp = JSON.stringify(exp);
test.equal(jpos, jexp, "Key converted incorrectly to position");
}
convert('foo', false, 'foo');
convert('foo', true, '[foo]');
convert('foo.bar', false, 'foo[bar]');
convert('foo.bar', true, '[foo][bar]');
convert('foo.bar.0', false, 'foo[bar][0]');
convert('foo.bar.0', true, '[foo][bar][0]');
});
//Test API:
//test.isFalse(v, msg)
//test.isTrue(v, msg)
//test.equal(actual, expected, message, not)
//test.length(obj, len)
//test.include(s, v)
//test.isNaN(v, msg)
//test.isUndefined(v, msg)
//test.isNotNull
//test.isNull
//test.throws(func)
//test.instanceOf(obj, klass)
//test.notEqual(actual, expected, message)
//test.runId()
//test.exception(exception)
//test.expect_fail()
//test.ok(doc)
//test.fail(doc)
//test.equal(a, b, msg)