-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added: coverage unit tests for redis cache component.
- Loading branch information
Showing
9 changed files
with
315 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ PG_PASSWORD='litespace' | |
PG_HOST='0.0.0.0' | ||
PG_PORT='5432' | ||
PG_DATABASE='litespace' | ||
REDIS_URL=redis://localhost:6379 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ PG_PASSWORD='litespace' | |
PG_HOST='0.0.0.0' | ||
PG_PORT='5432' | ||
PG_DATABASE='test' | ||
REDIS_URL=redis://localhost:6379 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ PG_PASSWORD='litespace' | |
PG_HOST='postgres' | ||
PG_PORT='5432' | ||
PG_DATABASE='test' | ||
REDIS_URL=redis://redis:6379 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 || ""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |