deno.land / x / mongoose@6.7.5 / test / schema.uuid.test.js

schema.uuid.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
'use strict';
const start = require('./common');const util = require('./util');
const bson = require('bson');
const assert = require('assert');
const mongoose = start.mongoose;const Schema = mongoose.Schema;
describe('SchemaUUID', function() { let Model; let TestSchema; let db;
before(async function() { TestSchema = new Schema({ x: { type: mongoose.Schema.Types.UUID }, y: [{ type: mongoose.Schema.Types.UUID }] }); db = await start().asPromise(); });
after(async function() { await db.close(); });
beforeEach(async() => { await db.deleteModel(/.*/); Model = db.model('Test', TestSchema); }); afterEach(() => util.clearTestData(db)); afterEach(() => util.stopRemainingOps(db));
it('basic functionality should work', async function() { const doc = new Model({ x: '09190f70-3d30-11e5-8814-0f4df9a59c41' }); assert.ifError(doc.validateSync()); assert.ok(typeof doc.x === 'string'); assert.strictEqual(doc.x, '09190f70-3d30-11e5-8814-0f4df9a59c41'); await doc.save();
const query = Model.findOne({ x: '09190f70-3d30-11e5-8814-0f4df9a59c41' }); assert.ok(typeof query._conditions.x === 'string');
const res = await query; assert.ifError(res.validateSync()); assert.ok(typeof res.x === 'string'); assert.strictEqual(res.x, '09190f70-3d30-11e5-8814-0f4df9a59c41');
// check that the data is actually a buffer in the database with the correct subtype const col = db.client.db(db.name).collection(Model.collection.name); const rawDoc = await col.findOne({ x: new bson.Binary(Buffer.from('09190f703d3011e588140f4df9a59c41', 'hex'), 4) }); assert.ok(rawDoc); assert.ok(rawDoc.x instanceof bson.Binary); assert.strictEqual(rawDoc.x.sub_type, 4); });
it('should throw error in case of invalid string', function() { const doc = new Model({ x: 'invalid' }); const res = doc.validateSync(); assert.ok(res !== null && res !== undefined); const errors = res.errors; assert.strictEqual(Object.keys(errors).length, 1); assert.ok(errors.x instanceof mongoose.Error.CastError); });
it('should work with $in and $nin and $all', async function() { const doc1 = new Model({ y: ['f8010af3-bc2c-45e6-85c6-caa30c4a7d34', 'c6f59133-4f84-45a8-bc1d-8f172803e4fe', 'df1309e0-58c5-427a-b22f-6c0fc445ccc0'] }); const doc2 = new Model({ y: ['13d51406-cd06-4fc2-93d1-4fad9b3eecd7', 'f004416b-e02a-4212-ac77-2d3fcf04898b', '5b544b71-8988-422b-a4df-bf691939fe4e'] });
await doc1.save(); await doc2.save();
// test $in const foundDocIn = await Model.find({ y: { $in: ['f8010af3-bc2c-45e6-85c6-caa30c4a7d34'] } }); assert.ok(foundDocIn); assert.strictEqual(foundDocIn.length, 1); assert.ok(foundDocIn[0].y); assert.strictEqual(foundDocIn[0].y.length, 3); assert.strictEqual(foundDocIn[0].y[0], 'f8010af3-bc2c-45e6-85c6-caa30c4a7d34'); assert.strictEqual(foundDocIn[0].y[1], 'c6f59133-4f84-45a8-bc1d-8f172803e4fe'); assert.strictEqual(foundDocIn[0].y[2], 'df1309e0-58c5-427a-b22f-6c0fc445ccc0');
// test $nin const foundDocNin = await Model.find({ y: { $nin: ['f8010af3-bc2c-45e6-85c6-caa30c4a7d34'] } }); assert.ok(foundDocNin); assert.strictEqual(foundDocNin.length, 1); assert.ok(foundDocNin[0].y); assert.strictEqual(foundDocNin[0].y.length, 3); assert.strictEqual(foundDocNin[0].y[0], '13d51406-cd06-4fc2-93d1-4fad9b3eecd7'); assert.strictEqual(foundDocNin[0].y[1], 'f004416b-e02a-4212-ac77-2d3fcf04898b'); assert.strictEqual(foundDocNin[0].y[2], '5b544b71-8988-422b-a4df-bf691939fe4e');
// test for $all const foundDocAll = await Model.find({ y: { $all: ['13d51406-cd06-4fc2-93d1-4fad9b3eecd7', 'f004416b-e02a-4212-ac77-2d3fcf04898b'] } }); assert.ok(foundDocAll); assert.strictEqual(foundDocAll.length, 1); assert.ok(foundDocAll[0].y); assert.strictEqual(foundDocAll[0].y.length, 3); assert.strictEqual(foundDocAll[0].y[0], '13d51406-cd06-4fc2-93d1-4fad9b3eecd7'); assert.strictEqual(foundDocAll[0].y[1], 'f004416b-e02a-4212-ac77-2d3fcf04898b'); assert.strictEqual(foundDocAll[0].y[2], '5b544b71-8988-422b-a4df-bf691939fe4e'); });
// the following are TODOs based on SchemaUUID.prototype.$conditionalHandlers which are not tested yet it('should work with $bits* operators'); it('should work with $all operator'); it('should work with $lt, $lte, $gt, $gte operators');});
mongoose

Version Info

Tagged at
a year ago