|
1 |
| -import {describe, it} from 'mocha'; |
| 1 | +import {describe, it, before, after, beforeEach, afterEach} from 'mocha'; |
| 2 | +import {expect} from 'chai'; |
| 3 | +import {Database} from '../src'; |
2 | 4 |
|
3 | 5 | describe('DocumentCollection API', () => {
|
| 6 | + let name = `testdb_${Date.now()}`; |
| 7 | + let db; |
| 8 | + let collection; |
| 9 | + before(done => { |
| 10 | + db = new Database(); |
| 11 | + db.createDatabase(name) |
| 12 | + .then(() => { |
| 13 | + db.useDatabase(name); |
| 14 | + done(); |
| 15 | + }) |
| 16 | + .catch(done); |
| 17 | + }); |
| 18 | + after(done => { |
| 19 | + db.useDatabase('_system'); |
| 20 | + db.dropDatabase(name) |
| 21 | + .then(() => void done()) |
| 22 | + .catch(done); |
| 23 | + }); |
| 24 | + beforeEach(done => { |
| 25 | + collection = db.collection(`c_${Date.now()}`); |
| 26 | + collection.create() |
| 27 | + .then(() => void done()) |
| 28 | + .catch(done); |
| 29 | + }); |
| 30 | + afterEach(done => { |
| 31 | + collection.drop() |
| 32 | + .then(() => void done()) |
| 33 | + .catch(done); |
| 34 | + }); |
4 | 35 | describe('documentCollection.document', () => {
|
5 | 36 | it('is missing tests');
|
6 | 37 | });
|
7 | 38 | describe('documentCollection.save', () => {
|
8 |
| - it('is missing tests'); |
| 39 | + it('creates a document in the collection', done => { |
| 40 | + let data = {foo: 'bar'}; |
| 41 | + collection.save(data) |
| 42 | + .then(meta => { |
| 43 | + expect(meta).to.be.an('object'); |
| 44 | + expect(meta).to.have.a.property('_id').that.is.a('string'); |
| 45 | + expect(meta).to.have.a.property('_rev').that.is.a('string'); |
| 46 | + expect(meta).to.have.a.property('_key').that.is.a('string'); |
| 47 | + return collection.document(meta._id) |
| 48 | + .then(doc => { |
| 49 | + expect(doc).to.have.keys('_key', '_id', '_rev', 'foo'); |
| 50 | + expect(doc._id).to.equal(meta._id); |
| 51 | + expect(doc._key).to.equal(meta._key); |
| 52 | + expect(doc._rev).to.equal(meta._rev); |
| 53 | + expect(doc.foo).to.equal(data.foo); |
| 54 | + }); |
| 55 | + }) |
| 56 | + .then(() => void done()) |
| 57 | + .catch(done); |
| 58 | + }); |
| 59 | + it('uses the given _key if provided', done => { |
| 60 | + let data = {potato: 'tomato', _key: 'banana'}; |
| 61 | + collection.save(data) |
| 62 | + .then(meta => { |
| 63 | + expect(meta).to.be.an('object'); |
| 64 | + expect(meta).to.have.a.property('_id').that.is.a('string'); |
| 65 | + expect(meta).to.have.a.property('_rev').that.is.a('string'); |
| 66 | + expect(meta).to.have.a.property('_key').that.equals(data._key); |
| 67 | + return collection.document(meta._id) |
| 68 | + .then(doc => { |
| 69 | + expect(doc).to.have.keys('_key', '_id', '_rev', 'potato'); |
| 70 | + expect(doc._id).to.equal(meta._id); |
| 71 | + expect(doc._rev).to.equal(meta._rev); |
| 72 | + expect(doc._key).to.equal(data._key); |
| 73 | + expect(doc.potato).to.equal(data.potato); |
| 74 | + }); |
| 75 | + }) |
| 76 | + .then(() => void done()) |
| 77 | + .catch(done); |
| 78 | + }); |
9 | 79 | });
|
10 | 80 | describe('documentCollection.replace', () => {
|
11 |
| - it('is missing tests'); |
| 81 | + it('replaces the given document', done => { |
| 82 | + let doc = {potato: 'tomato'}; |
| 83 | + collection.save(doc) |
| 84 | + .then(meta => { |
| 85 | + delete meta.error; |
| 86 | + Object.assign(doc, meta); |
| 87 | + return collection.replace(doc, {sup: 'dawg'}); |
| 88 | + }) |
| 89 | + .then(() => collection.document(doc._key)) |
| 90 | + .then(data => { |
| 91 | + expect(data).not.to.have.a.property('potato'); |
| 92 | + expect(data).to.have.a.property('sup').that.equals('dawg'); |
| 93 | + done(); |
| 94 | + }) |
| 95 | + .catch(done); |
| 96 | + }); |
12 | 97 | });
|
13 | 98 | describe('documentCollection.update', () => {
|
14 |
| - it('is missing tests'); |
| 99 | + it('updates the given document', done => { |
| 100 | + let doc = {potato: 'tomato', empty: false}; |
| 101 | + collection.save(doc) |
| 102 | + .then(meta => { |
| 103 | + delete meta.error; |
| 104 | + Object.assign(doc, meta); |
| 105 | + return collection.update(doc, {sup: 'dawg', empty: null}); |
| 106 | + }) |
| 107 | + .then(() => collection.document(doc._key)) |
| 108 | + .then(data => { |
| 109 | + expect(data).to.have.a.property('potato').that.equals(doc.potato); |
| 110 | + expect(data).to.have.a.property('sup').that.equals('dawg'); |
| 111 | + expect(data).to.have.a.property('empty').that.equals(null); |
| 112 | + done(); |
| 113 | + }) |
| 114 | + .catch(done); |
| 115 | + }); |
| 116 | + it('removes null values if keepNull is explicitly set to false', done => { |
| 117 | + let doc = {potato: 'tomato', empty: false}; |
| 118 | + collection.save(doc) |
| 119 | + .then(meta => { |
| 120 | + delete meta.error; |
| 121 | + Object.assign(doc, meta); |
| 122 | + return collection.update(doc, {sup: 'dawg', empty: null}, {keepNull: false}); |
| 123 | + }) |
| 124 | + .then(() => collection.document(doc._key)) |
| 125 | + .then(data => { |
| 126 | + expect(data).to.have.a.property('potato').that.equals(doc.potato); |
| 127 | + expect(data).to.have.a.property('sup').that.equals('dawg'); |
| 128 | + expect(data).not.to.have.a.property('empty'); |
| 129 | + done(); |
| 130 | + }) |
| 131 | + .catch(done); |
| 132 | + }); |
15 | 133 | });
|
16 | 134 | describe('documentCollection.remove', () => {
|
17 |
| - it('is missing tests'); |
| 135 | + let key = `d_${Date.now()}`; |
| 136 | + beforeEach(done => { |
| 137 | + collection.save({_key: key}) |
| 138 | + .then(() => void done()) |
| 139 | + .catch(done); |
| 140 | + }); |
| 141 | + it('deletes the given document', done => { |
| 142 | + collection.remove(key) |
| 143 | + .then(() => collection.document(key)) |
| 144 | + .then( |
| 145 | + () => Promise.reject(new Error('Should not succeed')), |
| 146 | + () => void done() |
| 147 | + ) |
| 148 | + .catch(done); |
| 149 | + }); |
18 | 150 | });
|
19 | 151 | describe('documentCollection.list', () => {
|
20 | 152 | it('is missing tests');
|
|
0 commit comments