-
Notifications
You must be signed in to change notification settings - Fork 11
/
local-persist-test.js
258 lines (200 loc) · 8.25 KB
/
local-persist-test.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
var data = [
{ firstName: 'Albert', lastName: 'Einstein', email: '[email protected]' },
{ firstName: 'Marie', lastName: 'Curie', email: '[email protected]' },
{ firstName: 'Max', lastName: 'Planck', email: '[email protected]' },
{ firstName: 'Werner', lastName: 'Heisenberg', email: '[email protected]' }
];
// test adding, retrieving and deleting data. the tests are a bit bogus since we can't
// reload the browser to exercise the persistence. the best we can do is to verify that
// we have stored the correct data.
Tinytest.add('Local Persist (Uncompressed) - Insert Data', function(test) {
var testCollection = new Meteor.Collection(null);
var testObserver = new LocalPersist(testCollection, 'Test-Collection');
data.forEach(function (doc) {
testCollection.insert(doc);
});
// right number of adds?
test.equal(testObserver._getStats().added, data.length);
// get the tracking list and verify it has the correct number of keys
var list = testObserver._getList();
test.equal(list.length, data.length);
});
Tinytest.add('Local Persist (Uncompressed) - Retrieve Data', function(test) {
var testCollection = new Meteor.Collection(null);
var testObserver = new LocalPersist(testCollection, 'Test-Collection');
// right number of adds?
test.equal(testObserver._getStats().added, data.length);
data.forEach(function (doc) {
m = testCollection.findOne({ lastName: doc.lastName });
a = testObserver._get(testObserver._makeDataKey(m._id));
test.equal(a, m);
});
});
Tinytest.add('Local Persist (Uncompressed) - Remove Data', function(test) {
var testCollection = new Meteor.Collection(null);
var testObserver = new LocalPersist(testCollection, 'Test-Collection');
// right number of adds?
test.equal(testObserver._getStats().added, data.length);
testCollection.remove({});
// right number of removes?
test.equal(testObserver._getStats().removed, data.length);
// the tracking list should be gone
var list = testObserver._get(testObserver._getKey());
test.equal(!! list, false);
});
// create emulated amplify data to match earlier versions of this package.
// init a collection, which will then auto-migrate. validate that it did.
Tinytest.add('Local Persist (Uncompressed) - Migrate Data', function(test) {
var dataCollection = new Meteor.Collection(null);
var keybase = '__amplify__browcol__Test-Collection';
var list = [];
var adata = {};
data.forEach(function (doc) {
dataCollection.insert(doc);
});
var cur = dataCollection.find({});
cur.forEach(function (doc) {
list.push(doc._id);
adata = {};
adata.data = doc;
adata.expires = null;
localStorage.setItem(keybase + '__' + doc._id, EJSON.stringify(adata));
});
adata = {};
adata.data = list;
adata.expires = null;
localStorage.setItem(keybase, EJSON.stringify(adata));
var testCollection = new Meteor.Collection(null);
var testObserver = new LocalPersist(testCollection, 'Test-Collection', { compress: false, migrate: true });
cur = testCollection.find({});
cur.forEach(function (doc) {
m = testCollection.findOne({ lastName: doc.lastName });
a = testObserver._get(testObserver._makeDataKey(m._id));
test.equal(a, m);
});
testCollection.remove({});
});
// repeat tests with compression enabled
Tinytest.add('Local Persist (Compressed) - Insert Data', function(test) {
var testCollection = new Meteor.Collection(null);
var testObserver = new LocalPersist(testCollection, 'Test-Collection', { compress: true, migrate: false });
data.forEach(function (doc) {
testCollection.insert(doc);
});
// right number of adds?
test.equal(testObserver._getStats().added, data.length);
// get the tracking list and verify it has the correct number of keys
var list = testObserver._getList();
test.equal(list.length, data.length);
});
Tinytest.add('Local Persist (Compressed) - Retrieve Data', function(test) {
var testCollection = new Meteor.Collection(null);
var testObserver = new LocalPersist(testCollection, 'Test-Collection', { compress: true, migrate: false });
// right number of adds?
test.equal(testObserver._getStats().added, data.length);
data.forEach(function (doc) {
m = testCollection.findOne({ lastName: doc.lastName });
a = testObserver._get(testObserver._makeDataKey(m._id));
test.equal(a, m);
});
});
Tinytest.add('Local Persist (Compressed) - Remove Data', function(test) {
var testCollection = new Meteor.Collection(null);
var testObserver = new LocalPersist(testCollection, 'Test-Collection', { compress: true, migrate: false });
// right number of adds?
test.equal(testObserver._getStats().added, data.length);
testCollection.remove({});
// right number of removes?
test.equal(testObserver._getStats().removed, data.length);
// the tracking list should be gone
var list = testObserver._get(testObserver._getKey());
test.equal(!! list, false);
});
// create emulated amplify data to match earlier versions of this package.
// init a collection, which will then auto-migrate. validate that it did.
Tinytest.add('Local Persist (Compressed) - Migrate Data', function(test) {
var dataCollection = new Meteor.Collection(null);
var keybase = '__amplify__browcol__Test-Collection';
var list = [];
var adata = {};
data.forEach(function (doc) {
dataCollection.insert(doc);
});
var cur = dataCollection.find({});
cur.forEach(function (doc) {
list.push(doc._id);
adata = {};
adata.data = doc;
adata.expires = null;
localStorage.setItem(keybase + '__' + doc._id, EJSON.stringify(adata));
});
adata = {};
adata.data = list;
adata.expires = null;
localStorage.setItem(keybase, EJSON.stringify(adata));
var testCollection = new Meteor.Collection(null);
var testObserver = new LocalPersist(testCollection, 'Test-Collection', { compress: true, migrate: true });
cur = testCollection.find({});
cur.forEach(function (doc) {
m = testCollection.findOne({ lastName: doc.lastName });
a = testObserver._get(testObserver._makeDataKey(m._id));
test.equal(a, m);
});
testCollection.remove({});
});
// repeat tests with both compressed and uncompressed data
Tinytest.add('Local Persist (Compressed and Uncompressed) - Insert Data', function(test) {
var testCollection = new Meteor.Collection(null);
var testObserver = new LocalPersist(testCollection, 'Test-Collection');
var i = 0;
data.forEach(function (doc) {
testObserver.compress = i++ % 2 === 0;
testCollection.insert(doc);
});
// right number of adds?
test.equal(testObserver._getStats().added, data.length);
// get the tracking list and verify it has the correct number of keys
var list = testObserver._getList();
test.equal(list.length, data.length);
});
Tinytest.add('Local Persist (Compressed and Uncompressed) - Retrieve Data', function(test) {
var testCollection = new Meteor.Collection(null);
var testObserver = new LocalPersist(testCollection, 'Test-Collection');
// right number of adds?
test.equal(testObserver._getStats().added, data.length);
data.forEach(function (doc) {
m = testCollection.findOne({ lastName: doc.lastName });
a = testObserver._get(testObserver._makeDataKey(m._id));
test.equal(a, m);
});
});
Tinytest.add('Local Persist (Compressed and Uncompressed) - Remove Data', function(test) {
var testCollection = new Meteor.Collection(null);
var testObserver = new LocalPersist(testCollection, 'Test-Collection');
// right number of adds?
test.equal(testObserver._getStats().added, data.length);
testCollection.remove({});
// right number of removes?
test.equal(testObserver._getStats().removed, data.length);
// the tracking list should be gone
var list = testObserver._get(testObserver._getKey());
test.equal(!! list, false);
});
Tinytest.add('Local Persist - Storage Limit Exceeded', function(test) {
var count = 0;
var testCollection = new Meteor.Collection(null);
var testObserver = new LocalPersist(testCollection, 'Test-Collection',
{
maxDocuments: data.length - 1,
storageFull: function(col, doc) {
++count;
col.remove({ _id: doc._id });
}
});
data.forEach(function (doc) {
testCollection.insert(doc);
});
test.equal(count, 1);
test.equal(testCollection.find({}).count(), data.length - 1);
testCollection.remove({});
});