-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.test.ts
221 lines (197 loc) · 7.21 KB
/
project.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
import { testSuite, expect } from "manten";
import { CREATED_AT_KEY, ID_KEY, UPDATED_AT_KEY } from "../../../src/collection";
import { nrml, testCollection } from "../../common";
export default testSuite(async ({ describe }) => {
describe("project", ({ test }) => {
test("implicit exclusion", () => {
const collection = testCollection({ timestamps: false });
collection.insert({ a: 1, b: 1, c: 1 });
collection.insert({ a: 2, b: 2, c: 2 });
collection.insert({ a: 3, b: 3, c: 3 });
const found = collection.find({ a: 1 }, { project: { b: 1 } });
expect(found).toEqual([{ b: 1 }]);
});
test("implicit inclusion", () => {
const collection = testCollection({ timestamps: false });
collection.insert({ a: 1, b: 1, c: 1 });
collection.insert({ a: 2, b: 2, c: 2 });
collection.insert({ a: 3, b: 3, c: 3 });
const found = collection.find({ a: 1 }, { project: { b: 0 } });
const id = found[0][ID_KEY];
expect(id).toBeDefined();
expect(found).toEqual([{ _id: id, a: 1, c: 1 }]);
});
test("implicit inclusion - _id implicitly included", () => {
const collection = testCollection({ timestamps: false });
collection.insert({ a: 1, b: 1, c: 1 });
collection.insert({ a: 2, b: 2, c: 2 });
collection.insert({ a: 3, b: 3, c: 3 });
const foundWithId = collection.find({ a: 1 }, { project: { b: 0 } });
const id = foundWithId[0][ID_KEY];
expect(id).toBeDefined();
expect(foundWithId).toEqual([{ _id: id, a: 1, c: 1 }]);
});
test("explicit", () => {
const collection = testCollection({ timestamps: false });
collection.insert({ a: 1, b: 1, c: 1 });
collection.insert({ a: 2, b: 2, c: 2 });
collection.insert({ a: 3, b: 3, c: 3 });
const found = nrml(collection.find({ a: 1 }, { project: { b: 1, c: 0 } }));
expect(found).toEqual([{ a: 1, b: 1 }]);
});
test("explicit - ID_KEY implicitly included", () => {
const collection = testCollection({ timestamps: false });
collection.insert({ a: 1, b: 1, c: 1 });
collection.insert({ a: 2, b: 2, c: 2 });
collection.insert({ a: 3, b: 3, c: 3 });
const foundWithId = collection.find(
{ a: 1 },
{
project: {
b: 1,
c: 0,
_created_at: 0,
_updated_at: 0,
},
}
);
const id = foundWithId[0][ID_KEY];
expect(id).toBeDefined();
expect(foundWithId).toEqual([{ _id: id, a: 1, b: 1 }]);
});
test("empty query respects projection", () => {
const collection = testCollection({ timestamps: false });
collection.insert({ a: 1, b: 1, c: 1 });
collection.insert({ a: 2, b: 2, c: 2 });
const found = collection.find({}, { project: { b: 1 } });
for (const doc of found) {
expect(doc[ID_KEY]).toBeUndefined();
expect(doc[CREATED_AT_KEY]).toBeUndefined();
expect(doc[UPDATED_AT_KEY]).toBeUndefined();
}
});
describe("aggregation", ({ test }) => {
test("$floor, $ceil, $sub, $add, $mult, $div", () => {
const collection = testCollection({ timestamps: false });
collection.insert({ a: 1, b: 1, c: 5.6 });
collection.insert({ a: 2, b: 2, c: 2 });
collection.insert({ a: 3, b: 3, c: 3 });
const found = collection.find(
{ a: 1 },
{
aggregate: {
flooredC: { $floor: "c" },
ceiledC: { $ceil: "c" },
subbed1: { $sub: ["c", "a"] },
subbed2: { $sub: [15, "flooredC", 0, 1] },
mult1: { $mult: ["c", 2, "subbed2"] },
div1: { $div: ["subbed2", 2, "a", 2] },
add1: { $add: ["c", 2, "subbed2"] },
},
project: {
b: 0,
_created_at: 0,
_updated_at: 0,
_id: 0,
},
}
);
expect(found).toEqual([{
a: 1,
c: 5.6,
flooredC: 5,
ceiledC: 6,
subbed1: 4.6,
subbed2: 9,
mult1: 100.8,
div1: 2.25,
add1: 16.6,
}]);
});
test("more realistic use-case", () => {
const collection = testCollection({ timestamps: false });
collection.insert({ math: 72, english: 82, science: 92 });
collection.insert({ math: 60, english: 70, science: 80 });
collection.insert({ math: 90, english: 72, science: 84 });
const found = nrml(collection.find(
{ $has: ["math", "english", "science"] },
{
aggregate: {
total: { $add: ["math", "english", "science"] },
average: { $div: ["total", 3] },
},
}
));
expect(found).toEqual([
{ math: 72, english: 82, science: 92, total: 246, average: 82 },
{ math: 60, english: 70, science: 80, total: 210, average: 70 },
{ math: 90, english: 72, science: 84, total: 246, average: 82 },
]);
});
test("remove intermediate aggregation properties with projection", () => {
const collection = testCollection();
collection.insert({ math: 72, english: 82, science: 92 });
collection.insert({ math: 60, english: 70, science: 80 });
collection.insert({ math: 90, english: 72, science: 84 });
const found = nrml(collection.find(
{ $has: ["math", "english", "science"] },
{
aggregate: {
total: { $add: ["math", "english", "science"] }, // <-- projected out
average: { $div: ["total", 3] },
},
project: {
math: 1,
english: 1,
science: 1,
average: 1,
},
}
));
expect(found).toEqual([
{ math: 72, english: 82, science: 92, average: 82 },
{ math: 60, english: 70, science: 80, average: 70 },
{ math: 90, english: 72, science: 84, average: 82 },
]);
});
test("accessing properties with dot notation", () => {
const collection = testCollection();
collection.insert({ a: { b: { c: 1 } } });
collection.insert({ a: { b: { c: 2 } } });
collection.insert({ a: { b: { c: 3 } } });
const found = collection.find(
{ a: { b: { c: 1 } } },
{
aggregate: {
d: { $add: ["a.b.c", 1] },
},
project: {
a: 0,
_created_at: 0,
_updated_at: 0,
_id: 0,
},
}
);
expect(found).toEqual([{ d: 2 }]);
});
test("$fn", () => {
const collection = testCollection();
collection.insert({ first: "John", last: "Doe" });
collection.insert({ first: "Jane", last: "Doe" });
const found = nrml(collection.find(
{ $has: ["first", "last"] },
{
aggregate: {
fullName: { $fn: (doc) => `${doc.first} ${doc.last}` },
},
}
));
expect(found).toEqual([
{ first: "John", last: "Doe", fullName: "John Doe" },
{ first: "Jane", last: "Doe", fullName: "Jane Doe" },
]);
});
});
});
});