forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlokijs-tests.ts
103 lines (71 loc) · 2.81 KB
/
lokijs-tests.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
/// <reference path="lokijs.d.ts" />
import Loki = require("lokijs");
namespace LokijsTest {
class Ant {
static uniqueId = 1;
id: number;
dob: Date;
health: number; // range [0.0, 1.0]
lengthMm: number; // in millimeters
weightMg: number; // in milligrams
constructor(id: number) {
this.id = id;
}
static createAnt() {
return new Ant(Ant.uniqueId++);
}
}
class QueenAnt extends Ant {
eggsBirthed: number;
constructor(id: number) {
super(id);
}
static createQueenAnt() {
return new QueenAnt(Ant.uniqueId++);
}
}
class AntColony {
ants: LokiCollection<Ant>;
queens: LokiCollection<QueenAnt>;
constructor(ants?: LokiCollection<Ant>, queens?: LokiCollection<QueenAnt>) {
this.ants = ants;
this.queens = queens;
}
}
export class Test {
static runAllTests() {
var lokiInst = new Loki("ant-colony", { autosave: false });
var colony = Test.createAntColony(lokiInst, 250);
var topAnts = colony.ants.addDynamicView("top-ants");
var topAntAry = topAnts.applyFind({ $gt: { health: 0.75 } }).data();
if (colony.ants.binaryIndices["id"] == null) {
throw new Error("missing 'id' binary index");
}
if (lokiInst.getCollection<Ant>("ants") != colony.ants) {
throw new Error("ant collections don't match");
}
var collNames = lokiInst.listCollections().map((coll) => coll.name);
if (collNames.length != 2 || collNames.indexOf("ants") < 0 || collNames.indexOf("queenAnts") < 0) {
throw new Error("collections [" + collNames + "] does not equal expected ['ants', 'queenAnts']");
}
var firstQueenId = (<any>colony.queens.findOne({}))["$loki"];
if (firstQueenId == null || colony.queens.get(firstQueenId) == null) {
throw new Error("queen object's '.$loki' property lookup failed");
}
var anotherColl = new Loki.Collection("anotherCollection");
}
static createAntColony(lokiInst: Loki, antCount: number, queenCount: number = 1) {
var ants = lokiInst.addCollection<Ant>("ants", { indices: "id" });
var queens = lokiInst.addCollection<QueenAnt>("queenAnts", { indices: "id" });
var antColony = new AntColony(ants, queens);
for (var i = 0; i < antCount; i++) {
ants.add(Ant.createAnt());
}
for (var i = 0; i < antCount; i++) {
queens.add(QueenAnt.createQueenAnt());
}
return antColony;
}
}
}
export = LokijsTest;