deno.land / x / mongoose@6.7.5 / test / model.hydrate.test.js

model.hydrate.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
/** * Test dependencies. */
'use strict';
const start = require('./common');
const assert = require('assert');
const mongoose = start.mongoose;const Schema = mongoose.Schema;const DocumentObjectId = mongoose.Types.ObjectId;
describe('model', function() { let schemaB; let schemaC;
before(function() { schemaB = new Schema({ title: String, type: String }, { discriminatorKey: 'type' });
schemaC = new Schema({ test: { type: String, default: 'test' } }, { discriminatorKey: 'type' }); });
describe('hydrate()', function() { let db; let B; let C; let Breakfast;
let breakfastSchema;
before(function() { breakfastSchema = new Schema({ food: { type: String, enum: ['bacon', 'eggs'] } });
db = start(); B = db.model('Test', schemaB); C = B.discriminator('C', schemaC); Breakfast = db.model('Test1', breakfastSchema);
return db; });
after(async function() { await db.close(); });
it('hydrates documents with no modified paths', function() { const hydrated = B.hydrate({ _id: '541085faedb2f28965d0e8e7', title: 'chair' });
assert.ok(hydrated.get('_id') instanceof DocumentObjectId); assert.equal(hydrated.title, 'chair');
assert.equal(hydrated.isNew, false); assert.equal(hydrated.isModified(), false); assert.equal(hydrated.isModified('title'), false); });
it('runs validators', async function() { const hydrated = Breakfast.hydrate({ _id: '000000000000000000000001', food: 'waffles' });
const err = await hydrated.validate().then(() => null, err => err);
assert.ok(err); assert.ok(err.errors.food); assert.deepEqual(['food'], Object.keys(err.errors)); });
it('supports projection (gh-9209)', function() { const schema = new Schema({ prop: String, arr: [String] }); const Model = db.model('Test2', schema);
const doc = Model.hydrate({ prop: 'test' }, { arr: 0 });
assert.equal(doc.isNew, false); assert.equal(doc.isModified(), false); assert.ok(!doc.$__delta()); });
it('works correctly with model discriminators', function() { const hydrated = B.hydrate({ _id: '541085faedb2f28965d0e8e8', title: 'chair', type: 'C' });
assert.equal(hydrated.test, 'test'); assert.deepEqual(hydrated.schema.tree, C.schema.tree); }); });});
mongoose

Version Info

Tagged at
a year ago