-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathand.test.ts
80 lines (73 loc) · 2.87 KB
/
and.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
import { testSuite, expect } from "manten";
import { nrml, testCollection } from "../../../common";
export default testSuite(async ({ describe }) => {
describe("$and", ({ test }) => {
test("works", () => {
const collection = testCollection();
collection.insert([
{ a: 1, b: 1, c: 1 },
{ a: 1, b: 1, c: 1 },
{ a: 1, b: 2, c: 3 },
{ a: 2, b: 2, c: 3 },
]);
const found = nrml(collection.find({ $and: [{ a: 1 }, { b: 2 }] }));
expect(found).toEqual([{ a: 1, b: 2, c: 3 }]);
});
test("nested operators", () => {
const collection = testCollection();
collection.insert([
{ foo: "bar", num: 5 },
{ foo: "baz", num: 10 },
{ foo: "boo", num: 20 },
]);
const found = nrml(collection.find({ $and: [{ foo: { $includes: "ba" } }, { num: { $gt: 9 } }] }));
expect(found).toEqual([{ foo: "baz", num: 10 }]);
});
test("deep selectors, explicit and implicit", () => {
const collection = testCollection();
collection.insert([
{ a: { b: { c: 1, d: 1 } } },
{ a: { b: { c: 1, d: 1 } } },
{ a: { b: { c: 1, d: 3 } } },
]);
const found = nrml(collection.find({ $and: [{ a: { b: { c: { $lt: 2 } } } }, { d: 3 }] }));
expect(found).toEqual([{ a: { b: { c: 1, d: 3 } } }]);
});
test("shallow and deep selectors", () => {
const collection = testCollection();
collection.insert([{ a: 15, b: 1 }, { a: 15, b: { c: { d: 100 } } }]);
const found = nrml(collection.find({ $and: [{ a: 15 }, { b: { c: { d: 100 } } }] }));
expect(found).toEqual([{ a: 15, b: { c: { d: 100 } } }]);
});
test("functions as conditions", () => {
const collection = testCollection();
collection.insert([
{ foo: "bar", num: 5 },
{ foo: "baz", num: 10 },
{ foo: "bazzz", num: 20 },
]);
const found = nrml(collection.find({ $and: [{ foo: { $includes: "ba" } }, { num: { $gt: 9 } }, { num: (v: number) => v % 10 === 0 }] }));
expect(found).toEqual([{ foo: "baz", num: 10 }, { foo: "bazzz", num: 20 }]);
});
test("and matches while respecting other query parameters", () => {
const collection = testCollection();
collection.insert([
{ a: 1, num: 5 },
{ a: 2, num: 10 },
{ a: 3, num: 20 },
]);
const found = nrml(collection.find({ a: 2, $and: [{ num: { $gt: 0 } }, { num: { $lt: 100 } }] }));
expect(found).toEqual([{ a: 2, num: 10 }]);
});
test("works with dot notation", () => {
const collection = testCollection();
collection.insert([
{ a: { b: 1, c: 1 }, d: 1 },
{ a: { b: 1, c: 1 }, d: 1 },
{ a: { b: 1, c: 3 }, d: 3 },
]);
const found = nrml(collection.find({ $and: [{ "a.b": 1 }, { "a.c": 3 }] }));
expect(found).toEqual([{ a: { b: 1, c: 3 }, d: 3 }]);
});
});
});