-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdocument.ts
131 lines (105 loc) · 3.15 KB
/
document.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
import chai from 'chai';
const should = chai.should();
import Database from '../../src/database';
import Document from '../../src/document';
import type Model from '../../src/model';
interface UserType {
name?: string;
age?: number;
comments?: string;
}
interface CommentType {
content?: string;
author?: string;
}
describe('Document', () => {
const db = new Database();
const Schema = Database.Schema;
const User: Model<UserType> = db.model('User', {
name: String,
age: Number,
comments: [{type: Schema.Types.CUID, ref: 'Comment'}]
});
const Comment: Model<CommentType> = db.model('Comment', {
content: String,
author: {type: Schema.Types.CUID, ref: 'User'}
});
it('constructor', () => {
const doc = User.new({
name: 'John',
age: 20
});
doc.should.be.an.instanceOf(Document);
doc.name.should.eql('John');
doc.age.should.eql(20);
});
it('constructor - no arguments', () => {
const doc = User.new();
doc.should.be.an.instanceOf(Document);
});
it('save() - insert', () => {
const doc = User.new({});
return doc.save().then(item => {
User.findById(doc._id).should.exist;
return User.removeById(item._id);
});
});
it('save() - replace', () => User.insert({}).then(doc => {
doc.name = 'A';
return doc.save();
}).then(doc => {
doc.name.should.eql('A');
return User.removeById(doc._id);
}));
it('update()', () => User.insert({}).then(doc => doc.update({name: 'A'})).then(doc => {
doc.name.should.eql('A');
return User.removeById(doc._id);
}));
it('replace()', () => User.insert({}).then(doc => doc.replace({name: 'A'})).then(doc => {
doc.name.should.eql('A');
return User.removeById(doc._id);
}));
it('remove()', () => User.insert({}).then(doc => doc.remove()).then(doc => {
should.not.exist(User.findById(doc._id));
}));
it('toObject()', () => {
const doc = User.new({});
doc.toObject().should.not.be.instanceOf(User.Document);
});
it('toObject() - don\'t deep clone getters', () => {
const db = new Database();
let User;
const userSchema = new Schema({
name: String,
age: Number
});
userSchema.virtual('users').get(() => User.find({}));
User = db.model('User', userSchema);
return User.insert({}).then(data => User.findById(data._id)).then(data => {
data.toObject().should.be.ok;
});
});
it('toString()', () => {
const doc = User.new({});
doc.toString().should.eql(JSON.stringify(doc));
});
it('populate() - object', () => User.insert({}).then(user => {
const comment = Comment.new({
author: user._id
});
comment.populate('author').author.should.eql(user);
return user;
}).then(user => User.removeById(user._id)));
it('populate() - array', () => Comment.insert([
{content: 'foo'},
{content: 'bar'},
{content: 'baz'},
{content: 'ha'}
]).then(comments => {
const user = User.new({
comments: comments.map(comment => comment._id)
});
user.populate('comments').comments.toArray().should.eql(comments);
return comments;
}).map<unknown, any>(comment => Comment.removeById(comment._id)));
});