feat(release): 1.0.0
This commit is contained in:
49
tests/Cache.Test.ts
Normal file
49
tests/Cache.Test.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { IndexedDbCache } from "../src/Databases/IndexedDb.Cache.js";
|
||||
import { Options } from "../src/index.js";
|
||||
import { WithId } from "../src/Types.js";
|
||||
|
||||
describe("IndexedDbCache", () => {
|
||||
let cache: IndexedDbCache;
|
||||
|
||||
beforeEach(() => {
|
||||
cache = new IndexedDbCache();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cache.flush();
|
||||
});
|
||||
|
||||
const sampleDocuments: WithId<{ name: string }>[] = [
|
||||
{ id: "doc1", name: "Document 1" },
|
||||
{ id: "doc2", name: "Document 2" }
|
||||
];
|
||||
|
||||
const sampleCriteria = { name: { $eq: "Document 1" } };
|
||||
const sampleOptions: Options = { sort: { name: 1 } };
|
||||
|
||||
test("hash", () => {
|
||||
const hashCode = cache.hash(sampleCriteria, sampleOptions);
|
||||
expect(typeof hashCode).toBe("number");
|
||||
});
|
||||
|
||||
test("set and get", () => {
|
||||
const hashCode = cache.hash(sampleCriteria, sampleOptions);
|
||||
cache.set(hashCode, sampleDocuments);
|
||||
const result = cache.get(hashCode);
|
||||
expect(result).toEqual(sampleDocuments);
|
||||
});
|
||||
|
||||
test("get undefined", () => {
|
||||
const hashCode = cache.hash(sampleCriteria, sampleOptions);
|
||||
const result = cache.get(hashCode);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
test("flush", () => {
|
||||
const hashCode = cache.hash(sampleCriteria, sampleOptions);
|
||||
cache.set(hashCode, sampleDocuments);
|
||||
cache.flush();
|
||||
const result = cache.get(hashCode);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
77
tests/Collection.Test.ts
Normal file
77
tests/Collection.Test.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { Collection } from "../src/Collection.js";
|
||||
import { MemoryStorage } from "../src/Databases/MemoryDb.Storage.js";
|
||||
import { getUsers, UserDocument } from "./Users.Mock.js";
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Unit Tests
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
describe("Collection", () => {
|
||||
it("should successfully create a new collection", () => {
|
||||
const collection = new Collection<UserDocument>("users", new MemoryStorage("users"));
|
||||
expect(collection.name).toEqual("users");
|
||||
collection.storage.destroy();
|
||||
});
|
||||
|
||||
describe("when finding document by id", () => {
|
||||
it("should return model instance if document exists", async () => {
|
||||
const collection = new Collection<UserDocument>("users", new MemoryStorage("users"));
|
||||
const users = getUsers();
|
||||
await collection.insertMany(users);
|
||||
expect(await collection.findById(users[0].id)).toEqual(users[0]);
|
||||
collection.storage.destroy();
|
||||
});
|
||||
|
||||
it("should return undefined if document does not exists", async () => {
|
||||
const collection = new Collection<UserDocument>("users", new MemoryStorage("users"));
|
||||
expect(await collection.findById("user-4")).toBeUndefined();
|
||||
collection.storage.destroy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("when finding document by filter", () => {
|
||||
it("should return model instances when matches are found", async () => {
|
||||
const collection = new Collection<UserDocument>("users", new MemoryStorage("users"));
|
||||
const users = getUsers();
|
||||
await collection.insertMany(users);
|
||||
expect(await collection.find({ name: "Jane Doe" })).toEqual([users[1]]);
|
||||
collection.storage.destroy();
|
||||
});
|
||||
|
||||
it("should return empty array when no matches are found", async () => {
|
||||
const collection = new Collection<UserDocument>("users", new MemoryStorage("users"));
|
||||
expect(await collection.find({ name: "Rick Doe" })).toEqual([]);
|
||||
collection.storage.destroy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("when finding single document by filter", () => {
|
||||
it("should return model instance if document exists", async () => {
|
||||
const collection = new Collection<UserDocument>("users", new MemoryStorage("users"));
|
||||
const users = getUsers();
|
||||
await collection.insertMany(users);
|
||||
expect(await collection.findOne({ name: "Jane Doe" })).toEqual(users[1]);
|
||||
collection.storage.destroy();
|
||||
});
|
||||
|
||||
it("should return undefined if document does not exists", async () => {
|
||||
const collection = new Collection<UserDocument>("users", new MemoryStorage("users"));
|
||||
expect(await collection.findOne({ name: "Rick Doe" })).toBeUndefined();
|
||||
collection.storage.destroy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("should count documents by filter", () => {
|
||||
it("should return correct filter count", async () => {
|
||||
const collection = new Collection<UserDocument>("users", new MemoryStorage("users"));
|
||||
const users = getUsers();
|
||||
await collection.insertMany(users);
|
||||
expect(await collection.count({ name: "Rick Doe" })).toEqual(0);
|
||||
expect(await collection.count({ name: "Jane Doe" })).toEqual(1);
|
||||
expect(await collection.count()).toEqual(2);
|
||||
collection.storage.destroy();
|
||||
});
|
||||
});
|
||||
});
|
||||
12
tests/Hash.Test.ts
Normal file
12
tests/Hash.Test.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { hashCodeQuery } from "../src/Hash.js";
|
||||
import { Options } from "../src/index.js";
|
||||
|
||||
describe("hashCodeQuery", () => {
|
||||
const filter = { name: { $eq: "Document 1" } };
|
||||
const options: Options = { sort: { name: 1 } };
|
||||
|
||||
test("return correct hash code", () => {
|
||||
const hashCode = hashCodeQuery(filter, options);
|
||||
expect(typeof hashCode).toBe("number");
|
||||
});
|
||||
});
|
||||
33
tests/Insert.Test.ts
Normal file
33
tests/Insert.Test.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Collection } from "../src/Collection.js";
|
||||
import { MemoryStorage } from "../src/Databases/MemoryDb.Storage.js";
|
||||
import { DuplicateDocumentError } from "../src/index.js";
|
||||
import { getUsers } from "./Users.Mock.js";
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Unit Tests
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
describe("Storage Insert", () => {
|
||||
it("should successfully insert a new document", async () => {
|
||||
const collection = new Collection("users", new MemoryStorage("users"));
|
||||
const users = getUsers();
|
||||
await collection.insertMany(users);
|
||||
expect(await collection.storage.findById(users[0].id)).toEqual(users[0]);
|
||||
expect(await collection.storage.findById(users[1].id)).toEqual(users[1]);
|
||||
collection.storage.destroy();
|
||||
});
|
||||
|
||||
it("should throw an error if the document already exists", async () => {
|
||||
const collection = new Collection("users", new MemoryStorage("users"));
|
||||
const users = getUsers();
|
||||
try {
|
||||
await collection.insertOne(users[0]);
|
||||
} catch (err) {
|
||||
expect(err instanceof DuplicateDocumentError).toEqual(true);
|
||||
expect(err).toEqual(new DuplicateDocumentError(users[0], collection.storage));
|
||||
}
|
||||
collection.storage.destroy();
|
||||
});
|
||||
});
|
||||
20
tests/Remove.Test.ts
Normal file
20
tests/Remove.Test.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Collection } from "../src/Collection.js";
|
||||
import { MemoryStorage } from "../src/Databases/MemoryDb.Storage.js";
|
||||
import { RemoveResult } from "../src/index.js";
|
||||
import { getUsers } from "./Users.Mock.js";
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Unit Tests
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
describe("Storage Remove", () => {
|
||||
it("should successfully delete document", async () => {
|
||||
const collection = new Collection("users", new MemoryStorage("users"));
|
||||
const users = getUsers();
|
||||
await collection.insertMany(users);
|
||||
expect(await collection.remove({ id: "user-1" })).toEqual(new RemoveResult(1));
|
||||
collection.storage.destroy();
|
||||
});
|
||||
});
|
||||
1369
tests/Update.Test.ts
Normal file
1369
tests/Update.Test.ts
Normal file
File diff suppressed because it is too large
Load Diff
45
tests/Users.Mock.ts
Normal file
45
tests/Users.Mock.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { clone } from "../src/Clone.js";
|
||||
import { WithId } from "../src/Types.js";
|
||||
|
||||
const users: WithId<UserDocument>[] = [
|
||||
{
|
||||
id: "user-1",
|
||||
name: "John Doe",
|
||||
email: "john.doe@test.none",
|
||||
friends: [
|
||||
{
|
||||
id: "user-2",
|
||||
alias: "Jane"
|
||||
}
|
||||
],
|
||||
interests: ["movies", "tv", "sports"]
|
||||
},
|
||||
{
|
||||
id: "user-2",
|
||||
name: "Jane Doe",
|
||||
email: "jane.doe@test.none",
|
||||
friends: [
|
||||
{
|
||||
id: "user-1",
|
||||
alias: "John"
|
||||
}
|
||||
],
|
||||
interests: ["movies", "fitness", "dance"]
|
||||
}
|
||||
];
|
||||
|
||||
export function getUsers(): WithId<UserDocument>[] {
|
||||
return clone(users);
|
||||
}
|
||||
|
||||
export type UserDocument = {
|
||||
name: string;
|
||||
email: string;
|
||||
friends: Friend[];
|
||||
interests: string[];
|
||||
};
|
||||
|
||||
type Friend = {
|
||||
id: string;
|
||||
alias: string;
|
||||
};
|
||||
Reference in New Issue
Block a user