summaryrefslogtreecommitdiff
path: root/rel/overlay/share/www/spec/jquery_couch_js_instance_methods_3_spec.js
blob: 5d27d8173dc6e667ddedc98b7152e90994e8d9db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// Specs for jquery_couch.js lines 300-411

describe 'jQuery couchdb db'
  before
    stubAlert();
  end
  
  after
    destubAlert();
  end
  
  before_each
    db = $.couch.db('spec_db');
    db.create();
  end
  
  after_each
    db.drop();
  end

  describe 'removeDoc'
    before_each
      doc = {"Name" : "Louanne Katraine", "Callsign" : "Kat", "_id" : "345"};
      saved_doc = {};
      db.saveDoc(doc, {
        success: function(resp){
          saved_doc = resp;
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should result in a deleted document'
      db.removeDoc({_id : "345", _rev : saved_doc.rev}, {
        success: function(resp){
          db.openDoc("345", {
            error: function(status, error, reason){
              status.should.eql 404
              error.should.eql "not_found"
              reason.should.eql "deleted"
            },
            success: function(resp){successCallback(resp)}
          });
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
  
    it 'should return ok true, the ID and the revision of the deleted document'
      db.removeDoc({_id : "345", _rev : saved_doc.rev}, {
        success: function(resp){
          resp.ok.should.be_true
          resp.id.should.eql "345"
          resp.rev.should.be_a String
          resp.rev.length.should.be_at_least 30
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
      
    it 'should record the revision in the deleted document'
      db.removeDoc({_id : "345", _rev : saved_doc.rev}, {
        success: function(resp){
          db.openDoc("345", {
            rev: resp.rev,
            success: function(resp2){
              resp2._rev.should.eql resp.rev
              resp2._id.should.eql resp.id
              resp2._deleted.should.be_true
            },
            error: function(status, error, reason){errorCallback(status, error, reason)}
          });
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should alert with an error message prefix'
      db.removeDoc({_id: "asdf"});
      alert_msg.should.match /The document could not be deleted/
    end
  end
  
  describe 'bulkRemove'
    before_each
      doc  = {"Name" : "Kara Thrace", "Callsign" : "Starbuck", "_id" : "123"};
      doc2 = {"Name" : "Karl C. Agathon", "Callsign" : "Helo", "_id" : "456"};
      doc3 = {"Name" : "Sharon Valerii", "Callsign" : "Boomer", "_id" : "789"};
      docs = [doc, doc2, doc3];
      
      db.bulkSave({"docs": docs}, {
        success: function(resp){
          for (var i = 0; i < docs.length; i++) {
            docs[i]._rev = resp[i].rev;
          }
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should remove all documents specified'
      db.bulkRemove({"docs": docs});
      db.allDocs({
        success: function(resp) {
          resp.total_rows.should.eql 0
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should not remove documents that should not have been deleted'
      db.bulkRemove({"docs": [doc3]});
      db.allDocs({
        success: function(resp) {
          resp.total_rows.should.eql 2
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should result in deleted documents'
      db.bulkRemove({"docs": docs}, {
        success: function(resp){
          db.openDoc("123", {
            error: function(status, error, reason){
              status.should.eql 404
              error.should.eql "not_found"
              reason.should.eql "deleted"
            },
            success: function(resp){successCallback(resp)}
          });
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
  
    it 'should return the ID and the revision of the deleted documents'
      db.bulkRemove({"docs": docs}, {
        success: function(resp){
          resp[0].id.should.eql "123"
          resp[0].rev.should.be_a String
          resp[0].rev.length.should.be_at_least 30
          resp[1].id.should.eql "456"
          resp[1].rev.should.be_a String
          resp[1].rev.length.should.be_at_least 30
          resp[2].id.should.eql "789"
          resp[2].rev.should.be_a String
          resp[2].rev.length.should.be_at_least 30
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
  
    it 'should record the revision in the deleted documents'
      db.bulkRemove({"docs": docs}, {
        success: function(resp){
          db.openDoc("123", {
            rev: resp[0].rev,
            success: function(resp2){
              resp2._rev.should.eql resp[0].rev
              resp2._id.should.eql resp[0].id
              resp2._deleted.should.be_true
            },
            error: function(status, error, reason){errorCallback(status, error, reason)}
          });
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should alert with an error message prefix'
      db.bulkRemove({docs: ["asdf"]});
      alert_msg.should.match /The documents could not be deleted/
    end
  end
  
  describe 'copyDoc'
    before_each
      doc = {"Name" : "Sharon Agathon", "Callsign" : "Athena", "_id" : "123"};
      db.saveDoc(doc);
    end
    
    it 'should result in another document with same data and new id'
      db.copyDoc("123", {
        success: function(resp){
          resp.id.should.eql "456"
          resp.rev.length.should.be_at_least 30
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      }, {
        headers: {"Destination":"456"}
      });
      
      db.openDoc("456", {
        success: function(resp){
          resp.Name.should.eql "Sharon Agathon"
          resp.Callsign.should.eql "Athena"
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should throw an error when trying to overwrite a document without providing a revision'
      doc2 = {"Name" : "Louanne Katraine", "Callsign" : "Kat", "_id" : "456"};
      db.saveDoc(doc2);
      
      db.copyDoc("123", {
        error: function(status, error, reason){
          status.should.eql 409
          error.should.eql "conflict"
          reason.should.eql "Document update conflict."
        },
        success: function(resp){successCallback(resp)}
      }, {
        headers: {"Destination":"456"}
      });
    end
    
    it 'should overwrite a document with the correct revision'
      doc2 = {"Name" : "Louanne Katraine", "Callsign" : "Kat", "_id" : "456"};
      var doc2_rev;
      db.saveDoc(doc2, {
        success: function(resp){
          doc2_rev = resp.rev;
        }
      });
      
      db.copyDoc("123", {
        success: function(resp){
          resp.id.should.eql "456"
          resp.rev.length.should.be_at_least 30
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      }, {
        headers: {"Destination":"456?rev=" + doc2_rev}
      });
      
      db.openDoc("456", {
        success: function(resp){
          resp.Name.should.eql "Sharon Agathon"
          resp.Callsign.should.eql "Athena"
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should alert with an error message prefix'
      db.copyDoc("asdf", {}, {});
      alert_msg.should.match /The document could not be copied/
    end
  end
  
  describe 'query'
    before_each
      db.saveDoc({"Name" : "Cally Tyrol",      "job" : "deckhand", "_id" : "789"});
      db.saveDoc({"Name" : "Felix Gaeta",      "job" : "officer",  "_id" : "123"});
      db.saveDoc({"Name" : "Samuel T. Anders", "job" : "pilot",    "_id" : "456"});
      map_function    = "function(doc) { emit(doc._id, 1); }";
      reduce_function = "function(key, values, rereduce) { return sum(values); }";
    end
    
    it 'should apply the map function'
      db.query(map_function, null, null, {
        success: function(resp){
          resp.rows.should.have_length 3
          resp.rows[0].id.should.eql "123"
          resp.rows[0].key.should.eql "123"
          resp.rows[0].value.should.eql 1
          resp.rows[1].id.should.eql "456"
          resp.rows[1].key.should.eql "456"
          resp.rows[1].value.should.eql 1
          resp.rows[2].id.should.eql "789"
          resp.rows[2].key.should.eql "789"
          resp.rows[2].value.should.eql 1
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should apply the reduce function'
      db.query(map_function, reduce_function, null, {
        success: function(resp){
          resp.rows.should.have_length 1
          resp.rows[0].key.should.be_null
          resp.rows[0].value.should_eql 3
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should pass through the options'
      db.query(map_function, null, null, {
        "startkey": "456",
        success: function(resp){
          resp.rows.should.have_length 2
          resp.rows[0].id.should.eql "456"
          resp.rows[0].key.should.eql "456"
          resp.rows[0].value.should.eql 1
          resp.rows[1].id.should.eql "789"
          resp.rows[1].key.should.eql "789"
          resp.rows[1].value.should.eql 1
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should pass through the keys'
      //shouldn't this better work? TODO: implement in jquery.couch.js
      console.log("shouldn't this better work? TODO: implement in jquery.couch.js")
      db.query(map_function, null, null, {
        "keys": ["456", "123"],
        success: function(resp){
          resp.rows.should.have_length 2
          resp.rows[0].id.should.eql "456"
          resp.rows[0].key.should.eql "456"
          resp.rows[0].value.should.eql 1
          resp.rows[1].id.should.eql "123"
          resp.rows[1].key.should.eql "123"
          resp.rows[1].value.should.eql 1
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
      
    it 'should pass through the options and the keys'
      //shouldn't this better work? TODO: implement in jquery.couch.js
      console.log("shouldn't this better work? TODO: implement in jquery.couch.js")
      db.query(map_function, null, null, {
        "include_docs":"true",
        "keys": ["456"],
        success: function(resp){
          resp.rows.should.have_length 1
          resp.rows[0].id.should.eql "456"
          resp.rows[0].key.should.eql "456"
          resp.rows[0].value.should.eql 1
          resp.rows[0].doc["job"].should.eql "pilot"
          resp.rows[0].doc["_rev"].length.should.be_at_least 30
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
      
    it 'should apply a query in erlang also'
      // when this test fails, read this: http://wiki.apache.org/couchdb/EnableErlangViews
      var erlang_map = 'fun({Doc}) -> ' +
                       'ID = proplists:get_value(<<"_id">>, Doc, null), ' +
                       'Emit(ID, 1) ' +
                       'end.';
      db.query(erlang_map, null, "erlang", {
        success: function(resp){
          resp.rows.should.have_length 3
          resp.rows[0].id.should.eql "123"
          resp.rows[0].key.should.eql "123"
          resp.rows[0].value.should.eql 1
          resp.rows[1].id.should.eql "456"
          resp.rows[1].key.should.eql "456"
          resp.rows[1].value.should.eql 1
          resp.rows[2].id.should.eql "789"
          resp.rows[2].key.should.eql "789"
          resp.rows[2].value.should.eql 1
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should alert with an error message prefix'
      db.query("asdf");
      alert_msg.should.match /An error occurred querying the database/
    end
  end
  
  describe 'view'
    before_each
      db.saveDoc({"Name" : "Cally Tyrol",      "job" : "deckhand", "_id" : "789"});
      db.saveDoc({"Name" : "Felix Gaeta",      "job" : "officer",  "_id" : "123"});
      db.saveDoc({"Name" : "Samuel T. Anders", "job" : "pilot",    "_id" : "456"});
      view = {
        "views" : {
          "people" : {
            "map" : "function(doc) { emit(doc._id, doc.Name); }"
          }
        },
        "_id" : "_design/spec_db"
      };
      db.saveDoc(view);
    end
    
    it 'should apply the view'
      db.view('spec_db/people', {
        success: function(resp){
          resp.rows.should.have_length 3
          resp.rows[0].id.should.eql "123"
          resp.rows[0].key.should.eql "123"
          resp.rows[0].value.should.eql "Felix Gaeta"
          resp.rows[1].id.should.eql "456"
          resp.rows[1].key.should.eql "456"
          resp.rows[1].value.should.eql "Samuel T. Anders"
          resp.rows[2].id.should.eql "789"
          resp.rows[2].key.should.eql "789"
          resp.rows[2].value.should.eql "Cally Tyrol"
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should pass through the options'
      db.view('spec_db/people', {
        "skip":"2",
        success: function(resp){
          resp.rows.should.have_length 1
          resp.rows[0].id.should.eql "789"
          resp.rows[0].key.should.eql "789"
          resp.rows[0].value.should.eql "Cally Tyrol"
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should pass through the keys'
      db.view('spec_db/people', {
        "keys":["456", "123"],
        success: function(resp){
          resp.rows.should.have_length 2
          resp.rows[0].id.should.eql "456"
          resp.rows[0].key.should.eql "456"
          resp.rows[0].value.should.eql "Samuel T. Anders"
          resp.rows[1].id.should.eql "123"
          resp.rows[1].key.should.eql "123"
          resp.rows[1].value.should.eql "Felix Gaeta"
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should pass through the options and the keys'
      db.view('spec_db/people', {
        "include_docs":"true",
        "keys":["456"],
        success: function(resp){
          resp.rows.should.have_length 1
          resp.rows[0].id.should.eql "456"
          resp.rows[0].key.should.eql "456"
          resp.rows[0].value.should.eql "Samuel T. Anders"
          resp.rows[0].doc["job"].should.eql "pilot"
          resp.rows[0].doc["_rev"].length.should.be_at_least 30
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should throw a 404 when the view doesnt exist'
      db.view('spec_db/non_existing_view', {
        error: function(status, error, reason){
          status.should.eql 404
          error.should.eql "not_found"
          reason.should.eql "missing_named_view"
        },
        success: function(resp){successCallback(resp)}
      });
    end
    
    it 'should alert with an error message prefix'
      db.view("asdf");
      alert_msg.should.match /An error occurred accessing the view/
    end
  end
  
  describe 'setDbProperty'
    it 'should return ok true'
      db.setDbProperty("_revs_limit", 1500, {
        success: function(resp){
          resp.ok.should.be_true
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should set a db property'
      db.setDbProperty("_revs_limit", 1500);
      db.getDbProperty("_revs_limit", {
        success: function(resp){
          resp.should.eql 1500
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
      db.setDbProperty("_revs_limit", 1200);
      db.getDbProperty("_revs_limit", {
        success: function(resp){
          resp.should.eql 1200
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
    
    it 'should alert with an error message prefix'
      db.setDbProperty("asdf");
      alert_msg.should.match /The property could not be updated/
    end
  end
  
  describe 'getDbProperty'
    it 'should get a db property'
      db.setDbProperty("_revs_limit", 1200);
      db.getDbProperty("_revs_limit", {
        success: function(resp){
          resp.should.eql 1200
        },
        error: function(status, error, reason){errorCallback(status, error, reason)}
      });
    end
   
    it 'should throw a 404 when the property doesnt exist'
      db.getDbProperty("_doesnt_exist", {
        error: function(status, error, reason){
          status.should.eql 404
          error.should.eql "not_found"
          reason.should.eql "missing"
        },
        success: function(resp){successCallback(resp)}
      });
    end
    
    it 'should alert with an error message prefix'
      db.getDbProperty("asdf");
      alert_msg.should.match /The property could not be retrieved/
    end
  end
end