From 3f9f15e9f7a95d5f125206a605c31e52164d134b Mon Sep 17 00:00:00 2001 From: awais-codes Date: Tue, 7 May 2024 09:40:14 +0500 Subject: [PATCH 1/2] feat: crud tests for template routes --- .../src/controllers/template/addTemplate.ts | 2 +- server/src/drizzle/methods/template.ts | 18 ++--- .../services/template/addTemplateService.ts | 2 +- .../services/template/editTemplateService.ts | 2 +- server/src/tests/routes/template.spec.ts | 76 +++++++++++++++++++ 5 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 server/src/tests/routes/template.spec.ts diff --git a/server/src/controllers/template/addTemplate.ts b/server/src/controllers/template/addTemplate.ts index 01df0775d..c338d37a9 100644 --- a/server/src/controllers/template/addTemplate.ts +++ b/server/src/controllers/template/addTemplate.ts @@ -43,7 +43,7 @@ export function addTemplateRoute() { .mutation(async (opts) => { const { type, templateId, isGlobalTemplate, createdBy } = opts.input; const userClass = new User(); - const user = await userClass.findById(createdBy); + const user = await userClass.findUser({ userId: createdBy }); if (!user) { throw new Error(UserNotFoundError.message); } diff --git a/server/src/drizzle/methods/template.ts b/server/src/drizzle/methods/template.ts index e72a6353a..2e09bf968 100644 --- a/server/src/drizzle/methods/template.ts +++ b/server/src/drizzle/methods/template.ts @@ -1,20 +1,14 @@ import { eq } from 'drizzle-orm'; -import { createDb } from '../../db/client'; +import { DbClient } from '../../db/client'; import { type InsertTemplate, template as TemplateTable, } from '../../db/schema'; -import { getDB } from '../../trpc/context'; export class Template { - async createInstance() { - const dbInstance = await createDb(getDB()); - return dbInstance; - } - async findTemplate(templateId: string, includeRelated: boolean = false) { try { - const template = (await this.createInstance()).query.template.findFirst({ + const template = await DbClient.instance.query.template.findFirst({ where: eq(TemplateTable.id, templateId), // If includeRelated is true, then include with property ...(includeRelated && { @@ -36,7 +30,7 @@ export class Template { async findMany() { try { - const templates = (await this.createInstance()).query.template.findMany({ + const templates = await DbClient.instance.query.template.findMany({ with: { createdBy: { columns: { @@ -53,7 +47,7 @@ export class Template { async create(data: InsertTemplate) { try { - const createdTemplate = (await this.createInstance()) + const createdTemplate = await DbClient.instance .insert(TemplateTable) .values(data) .returning() @@ -69,7 +63,7 @@ export class Template { filter = eq(TemplateTable.id, data.templateId), ) { try { - const updatedTemplate = (await this.createInstance()) + const updatedTemplate = await DbClient.instance .update(TemplateTable) .set({ type: data.type, @@ -85,7 +79,7 @@ export class Template { async delete(id: string, filter = eq(TemplateTable.id, id)) { try { - const deletedTemplate = (await this.createInstance()) + const deletedTemplate = await DbClient.instance .delete(TemplateTable) .where(filter) .returning(); diff --git a/server/src/services/template/addTemplateService.ts b/server/src/services/template/addTemplateService.ts index 61107e784..029292cee 100644 --- a/server/src/services/template/addTemplateService.ts +++ b/server/src/services/template/addTemplateService.ts @@ -19,7 +19,7 @@ export const addTemplateService = async ( try { const userClass = new User(); const templateClass = new Template(); - const user = await userClass.findById(createdBy); + const user = await userClass.findUser({ userId: createdBy }); if (!user) { throw new Error('User not found!'); } diff --git a/server/src/services/template/editTemplateService.ts b/server/src/services/template/editTemplateService.ts index 43799380b..0e3b45f23 100644 --- a/server/src/services/template/editTemplateService.ts +++ b/server/src/services/template/editTemplateService.ts @@ -15,7 +15,7 @@ export const editTemplateService = async ( ) => { try { const templateClass = new Template(); - const template = await templateClass.findUnique(templateId); + const template = await templateClass.findTemplate(templateId); if (!template) { throw new Error('Template not found'); } diff --git a/server/src/tests/routes/template.spec.ts b/server/src/tests/routes/template.spec.ts new file mode 100644 index 000000000..6f4289dc3 --- /dev/null +++ b/server/src/tests/routes/template.spec.ts @@ -0,0 +1,76 @@ +import { describe, it, expect, beforeAll } from 'vitest'; +import { setupTest } from '../testHelpers'; +import type { trpcCaller } from '../testHelpers'; +import type { Template, User } from '../../db/schema'; +import { env } from 'cloudflare:test'; + +describe('Template Routes', () => { + let caller: trpcCaller; + let user: User; + let template: Template; + + beforeAll(async () => { + caller = await setupTest(env); + user = await caller.signUp({ + email: 'test@abc.com', + name: 'test', + username: 'test', + password: 'test123', + }); + }); + + describe('addTemplate', () => { + it('should create a template', async () => { + const result = await caller.addTemplate({ + type: 'pack', + createdBy: user.id, + isGlobalTemplate: true, + templateId: 'test-id', + }); + expect(result.message).toEqual('Template added successfully'); + }); + }); + + describe('getTemplates', () => { + it('should get templates', async () => { + const templates = await caller.getTemplates(); + expect(templates).toBeDefined(); + [template] = templates; + }); + }); + + describe('getTemplateById', () => { + it('should get template by id', async () => { + if (template) { + const foundTemplate = await caller.getTemplateById({ + templateId: template.id, + }); + expect(foundTemplate.id).toEqual(template.id); + } + }); + }); + + describe('editTemplate', () => { + it('should edit template type', async () => { + if (template) { + const typeToBeUpdated = 'item'; + const [updatedTemplate] = await caller.editTemplate({ + ...template, + type: typeToBeUpdated, + }); + expect(updatedTemplate.type).toEqual(typeToBeUpdated); + } + }); + }); + + describe('deleteTemplate', () => { + it('should delete template by id', async () => { + if (template) { + const response = await caller.deleteTemplate({ + templateId: template.id, + }); + expect(response).toEqual('Template removed'); + } + }); + }); +}); From 3852bfa3f0d24c577b73a5d4e479cec60dfd6f4a Mon Sep 17 00:00:00 2001 From: awais-codes Date: Tue, 7 May 2024 18:36:11 +0500 Subject: [PATCH 2/2] chore: used model method classes in test cases --- server/src/tests/routes/template.spec.ts | 66 +++++++++++++----------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/server/src/tests/routes/template.spec.ts b/server/src/tests/routes/template.spec.ts index 6f4289dc3..0bcca47d9 100644 --- a/server/src/tests/routes/template.spec.ts +++ b/server/src/tests/routes/template.spec.ts @@ -1,26 +1,38 @@ -import { describe, it, expect, beforeAll } from 'vitest'; +import { describe, it, expect, beforeAll, beforeEach } from 'vitest'; import { setupTest } from '../testHelpers'; import type { trpcCaller } from '../testHelpers'; -import type { Template, User } from '../../db/schema'; import { env } from 'cloudflare:test'; +import { Template as TemplateClass } from '../../drizzle/methods/template'; +import { User as UserClass } from '../../drizzle/methods/User'; +import type { Template } from '../../db/schema'; describe('Template Routes', () => { let caller: trpcCaller; - let user: User; - let template: Template; + const templateClass = new TemplateClass(); + const userClass = new UserClass(); + + let createdTemplate: Template; beforeAll(async () => { caller = await setupTest(env); - user = await caller.signUp({ - email: 'test@abc.com', - name: 'test', - username: 'test', - password: 'test123', + }); + + beforeEach(async () => { + createdTemplate = await templateClass.create({ + templateId: 'test', + isGlobalTemplate: true, + type: 'pack', }); }); describe('addTemplate', () => { it('should create a template', async () => { + const user = await userClass.create({ + email: 'test@abc.com', + name: 'test', + username: 'test', + password: 'test123', + }); const result = await caller.addTemplate({ type: 'pack', createdBy: user.id, @@ -35,42 +47,36 @@ describe('Template Routes', () => { it('should get templates', async () => { const templates = await caller.getTemplates(); expect(templates).toBeDefined(); - [template] = templates; }); }); describe('getTemplateById', () => { it('should get template by id', async () => { - if (template) { - const foundTemplate = await caller.getTemplateById({ - templateId: template.id, - }); - expect(foundTemplate.id).toEqual(template.id); - } + const foundTemplate = await caller.getTemplateById({ + templateId: createdTemplate.id, + }); + expect(foundTemplate.id).toEqual(createdTemplate.id); }); }); describe('editTemplate', () => { it('should edit template type', async () => { - if (template) { - const typeToBeUpdated = 'item'; - const [updatedTemplate] = await caller.editTemplate({ - ...template, - type: typeToBeUpdated, - }); - expect(updatedTemplate.type).toEqual(typeToBeUpdated); - } + const typeToBeUpdated = 'item'; + const [updatedTemplate] = await caller.editTemplate({ + ...createdTemplate, + templateId: createdTemplate.id, + type: typeToBeUpdated, + }); + expect(updatedTemplate.type).toEqual(typeToBeUpdated); }); }); describe('deleteTemplate', () => { it('should delete template by id', async () => { - if (template) { - const response = await caller.deleteTemplate({ - templateId: template.id, - }); - expect(response).toEqual('Template removed'); - } + const response = await caller.deleteTemplate({ + templateId: createdTemplate.id, + }); + expect(response.message).toEqual('Template removed'); }); }); });