forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.test.ts
156 lines (127 loc) · 3.74 KB
/
document.test.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
import { Schema, model, Model, Document, Error, Types } from 'mongoose';
import { expectError, expectType } from 'tsd';
const schema: Schema = new Schema({ name: { type: 'String', required: true }, address: new Schema({ city: { type: String, required: true } }) });
interface ITestBase {
name?: string;
}
interface ITest extends ITestBase, Document {}
const Test = model<ITest>('Test', schema);
void async function main() {
const doc: ITest = await Test.findOne().orFail();
expectType<Promise<ITest>>(doc.remove());
expectType<void>(doc.remove({}, (err, doc) => {
expectType<Error | null>(err);
expectType<any>(doc);
}));
expectType<void>(doc.remove((err, doc) => {
expectType<Error | null>(err);
expectType<any>(doc);
}));
}();
void async function run() {
const user = new Test({ name: {}, address: {} });
const error = user.validateSync();
if (error != null) {
const _error = error.errors.address;
}
}();
(function() {
const test = new Test();
test.validate({ pathsToSkip: ['hello'] });
test.validate({ pathsToSkip: 'name age' });
test.validateSync({ pathsToSkip: ['name', 'age'] });
test.validateSync({ pathsToSkip: 'name age' });
test.validateSync({ pathsToSkip: 'name age', blub: 1 });
expectType<Promise<ITest & { _id: any; }>>(test.save());
expectType<Promise<ITest & { _id: any; }>>(test.save({}));
expectType<void>(test.save({}, (err, doc) => {
expectType<Error | null>(err);
expectType<ITest & { _id: any; }>(doc);
}));
expectType<void>(test.save((err, doc) => {
expectType<Error | null>(err);
expectType<ITest & { _id: any; }>(doc);
}));
})();
function gh10526<U extends ITest>(arg1: Model<U>) {
const t = new arg1({ name: 'hello' });
}
function testMethods(): void {
interface IUser {
first: string;
last: string;
}
interface IUserMethods {
fullName(): string;
}
type User = Model<IUser, {}, IUserMethods>
const schema = new Schema<IUser, User>({ first: String, last: String });
schema.methods.fullName = function(): string {
return this.first + ' ' + this.last;
};
const UserModel = model<IUser, User>('User', schema);
const doc = new UserModel({ first: 'test', last: 'test' });
expectType<string>(doc.fullName());
}
function testRequiredId(): void {
// gh-10657
interface Foo {
_id: string;
label: string;
}
const FooSchema = new Schema<Foo, Model<Foo>, Foo>({
_id: String,
label: { type: String }
});
const Foo: Model<Foo> = model<Foo>('Foo', FooSchema);
type FooInput = {
label: string;
};
type FooOutput = {
_id: string;
label: string;
};
const createFoo = async(foo: FooInput): Promise<FooOutput> => {
return await Foo.create(foo);
};
}
async function gh11117(): Promise<void> {
interface Foo {
someDate: Date;
someId: Types.ObjectId;
someNumber: number;
someString: string;
}
const fooSchema = new Schema<Foo, Model<Foo>>({
someDate: { required: true, type: Date },
someId: { required: true, type: Schema.Types.ObjectId },
someNumber: { required: true, type: Number },
someString: { required: true, type: String }
});
const fooModel = model('foos', fooSchema);
const items = await fooModel.create<Foo>([
{
someId: new Types.ObjectId(),
someDate: new Date(),
someNumber: 5,
someString: 'test'
}
]);
const json = items[0].toJSON();
expectType<Date>(json.someDate);
}
function gh11085(): void {
interface User {
username: string;
email: string;
}
const userSchema = new Schema<User>({
username: String,
email: String
});
const UserModel = model('User', userSchema);
const newUser = new UserModel();
let _id: number;
expectError(_id = newUser._id);
const _id2: Types.ObjectId = newUser._id;
}