From 08477d7c3dbe20b20712740a25fbdb450bd3e122 Mon Sep 17 00:00:00 2001 From: TeamworkGuy2 Date: Sat, 9 Dec 2017 15:06:00 +0000 Subject: [PATCH] [lokijs] additional tests --- types/lokijs/lokijs-tests.ts | 110 +++++++++++++++++++++++++++++++++-- 1 file changed, 106 insertions(+), 4 deletions(-) diff --git a/types/lokijs/lokijs-tests.ts b/types/lokijs/lokijs-tests.ts index 167adba4ad..ac16e5cc15 100644 --- a/types/lokijs/lokijs-tests.ts +++ b/types/lokijs/lokijs-tests.ts @@ -1,7 +1,18 @@ - import Loki = require("lokijs"); + +interface EarthLocation { + longitude: number; + latitude: number; +} + +interface User { + name: string; + age: number; +} + + class Ant { static uniqueId = 1; @@ -37,10 +48,12 @@ class QueenAnt extends Ant { class AntColony { + location: EarthLocation; ants: Loki.Collection; queens: Loki.Collection; - constructor(ants: Loki.Collection, queens: Loki.Collection) { + constructor(location: EarthLocation, ants: Loki.Collection, queens: Loki.Collection) { + this.location = location; this.ants = ants; this.queens = queens; } @@ -73,15 +86,104 @@ class Test { throw new Error("queen object's '.$loki' property lookup failed"); } - var anotherColl = new Loki.Collection("anotherCollection"); + Test.events(lokiInst); + Test.insertUpdateRemove(lokiInst); + Test.dynamicViews(lokiInst); + Test.transform(lokiInst); + } + + static events(lokiInst: Loki) { + var coll = new Loki.Collection("anotherCollection", {}); + var onUpdate: (t: any) => void; + + coll.on("update", onUpdate = function (target) { + console.log("update", target); + }); + + coll.on("error", function (target) { + console.log("error", target); + }); + + coll.emit("update", { data: "abc" }); + + coll.removeListener("update", onUpdate); + } + + + static insertUpdateRemove(lokiInst: Loki) { + var coll = new Loki.Collection("anotherCollection", { + unique: <["id"]>["id"], + indices: <["dob"]>["dob"], + autoupdate: true + }); + + coll.insert({ + id: 127, + dob: new Date(2012, 9, 1), + health: 1.0, + lengthMm: 3.5, + weightMg: 0.4, + }); + + var doc = coll.by("id", 127); + if (doc) doc.health -= 0.25; + + // untyped collection (collection type should default to 'any') + var tmpColl = lokiInst.addCollection("_temp"); + tmpColl.insert(coll.find()); + var res = tmpColl.findOne({ id: 127 }); + if (res) res.health -= 0.25; + + coll.clear({ removeIndices: true }); + + tmpColl.removeWhere({ id: 127 }); + + if(doc) coll.add(doc); + + coll.remove(1); + coll.removeDataOnly(); + } + + + static dynamicViews(lokiInst: Loki) { + var users = lokiInst.addCollection("users"); + + users.insert({ name: "joe", age: 39 }); + users.insert({ name: "jack", age: 20 }); + users.insert({ name: "jim", age: 40 }); + users.insert([ + { name: "dave", age: 33 }, + { name: "eric", age: 29 }, + { name: "dave", age: 21 } + ]); + + var dv = users.addDynamicView("testview"); + dv.applyWhere(function (obj) { + return obj.name.length > 3; + }); + + console.log("expect 2 == " + dv.data().length); + + users.removeWhere(function (obj: User) { + return obj.age > 35; + }); + + console.log("expect 0 == " + dv.data().length); + } + + + static transform(lokiInst: Loki) { + var ants = lokiInst.addCollection("tmpAnts"); + var len = ants.chain().transform([{ property: "id" }]).data().length; + ants.addTransform("tmpAnts", [{ type: "map", mapFun: (a: any) => a, desc: true }]); } static createAntColony(lokiInst: Loki, antCount: number, queenCount: number = 1) { var ants = lokiInst.addCollection("ants", { indices: "id" }); var queens = lokiInst.addCollection("queenAnts", { indices: "id" }); - var antColony = new AntColony(ants, queens); + var antColony = new AntColony({ latitude: 0, longitude: 0 }, ants, queens); for (var i = 0; i < antCount; i++) { ants.add(Ant.createAnt());