-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.test.ts
249 lines (224 loc) · 5.62 KB
/
index.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import adapterTests from "@feathersjs/adapter-tests";
import errors from "@feathersjs/errors";
import { feathers } from "@feathersjs/feathers";
import NeDB from "@seald-io/nedb";
import assert from "assert";
import path from "path";
import { NeDBService } from "../src";
const testSuite = adapterTests([
".options",
".events",
"._get",
"._find",
"._create",
"._update",
"._patch",
"._remove",
".get",
".get + $select",
".get + id + query",
".get + id + query id",
".update + id + query id",
".get + NotFound",
".find",
".remove",
".remove + $select",
".remove + id + query",
".remove + id + query id",
".remove + multi",
".remove + multi no pagination",
".update",
".update + $select",
".update + id + query",
".update + NotFound",
".update + query + NotFound",
".patch",
".patch + $select",
".patch + id + query",
".patch + id + query id",
".patch multiple",
".patch multiple no pagination",
".patch multi query same",
".patch multi query changed",
".patch + NotFound",
".patch + query + NotFound",
".create",
".create + $select",
".create multi",
".create ignores query",
"internal .find",
"internal .get",
"internal .create",
"internal .update",
"internal .patch",
"internal .remove",
".find + equal",
".find + equal multiple",
".find + $sort",
".find + $sort + string",
".find + $limit",
".find + $limit 0",
".find + $skip",
".find + $select",
".find + $and",
".find + $or",
".find + $and + $or",
".find + $in",
".find + $nin",
".find + $lt",
".find + $lte",
".find + $gt",
".find + $gte",
".find + $ne",
".find + $gt + $lt + $sort",
".find + $or nested + $sort",
".find + paginate",
".find + paginate + query",
".find + paginate + $limit + $skip",
".find + paginate + $limit 0",
".find + paginate + params",
"params.adapter + paginate",
"params.adapter + multi",
]);
const createService = (name: string, options: any) => {
const filename = path.join("db-data", name);
const db = new NeDB({
filename,
autoload: true,
});
return new NeDBService({
Model: db,
events: ["testing"],
...options,
});
};
describe("NeDB Service", () => {
const app = feathers()
.use(
"/people",
createService("people", {
whitelist: ["$regex"],
})
)
.use(
"/people-customid",
createService("people-customid", {
id: "customid",
})
);
const service = app.service("people");
describe("nedb params", () => {
/*
it("$select excludes id field if not explicitly selected (#66)", async () => {
const mod = await service.create({
name: "Modifier",
age: 222,
});
const data = await service.find({
query: {
age: 222,
$select: ["name"],
},
});
await service.remove(mod._id);
assert.ok(!data[0]._id);
});
it("can $select with only id (#100)", async () => {
const mod = await service.create({
name: "Modifier",
age: 343,
});
const data = await service.find({
query: {
age: 343,
$select: ["_id"],
},
});
await service.remove(mod._id);
assert.ok(data[0]._id);
assert.ok(!data[0].name);
});
it("allows whitelisted parameters in query", async () => {
const person = await service.create({
name: "Param test",
});
const data = await service.find({
query: {
name: { $regex: /haha/ },
},
});
await service.remove(person._id);
assert.strictEqual(data.length, 0);
});
*/
it("throws error when model is missing", () => {
try {
//@ts-ignore
const s = new NeDBService();
throw new Error("Should never get here");
} catch (error: any) {
assert.strictEqual(
error?.message,
"NeDB datastore `Model` needs to be provided"
);
}
});
it("allows to set params.nedb to upsert", async () => {
//@ts-ignore
const person = await service.update(
"testing",
{
name: "Upsert tester",
},
{
nedb: {
upsert: true,
},
}
);
await service.remove(person._id);
assert.deepStrictEqual(person, {
_id: "testing",
name: "Upsert tester",
});
});
it("allows NeDB modifiers (#59)", async () => {
const person = await service.create({
name: "Modifier",
data: ["first"],
});
const updated = await service.update(person._id, {
$push: { data: "second" },
});
await service.remove(person._id);
//@ts-ignore
assert.deepStrictEqual(updated.data, ["first", "second"]);
});
it("allows NeDB modifiers in patch (#65)", async () => {
const service = app.service("people");
const person = await service.create({
name: "Modifier",
data: ["first"],
});
const updated = await service.patch(person._id, {
$push: { data: "second" },
});
await service.remove(person._id);
//@ts-ignore
assert.deepStrictEqual(updated.data, ["first", "second"]);
});
it("_patch sets default params", async () => {
const person = await service.create({
name: "Param test",
});
//@ts-ignore
const patchedPerson = await service._patch(person._id, {
name: "Updated",
});
await service.remove(person._id);
assert.strictEqual(patchedPerson.name, "Updated");
});
});
testSuite(app, errors, "people", "_id");
testSuite(app, errors, "people-customid", "customid");
});