1 | var should= require('chai').should(),
|
2 | assert= require('chai').assert,
|
3 | AWS = require('aws-sdk'),
|
4 | _= require('underscore'),
|
5 | dyngo= require('../index.js');
|
6 |
|
7 | const noerr= function (done)
|
8 | {
|
9 | return function (err)
|
10 | {
|
11 | should.not.exist(err);
|
12 | done();
|
13 | };
|
14 | },
|
15 | accept= function (code,done)
|
16 | {
|
17 | return function (err)
|
18 | {
|
19 | if (err.code==code)
|
20 | done();
|
21 | else
|
22 | done(err);
|
23 | };
|
24 | };
|
25 |
|
26 |
|
27 | describe('database',function ()
|
28 | {
|
29 | var db;
|
30 |
|
31 | before(function (done)
|
32 | {
|
33 | dyngo({ dynamo: { endpoint: new AWS.Endpoint('http://localhost:8000') }, hints: false },
|
34 | function (err,_db)
|
35 | {
|
36 | db= _db;
|
37 | db.test.remove().success(done)
|
38 | .error(done);
|
39 | });
|
40 | });
|
41 |
|
42 | beforeEach(function (done)
|
43 | {
|
44 | db.test.remove().success(done)
|
45 | .error(done);
|
46 | });
|
47 |
|
48 | it('Can connect', function (done)
|
49 | {
|
50 | should.exist(db);
|
51 | done();
|
52 | });
|
53 |
|
54 | it('test table should be empty', function (done)
|
55 | {
|
56 | db.test.find().results(function (items)
|
57 | {
|
58 | items.length.should.equal(0);
|
59 | done();
|
60 | })
|
61 | .error(noerr(done));
|
62 | });
|
63 |
|
64 | describe('save',function ()
|
65 | {
|
66 | it('Can insert a new object, and then find it', function (done)
|
67 | {
|
68 | var _noerr= noerr(done);
|
69 |
|
70 | db.test.save({ somedata: 'ok' })
|
71 | .success(function ()
|
72 | {
|
73 | db.test.findOne({ somedata: 'ok' })
|
74 | .result(function (obj)
|
75 | {
|
76 | obj.somedata.should.equal('ok');
|
77 | done();
|
78 | })
|
79 | .error(_noerr);
|
80 | })
|
81 | .error(_noerr);
|
82 | });
|
83 |
|
84 | it('Can insert an object with a child object, and have it back', function (done)
|
85 | {
|
86 | var _noerr= noerr(done);
|
87 |
|
88 | db.test.save({ somedata: 'parent', child: { somedata: 'child' } })
|
89 | .success(function ()
|
90 | {
|
91 | db.test.findOne({ somedata: 'parent' })
|
92 | .result(function (obj)
|
93 | {
|
94 | obj.somedata.should.equal('parent');
|
95 | obj.child.somedata.should.equal('child');
|
96 | done();
|
97 | })
|
98 | .error(_noerr);
|
99 | })
|
100 | .error(_noerr);
|
101 | });
|
102 |
|
103 | it('Can insert an object with a child object, and then remove the child', function (done)
|
104 | {
|
105 | var _noerr= noerr(done);
|
106 |
|
107 | var par= { somedata: 'parentrchild', child: { somedata: 'child' } };
|
108 |
|
109 | db.test.save(par)
|
110 | .success(function ()
|
111 | {
|
112 | delete par.child;
|
113 |
|
114 | db.test.save(par)
|
115 | .success(function ()
|
116 | {
|
117 | db.test.findOne({ _id: par._id })
|
118 | .result(function (obj)
|
119 | {
|
120 | obj.somedata.should.equal('parentrchild');
|
121 | should.not.exist(obj.child);
|
122 | done();
|
123 | })
|
124 | .error(_noerr);
|
125 | })
|
126 | .error(_noerr);
|
127 | })
|
128 | .error(_noerr);
|
129 | });
|
130 |
|
131 | it('Can insert an object with a child object, and then change the type of the child', function (done)
|
132 | {
|
133 | var _noerr= noerr(done);
|
134 |
|
135 | var par= { somedata: 'parentrchild', child: { somedata: 'child' } };
|
136 |
|
137 | db.test.save(par)
|
138 | .success(function ()
|
139 | {
|
140 | par.child= 'child';
|
141 |
|
142 | db.test.save(par)
|
143 | .success(function ()
|
144 | {
|
145 | db.test.findOne({ _id: par._id })
|
146 | .result(function (obj)
|
147 | {
|
148 | obj.somedata.should.equal('parentrchild');
|
149 | obj.child.should.equal('child');
|
150 | done();
|
151 | })
|
152 | .error(_noerr);
|
153 | })
|
154 | .error(_noerr);
|
155 | })
|
156 | .error(_noerr);
|
157 | });
|
158 |
|
159 | it('Can insert an object with a child object array, and have it back', function (done)
|
160 | {
|
161 | var _noerr= noerr(done);
|
162 |
|
163 | db.test.save({ somedata: 'parentarr', childs: [{ somedata: 'child1' },{ somedata: 'child2' },{ somedata: 'child3' }] })
|
164 | .success(function ()
|
165 | {
|
166 | db.test.findOne({ somedata: 'parentarr' })
|
167 | .result(function (obj)
|
168 | {
|
169 | obj.somedata.should.equal('parentarr');
|
170 | obj.childs.length.should.equal(3);
|
171 | _.pluck(obj.childs,'somedata').should.contain('child1');
|
172 | _.pluck(obj.childs,'somedata').should.contain('child2');
|
173 | _.pluck(obj.childs,'somedata').should.contain('child3');
|
174 | done();
|
175 | })
|
176 | .error(_noerr);
|
177 | })
|
178 | .error(_noerr);
|
179 | });
|
180 |
|
181 | it('Can insert an object with a child object array, and then update the parent without loosing the child object array', function (done)
|
182 | {
|
183 | var _noerr= noerr(done);
|
184 |
|
185 | var par= { somedata: 'parentrarrupd', childs: [{ somedata: 'child1' },{ somedata: 'child2' },{ somedata: 'child3' }] };
|
186 |
|
187 | db.test.save(par)
|
188 | .success(function ()
|
189 | {
|
190 | db.test.save(par)
|
191 | .success(function ()
|
192 | {
|
193 | db.test.findOne({ _id: par._id })
|
194 | .result(function (obj)
|
195 | {
|
196 | obj.somedata.should.equal('parentrarrupd');
|
197 | should.exist(obj.childs);
|
198 | obj.childs.length.should.equal(3);
|
199 | _.pluck(obj.childs,'somedata').should.contain('child1');
|
200 | _.pluck(obj.childs,'somedata').should.contain('child2');
|
201 | _.pluck(obj.childs,'somedata').should.contain('child3');
|
202 | done();
|
203 | })
|
204 | .error(_noerr);
|
205 | })
|
206 | .error(_noerr);
|
207 | })
|
208 | .error(_noerr);
|
209 | });
|
210 |
|
211 | it('Can insert an object with a child object array, and then remove the child object array', function (done)
|
212 | {
|
213 | var _noerr= noerr(done);
|
214 |
|
215 | var par= { somedata: 'parentrarrrm', childs: [{ somedata: 'child1' },{ somedata: 'child2' },{ somedata: 'child3' }] };
|
216 |
|
217 | db.test.save(par)
|
218 | .success(function ()
|
219 | {
|
220 | delete par.childs;
|
221 |
|
222 | db.test.save(par)
|
223 | .success(function ()
|
224 | {
|
225 | db.test.findOne({ _id: par._id })
|
226 | .result(function (obj)
|
227 | {
|
228 | obj.somedata.should.equal('parentrarrrm');
|
229 | should.not.exist(obj.childs);
|
230 | done();
|
231 | })
|
232 | .error(_noerr);
|
233 | })
|
234 | .error(_noerr);
|
235 | })
|
236 | .error(_noerr);
|
237 | });
|
238 |
|
239 | it('Can insert an object with a child object array, and then change the type of the child array', function (done)
|
240 | {
|
241 | var _noerr= noerr(done);
|
242 |
|
243 | var par= { somedata: 'parentcarr', childs: [{ somedata: 'child1' },{ somedata: 'child2' },{ somedata: 'child3' }] };
|
244 |
|
245 | db.test.save(par)
|
246 | .success(function ()
|
247 | {
|
248 | par.childs= ['child1','child2','child3'];
|
249 |
|
250 | db.test.save(par)
|
251 | .success(function ()
|
252 | {
|
253 | db.test.findOne({ _id: par._id })
|
254 | .result(function (obj)
|
255 | {
|
256 | obj.somedata.should.equal('parentcarr');
|
257 | obj.childs.length.should.equal(3);
|
258 | obj.childs.should.contain('child1');
|
259 | obj.childs.should.contain('child2');
|
260 | obj.childs.should.contain('child3');
|
261 | done();
|
262 | })
|
263 | .error(_noerr);
|
264 | })
|
265 | .error(_noerr);
|
266 | })
|
267 | .error(_noerr);
|
268 | });
|
269 |
|
270 | it('supports Date objects', function (done)
|
271 | {
|
272 | var _noerr= noerr(done), d= new Date();
|
273 |
|
274 | var par= { val: d };
|
275 |
|
276 | db.test.save(par)
|
277 | .success(function ()
|
278 | {
|
279 | db.test.findOne({ _id: par._id })
|
280 | .result(function (obj)
|
281 | {
|
282 | should.exist(obj.val);
|
283 | obj.val.should.equal(d.toISOString());
|
284 | done();
|
285 | })
|
286 | .error(_noerr);
|
287 | })
|
288 | .error(_noerr);
|
289 | });
|
290 | });
|
291 |
|
292 | describe('remove',function ()
|
293 | {
|
294 | it('Can delete objects found by _id, with client side filtering', function (done)
|
295 | {
|
296 | // if filter attributes are not included in projection by the finder
|
297 | // when it can't filter them, this test should fail
|
298 | db.test.save({ _id: 'toremove', uid: 'andrea' })
|
299 | .success(function ()
|
300 | {
|
301 | db.test.remove({ _id: 'toremove', uid: 'andrea' })
|
302 | .success(function ()
|
303 | {
|
304 | db.test.findOne({ _id: 'toremove', uid: 'andrea' })
|
305 | .result(should.not.exist)
|
306 | .error(accept('notfound',done));
|
307 | })
|
308 | .error(done);
|
309 | })
|
310 | .error(done);
|
311 | });
|
312 | });
|
313 | });
|