UNPKG

2.31 kBJavaScriptView Raw
1const {expect} = require('chai'),
2 Datastore = require('../lib/Datastore');
3
4describe('Update', () => {
5 let documents = [
6 { name: 'first document' },
7 { name: 'second document' },
8 { name: 'third document' }
9 ];
10
11 describe(`single`, () => {
12 it('should update single document', () => {
13 let db = Datastore.create();
14 return db.insert(documents)
15 .then((inserted) => {
16 return db.update(
17 { name: 'first document' },
18 { name: 'updated document' },
19 { multi: false }
20 );
21 })
22 .then((numAffected) => {
23 expect(numAffected).to.equal(1);
24 });
25 });
26 });
27
28 describe(`single affected`, () => {
29 it('should update and return single document', () => {
30 let db = Datastore.create();
31 return db.insert(documents)
32 .then((inserted) => {
33 return db.update(
34 { name: 'first document' },
35 { name: 'updated document' },
36 { multi: false, returnUpdatedDocs: true }
37 );
38 })
39 .then(([numAffected, affectedDocument]) => {
40 expect(numAffected).to.equal(1);
41 expect(affectedDocument).to.deep.include({
42 name: 'updated document'
43 });
44 });
45 });
46 });
47
48 describe(`bulk`, () => {
49 it('should update multiple documents', () => {
50 let db = Datastore.create();
51 return db.insert(documents)
52 .then((inserted) => {
53 return db.update(
54 { name: { $regex: /document$/ } },
55 { $set: { test: true } },
56 { multi: true }
57 );
58 })
59 .then((numAffected) => {
60 expect(numAffected).to.equal(3);
61 });
62 });
63 });
64
65 describe(`bulk affected`, () => {
66 it('should update and return multiple documents', () => {
67 let db = Datastore.create();
68 return db.insert(documents)
69 .then((inserted) => {
70 return db.update(
71 { name: { $regex: /document$/ } },
72 { $set: { test: true } },
73 { multi: true, returnUpdatedDocs: true }
74 );
75 })
76 .then(([numAffected, affectedDocuments]) => {
77 expect(numAffected).to.equal(3);
78 expect(affectedDocuments).to.be.an('array').that.has.lengthOf(3);
79 });
80 });
81 });
82});
\No newline at end of file