deno.land / x / mongoose@6.7.5 / test / types.number.test.js

types.number.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
159
160
161
'use strict';
/** * Module dependencies. */
const mongoose = require('./common').mongoose;
const assert = require('assert');
const SchemaNumber = mongoose.Schema.Types.Number;
/** * Test. */
describe('types.number', function() { it('an empty string casts to null', function(done) { const n = new SchemaNumber(); assert.strictEqual(n.cast(''), null); done(); });
it('a null number should castForQuery to null', function(done) { const n = new SchemaNumber(); assert.strictEqual(n.castForQuery(null), null); done(); });
it('array throws cast number error', function(done) { const n = new SchemaNumber(); let err; try { n.cast([]); } catch (e) { err = e; } assert.strictEqual(true, !!err); done(); });
it('three throws cast number error', function(done) { const n = new SchemaNumber(); let err; try { n.cast('three'); } catch (e) { err = e; } assert.strictEqual(true, !!err); done(); });
it('{} throws cast number error', function(done) { const n = new SchemaNumber(); let err; try { n.cast({}); } catch (e) { err = e; } assert.strictEqual(true, !!err); done(); });
it('does not throw number cast error', function(done) { const n = new SchemaNumber(); const items = [1, '2', '0', null, '', new String('47'), new Number(5), Number(47), Number('09'), 0x12]; let err; try { for (const item of items) { n.cast(item); } } catch (e) { err = e; } assert.strictEqual(false, !!err, err); done(); });
it('boolean casts to 0/1 (gh-3475)', function(done) { const n = new SchemaNumber(); assert.strictEqual(n.cast(true), 1); assert.strictEqual(n.cast(false), 0); done(); });
it('prefers valueOf function if one exists (gh-6299)', function(done) { const n = new SchemaNumber(); const obj = { str: '10', valueOf: function() { return this.str; }, toString: function() { return '11'; } }; assert.strictEqual(n.cast(obj), 10); done(); });
it('throws a CastError with a bad conditional (gh-6927)', function() { const n = new SchemaNumber(); let err; try { n.castForQuery({ somePath: { $x: 43 } }); } catch (e) { err = e; } assert.ok(/CastError/.test(err)); });
describe('custom caster (gh-7045)', function() { let original = {};
beforeEach(function() { original = SchemaNumber.cast(); });
afterEach(function() { SchemaNumber.cast(original); });
it('disallow empty string', function() { SchemaNumber.cast(v => { assert.ok(v !== ''); return original(v); });
const num = new SchemaNumber();
let err; try { num.cast(''); } catch (e) { err = e; } assert.ok(/CastError/.test(err));
num.cast('123'); // Should be ok });
it('disable casting', function() { SchemaNumber.cast(false);
const num = new SchemaNumber();
let err; try { num.cast('123'); } catch (e) { err = e; } assert.ok(/CastError/.test(err));
num.cast(123); // Should be ok }); });});
mongoose

Version Info

Tagged at
a year ago