From 71f731d4f61fe2af5d000b2c32047dabd7fb4272 Mon Sep 17 00:00:00 2001 From: Tejas Mehta Date: Sun, 3 Nov 2024 23:38:34 -0500 Subject: [PATCH] fix misc controller issues --- .../file_config/file_config.controller.ts | 23 +++-- .../file_config/file_config.service.ts | 10 +- .../db-service/test/file_config.e2e-spec.ts | 96 +++++++++++++------ 3 files changed, 88 insertions(+), 41 deletions(-) diff --git a/packages/db-service/src/modules/file_config/file_config.controller.ts b/packages/db-service/src/modules/file_config/file_config.controller.ts index 11236f14..dc4226ad 100644 --- a/packages/db-service/src/modules/file_config/file_config.controller.ts +++ b/packages/db-service/src/modules/file_config/file_config.controller.ts @@ -17,11 +17,18 @@ export class FileConfigController async createConfig( request: FileConfigProto.CreateFileServiceConfigRequest, ): Promise { - const configId = request.projectId.toString(); + const configId = request.projectId; - const existingConfig = - await this.fileServiceConfigService.getConfig(configId); - if (existingConfig) { + try { + const existingConfig = + await this.fileServiceConfigService.getConfig(configId); + if (existingConfig) { + throw new RpcException({ + code: status.ALREADY_EXISTS, + message: 'Config already exists', + }); + } + } catch (error) { throw new RpcException({ code: status.ALREADY_EXISTS, message: 'Config already exists', @@ -40,9 +47,7 @@ export class FileConfigController async getConfig( request: FileConfigProto.GetFileServiceConfigRequest, ): Promise { - const config = await this.fileServiceConfigService.getConfig( - request.id.toString(), - ); + const config = await this.fileServiceConfigService.getConfig(request.id); if (!config) { throw new RpcException({ code: status.NOT_FOUND, @@ -62,7 +67,7 @@ export class FileConfigController ): Promise { try { const config = await this.fileServiceConfigService.updateConfig( - request.id.toString(), + request.id, request, ); @@ -93,7 +98,7 @@ export class FileConfigController ): Promise { try { const config = await this.fileServiceConfigService.deleteConfig( - request.id.toString(), + request.id, ); return { id: config.id, diff --git a/packages/db-service/src/modules/file_config/file_config.service.ts b/packages/db-service/src/modules/file_config/file_config.service.ts index 1b3278b5..dd78dd48 100644 --- a/packages/db-service/src/modules/file_config/file_config.service.ts +++ b/packages/db-service/src/modules/file_config/file_config.service.ts @@ -14,7 +14,7 @@ export class FileServiceConfigService { data: { Project: { connect: { - id: configData.projectId, + id: Number(configData.projectId), }, }, }, @@ -26,7 +26,7 @@ export class FileServiceConfigService { }); } - async getConfig(configId: string): Promise { + async getConfig(configId: number): Promise { return this.prisma.fileServiceConfig.findUnique({ where: { id: Number(configId), @@ -40,7 +40,7 @@ export class FileServiceConfigService { } async updateConfig( - configId: string, + configId: number, configData: FileConfigProto.UpdateFileServiceConfigRequest, ): Promise { return this.prisma.fileServiceConfig.update({ @@ -56,7 +56,7 @@ export class FileServiceConfigService { }, Project: { connect: { - id: configData.id, + id: Number(configData.id), }, }, FileServiceFile: { @@ -80,7 +80,7 @@ export class FileServiceConfigService { }); } - async deleteConfig(configId: string): Promise { + async deleteConfig(configId: number): Promise { return this.prisma.fileServiceConfig.delete({ where: { id: Number(configId), diff --git a/packages/db-service/test/file_config.e2e-spec.ts b/packages/db-service/test/file_config.e2e-spec.ts index 71181396..8e3c85b7 100644 --- a/packages/db-service/test/file_config.e2e-spec.ts +++ b/packages/db-service/test/file_config.e2e-spec.ts @@ -4,10 +4,15 @@ import { AppModule } from '../src/app.module'; import * as ProtoLoader from '@grpc/proto-loader'; import * as GRPC from '@grpc/grpc-js'; import { MicroserviceOptions, Transport } from '@nestjs/microservices'; -import { FileConfigProto, FileConfigProtoFile } from 'juno-proto'; +import { + FileConfigProto, + FileConfigProtoFile, + ResetProto, + ResetProtoFile, +} from 'juno-proto'; let app: INestMicroservice; -let createdConfigId: string; +let createdConfigId: number; jest.setTimeout(15000); @@ -19,8 +24,11 @@ async function initApp() { const app = moduleFixture.createNestMicroservice({ transport: Transport.GRPC, options: { - package: [FileConfigProto.JUNO_FILE_SERVICE_CONFIG_PACKAGE_NAME], - protoPath: [FileConfigProtoFile], + package: [ + FileConfigProto.JUNO_FILE_SERVICE_CONFIG_PACKAGE_NAME, + ResetProto.JUNO_RESET_DB_PACKAGE_NAME, + ], + protoPath: [FileConfigProtoFile, ResetProtoFile], url: process.env.DB_SERVICE_ADDR, }, }); @@ -32,12 +40,47 @@ async function initApp() { beforeAll(async () => { app = await initApp(); + const proto = ProtoLoader.loadSync([ResetProtoFile]) as any; + + const protoGRPC = GRPC.loadPackageDefinition(proto) as any; + + const resetClient = new protoGRPC.juno.reset_db.DatabaseReset( + process.env.DB_SERVICE_ADDR, + GRPC.credentials.createInsecure(), + ); + + await new Promise((resolve) => { + resetClient.resetDb({}, () => { + resolve(0); + }); + }); + + app.close(); +}); + +beforeEach(async () => { + app = await initApp(); }); afterEach(async () => { await app.close(); }); +function tryCreateConfig(configClient: any) { + return new Promise((resolve) => { + configClient.createConfig( + { + projectId: 0, + buckets: [], + files: [], + }, + () => { + resolve(); + }, + ); + }); +} + describe('File Service Config Tests', () => { let configClient: any; @@ -56,11 +99,9 @@ describe('File Service Config Tests', () => { (resolve, reject) => { configClient.createConfig( { - config: { - projectId: '1', - buckets: [], - files: [], - }, + projectId: 0, + buckets: [], + files: [], }, (err, res) => { if (err) { @@ -82,11 +123,9 @@ describe('File Service Config Tests', () => { await new Promise((resolve, reject) => { configClient.createConfig( { - config: { - projectId: '1', - buckets: [], - files: [], - }, + projectId: 0, + buckets: [], + files: [], }, (err, res) => { if (err) { @@ -103,6 +142,7 @@ describe('File Service Config Tests', () => { }); it('deletes a config', async () => { + await tryCreateConfig(configClient); const deleteResponse = await new Promise((resolve, reject) => { configClient.deleteConfig({ id: createdConfigId }, (err, res) => { if (err) { @@ -119,13 +159,16 @@ describe('File Service Config Tests', () => { it('deletes a nonexistent config', async () => { try { await new Promise((resolve, reject) => { - configClient.deleteConfig({ id: 'nonexistent-id' }, (err, res) => { - if (err) { - reject(err); - } else { - resolve(res); - } - }); + configClient.deleteConfig( + { id: createdConfigId + 1000000 }, + (err, res) => { + if (err) { + reject(err); + } else { + resolve(res); + } + }, + ); }); } catch (err) { expect(err).toBeDefined(); @@ -133,15 +176,13 @@ describe('File Service Config Tests', () => { }); it('updates a config', async () => { + await tryCreateConfig(configClient); const updateResponse = await new Promise((resolve, reject) => { configClient.updateConfig( { id: createdConfigId, - config: { - projectId: '1', - buckets: [], - files: [], - }, + buckets: [], + files: [], }, (err, res) => { if (err) { @@ -159,7 +200,7 @@ describe('File Service Config Tests', () => { it('reading a nonexistent config', async () => { try { await new Promise((resolve, reject) => { - configClient.getConfig({ id: 'nonexistent-id' }, (err, res) => { + configClient.getConfig({ id: createdConfigId + 10000 }, (err, res) => { if (err) { reject(err); } else { @@ -173,6 +214,7 @@ describe('File Service Config Tests', () => { }); it('reading a config', async () => { + await tryCreateConfig(configClient); const readResponse: FileConfigProto.FileServiceConfig = await new Promise( (resolve, reject) => { configClient.getConfig({ id: createdConfigId }, (err, res) => {