-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathschema.ts
162 lines (123 loc) · 4.53 KB
/
schema.ts
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
162
import chai from 'chai';
const should = chai.should(); // eslint-disable-line
import Database from '../../src/database';
describe('Schema', () => {
const Schema = Database.Schema;
it('add()', () => {
const schema = new Schema();
schema.add({
str1: String,
str2: {type: String},
num1: Number,
num2: {type: Number},
bool1: Boolean,
bool2: {type: Boolean},
date1: Date,
date2: {type: Date},
arr1: [],
arr2: [String],
arr3: [{type: String}],
obj1: Object,
obj2: {
foo: String,
bar: {
baz: {type: Number}
}
},
id: {type: Schema.Types.CUID, required: true}
});
// string
schema.paths.str1.should.be.an.instanceOf(Schema.Types.String);
schema.paths.str2.should.be.an.instanceOf(Schema.Types.String);
// number
schema.paths.num1.should.be.an.instanceOf(Schema.Types.Number);
schema.paths.num2.should.be.an.instanceOf(Schema.Types.Number);
// boolean
schema.paths.bool1.should.be.an.instanceOf(Schema.Types.Boolean);
schema.paths.bool2.should.be.an.instanceOf(Schema.Types.Boolean);
// date
schema.paths.date1.should.be.an.instanceOf(Schema.Types.Date);
schema.paths.date2.should.be.an.instanceOf(Schema.Types.Date);
// array
schema.paths.arr1.should.be.an.instanceOf(Schema.Types.Array);
// @ts-ignore
schema.paths.arr1.child.should.be.an.instanceOf(Schema.Types.Mixed);
schema.paths.arr2.should.be.an.instanceOf(Schema.Types.Array);
// @ts-ignore
schema.paths.arr2.child.should.be.an.instanceOf(Schema.Types.String);
schema.paths.arr3.should.be.an.instanceOf(Schema.Types.Array);
// @ts-ignore
schema.paths.arr3.child.should.be.an.instanceOf(Schema.Types.String);
// object
schema.paths.obj1.should.be.an.instanceOf(Schema.Types.Object);
schema.paths.obj2.should.be.an.instanceOf(Schema.Types.Object);
schema.paths['obj2.foo'].should.be.an.instanceOf(Schema.Types.String);
schema.paths['obj2.bar'].should.be.an.instanceOf(Schema.Types.Object);
schema.paths['obj2.bar.baz'].should.be.an.instanceOf(Schema.Types.Number);
// id
schema.paths.id.should.be.an.instanceOf(Schema.Types.CUID);
schema.paths.id.options.required.should.be.true;
});
it('virtual() - without getter', () => {
const schema = new Schema();
schema.virtual('test');
schema.paths.test.should.be.an.instanceOf(Schema.Types.Virtual);
});
it('virtual() - with getter', () => {
const schema = new Schema();
schema.virtual('test', () => {});
schema.paths.test.should.be
.an.instanceOf(Schema.Types.Virtual)
.have.property('getter');
});
it('pre()', () => {
const schema = new Schema();
// save
schema.pre('save', () => {});
schema.hooks.pre.save.should.have.length(1);
// remove
schema.pre('remove', () => {});
schema.hooks.pre.remove.should.have.length(1);
// incompatible type
// @ts-expect-error
(() => schema.pre('wtf', () => {})).should.to.throw(TypeError, 'Hook type must be `save` or `remove`!');
// hook is not a function
// @ts-expect-error
(() => schema.pre('save', {})).should.to.throw(TypeError, 'Hook must be a function!');
});
it('post()', () => {
const schema = new Schema();
// save
schema.post('save', () => {});
schema.hooks.post.save.should.have.length(1);
// remove
schema.post('remove', () => {});
schema.hooks.post.remove.should.have.length(1);
// incompatible type
// @ts-expect-error
(() => schema.post('wtf', () => {})).should.throw(TypeError, 'Hook type must be `save` or `remove`!');
// hook is not a function
// @ts-expect-error
(() => schema.post('save', {})).should.to.throw(TypeError, 'Hook must be a function!');
});
it('method()', () => {
const schema = new Schema();
schema.method('test', () => {});
schema.methods.test.should.exist;
// without name
schema.method.should.to.throw(TypeError, 'Method name is required!');
// without function
// @ts-expect-error
(() => schema.method('wtf', {})).should.to.throw(TypeError, 'Instance method must be a function!');
});
it('static()', () => {
const schema = new Schema();
schema.static('test', () => {});
schema.statics.test.should.exist;
// without name
schema.static.should.to.throw(TypeError, 'Method name is required!');
// without function
// @ts-expect-error
(() => schema.static('wtf', {})).should.to.throw(TypeError, 'Static method must be a function!');
});
});