-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.test.ts
131 lines (115 loc) · 5.31 KB
/
basic.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
import { testSuite, expect } from "manten";
import { nrml, testCollection } from "../../common";
export default testSuite(async ({ describe }) => {
describe("find", ({ test }) => {
test("no results should return an empty array", () => {
const collection = testCollection();
collection.insert({ a: 1 });
collection.insert({ a: 2 });
collection.insert({ a: 3 });
const found = nrml(collection.find({ a: 4 }));
expect(found).toEqual([]);
});
test("empty find returns everything", () => {
const collection = testCollection();
collection.remove({ xxx: "xxx" });
collection.remove({ yyy: "yyy" });
collection.remove({ zzz: "zzz" });
collection.insert({ a: 1 });
collection.insert({ a: 2 });
collection.insert({ a: 3 });
const found = nrml(collection.find({}));
expect(found).toEqual([{ a: 1 }, { a: 2 }, { a: 3 }]);
});
test("simple find", () => {
const collection = testCollection();
collection.insert({ foo: "bar" });
collection.insert({ foo: "baz" });
collection.insert({ foo: "boo" });
const found = nrml(collection.find({ foo: "bar" }));
expect(found).toEqual([{ foo: "bar" }]);
});
test("simple find, more criteria", () => {
const collection = testCollection();
collection.insert({ a: 1, b: 2, c: 3 });
collection.insert({ a: 1, b: 2, c: 4 });
collection.insert({ a: 2, b: 3, c: 4 });
const found = nrml(collection.find({ a: 1, b: 2 }));
expect(found).toEqual([{ a: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 4 }]);
});
test("simple find - deep false", () => {
const collection = testCollection();
collection.insert({ foo: { bar: "bar" } });
collection.insert({ foo: { bar: "baz" } });
collection.insert({ foo: { bar: "boo" } });
const found = nrml(collection.find({ bar: { $includes: "ba" } }, { deep: false }));
expect(found).toEqual([]);
});
test("simple find - deep true", () => {
const collection = testCollection();
collection.insert({ foo: { bar: "baz" } });
collection.insert({ foo: { bar: "boo" } });
collection.insert({ foo: { bar: "baz" } });
const found = nrml(collection.find({ foo: { bar: "baz" } }));
expect(found).toEqual([{ foo: { bar: "baz" } }, { foo: { bar: "baz" } }]);
});
test("normal match if deep is false but toplevel matches", () => {
const collection = testCollection();
collection.insert({ foo: { bar: "bar" } });
collection.insert({ foo: { bar: "baz" } });
collection.insert({ foo: { bar: "boo" } });
const found = nrml(collection.find({ foo: { bar: "bar" } }, { deep: false }));
expect(found).toEqual([{ foo: { bar: "bar" } }]);
});
test("multilevel results", () => {
const collection = testCollection();
collection.insert({ bar: "baz" });
collection.insert({ foo: { bar: "boo" } });
collection.insert({ foo: { bar: "baz" } });
const found = nrml(collection.find({ foo: { bar: "baz" } }));
expect(found).toEqual([{ bar: "baz" }, { foo: { bar: "baz" } }]);
});
test("array literal", () => {
const collection = testCollection();
collection.insert({ foo: ["bar", "baz"] });
collection.insert({ foo: ["bar", "boo"] });
collection.insert({ foo: ["baz", "bar"] });
const found = nrml(collection.find({ foo: ["bar", "baz"] }));
expect(found).toEqual([{ foo: ["bar", "baz"] }, { foo: ["baz", "bar"] }]);
collection.insert({ nums: [1, 2, 3] });
collection.insert({ nums: [2, 3, 1] });
collection.insert({ nums: [1, 3, 5] });
const found2 = nrml(collection.find({ nums: [3, 2, 1] }));
expect(found2).toEqual([{ nums: [1, 2, 3] }, { nums: [2, 3, 1] }]);
});
test("array literal should exclude items that don't match the exact array", () => {
const collection = testCollection();
collection.insert({ foo: ["bar", 1] });
collection.insert({ foo: ["bar", 2] });
collection.insert({ foo: ["bar", 2, 2] });
collection.insert({ foo: ["bar", 3] });
collection.insert({ a: { b: { foo: ["bar", 2] } } });
const found = nrml(collection.find({ foo: ["bar", 2] }));
expect(found).toEqual([{ foo: ["bar", 2] }, { a: { b: { foo: ["bar", 2] } } }]);
});
test("find array using object syntax", () => {
const collection = testCollection();
collection.insert({ a: { b: [ {c: 1}, {c: 2}, {c: 3} ] } });
const found = nrml(collection.find({ b: { c: 2 } }));
expect(found).toEqual([{ a: { b: [ {c: 1}, {c: 2}, {c: 3} ] } }]);
});
test("multiple queries, merged result set", () => {
const collection = testCollection();
collection.insert({ x: { a: 1 } });
collection.insert({ y: { b: 1 } });
const found = nrml(collection.find([{ a: 1 }, { b: 1 }]));
expect(found).toEqual([{ x: { a: 1 } }, { y: { b: 1 } }]);
});
test("really deep specificity", () => {
const collection = testCollection();
collection.insert({ a: { b: { c: { d: { e: { f: { g: { h: { i: { j: { k: 1 } } } } } } } } } } });
const found = nrml(collection.find({ a: { b: { c: { d: { e: { f: { g: { h: { i: { j: { k: 1 } } } } } } } } } } }));
expect(found).toEqual([{ a: { b: { c: { d: { e: { f: { g: { h: { i: { j: { k: 1 } } } } } } } } } } }]);
});
});
});