UNPKG

5.76 kBJavaScriptView Raw
1
2/*
3 * @list dependencies
4 */
5
6var vows, assert, mongoose;
7
8vows = require('vows');
9assert = require('assert');
10mongoose = require('../lib/mongoose-paginate');
11
12/*
13 * connect to MongoDB with Mongoose
14 */
15
16mongoose.connect(process.env.MONGO_DB || 'mongodb://localhost/test');
17
18/*
19 * @tests setup
20 */
21
22var TestSchema = new mongoose.Schema({
23 id : mongoose.Schema.ObjectId,
24 title : String,
25 date : Date,
26 child : { type: mongoose.Schema.ObjectId, ref: 'TestSubEntries' }
27});
28
29var TestEntry = mongoose.model('TestEntries', TestSchema);
30
31var TestSubSchema = new mongoose.Schema({
32 id : mongoose.Schema.ObjectId,
33 title : String,
34 date : Date
35});
36
37var TestSubEntry = mongoose.model('TestSubEntries', TestSubSchema);
38
39function setup(callback) {
40 var newSubEntry = new TestSubEntry({
41 title: 'SubItem #1',
42 });
43 newSubEntry.save(function(error, subEntry) {
44 var complete = 0;
45 for (var i = 1; i < 101; i++) {
46 var newEntry = new TestEntry({
47 title : 'Item #'+i,
48 child : subEntry._id,
49 });
50 newEntry.save(function(error, result) {
51 if (error) {
52 console.error(error);
53 } else {
54 complete++;
55 if (complete === 100) {
56 callback(null, 100);
57 }
58 }
59 });
60 }
61 });
62}
63
64/*
65 * teardown
66 */
67
68function teardown(callback){
69 TestSubEntry.remove({}, function(error) {
70 if (error) {
71 callback(error, null);
72 } else {
73 var complete = 0;
74 TestEntry.find({}, function(error, results) {
75 if (error) {
76 callback(error, null);
77 } else {
78 for (result in results) {
79 results[result].remove(function(error) {
80 if (error) {
81 callback(error, null);
82 } else {
83 complete++;
84 if (complete === 100) {
85 callback(null, 100);
86 }
87 }
88 });
89 }
90 }
91 });
92 }
93 });
94};
95
96/*
97 * @tests vows
98 */
99
100vows.describe('pagination module basic tests')
101
102.addBatch({
103 'when requiring `mongoose-paginate`':{
104 topic:function(){
105 return mongoose;
106 },
107 'there should be no errors and paginate should be an object':function(topic) {
108 assert.equal(typeof(topic), 'object');
109 }
110 }
111})
112
113.addBatch({
114 'when creating 100 dummy documents with our test mongodb string':{
115 topic:function(){
116 setup(this.callback);
117 },
118 'there should be no errors and resultCount should be 100':function(error, resultCount) {
119 assert.equal(error, null);
120 assert.equal(resultCount, 100);
121 }
122 }
123})
124
125.addBatch({
126 'when paginating TestEntry querying for all documents, with page 1, 10 results per page':{
127 topic:function(){
128 TestEntry.paginate({}, 1, 10, this.callback, { columns: 'title' });
129 },
130 'there should be no errors':function(error, pageCount, results) {
131 assert.equal(error, null);
132 },
133 'results.length should be 10':function(error, pageCount, results) {
134 assert.equal(results.length, 10);
135 },
136 'the first result should contain the correct index #(1)':function(error, pageCount, results) {
137 assert.equal(results[0].title, 'Item #1');
138 }
139 }
140})
141
142.addBatch({
143 'when paginating TestEntry querying for all documents, with page 2, 10 results per page':{
144 topic:function(){
145 TestEntry.paginate({}, 2, 10, this.callback, { columns: 'title' });
146 },
147 'there should be no errors':function(error, pageCount, results, count) {
148 assert.equal(error, null);
149 },
150 'results.length should be 10':function(error, pageCount, results, count) {
151 assert.equal(results.length, 10);
152 },
153 'the first result should contain the correct index #(11)':function(error, pageCount, results, count) {
154 assert.equal(results[0].title, 'Item #11');
155 },
156 'there should be 100 items as results':function(error, pageCount, results, count) {
157 assert.equal(count, 100);
158 }
159 }
160})
161
162.addBatch({
163 'when paginating TestEntry querying for all documents, with page 10, 11 results per page':{
164 topic:function(){
165 TestEntry.paginate({}, 10, 10, this.callback, { columns: 'title' });
166 },
167 'there should be no errors':function(error, pageCount, results, count) {
168 assert.equal(error, null);
169 },
170 'results.length should be 10':function(error, pageCount, results, count) {
171 assert.equal(results.length, 10);
172 },
173 'the first result should contain the correct index #(100)':function(error, pageCount, results, count) {
174 assert.equal(results[9].title, 'Item #100');
175 }
176 }
177})
178
179.addBatch({
180 'when paginating TestEntry querying for all documents, with page 2, 10 results per page with populate and without columns':{
181 topic:function(){
182 TestEntry.paginate({}, 2, 10, this.callback, { populate: 'child' });
183 },
184 'there should be no errors':function(error, pageCount, results, count) {
185 assert.equal(error, null);
186 },
187 'results.length should be 10':function(error, pageCount, results, count) {
188 assert.equal(results.length, 10);
189 },
190 'the first result should contain the correct index #(11)':function(error, pageCount, results, count) {
191 assert.equal(results[0].title, 'Item #11');
192 },
193 'the first result should contain the correct SubItem #(1)':function(error, pageCount, results, count) {
194 assert.equal(results[0].child.title, 'SubItem #1');
195 }
196 }
197})
198
199.addBatch({
200 'when deleting all of our 100 dummy documents with our test mongodb string':{
201 topic:function(){
202 teardown(this.callback);
203 },
204 'there should be no errors and resultCount should be a number':function(error, resultCount) {
205 assert.equal(error, null);
206 assert.equal(resultCount, 100);
207 }
208 }
209})
210
211.export(module);