1 | const
|
2 | { expect } = require('chai'),
|
3 | Datastore = require('../src/Datastore')
|
4 |
|
5 | describe('testing document update', () => {
|
6 | let documents = [
|
7 | { name: 'first document' },
|
8 | { name: 'second document' },
|
9 | { name: 'third document' }
|
10 | ]
|
11 |
|
12 | describe('single', () => {
|
13 | it('should update single document', () => {
|
14 | let datastore = Datastore.create()
|
15 | return datastore.insert(documents)
|
16 | .then((inserted) => {
|
17 | return datastore.update(
|
18 | { name: 'first document' },
|
19 | { name: 'updated document' },
|
20 | { multi: false }
|
21 | )
|
22 | })
|
23 | .then((numAffected) => {
|
24 | expect(numAffected).to.equal(1)
|
25 | })
|
26 | })
|
27 | })
|
28 |
|
29 | describe('single with returnUpdatedDocs', () => {
|
30 | it('should update and return single document', () => {
|
31 | let datastore = Datastore.create()
|
32 | return datastore.insert(documents)
|
33 | .then((inserted) => {
|
34 | return datastore.update(
|
35 | { name: 'first document' },
|
36 | { name: 'updated document' },
|
37 | { multi: false, returnUpdatedDocs: true }
|
38 | )
|
39 | })
|
40 | .then((affectedDocument) => {
|
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 datastore = Datastore.create()
|
51 | return datastore.insert(documents)
|
52 | .then((inserted) => {
|
53 | return datastore.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 with returnUpdatedDocs', () => {
|
66 | it('should update and return multiple documents', () => {
|
67 | let datastore = Datastore.create()
|
68 | return datastore.insert(documents)
|
69 | .then((inserted) => {
|
70 | return datastore.update(
|
71 | { name: { $regex: /document$/ } },
|
72 | { $set: { test: true } },
|
73 | { multi: true, returnUpdatedDocs: true }
|
74 | )
|
75 | })
|
76 | .then((affectedDocuments) => {
|
77 | expect(affectedDocuments).to.be.an('array').that.has.lengthOf(3)
|
78 | })
|
79 | })
|
80 | })
|
81 | })
|