1 | ;
|
2 |
|
3 | const _ = require('lodash');
|
4 | const fs = require('fs');
|
5 | const expect = require('chai').expect;
|
6 | const connect = require('../index').connect;
|
7 | const Document = require('../index').Document;
|
8 | const validateId = require('./util').validateId;
|
9 |
|
10 | describe('NeDbClient', function() {
|
11 |
|
12 | const url = 'nedb://memory';
|
13 | let database = null;
|
14 |
|
15 | // TODO: This is acting weird. Randomly passes/fails. Seems to
|
16 | // be caused by document.test.js. When that one doesn't run,
|
17 | // this one always passes. Maybe some leftover files are still
|
18 | // floating around due to document.test.js?
|
19 | before(function(done) {
|
20 | connect(url).then(function(db) {
|
21 | database = db;
|
22 | return database.dropDatabase();
|
23 | }).then(function(){
|
24 | return done();
|
25 | });
|
26 | });
|
27 |
|
28 | beforeEach(function(done) {
|
29 | done();
|
30 | });
|
31 |
|
32 | afterEach(function(done) {
|
33 | database.dropDatabase().then(function() {}).then(done, done);
|
34 | });
|
35 |
|
36 | after(function(done) {
|
37 | done();
|
38 | });
|
39 |
|
40 | /*describe('#dropDatabase()', function() {
|
41 | it('should drop the database and delete all its data', function(done) {
|
42 |
|
43 | console.log('here-2');
|
44 |
|
45 | let data1 = getData1();
|
46 | let data2 = getData2();
|
47 |
|
48 | console.log('here-22');
|
49 |
|
50 | data1.save().then(function(d) {
|
51 | console.log('here-1');
|
52 | validateId(d);
|
53 | return data2.save();
|
54 | }).then(function(d) {
|
55 | console.log('here00');
|
56 | validateId(d);
|
57 | }).then(function() {
|
58 | console.log('here0');
|
59 | // Validate the client CREATED the necessary file(s)
|
60 | expect(_.isEmpty(database.driver())).to.not.be.true;
|
61 | return new Promise(function(resolve, reject) {
|
62 | console.log('here1');
|
63 | fs.readdir(database._path, function(error, files) {
|
64 | let dbFiles = [];
|
65 | files.forEach(function(f) {
|
66 | if (_.endsWith(f, '.db')) dbFiles.push(f);
|
67 | });
|
68 | expect(dbFiles).to.have.length(1);
|
69 | resolve();
|
70 | });
|
71 | });
|
72 | }).then(function() {
|
73 | console.log('here2');
|
74 | return database.dropDatabase();
|
75 | }).then(function() {
|
76 | console.log('here3');
|
77 | // Validate the client DELETED the necessary file(s)
|
78 | expect(_.isEmpty(database.driver())).to.be.true;
|
79 | return new Promise(function(resolve, reject) {
|
80 | console.log('here4');
|
81 | fs.readdir(database._path, function(error, files) {
|
82 | let dbFiles = [];
|
83 | files.forEach(function(f) {
|
84 | if (_.endsWith(f, '.db')) dbFiles.push(f);
|
85 | });
|
86 | expect(dbFiles).to.have.length(0);
|
87 | resolve();
|
88 | });
|
89 | });
|
90 | }).then(done, done);
|
91 | });
|
92 | });*/
|
93 |
|
94 | describe('id', function() {
|
95 | it('should allow custom _id values', function(done) {
|
96 | class School extends Document {
|
97 | constructor() {
|
98 | super();
|
99 |
|
100 | this.name = String;
|
101 | }
|
102 | }
|
103 |
|
104 | let school = School.create();
|
105 | school._id = '1234567890abcdef';
|
106 | school.name = 'South Park Elementary';
|
107 |
|
108 | school.save().then(function() {
|
109 | validateId(school);
|
110 | expect(school._id).to.be.equal('1234567890abcdef');
|
111 | return School.findOne();
|
112 | }).then(function(s) {
|
113 | validateId(s);
|
114 | expect(s._id).to.be.equal('1234567890abcdef');
|
115 | }).then(done, done);
|
116 | });
|
117 | });
|
118 |
|
119 | describe('indexes', function() {
|
120 | it('should reject documents with duplicate values in unique-indexed fields', function(done) {
|
121 | class User extends Document {
|
122 | constructor() {
|
123 | super();
|
124 |
|
125 | this.schema({
|
126 | name: String,
|
127 | email: {
|
128 | type: String,
|
129 | unique: true
|
130 | }
|
131 | });
|
132 | }
|
133 | }
|
134 |
|
135 | let user1 = User.create();
|
136 | user1.name = 'Bill';
|
137 | user1.email = 'billy@example.com';
|
138 |
|
139 | let user2 = User.create();
|
140 | user1.name = 'Billy';
|
141 | user2.email = 'billy@example.com';
|
142 |
|
143 | Promise.all([user1.save(), user2.save()]).then(function() {
|
144 | expect.fail(null, Error, 'Expected error, but got none.');
|
145 | }).catch(function(error) {
|
146 | expect(error.errorType).to.be.equal('uniqueViolated');
|
147 | }).then(done, done);
|
148 | });
|
149 |
|
150 | it('should accept documents with duplicate values in non-unique-indexed fields', function(done) {
|
151 | class User extends Document {
|
152 | constructor() {
|
153 | super();
|
154 |
|
155 | this.schema({
|
156 | name: String,
|
157 | email: {
|
158 | type: String,
|
159 | unique: false
|
160 | }
|
161 | });
|
162 | }
|
163 | }
|
164 |
|
165 | let user1 = User.create();
|
166 | user1.name = 'Bill';
|
167 | user1.email = 'billy@example.com';
|
168 |
|
169 | let user2 = User.create();
|
170 | user1.name = 'Billy';
|
171 | user2.email = 'billy@example.com';
|
172 |
|
173 | Promise.all([user1.save(), user2.save()]).then(function() {
|
174 | validateId(user1);
|
175 | validateId(user2);
|
176 | expect(user1.email).to.be.equal('billy@example.com');
|
177 | expect(user2.email).to.be.equal('billy@example.com');
|
178 | }).then(done, done);
|
179 | });
|
180 | });
|
181 | }); |
\ | No newline at end of file |