deno.land / x / mongoose@6.7.5 / test / model.populate.divergent.test.js

model.populate.divergent.test.js
نووسراو ببینە
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

/** * Test dependencies. */
'use strict';
const start = require('./common');
const assert = require('assert');
const mongoose = start.mongoose;const DivergentArrayError = mongoose.Error.DivergentArrayError;
/** * Tests. */
describe('model: populate: divergent arrays', function() { // match // skip // limit // -_id // // $set // $pop -1 // $pop 1
let db, C, M;
before(async function() { db = start(); C = db.model('Child', { _id: Number, name: String }); M = db.model('Parent', { array: { type: [{ type: Number, ref: 'Child' }] } });
await C.create( { _id: 0, name: 'zero' }, { _id: 1, name: 'one' }, { _id: 2, name: 'two' } );
await M.create({ array: [0, 1, 2] }); });
after(async function() { await db.close(); });
function test(check, fn) { it('using $set', function(done) { fn(function(err, doc) { assert.ifError(err); doc.array.unshift({ _id: 10, name: 'ten' }); doc.save(function(err) { check(err); done(); }); }); }); it('using $pop 1', function(done) { fn(function(err, doc) { assert.ifError(err); doc.array.$pop(); doc.save(function(err) { check(err); done(); }); }); }); it('using $pop -1', function(done) { fn(function(err, doc) { assert.ifError(err); doc.array.$shift(); doc.save(function(err) { check(err); done(); }); }); }); }
function testOk(fn) { test(assert.ifError.bind(assert), fn); }
function testFails(fn) { test(function(err) { assert.ok(err instanceof DivergentArrayError, 'non-divergent error: ' + err); assert.ok(/\sarray/.test(err.message)); }, fn); }
describe('from match', function() { testFails(function(cb) { M.findOne().populate({ path: 'array', match: { name: 'one' } }).exec(cb); }); }); describe('from skip', function() { describe('2', function() { testFails(function(cb) { M.findOne().populate({ path: 'array', options: { skip: 2 } }).exec(cb); }); }); describe('0', function() { testOk(function(cb) { M.findOne().populate({ path: 'array', options: { skip: 0 } }).exec(cb); }); }); }); describe('from limit', function() { describe('0', function() { testFails(function(cb) { M.findOne().populate({ path: 'array', options: { limit: 0 } }).exec(cb); }); }); describe('1', function() { testFails(function(cb) { M.findOne().populate({ path: 'array', options: { limit: 1 } }).exec(cb); }); }); }); describe('from deselected _id', function() { describe('using string and only -_id', function() { testFails(function(cb) { M.findOne().populate({ path: 'array', select: '-_id' }).exec(cb); }); }); describe('using string', function() { testFails(function(cb) { M.findOne().populate({ path: 'array', select: 'name -_id' }).exec(cb); }); }); describe('using object and only _id: 0', function() { testFails(function(cb) { M.findOne().populate({ path: 'array', select: { _id: 0 } }).exec(cb); }); }); describe('using object', function() { testFails(function(cb) { M.findOne().populate({ path: 'array', select: { _id: 0, name: 1 } }).exec(cb); }); }); });});
mongoose

Version Info

Tagged at
a year ago