Skip to content

Commit

Permalink
Add tests for program execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Odraxs committed Dec 5, 2023
1 parent 07eb72f commit 8920e28
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/program/dtos/program.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AllowedProgrammingLanguages } from '../allowedProgrammingLanguages';
/**
* params
* @userId
* @name
* @executable
* @language
*/
Expand Down
4 changes: 2 additions & 2 deletions src/program/programExec/ProgramExecJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ProgramExecJs implements IProgramExec {
async runExecutable(programDto: ProgramDto): Promise<string> {
const { filePath } = this.fetchFilePath(programDto);
try {
const result = await this.executeCommand('node', [filePath]);
const result = (await this.executeCommand('node', [filePath])).trim();
return result;
} catch (error) {
throw error;
Expand All @@ -50,7 +50,7 @@ export class ProgramExecJs implements IProgramExec {
shell: true,
});
if (process.error) {
throw new Error('a');
throw new Error();
}

const stdout = process.stdout?.toString();
Expand Down
99 changes: 99 additions & 0 deletions src/program/test/program.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { DeepMockProxy, mockDeep } from 'jest-mock-extended';
import { PrismaService } from '../../common/services/prisma.service';
import { Test, TestingModule } from '@nestjs/testing';
import { AllowedProgrammingLanguages } from '../allowedProgrammingLanguages';
import { ProgramService } from '../program.service';
import { BadRequestException } from '@nestjs/common';
import { ProgramExecFactory } from '../programExec/ProgramExecFactory';
import mockUser from '../../../test/mockUser';
import * as fs from 'fs';
import * as path from 'path';
import { ProgramExecResultDto } from '../dtos/programExecResult.dto';

describe('UserService', () => {
const BASE_DIRECTORY: string = 'uploadedPrograms';
const testFileName: string = 'testCode';
const testFilePath = () => {
const rootPath = path.resolve(__dirname, '../../..');
const directoryPath = path.join(rootPath, BASE_DIRECTORY, mockUser.id);
return directoryPath;
};

let service: ProgramService;
let spyPrismaService: DeepMockProxy<PrismaService>;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let programExecFactory: ProgramExecFactory;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ProgramService,
ProgramExecFactory,
{
provide: PrismaService,
useFactory: () => mockDeep<PrismaService>(),
},
],
}).compile();

service = module.get<ProgramService>(ProgramService);
programExecFactory = module.get<ProgramExecFactory>(ProgramExecFactory);
spyPrismaService = module.get(
PrismaService,
) as DeepMockProxy<PrismaService>;
});

afterEach(async () => {
const filePath = testFilePath();
fs.rmSync(filePath, { recursive: true, force: true });
});

describe('programService', () => {
it('should compile and retrieve the code result', async () => {
const date: Date = new Date();
// Executable in string is:
// const sum = 1 + 1;
// console.log(sum);
const validExecutable =
'Y29uc3Qgc3VtID0gMSArIDE7DQpjb25zb2xlLmxvZyhzdW0pOw==';
const program = {
id: 'fcd2fa2d-f5f4-4ed0-9d75-f3ca6ddd4c36',
userId: mockUser.id,
name: testFileName,
executable: validExecutable,
language: AllowedProgrammingLanguages.JS,
createdAt: date,
updatedAt: date,
};

const result = new ProgramExecResultDto(
program.name,
program.language,
'2',
);

spyPrismaService.program.create.mockResolvedValue(program);
expect(await service.storeExecuteProgram(program)).toStrictEqual(result);
});

it('should retrieve a bad request exception', async () => {
const date: Date = new Date();
// Executable in string is:
// console.log(1 + 1
const validExecutable = 'Y29uc29sZS5sb2coMSArIDE=';
const program = {
id: 'fcd2fa2d-f5f4-4ed0-9d75-f3ca6ddd4c36',
userId: mockUser.id,
name: testFileName,
executable: validExecutable,
language: AllowedProgrammingLanguages.JS,
createdAt: date,
updatedAt: date,
};

await expect(service.storeExecuteProgram(program)).rejects.toThrow(
BadRequestException,
);
});
});
});

0 comments on commit 8920e28

Please sign in to comment.