Skip to content

Commit

Permalink
testUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
easbittm committed Oct 13, 2021
1 parent cf04239 commit f62f477
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
File renamed without changes.
File renamed without changes.
18 changes: 16 additions & 2 deletions test/Table.js → test/003_Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe("Table", () => {

})

it("should use index on AND syntax", async () => {
it("should use index with AND syntax", async () => {
let name = "Maxi Mustermann";
await db.table(tableName).push({name: name, test: true});

Expand All @@ -119,7 +119,7 @@ describe("Table", () => {
})


it("should use index on OR syntax", async () => {
it("should use index with OR syntax", async () => {
let name = "Maxi Mustermann";
await db.table(tableName).push({name: name, test: true});

Expand All @@ -136,5 +136,19 @@ describe("Table", () => {
expect(q.indexes.test).to.be.a("number");
})

it("should work without index", async () => {
let notIndexed = "123";
let id = await db.table(tableName).push({notIndexed, test: true});

let result = await db.table(tableName).find((l) => {
return l.notIndexed === notIndexed;
}, {
notIndexed
});

expect(result).to.be.a("object");
expect(result.notIndexed).to.be.equal(notIndexed);
expect(result._id).to.be.equal(id);
})
});
});
91 changes: 91 additions & 0 deletions test/004_Querys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import Database from "../lib/Database.js";
import Backup from "../lib/Backup.js";
import fs from "fs/promises";
import { expect } from "chai";

let dbname = "querysTest";

describe("Querys", () => {
let db;
let maxMustermann;

before(async () => {
try {
await fs.rm(`./storage/${dbname}`, {
recursive: true,
});

} catch {}


db = await new Database(dbname);
await db.table("persons").ensureIndex("name");


await db.table("persons").push({name: "Maxi Mustermann", birthdate: "1976-02-01T00:00:00.000Z"});
await db.table("persons").push({name: "Maxi Mustermann", birthdate: "1976-02-01T00:00:00.000Z"});
await db.table("persons").push({name: "Maxi Mustermann", birthdate: "1976-02-01T00:00:00.000Z"});
maxMustermann = await db.table("persons").push({name: "Max Mustermann", birthdate: "1976-02-01T00:00:00.000Z"});

});

describe("return row.name === name", async () => {
let name = "Max Mustermann"
let result

before(async () => {
result = await db.table("persons").filter((row) => {
return row.name === name
}, { name })
});

it("should find one entry", async () => {
expect(result.length).to.be.equal(1);
});

it("should find entry with correct data", async () => {
expect(result[0].name).to.be.equal(name);
expect(result[0]._id).to.be.equal(maxMustermann);
});

it("should use index", async () => {
let q = result.getQuery();
expect(q.indexes.name).to.be.gte(0);
expect(q.interpreterNeeded).to.be.equal(false)
});
});

describe("return row.name === name && row.birthdate === birthdate", async () => {
let name = "Maxi Mustermann"
let birthdate = "1976-02-01T00:00:00.000Z"
let result

before(async () => {
result = await db.table("persons").filter((row) => {
return row.name === name && row.birthdate === birthdate
}, { name, birthdate })
});

it("should find 3 entries only", async () => {
expect(result.length).to.be.equal(3);
});

it("should find entry with correct data", async () => {
for(let row of result) {
expect(row.name).to.be.equal(name);
expect(row.birthdate).to.be.equal(birthdate);
}
});

/*it("should use the avaiable index and interpreter", async () => {
let q = result.getQuery();
console.log(q);
expect(q.indexes.name).to.be.gte(0);
expect(q.interpreterNeeded).to.be.equal(true)
});*/
});

after(async () => {
await db.delete();
});
})

0 comments on commit f62f477

Please sign in to comment.