viewdb 0.5.12 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/merger.js CHANGED
@@ -1,163 +1,196 @@
1
- var should = require('should');
2
1
  var _ = require('lodash');
3
2
  var merge = require('../lib/merger');
4
3
 
5
- var logger = {
6
- added: function() { console.log("added: "); console.log(arguments); },
7
- removed: function() { console.log("removed: "); console.log(arguments); },
8
- changed: function() { console.log("changed: "); console.log(arguments); },
9
- moved: function() { console.log("moved: "); console.log(arguments); },
10
- }
11
-
12
- describe('Merger', function() {
13
- it('#merge with remove element', function(done) {
14
- var l1 = [{a:1}, 'b', 'd'];
15
- var l2 = [{a:1}, 'b'];
16
-
17
- var res = merge(l1,l2, {
18
- removed: function(e) {
19
- e.should.equal('d');
20
- done();
21
- },
22
- comparatorId: _.isEqual,
23
- comparator: _.isEqual
24
- });
25
- _.isEqual(l2, res).should.be.true;
26
-
27
- // console.log(_.difference(l1,l2));
28
- });
29
- it('#merge with remove complex element', function(done) {
30
- var l1 = [{a:1}, 'b', 'd', {e:1}];
31
- var l2 = [{a:1}, 'b'];
32
- var removed = [];
33
- var res = merge(l1,l2, {
34
- removed: function(e) {
35
- removed.push(e);
36
- },
37
- comparatorId: _.isEqual,
38
- comparator: _.isEqual
39
- });
40
- removed.length.should.equal(2);
41
- _.isEqual(l2, res).should.be.true;
42
- done();
43
-
44
- // console.log(_.difference(l1,l2));
45
- });
46
-
47
- it('#merge with objects instead of arrays', function(done) {
48
- var l1 = {"0":{a:1}, "1":'b', "2":'d', "3":{e:1}};
49
- var l2 = [{a:1}, 'b'];
50
- var removed = [];
51
- var res = merge(l1,l2, {
52
- removed: function(e) {
53
- removed.push(e);
54
- },
55
- comparatorId: _.isEqual,
56
- comparator: _.isEqual
57
- });
58
- removed.length.should.equal(2);
59
- _.isEqual(l2, res).should.be.true;
60
- done();
61
-
62
- // console.log(_.difference(l1,l2));
63
- });
64
-
65
-
66
- it('#merge with one add element', function(done) {
67
- var l1 = [{a:1}, 'b'];
68
- var l2 = [{a:1}, 'b', 'c'];
69
-
70
- var res = merge(l1,l2, {
71
- added: function(e) {
72
- e.should.equal('c');
73
- done();
74
- },
75
- removed: function(e) {
76
- done(new Error('should not be called'));
77
- }
78
- });
79
- _.isEqual(l2, res).should.be.true;
80
- });
81
- it('#merge with one complex add element', function(done) {
82
- var l1 = [{a:1}, 'b'];
83
- var l2 = [{a:1}, 'b', {c:1}];
84
-
85
- var res = merge(l1,l2, {
86
- added: function(e) {
87
- _.isEqual(e,{c:1}).should.be.true;
88
- done();
89
- },
90
- removed: function(e) {
91
- done(new Error('should not be called'));
92
- }
93
- });
94
- _.isEqual(l2, res).should.be.true;
95
- });
96
- it('#merge with one move element', function(done) {
97
- var l1 = [{a:1}, 'b', {c:1}];
98
- var l2 = [{a:1}, {c:1}, 'b'];
99
- var moved = [];
100
- var res = merge(l1,l2, {
101
- added: function(e) {
102
- done(new Error('should not be called'));
103
- },
104
- removed: function(e) {
105
- done(new Error('should not be called'));
106
- },
107
- moved: function(e, oldIndex, newIndex) {
108
- moved.push(arguments);
109
- }
110
- });
111
- moved.length.should.equal(1);
112
-
113
- _.isEqual(l2, res).should.be.true;
114
-
115
- done();
116
- });
117
- it('#merge with one changing elements', function(done) {
118
- var l1 = [{_id:1, a:'Hello'}];
119
- var l2 = [{_id:1, a:'Hej'}];
120
- var res = merge(l1,l2, {
121
- added: function(e) {
122
- done(new Error('should not be called'));
123
- },
124
- removed: function(e) {
125
- done(new Error('should not be called'));
126
- },
127
- moved: function(e, oldIndex, newIndex) {
128
- done(new Error('should not be called'));
129
- },
130
- changed: function(o, n, index) {
131
- },
132
- comparatorId: function(a,b) {return a._id === b._id}
133
- });
134
- _.isEqual(l2, res).should.be.true;
135
- done();
136
- });
137
-
138
- it('#merge true and false array', function(done) {
139
- var l1 = [true, false];
140
- var l2 = [false, true];
141
- var res = merge(l1,l2);
142
- _.isEqual(l2, res).should.be.true;
143
- done();
144
- });
145
- it('#merge complex moves', function(done) {
146
- var l1 = [{_id:1, a:'Hello1'}, {_id:2, a:'Hello2'}, {_id:3, a:'Hello3'}, {_id:4, a:'Hello4'}];
147
- var l2 = [{_id:4, a:'Hej4'}, {_id:3, a:'Hej3'}, {_id:2, a:'Hej2'}, {_id:1, a:'Hej1'}];
148
-
149
- var res = merge(l1,l2, _.defaults({comparatorId: function(a,b) {return a._id === b._id}}, {}));
150
- _.isEqual(l2, res).should.be.true;
151
- done();
152
- });
153
- it('#merge complex moves and add and remove', function(done) {
154
- var l1 = [{_id:1, a:'Hello1'}, {_id:2, a:'Hello2'}, {_id:3, a:'Hello3'}, {_id:4, a:'Hello4'}];
155
- var l2 = [{_id:4, a:'Hej4'}, {_id:99, a:'Hej99'}, {_id:2, a:'Hej2'}, {_id:1, a:'Hej1'}, {_id:100, a:'Hej100'}];
156
-
157
- var res = merge(l1,l2, _.defaults({comparatorId: function(a,b) {return a._id === b._id}}, {}));
158
- _.isEqual(l2, res).should.be.true;
159
- done();
160
- });
161
-
162
- })
163
-
4
+ describe('Merger', () => {
5
+ it('#merge with remove element', (done) => {
6
+ var l1 = [{ a: 1 }, 'b', 'd'];
7
+ var l2 = [{ a: 1 }, 'b'];
8
+
9
+ var res = merge(l1, l2, {
10
+ removed: function (e) {
11
+ expect(e).toBe('d');
12
+ done();
13
+ },
14
+ comparatorId: _.isEqual,
15
+ comparator: _.isEqual
16
+ });
17
+ expect(_.isEqual(l2, res)).toBe(true);
18
+
19
+ // console.log(_.difference(l1,l2));
20
+ });
21
+ it('#merge with remove complex element', (done) => {
22
+ var l1 = [{ a: 1 }, 'b', 'd', { e: 1 }];
23
+ var l2 = [{ a: 1 }, 'b'];
24
+ var removed = [];
25
+ var res = merge(l1, l2, {
26
+ removed: function (e) {
27
+ removed.push(e);
28
+ },
29
+ comparatorId: _.isEqual,
30
+ comparator: _.isEqual
31
+ });
32
+ expect(removed.length).toBe(2);
33
+ expect(_.isEqual(l2, res)).toBe(true);
34
+ done();
35
+
36
+ // console.log(_.difference(l1,l2));
37
+ });
38
+
39
+ it('#merge with objects instead of arrays', (done) => {
40
+ var l1 = { 0: { a: 1 }, 1: 'b', 2: 'd', 3: { e: 1 } };
41
+ var l2 = [{ a: 1 }, 'b'];
42
+ var removed = [];
43
+ var res = merge(l1, l2, {
44
+ removed: function (e) {
45
+ removed.push(e);
46
+ },
47
+ comparatorId: _.isEqual,
48
+ comparator: _.isEqual
49
+ });
50
+ expect(removed.length).toBe(2);
51
+ expect(_.isEqual(l2, res)).toBe(true);
52
+ done();
53
+
54
+ // console.log(_.difference(l1,l2));
55
+ });
56
+
57
+ it('#merge with one add element', (done) => {
58
+ var l1 = [{ a: 1 }, 'b'];
59
+ var l2 = [{ a: 1 }, 'b', 'c'];
60
+
61
+ var res = merge(l1, l2, {
62
+ added: function (e) {
63
+ expect(e).toBe('c');
64
+ done();
65
+ },
66
+ removed: function () {
67
+ done(new Error('should not be called'));
68
+ }
69
+ });
70
+ expect(_.isEqual(l2, res)).toBe(true);
71
+ });
72
+ it('#merge with one complex add element', (done) => {
73
+ var l1 = [{ a: 1 }, 'b'];
74
+ var l2 = [{ a: 1 }, 'b', { c: 1 }];
75
+
76
+ var res = merge(l1, l2, {
77
+ added: function (e) {
78
+ expect(_.isEqual(e, { c: 1 })).toBe(true);
79
+ done();
80
+ },
81
+ removed: function () {
82
+ done(new Error('should not be called'));
83
+ }
84
+ });
85
+ expect(_.isEqual(l2, res)).toBe(true);
86
+ });
87
+ it('#merge with one move element', (done) => {
88
+ var l1 = [{ a: 1 }, 'b', { c: 1 }];
89
+ var l2 = [{ a: 1 }, { c: 1 }, 'b'];
90
+ var moved = [];
91
+ var res = merge(l1, l2, {
92
+ added: function () {
93
+ done(new Error('should not be called'));
94
+ },
95
+ removed: function () {
96
+ done(new Error('should not be called'));
97
+ },
98
+ moved: function (e, oldIndex, newIndex) {
99
+ moved.push(arguments);
100
+ }
101
+ });
102
+ expect(moved.length).toBe(1);
103
+
104
+ expect(_.isEqual(l2, res)).toBe(true);
105
+
106
+ done();
107
+ });
108
+ it('#merge with one changing elements', (done) => {
109
+ var l1 = [{ _id: 1, a: 'Hello' }];
110
+ var l2 = [{ _id: 1, a: 'Hej' }];
111
+ var res = merge(l1, l2, {
112
+ added: function () {
113
+ done(new Error('should not be called'));
114
+ },
115
+ removed: function () {
116
+ done(new Error('should not be called'));
117
+ },
118
+ moved: function () {
119
+ done(new Error('should not be called'));
120
+ },
121
+ changed: function (o, n, index) {},
122
+ comparatorId: function (a, b) {
123
+ return a._id === b._id;
124
+ }
125
+ });
126
+ expect(_.isEqual(l2, res)).toBe(true);
127
+ done();
128
+ });
129
+
130
+ it('#merge true and false array', (done) => {
131
+ var l1 = [true, false];
132
+ var l2 = [false, true];
133
+ var res = merge(l1, l2);
134
+ expect(_.isEqual(l2, res)).toBe(true);
135
+ done();
136
+ });
137
+ it('#merge complex moves', (done) => {
138
+ var l1 = [
139
+ { _id: 1, a: 'Hello1' },
140
+ { _id: 2, a: 'Hello2' },
141
+ { _id: 3, a: 'Hello3' },
142
+ { _id: 4, a: 'Hello4' }
143
+ ];
144
+ var l2 = [
145
+ { _id: 4, a: 'Hej4' },
146
+ { _id: 3, a: 'Hej3' },
147
+ { _id: 2, a: 'Hej2' },
148
+ { _id: 1, a: 'Hej1' }
149
+ ];
150
+
151
+ var res = merge(
152
+ l1,
153
+ l2,
154
+ _.defaults(
155
+ {
156
+ comparatorId: function (a, b) {
157
+ return a._id === b._id;
158
+ }
159
+ },
160
+ {}
161
+ )
162
+ );
163
+ expect(_.isEqual(l2, res)).toBe(true);
164
+ done();
165
+ });
166
+ it('#merge complex moves and add and remove', (done) => {
167
+ var l1 = [
168
+ { _id: 1, a: 'Hello1' },
169
+ { _id: 2, a: 'Hello2' },
170
+ { _id: 3, a: 'Hello3' },
171
+ { _id: 4, a: 'Hello4' }
172
+ ];
173
+ var l2 = [
174
+ { _id: 4, a: 'Hej4' },
175
+ { _id: 99, a: 'Hej99' },
176
+ { _id: 2, a: 'Hej2' },
177
+ { _id: 1, a: 'Hej1' },
178
+ { _id: 100, a: 'Hej100' }
179
+ ];
180
+
181
+ var res = merge(
182
+ l1,
183
+ l2,
184
+ _.defaults(
185
+ {
186
+ comparatorId: function (a, b) {
187
+ return a._id === b._id;
188
+ }
189
+ },
190
+ {}
191
+ )
192
+ );
193
+ expect(_.isEqual(l2, res)).toBe(true);
194
+ done();
195
+ });
196
+ });
package/test/observe.js CHANGED
@@ -1,16 +1,14 @@
1
- var should = require('should');
2
-
3
1
  var ViewDb = require('..');
4
2
  var _ = require('lodash');
5
3
 
6
- describe('Observe', function () {
7
- it('#observe with insert', function (done) {
4
+ describe('Observe', () => {
5
+ it('#observe with insert', (done) => {
8
6
  var store = new ViewDb();
9
7
  store.open().then(function () {
10
8
  var cursor = store.collection('dollhouse').find({});
11
9
  var handle = cursor.observe({
12
10
  added: function (x) {
13
- x._id.should.equal('echo');
11
+ expect(x._id).toBe('echo');
14
12
  handle.stop();
15
13
  done();
16
14
  }
@@ -18,31 +16,32 @@ describe('Observe', function () {
18
16
  store.collection('dollhouse').insert({ _id: 'echo' });
19
17
  });
20
18
  });
21
- it('#observe with query and insert', function (done) {
19
+ it('#observe with query and insert', (done) => {
22
20
  var store = new ViewDb();
23
21
  store.open().then(function () {
24
22
  store.collection('dollhouse').insert({ _id: 'echo' });
25
23
  var cursor = store.collection('dollhouse').find({ _id: 'echo2' });
26
- var handle = cursor.observe({
24
+ cursor.observe({
27
25
  added: function (x) {
28
- x._id.should.equal('echo2');
26
+ expect(x._id).toBe('echo2');
29
27
  done();
30
28
  }
31
29
  });
32
30
  store.collection('dollhouse').insert({ _id: 'echo2' });
33
31
  });
34
32
  });
35
- it('#observe with query and update', function (done) {
33
+ it('#observe with query and update', (done) => {
36
34
  var store = new ViewDb();
37
35
  store.open().then(function () {
38
36
  var cursor = store.collection('dollhouse').find({ _id: 'echo' });
39
37
  var handle = cursor.observe({
40
38
  added: function (x) {
41
- x.age.should.equal(10);
42
- x._id.should.equal('echo');
43
- }, changed: function (o, n) {
44
- o.age.should.equal(10);
45
- n.age.should.equal(100);
39
+ expect(x.age).toBe(10);
40
+ expect(x._id).toBe('echo');
41
+ },
42
+ changed: function (o, n) {
43
+ expect(o.age).toBe(10);
44
+ expect(n.age).toBe(100);
46
45
  handle.stop();
47
46
  done();
48
47
  }
@@ -53,7 +52,7 @@ describe('Observe', function () {
53
52
  });
54
53
  });
55
54
  });
56
- it('#observe with query and skip', function (done) {
55
+ it('#observe with query and skip', (done) => {
57
56
  var store = new ViewDb();
58
57
  store.open().then(function () {
59
58
  store.collection('dollhouse').insert({ _id: 'echo' });
@@ -64,40 +63,40 @@ describe('Observe', function () {
64
63
  cursor.limit(1);
65
64
  var realDone = _.after(3, function () {
66
65
  cursor.toArray(function (err, res) {
67
- res.length.should.equal(0);
66
+ expect(res.length).toBe(0);
68
67
  handle.stop();
69
68
  done();
70
- })
69
+ });
71
70
  });
72
71
  var handle = cursor.observe({
73
- added: function (x) {
72
+ added: function () {
74
73
  cursor.skip(++skip);
75
74
  realDone();
76
75
  }
77
76
  });
78
77
  });
79
78
  });
80
- it('#observe with no results', function (done) {
79
+ it('#observe with no results', (done) => {
81
80
  var store = new ViewDb();
82
81
  store.open().then(function () {
83
82
  var cursor = store.collection('dollhouse').find({});
84
83
  var handle = cursor.observe({
85
84
  init: function (coll) {
86
- coll.length.should.equal(0);
85
+ expect(coll.length).toBe(0);
87
86
  handle.stop();
88
87
  done();
89
88
  }
90
89
  });
91
90
  });
92
91
  });
93
- it('#observe with init after one insert', function (done) {
92
+ it('#observe with init after one insert', (done) => {
94
93
  var store = new ViewDb();
95
94
  store.collection('dollhouse').insert({ _id: 'echo' }, function () {
96
95
  store.open().then(function () {
97
96
  var cursor = store.collection('dollhouse').find({});
98
97
  var handle = cursor.observe({
99
98
  init: function (coll) {
100
- coll.length.should.equal(1);
99
+ expect(coll.length).toBe(1);
101
100
  handle.stop();
102
101
  done();
103
102
  }
@@ -105,16 +104,16 @@ describe('Observe', function () {
105
104
  });
106
105
  });
107
106
  });
108
- it('#observe with one insert after init', function (done) {
107
+ it('#observe with one insert after init', (done) => {
109
108
  var store = new ViewDb();
110
109
  store.open().then(function () {
111
110
  var cursor = store.collection('dollhouse').find({});
112
111
  var handle = cursor.observe({
113
112
  init: function (coll) {
114
- coll.length.should.equal(0);
113
+ expect(coll.length).toBe(0);
115
114
  },
116
115
  added: function (a) {
117
- a._id.should.equal('echo');
116
+ expect(a._id).toBe('echo');
118
117
  handle.stop();
119
118
  done();
120
119
  }
@@ -124,4 +123,43 @@ describe('Observe', function () {
124
123
  store.collection('dollhouse').insert({ _id: 'echo' });
125
124
  }, 5);
126
125
  });
126
+ it('#observe with query update', (done) => {
127
+ const store = new ViewDb();
128
+ store.open().then(() => {
129
+ const cursor = store.collection('dollhouse').find({});
130
+ const handle = cursor.observe({
131
+ init: (docs) => {
132
+ expect(docs.length).toBe(0);
133
+ },
134
+ added: (doc) => {
135
+ expect(doc._id).toMatch(/^echo/);
136
+ },
137
+ changed: (found, e) => {
138
+ expect(found).toEqual({ _id: 'echo1', name: 'marco' });
139
+ expect(e).toEqual({ _id: 'echo1', name: 'marco', data: 'changed' });
140
+
141
+ handle.stop();
142
+ done();
143
+ },
144
+ removed: (doc) => {
145
+ expect(doc).toEqual({ _id: 'echo3', name: 'polo' });
146
+
147
+ store.collection('dollhouse').save([{ _id: 'echo3', name: 'polo', data: 'changed' }], () => {
148
+ store.collection('dollhouse').save([{ _id: 'echo1', name: 'marco', data: 'changed' }]);
149
+ });
150
+ }
151
+ });
152
+
153
+ store.collection('dollhouse').insert(
154
+ [
155
+ { _id: 'echo1', name: 'marco' },
156
+ { _id: 'echo2', name: 'marco' },
157
+ { _id: 'echo3', name: 'polo' }
158
+ ],
159
+ () => {
160
+ cursor.updateQuery({ name: 'marco' });
161
+ }
162
+ );
163
+ });
164
+ });
127
165
  });