Skip to content

Commit

Permalink
Merge pull request #242 from litespace-org/moehab/cache-coverage-tests
Browse files Browse the repository at this point in the history
added: coverage unit tests for redis cache component.
  • Loading branch information
neuodev authored Dec 24, 2024
2 parents 15543c4 + 9ce9716 commit e49e5c6
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/models/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ PG_PASSWORD='litespace'
PG_HOST='0.0.0.0'
PG_PORT='5432'
PG_DATABASE='litespace'
REDIS_URL=redis://localhost:6379
1 change: 1 addition & 0 deletions packages/models/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ PG_PASSWORD='litespace'
PG_HOST='0.0.0.0'
PG_PORT='5432'
PG_DATABASE='test'
REDIS_URL=redis://localhost:6379
1 change: 1 addition & 0 deletions packages/models/.env.test.ci
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ PG_PASSWORD='litespace'
PG_HOST='postgres'
PG_PORT='5432'
PG_DATABASE='test'
REDIS_URL=redis://redis:6379
2 changes: 2 additions & 0 deletions packages/models/fixtures/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Cache } from "@litespace/models";
export const cache = new Cache(process.env.REDIS_URL || "");
62 changes: 62 additions & 0 deletions packages/models/tests/cache/call.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { cache } from "@fixtures/cache";
import { expect } from "chai";

describe("Testing cache/call functions", () => {
beforeAll(async () => {
await cache.connect();
});

afterAll(async () => {
await cache.disconnect();
});

beforeEach(async () => {
await cache.flush();
});

it("should add/retrieve members to/from the cache", async () => {
await cache.call.addMember({ callId: 1, userId: 1 });
await cache.call.addMember({ callId: 1, userId: 2 });
await cache.call.addMember({ callId: 2, userId: 3 });

const res1 = await cache.call.getMembers(1);
expect(res1).to.have.length(2);
expect(res1).to.contains(1);
expect(res1).to.contains(2);

const res2 = await cache.call.getMembers(2);
expect(res2).to.have.length(1);
expect(res2).to.contains(3);
});

it("should NOT add (duplicate) the same user to the same call twice", async () => {
await cache.call.addMember({ callId: 1, userId: 1 });
await cache.call.addMember({ callId: 1, userId: 1 });

const res = await cache.call.getMembers(1);
expect(res).to.have.length(1);
});

it("should remove member from the cache", async () => {
await cache.call.addMember({ callId: 1, userId: 1 });
await cache.call.addMember({ callId: 1, userId: 2 });

await cache.call.removeMemberByUserId(1);

const res = await cache.call.getMembers(1);
expect(res).to.deep.eq([2]);
});

it("should check if a specific member exists in the cache", async () => {
await cache.call.addMember({ callId: 1, userId: 1 });
await cache.call.addMember({ callId: 1, userId: 2 });

const res1 = await cache.call.isMember({ callId: 1, userId: 1 });
const res2 = await cache.call.isMember({ callId: 1, userId: 3 });
const res3 = await cache.call.isMember({ callId: 2, userId: 2 });

expect(res1).to.true;
expect(res2).to.false;
expect(res3).to.false;
});
});
56 changes: 56 additions & 0 deletions packages/models/tests/cache/onlineStatus.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { cache } from "@fixtures/cache";
import { expect } from "chai";

describe("Testing cache/onlineStatus functions", () => {
beforeAll(async () => {
await cache.connect();
});

afterAll(async () => {
await cache.disconnect();
});

beforeEach(async () => {
await cache.flush();
});

it("should add (check if) user (is) in the online collection", async () => {
let res = await cache.onlineStatus.isOnline(1);
expect(res).false;

await cache.onlineStatus.addUser(1);

res = await cache.onlineStatus.isOnline(1);
expect(res).true;
});

it("should remove user from the online collection", async () => {
await cache.onlineStatus.addUser(1);
await cache.onlineStatus.removeUser(1);
const res = await cache.onlineStatus.isOnline(1);
expect(res).false;
});

it("should retrieve all online users", async () => {
await cache.onlineStatus.addUser(1);
await cache.onlineStatus.addUser(2);
await cache.onlineStatus.addUser(3);

const res = await cache.onlineStatus.getAll();
expect(res["1"]).to.eq("1");
expect(res["2"]).to.eq("1");
expect(res["3"]).to.eq("1");
});

it("should check if a batch of users are online or not", async () => {
await cache.onlineStatus.addUser(1);
await cache.onlineStatus.addUser(2);
await cache.onlineStatus.addUser(3);

const res = await cache.onlineStatus.isOnlineBatch([1, 2, 3, 4]);
expect(res.get(1)).to.true;
expect(res.get(2)).to.true;
expect(res.get(3)).to.true;
expect(res.get(4)).to.false;
});
});
55 changes: 55 additions & 0 deletions packages/models/tests/cache/peer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { cache } from "@fixtures/cache";
import { expect } from "chai";

describe("Testing cache/peer functions", () => {
beforeAll(async () => {
await cache.connect();
});

afterAll(async () => {
await cache.disconnect();
});

beforeEach(async () => {
await cache.flush();
});

it("should set/retrieve user peer id in/from the cache", async () => {
await cache.peer.setUserPeerId(1, "testing");
const res = await cache.peer.getUserPeerId(1);
expect(res).to.eq("testing");
});

it("should set/retrieve ghost peer id in/from the cache", async () => {
await cache.peer.setGhostPeerId(1, "testing");
const res = await cache.peer.getGhostPeerId(1);
expect(res).to.eq("testing");
});

it("should NOT exist overridding between the ghost and the user", async () => {
await cache.peer.setGhostPeerId(1, "dump");
// checking if user overrides the ghost or vice versa
await cache.peer.setUserPeerId(1, "user testing");
await cache.peer.setGhostPeerId(1, "ghost testing");

const res1 = await cache.peer.getUserPeerId(1);
const res2 = await cache.peer.getGhostPeerId(1);

expect(res1).to.eq("user testing");
expect(res2).to.eq("ghost testing");
});

it("should remove user peer id from the cache", async () => {
await cache.peer.setUserPeerId(1, "testing");
await cache.peer.removeUserPeerId(1);
const res = await cache.peer.getUserPeerId(1);
expect(res).to.eq(null);
});

it("should remove ghost peer id from the cache", async () => {
await cache.peer.setGhostPeerId(1, "testing");
await cache.peer.removeGhostPeerId(1);
const res = await cache.peer.getGhostPeerId(1);
expect(res).to.eq(null);
});
});
73 changes: 73 additions & 0 deletions packages/models/tests/cache/rule.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { cache } from "@fixtures/cache";
import { expect } from "chai";
import dayjs from "dayjs";
import { first } from "lodash";

describe("Testing cache/rules functions", () => {
beforeAll(async () => {
await cache.connect();
});

afterAll(async () => {
await cache.disconnect();
});

beforeEach(async () => {
await cache.flush();
});

it("should set/retreive one rule in/from the cache", async () => {
await cache.rules.setOne({ tutor: 1, rule: 1, events: [] })
let res = await cache.rules.getOne({ tutor: 1, rule: 1 })
expect(res).to.have.length(0);

const mockRuleEvent = {
id: 1,
start: dayjs().toISOString(),
end: dayjs().add(1, "hour").toISOString(),
}

await cache.rules.setOne({ tutor: 1, rule: 1, events: [mockRuleEvent] });
res = await cache.rules.getOne({ tutor: 1, rule: 1 })
expect(res).to.have.length(1);
expect(first(res)).to.deep.eq(mockRuleEvent);
});

it("should set/retreive many rules in/from the cache", async () => {
const mockRuleEvents = [
{
id: 1,
start: dayjs().toISOString(),
end: dayjs().add(1, "hour").toISOString(),
},
{
id: 2,
start: dayjs().add(1, "day").toISOString(),
end: dayjs().add(2, "days").toISOString(),
},
]

const mock1 = { tutor: 1, rule: 1, events: [mockRuleEvents[0]] };
const mock2 = { tutor: 2, rule: 2, events: [mockRuleEvents[1]] };

await cache.rules.setMany([mock1, mock2]);

const res = await cache.rules.getAll()
expect(res).to.have.length(2);
expect(res).to.deep.contains(mock1);
expect(res).to.deep.contains(mock2);
});

it("should remove one rule from the cache", async () => {
await cache.rules.setOne({ tutor: 1, rule: 1, events: [] })
await cache.rules.deleteOne({ tutor: 1, rule: 1 });
const res = await cache.rules.getOne({ tutor: 1, rule: 1 });
expect(res).to.eq(null);
});

it("should check if rules key does exist in the cache", async () => {
expect(await cache.rules.exists()).to.false;
await cache.rules.setOne({ tutor: 1, rule: 1, events: [] })
expect(await cache.rules.exists()).to.true;
});
});
64 changes: 64 additions & 0 deletions packages/models/tests/cache/tutor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { cache } from "@fixtures/cache";
import { faker } from "@faker-js/faker/locale/ar";
import { IUser } from "@litespace/types";
import { expect } from "chai";
import { sample } from "lodash";

const getMockTutorCache = (id: number) => ({
id,
name: faker.word.noun(),
image: faker.internet.url(),
video: faker.internet.url(),
bio: faker.lorem.words(30),
about: faker.lorem.sentence(5),
gender: sample([IUser.Gender.Male, IUser.Gender.Female]),
notice: faker.number.int(),
topics: faker.lorem.words(5).split(" "),
avgRating: faker.number.float(),
studentCount: faker.number.int(),
lessonCount: faker.number.int(),
})

describe("Testing cache/tutors functions", () => {
beforeAll(async () => {
await cache.connect();
});

afterAll(async () => {
await cache.disconnect();
});

beforeEach(async () => {
await cache.flush();
});

it("should set/retreive one tutor in/from the cache", async () => {
const mockData = getMockTutorCache(1);
await cache.tutors.setOne(mockData);
const res = await cache.tutors.getOne(1);
expect(res).to.deep.eq(mockData);
});

it("should set/retreive many tutors in/from the cache", async () => {
const tutor1 = getMockTutorCache(1);
const tutor2 = getMockTutorCache(2);

await cache.tutors.setMany([tutor1, tutor2]);

const res = await cache.tutors.getAll();
expect(res).to.have.length(2);
expect(res).to.deep.contains(tutor1);
expect(res).to.deep.contains(tutor2);
});

it("should check if tutors key exists in the cache or not", async () => {
const res1 = await cache.tutors.exists();
expect(res1).to.false;

const mockData = getMockTutorCache(1);
await cache.tutors.setOne(mockData);

const res2 = await cache.tutors.exists();
expect(res2).to.true;
});
});
14 changes: 12 additions & 2 deletions packages/models/tests/topics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,18 @@ describe("Topics", () => {

describe(nameof(topics.isExistsBatch), () => {
it("should return a map that tells if the passed topic ids exists in the db or not.", async () => {
const topic1 = await fixtures.topic();
const topic2 = await fixtures.topic();
const topic1 = await fixtures.topic({
name: {
ar: "dump-topic-name-ar",
en: "dump-topic-name-en",
}
});
const topic2 = await fixtures.topic({
name: {
ar: "another-dump-topic-name-ar",
en: "another-dump-topic-name-en",
}
});

const list = [topic1.id, topic2.id, 123];
const existanceMap = await topics.isExistsBatch(list);
Expand Down

0 comments on commit e49e5c6

Please sign in to comment.