Skip to content

Commit

Permalink
Tests (#128)
Browse files Browse the repository at this point in the history
* Fixed minor type bug

* Added more tests to lockProviderTestSuite function

* Added more tests to cacheTestSuite function

* Added more test to AsyncIterableCollection class

* Added more test to IterableCollection class

* Added more test to ListCollection class

* Added changeset file
  • Loading branch information
yousif-khalil-abdulkarim authored Feb 7, 2025
1 parent 604e43f commit ef2315b
Show file tree
Hide file tree
Showing 8 changed files with 689 additions and 130 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-dodos-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@daiso-tech/core": patch
---

Fixed a minor type bug
94 changes: 75 additions & 19 deletions src/cache/implementations/_shared/cache.test-suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from "@/cache/contracts/_module";
import { type Promisable } from "@/utilities/_module";
import { TimeSpan } from "@/utilities/_module";
import { delay } from "@/async/_module";
import { delay, LazyPromise } from "@/async/_module";

/**
* @group Utilities
Expand Down Expand Up @@ -235,36 +235,54 @@ export function cacheTestSuite(settings: CacheTestSuiteSettings): void {
expect(await cacheA.getOr("a", -1)).toBe(1);
});
describe("Should return default value when key doesnt exists", () => {
test("Eager", async () => {
test("Value", async () => {
expect(await cacheA.getOr("a", -1)).toBe(-1);
});
test("Lazy", async () => {
test("Function", async () => {
expect(await cacheA.getOr("a", () => -1)).toBe(-1);
});
test("Async lazy", async () => {
test("Async function", async () => {
expect(
await cacheA.getOr("a", () => Promise.resolve(-1)),
).toBe(-1);
});
test("LazyPromise", async () => {
expect(
await cacheA.getOr(
"a",
new LazyPromise(() => Promise.resolve(-1)),
),
).toBe(-1);
});
});
describe("Should return default value when key is expired", () => {
test("Eager", async () => {
test("Value", async () => {
await cacheA.add("a", 1, TTL);
await delay(TTL);
expect(await cacheA.getOr("a", -1)).toBe(-1);
});
test("Lazy", async () => {
test("Function", async () => {
await cacheA.add("a", 1, TTL);
await delay(TTL);
expect(await cacheA.getOr("a", () => -1)).toBe(-1);
});
test("Async lazy", async () => {
test("Async function", async () => {
await cacheA.add("a", 1, TTL);
await delay(TTL);
expect(
await cacheA.getOr("a", () => Promise.resolve(-1)),
).toBe(-1);
});
test("LazyPromise", async () => {
await cacheA.add("a", 1, TTL);
await delay(TTL);
expect(
await cacheA.getOr(
"a",
new LazyPromise(() => Promise.resolve(-1)),
),
).toBe(-1);
});
});
});
describe("method: getOrFail", () => {
Expand Down Expand Up @@ -297,21 +315,21 @@ export function cacheTestSuite(settings: CacheTestSuiteSettings): void {
});
});
describe("Should return only default values when all keys doesnt exists", () => {
test("Eager", async () => {
test("Value", async () => {
expect(await cacheA.getOrMany({ a: -1, b: -1 })).toEqual({
a: -1,
b: -1,
});
});
test("Lazy", async () => {
test("Function", async () => {
expect(
await cacheA.getOrMany({ a: () => -1, b: () => -1 }),
).toEqual({
a: -1,
b: -1,
});
});
test("Async lazy", async () => {
test("Async function", async () => {
expect(
await cacheA.getOrMany({
a: () => Promise.resolve(-1),
Expand All @@ -322,9 +340,20 @@ export function cacheTestSuite(settings: CacheTestSuiteSettings): void {
b: -1,
});
});
test("LazyPromise", async () => {
expect(
await cacheA.getOrMany({
a: new LazyPromise(() => Promise.resolve(-1)),
b: new LazyPromise(() => Promise.resolve(-1)),
}),
).toEqual({
a: -1,
b: -1,
});
});
});
describe("Should return default value when key is expired", () => {
test("Eager", async () => {
test("Value", async () => {
await cacheA.add("a", 1, TTL);
await delay(TTL);
expect(
Expand All @@ -335,7 +364,7 @@ export function cacheTestSuite(settings: CacheTestSuiteSettings): void {
a: -1,
});
});
test("Lazy", async () => {
test("Function", async () => {
await cacheA.add("a", 1, TTL);
await delay(TTL);
expect(
Expand All @@ -346,7 +375,7 @@ export function cacheTestSuite(settings: CacheTestSuiteSettings): void {
a: -1,
});
});
test("Async lazy", async () => {
test("Async function", async () => {
await cacheA.add("a", 1, TTL);
await delay(TTL);
expect(
Expand All @@ -357,6 +386,17 @@ export function cacheTestSuite(settings: CacheTestSuiteSettings): void {
a: -1,
});
});
test("LazyPromise", async () => {
await cacheA.add("a", 1, TTL);
await delay(TTL);
expect(
await cacheA.getOrMany({
a: new LazyPromise(() => Promise.resolve(-1)),
}),
).toEqual({
a: -1,
});
});
});
test("Should return values and default values when some keys exists", async () => {
await cacheA.add("a", 1);
Expand Down Expand Up @@ -792,38 +832,54 @@ export function cacheTestSuite(settings: CacheTestSuiteSettings): void {
expect(await cacheA.getOrAdd("a", -1)).toBe(1);
});
describe("Should persist insertion when key doesnt exists", () => {
test("Eager", async () => {
test("Value", async () => {
await cacheA.getOrAdd("a", -1);
expect(await cacheA.get("a")).toBe(-1);
});
test("Lazy", async () => {
test("Function", async () => {
await cacheA.getOrAdd("a", () => -1);
expect(await cacheA.get("a")).toBe(-1);
});
test("Async lazy", async () => {
test("Async function", async () => {
await cacheA.getOrAdd("a", () => Promise.resolve(-1));
expect(await cacheA.get("a")).toBe(-1);
});
test("LazyPromise", async () => {
await cacheA.getOrAdd(
"a",
new LazyPromise(() => Promise.resolve(-1)),
);
expect(await cacheA.get("a")).toBe(-1);
});
});
describe("Should persist insertion when key is expired", () => {
test("Eager", async () => {
test("Value", async () => {
await cacheA.add("a", 1, TTL);
await delay(TTL);
await cacheA.getOrAdd("a", -1);
expect(await cacheA.get("a")).toBe(-1);
});
test("Lazy", async () => {
test("Function", async () => {
await cacheA.add("a", 1, TTL);
await delay(TTL);
await cacheA.getOrAdd("a", () => -1);
expect(await cacheA.get("a")).toBe(-1);
});
test("Async lazy", async () => {
test("Async function", async () => {
await cacheA.add("a", 1, TTL);
await delay(TTL);
await cacheA.getOrAdd("a", () => Promise.resolve(-1));
expect(await cacheA.get("a")).toBe(-1);
});
test("LazyPromise", async () => {
await cacheA.add("a", 1, TTL);
await delay(TTL);
await cacheA.getOrAdd(
"a",
new LazyPromise(() => Promise.resolve(-1)),
);
expect(await cacheA.get("a")).toBe(-1);
});
});
});
describe("method: increment", () => {
Expand Down
Loading

0 comments on commit ef2315b

Please sign in to comment.