-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxor.test.ts
75 lines (70 loc) · 3.11 KB
/
xor.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
import { testSuite, expect } from "manten";
import { nrml, testCollection } from "../../../common";
export default testSuite(async ({ describe }) => {
describe("$xor", ({ test }) => {
test("works", () => {
const collection = testCollection();
collection.insert([
{ a: 1, b: 1, c: 1 },
{ a: 1, b: 2, c: 2 }, // not included because a is 1 and b is 2, which matches the query exactly
{ a: 2, b: 2, c: 3 },
]);
const found = nrml(collection.find({ $xor: [{ a: 1 }, { b: 2 }] }));
expect(found).toEqual([
{ a: 1, b: 1, c: 1 }, // <-- a was 1, but but was not 2
{ a: 2, b: 2, c: 3 }, // <-- a was not 1, but b was 2
]);
});
test("with nested operators", () => {
const collection = testCollection();
collection.insert([
{ a: 1 },
{ b: 2 },
{ c: 3 },
{ a: 1, b: 2 }, // not included, because both properties exist in the query
{ a: 1, c: 3 },
{ b: 2, c: 3 },
]);
const found = nrml(collection.find({ $xor: [{ $has: "a" }, { $has: "b" }] }));
expect(found).toEqual([
{ a: 1 }, // <-- only has "a"
{ b: 2 }, // <-- only has "b"
{ a: 1, c: 3 }, // <-- only has "a"
{ b: 2, c: 3 }, // <-- only has "b"
]);
});
test("nested operators", () => {
const collection = testCollection();
collection.insert([
{ foo: "bar", num: 5 }, // not included, because properties both match the query
{ foo: "bee", num: 8 },
{ foo: "baz", num: 10 },
{ foo: "boo", num: 20 }, // not included, because neither property matches the query
]);
const found = nrml(collection.find({ $xor: [{ foo: { $includes: "ba" } }, { num: { $lt: 9 } }] }));
expect(found).toEqual([
{ foo: "bee", num: 8 }, // <-- foo does not include "ba", but num is less than 9
{ foo: "baz", num: 10 }, // <-- foo includes "ba", but num is not less than 9
]);
});
test("nested operators, dot notation, implicitly and explicitly deep", () => {
const collection = testCollection({ populate: false });
collection.insert([
{ a: { b: { c: { foo: "bar", num: 5 } } } }, // not included, because properties both match the query
{ a: { b: { c: { foo: "bee", num: 8 } } } },
{ a: { b: { c: { foo: "baz", num: 10 } } } },
{ a: { b: { c: { foo: "boo", num: 20 } } } }, // not included, because neither property matches the query
]);
const found = nrml(collection.find({ $xor: [{ "a.b.c.foo": { $includes: "ba" } }, { num: { $lt: 9 } }] }));
expect(found).toEqual([
{ a: { b: { c: { foo: "bee", num: 8 } } } }, // <-- foo does not include "ba", but num is less than 9
{ a: { b: { c: { foo: "baz", num: 10 } } } }, // <-- foo includes "ba", but num is not less than 9
]);
});
test("throws when given anything other than 2 parameters", () => {
const collection = testCollection();
expect(() => collection.find({ $xor: [{ a: 1 }, { b: 2 }, { c: 3 }] })).toThrow();
expect(() => collection.find({ $xor: [{ a: 1 }] })).toThrow();
});
});
});