UNPKG

1.41 kBJavaScriptView Raw
1var describe = require('jody').describe,
2 Model = require('../lib/index');
3
4describe("Custom views", function (spec) {
5
6 spec.beforeAll(function (done) {
7 var Album = Model.define("Album",{band: String, title: String, rating: Number});
8
9 Album.addView('BestIncubusAlbums',{
10 map: function (doc) {
11 if (doc.model_type === 'Album' && doc.band === 'Incubus' && doc.rating === 5) {
12 emit(null,doc);
13 }
14 }
15 });
16
17 Model.load(function () {
18 });
19
20 var Album = Model('Album');
21 Album.create({band: "Incubus", rating: 5, title: "Fungus Amongus"}).save();
22 Album.create({band: "Incubus", rating: 3, title: "S.C.I.E.N.C.E."}).save();
23 Album.create({band: "Incubus", rating: 3, title: "Make Yourself"}).save();
24 Album.create({band: "Incubus", rating: 5, title: "Morning View"}).save();
25 Album.create({band: "Incubus", rating: 2, title: "A Crow Left of the Murder..."}).save();
26 Album.create({band: "Incubus", rating: 3, title: "Light Grenades"}).save();
27 Album.create({band: "Incubus", rating: 5, title: "If Not Now, When?"}).save(function () {
28 done();
29
30 });
31
32 });
33
34
35
36
37 spec.it("Should be able to define custom view for model", function (async) {
38 var Album = Model('Album');
39 Album.view('BestIncubusAlbums', async( function (err, albums) {
40 albums.length.should().beEqual(3);
41 }));
42
43 });
44
45});