UNPKG

6.88 kBJavaScriptView Raw
1'use strict';
2
3var
4 assert = require('assert'),
5 fixtures = require('./fixtures'),
6 customMapper = function (name) {
7 return 'q' + name.charAt(0).toUpperCase() + name.substring(1);
8 },
9 // NOTE: without spread option!
10 mongoose = require('../index')(require('mongoose'), {mapper: customMapper}),
11 funcNames = require('../func_names'),
12 schemas = require('./schemas'),
13 UserModel = mongoose.model('User', schemas.UserSchema),
14 PostModel = mongoose.model('Post', schemas.PostSchema),
15 debug = require('debug')('test');
16
17describe('mongooseq', function () {
18 beforeEach(function (done) {
19 var fixturesLoader = require('pow-mongodb-fixtures').connect('test');
20 fixturesLoader.clearAndLoad(fixtures, function (err) {
21 if (err) throw err;
22 fixturesLoader.client.close();
23 mongoose.connect('mongodb://localhost/test');
24 done();
25 });
26 });
27 afterEach(function (done) {
28 mongoose.disconnect();
29 done();
30 });
31 it('should wrap model statics', function () {
32 funcNames.MODEL_STATICS.forEach(function (funcName) {
33 debug(funcName);
34 assert(typeof UserModel[customMapper(funcName)] === 'function');
35 });
36 });
37 it('should wrap model methods', function () {
38 var model = new UserModel();
39 funcNames.MODEL_METHODS.forEach(function (funcName) {
40 debug(funcName);
41 assert(typeof model[customMapper(funcName)] === 'function');
42 });
43 });
44 it('should wrap query methods', function () {
45 var query = UserModel.find();
46 funcNames.QUERY_METHODS.forEach(function (funcName) {
47 debug(funcName);
48 assert(typeof query[customMapper(funcName)] === 'function');
49 });
50 });
51 it('should wrap aggregate methods', function () {
52 var aggregate = UserModel.aggregate();
53 funcNames.AGGREGATE_METHODS.forEach(function (funcName) {
54 debug(funcName);
55 assert(typeof aggregate[customMapper(funcName)] === 'function');
56 });
57 });
58 it('should findById and populate', function (done) {
59 PostModel.qFindById(fixtures.posts.p1._id)
60 .then(function (result) {
61 debug('Model.findById-->', result);
62 assert.ok(result);
63 return result.qPopulate('author');
64 })
65 .then(function (result) {
66 debug('Model#populate-->', result);
67 assert.ok(result);
68 assert.ok(result.author._id);
69 assert.ok(result.author.name);
70 })
71 .catch(assert.ifError)
72 .done(done);
73 });
74 it('should findById__and__exec', function (done) {
75 PostModel.findById(fixtures.posts.p1._id).qExec()
76 .then(function (result) {
77 debug('Model.findById and Query#exec-->', result);
78 assert.ok(result);
79 })
80 .catch(assert.ifError)
81 .done(done);
82 });
83 it('should create', function (done) {
84 UserModel.qCreate({name: 'hello'}, {name: 'world'})
85 .then(function (createdUser1, createdUser2) {
86 debug('Model.create:', arguments);
87 assert.equal(createdUser1.name, 'hello');
88 // NOTE: you couldn't get remaing result without 'spread' option!
89 assert.ok(typeof createdUser2 === 'undefined');
90 })
91 .catch(assert.ifError)
92 .done(done);
93 });
94 it('should update', function (done) {
95 PostModel.qUpdate({_id: fixtures.posts.p1._id}, { title: 'changed'})
96 .then(function (affectedRows, raw) {
97 debug('Model.update:', arguments);
98 assert.equal(affectedRows, 1);
99 // NOTE: you couldn't get remaing result without 'spread' option!
100 assert.ok(typeof raw === 'undefined');
101 })
102 .catch(assert.ifError)
103 .done(done);
104 });
105 it('should save', function (done) {
106 var post = new PostModel();
107 post.__pre_save_called = false;
108 post.__post_save_called = false;
109 post.title = 'new-title';
110 post.author = fixtures.users.u1._id;
111 assert.ok(post.isNew);
112 post.qSave()
113 .then(function (result, affectedRows) {
114 debug('Model#save-->', arguments);
115 assert.ok(result);
116 assert.ok(!result.isNew);
117 assert.ok(result._id);
118 assert.equal(result.title, 'new-title');
119 assert.equal(result.author.toString(), fixtures.users.u1._id.toString());
120 // NOTE: you couldn't get remaing result without 'spread' option!
121 assert.ok(typeof affectedRows === 'undefined');
122 })
123 .catch(assert.ifError)
124 .done(function () {
125 assert(post.__pre_save_called && post.__post_save_called);
126 done();
127 });
128 });
129 it('should aggregate', function (done) {
130 // fix issue6
131 var agg = UserModel.aggregate();
132 agg
133 .match({name: {$regex: '^b.*' }})
134 .qExec()
135 .then(function (result) {
136 debug('result:', result);
137 assert.ok(result);
138 })
139 .catch(assert.ifError)
140 .done(done);
141 });
142 it('should assert issue2', function (done) {
143 UserModel.qFindById(fixtures.users.u1._id)
144 .then(function (user) {
145 return [ user, PostModel.find().populate('author').qExec() ];
146 })
147 .spread(function (user, users) {
148 debug('user:', user);
149 debug('users:', users);
150 assert.ok(user);
151 assert.ok(users);
152 })
153 .catch(assert.ifError)
154 .done(done);
155 });
156 it('should assert issue22', function (done) {
157 PostModel.qFindById(fixtures.posts.p1._id)
158 .then(function (result) {
159 debug('Model.findById-->', result);
160 assert.ok(result);
161 return result.qPopulate([
162 {path:'author'},
163 {path:'comments.author'}
164 ]);
165 })
166 .then(function (result) {
167 debug('Model#populate author-->', result.author);
168 assert.ok(result);
169 assert.ok(result.author._id);
170 assert.ok(result.author.name);
171 result.comments.forEach(function (comment) {
172 debug('Model#populate comments.author-->', comment.author);
173 assert.ok(comment.author._id);
174 assert.ok(comment.author.name);
175 });
176 })
177 .catch(assert.ifError)
178 .done(done);
179 });
180});