1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const expect = require('chai').expect;
|
5 | const ObjectId = require('mongodb').ObjectId;
|
6 | const connect = require('../index').connect;
|
7 | const Document = require('../index').Document;
|
8 | const validateId = require('./util').validateId;
|
9 |
|
10 | describe('MongoClient', function() {
|
11 |
|
12 | const url = 'mongodb://localhost/camo_test';
|
13 | let database = null;
|
14 |
|
15 | before(function(done) {
|
16 | connect(url).then(function(db) {
|
17 | database = db;
|
18 | return database.dropDatabase();
|
19 | }).then(function(){
|
20 | return done();
|
21 | });
|
22 | });
|
23 |
|
24 | beforeEach(function(done) {
|
25 | done();
|
26 | });
|
27 |
|
28 | afterEach(function(done) {
|
29 | database.dropDatabase().then(function() {}).then(done, done);
|
30 | });
|
31 |
|
32 | after(function(done) {
|
33 | done();
|
34 | });
|
35 |
|
36 | describe('id', function() {
|
37 | it('should allow custom _id values', function(done) {
|
38 | class School extends Document {
|
39 | constructor() {
|
40 | super();
|
41 |
|
42 | this.name = String;
|
43 | }
|
44 | }
|
45 |
|
46 | let school = School.create();
|
47 | school._id = new ObjectId('1234567890abcdef12345678');
|
48 | school.name = 'Springfield Elementary';
|
49 |
|
50 | school.save().then(function() {
|
51 | validateId(school);
|
52 | expect(school._id.toString()).to.be.equal('1234567890abcdef12345678');
|
53 | return School.findOne();
|
54 | }).then(function(s) {
|
55 | validateId(s);
|
56 | expect(s._id.toString()).to.be.equal('1234567890abcdef12345678');
|
57 | }).then(done, done);
|
58 | });
|
59 | });
|
60 |
|
61 | describe('query', function() {
|
62 | class User extends Document {
|
63 | constructor() {
|
64 | super();
|
65 | this.firstName = String;
|
66 | this.lastName = String;
|
67 | }
|
68 | }
|
69 |
|
70 | |
71 |
|
72 |
|
73 |
|
74 |
|
75 | it('should automatically cast string ID in query to ObjectID', function(done) {
|
76 | let user = User.create();
|
77 | user.firstName = 'Billy';
|
78 | user.lastName = 'Bob';
|
79 |
|
80 | user.save().then(function() {
|
81 | validateId(user);
|
82 |
|
83 | let id = String(user._id);
|
84 | return User.findOne({_id: id});
|
85 | }).then(function(u) {
|
86 | validateId(u);
|
87 | }).then(done, done);
|
88 | });
|
89 |
|
90 | |
91 |
|
92 |
|
93 |
|
94 | it('should automatically cast string ID in query to ObjectID', function(done) {
|
95 | let user = User.create();
|
96 | user.firstName = 'Billy';
|
97 | user.lastName = 'Bob';
|
98 |
|
99 | user.save().then(function() {
|
100 | validateId(user);
|
101 |
|
102 | return User.findOne({_id: user._id});
|
103 | }).then(function(u) {
|
104 | validateId(u);
|
105 | }).then(done, done);
|
106 | });
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 | it('should automatically cast string IDs in \'$in\' operator to ObjectIDs', function(done) {
|
113 | let user1 = User.create();
|
114 | user1.firstName = 'Billy';
|
115 | user1.lastName = 'Bob';
|
116 |
|
117 | let user2 = User.create();
|
118 | user2.firstName = 'Jenny';
|
119 | user2.lastName = 'Jane';
|
120 |
|
121 | let user3 = User.create();
|
122 | user3.firstName = 'Danny';
|
123 | user3.lastName = 'David';
|
124 |
|
125 | Promise.all([user1.save(), user2.save(), user3.save()]).then(function() {
|
126 | validateId(user1);
|
127 | validateId(user2);
|
128 |
|
129 | let id1 = String(user1._id);
|
130 | let id3 = String(user3._id);
|
131 | return User.find({ _id: { '$in': [ id1, id3 ] } });
|
132 | }).then(function(users) {
|
133 | expect(users).to.have.length(2);
|
134 |
|
135 | let u1 = String(users[0]._id) === String(user1._id) ? users[0] : users[1];
|
136 | let u3 = String(users[1]._id) === String(user3._id) ? users[1] : users[0];
|
137 |
|
138 | expect(String(u1._id)).to.be.equal(String(user1._id));
|
139 | expect(String(u3._id)).to.be.equal(String(user3._id));
|
140 | }).then(done, done);
|
141 | });
|
142 |
|
143 | it('should automatically cast string IDs in deep query objects', function(done) {
|
144 | let user1 = User.create();
|
145 | user1.firstName = 'Billy';
|
146 | user1.lastName = 'Bob';
|
147 |
|
148 | let user2 = User.create();
|
149 | user2.firstName = 'Jenny';
|
150 | user2.lastName = 'Jane';
|
151 |
|
152 | let user3 = User.create();
|
153 | user3.firstName = 'Danny';
|
154 | user3.lastName = 'David';
|
155 |
|
156 | Promise.all([user1.save(), user2.save(), user3.save()]).then(function() {
|
157 | validateId(user1);
|
158 | validateId(user2);
|
159 |
|
160 | let id1 = String(user1._id);
|
161 | let id3 = String(user3._id);
|
162 | return User.find({ $or: [ {_id: id1 }, {_id: id3 } ] });
|
163 | }).then(function(users) {
|
164 | expect(users).to.have.length(2);
|
165 |
|
166 | let u1 = String(users[0]._id) === String(user1._id) ? users[0] : users[1];
|
167 | let u3 = String(users[1]._id) === String(user3._id) ? users[1] : users[0];
|
168 |
|
169 | expect(String(u1._id)).to.be.equal(String(user1._id));
|
170 | expect(String(u3._id)).to.be.equal(String(user3._id));
|
171 | }).then(done, done);
|
172 | });
|
173 | });
|
174 |
|
175 | describe('indexes', function() {
|
176 | it('should reject documents with duplicate values in unique-indexed fields', function(done) {
|
177 | class User extends Document {
|
178 | constructor() {
|
179 | super();
|
180 |
|
181 | this.schema({
|
182 | name: String,
|
183 | email: {
|
184 | type: String,
|
185 | unique: true
|
186 | }
|
187 | });
|
188 | }
|
189 | }
|
190 |
|
191 | let user1 = User.create();
|
192 | user1.name = 'Bill';
|
193 | user1.email = 'billy@example.com';
|
194 |
|
195 | let user2 = User.create();
|
196 | user1.name = 'Billy';
|
197 | user2.email = 'billy@example.com';
|
198 |
|
199 | Promise.all([user1.save(), user2.save()]).then(function() {
|
200 | expect.fail(null, Error, 'Expected error, but got none.');
|
201 | }).catch(function(error) {
|
202 | expect(error instanceof Error).to.be.true;
|
203 | }).then(done, done);
|
204 | });
|
205 |
|
206 | it('should accept documents with duplicate values in non-unique-indexed fields', function(done) {
|
207 | class User extends Document {
|
208 | constructor() {
|
209 | super();
|
210 |
|
211 | this.schema({
|
212 | name: String,
|
213 | email: {
|
214 | type: String,
|
215 | unique: false
|
216 | }
|
217 | });
|
218 | }
|
219 | }
|
220 |
|
221 | let user1 = User.create();
|
222 | user1.name = 'Bill';
|
223 | user1.email = 'billy@example.com';
|
224 |
|
225 | let user2 = User.create();
|
226 | user1.name = 'Billy';
|
227 | user2.email = 'billy@example.com';
|
228 |
|
229 | Promise.all([user1.save(), user2.save()]).then(function() {
|
230 | validateId(user1);
|
231 | validateId(user2);
|
232 | expect(user1.email).to.be.equal('billy@example.com');
|
233 | expect(user2.email).to.be.equal('billy@example.com');
|
234 | }).then(done, done);
|
235 | });
|
236 | });
|
237 | }); |
\ | No newline at end of file |