deno.land / x / mongoose@6.7.5 / test / docs / date.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
146
147
148
149
150
151
152
153
154
155
156
157
158
'use strict';
const assert = require('assert');const start = require('../common');
describe('Date Tutorial', function() { let User; // let db;
const mongoose = new start.mongoose.Mongoose();
before(function() { const userSchema = new mongoose.Schema({ name: String, // `lastActiveAt` is a date lastActiveAt: Date }); User = mongoose.model('User', userSchema);
return mongoose.connect(start.uri); });
it('Example 1.2: casts strings to dates', function() { const user = new User({ name: 'Jean-Luc Picard', lastActiveAt: '2002-12-09' }); user.lastActiveAt instanceof Date; // true // acquit:ignore:start assert.ok(user.lastActiveAt instanceof Date); // acquit:ignore:end });
it('Example 1.3: cast error', function() { const user = new User({ name: 'Jean-Luc Picard', lastActiveAt: 'not a date' }); user.lastActiveAt instanceof Date; // false user.validateSync().errors['lastActiveAt']; // CastError // acquit:ignore:start assert.ok(!(user.lastActiveAt instanceof Date)); assert.equal(user.validateSync().errors['lastActiveAt'].name, 'CastError'); // acquit:ignore:end });
it('Example 1.2.1: min, max', function() { const episodeSchema = new mongoose.Schema({ title: String, airedAt: { type: Date, // The dates of the first and last episodes of // Star Trek: The Next Generation min: '1987-09-28', max: '1994-05-23' } }); const Episode = mongoose.model('Episode', episodeSchema);
const ok = new Episode({ title: 'Encounter at Farpoint', airedAt: '1987-09-28' }); ok.validateSync(); // No error // acquit:ignore:start assert.ifError(ok.validateSync()); // acquit:ignore:end
const bad = new Episode({ title: 'What You Leave Behind', airedAt: '1999-06-02' }); bad.airedAt; // "1999-06-02T00:00:00.000Z"
// Path `airedAt` (Tue Jun 01 1999 20:00:00 GMT-0400 (EDT)) is after // maximum allowed value (Sun May 22 1994 20:00:00 GMT-0400 (EDT)). bad.validateSync(); // acquit:ignore:start assert.ok(bad.airedAt instanceof Date); assert.ok(bad.validateSync().toString().includes('after maximum')); // acquit:ignore:end });
describe('Example 1.3.1', function() { let Episode; let db;
before(async function() { const episodeSchema = new mongoose.Schema({ title: String, airedAt: { type: Date, // The dates of the first and last episodes of // Star Trek: The Next Generation min: '1987-09-28', max: '1994-05-23' } }); db = await start().asPromise(); Episode = db.model('Episode', episodeSchema);
await Episode.create([ { title: 'Encounter at Farpoint', airedAt: '1987-09-28' }, { title: 'The Last Outpost', airedAt: '1987-10-19' }, { title: 'Where No One Has Gone Before', airedAt: '1987-10-26' } ]); });
it('date queries', function() { // Find episodes that aired on this exact date return Episode.find({ airedAt: new Date('1987-10-26') }). then(episodes => { episodes[0].title; // "Where No One Has Gone Before" // acquit:ignore:start assert.equal(episodes[0].title, 'Where No One Has Gone Before'); // acquit:ignore:end // Find episodes within a range of dates, sorted by date ascending return Episode. find({ airedAt: { $gte: '1987-10-19', $lte: '1987-10-26' } }). sort({ airedAt: 1 }); }). then(episodes => { episodes[0].title; // "The Last Outpost" episodes[1].title; // "Where No One Has Gone Before" // acquit:ignore:start assert.equal(episodes[0].title, 'The Last Outpost'); assert.equal(episodes[1].title, 'Where No One Has Gone Before'); // acquit:ignore:end }); }); });
it('Example 1.4.1: moment', function() { const moment = require('moment'); const user = new User({ name: 'Jean-Luc Picard', lastActiveAt: moment.utc('2002-12-09') }); user.lastActiveAt; // "2002-12-09T00:00:00.000Z" // acquit:ignore:start assert.ok(user.lastActiveAt instanceof Date); assert.equal(user.lastActiveAt.toISOString(), '2002-12-09T00:00:00.000Z'); // acquit:ignore:end });
it('Example 1.4.3: numeric strings', function() { const user = new User({ name: 'Jean-Luc Picard', lastActiveAt: '1552261496289' }); user.lastActiveAt; // "2019-03-10T23:44:56.289Z" // acquit:ignore:start assert.ok(user.lastActiveAt instanceof Date); assert.equal(user.lastActiveAt.toISOString(), '2019-03-10T23:44:56.289Z'); // acquit:ignore:end });});
mongoose

Version Info

Tagged at
a year ago