Skip to content

Commit

Permalink
Test python runner
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoVictor committed Mar 16, 2024
1 parent c773333 commit 077a66e
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions container/tests/unit/use-cases/run-python.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const { LANGUAGE } = require("../../../src/common/constants");
const { main } = require("../../../src/use-cases/run-python");

const mockGetTemplateByLanguage = jest.fn();
jest.mock("../../../src/common/file", () => {
return {
getTemplateByLanguage: (params) => mockGetTemplateByLanguage(params),
};
});

const mockWriteFile = jest.fn();
jest.mock("node:fs/promises", () => {
return {
writeFile: (filename, content) => mockWriteFile(filename, content),
};
});

const mockRandomUUID = jest.fn();

const uuid = "6c095a0d-b1da-40cc-aa0b-96580ef29b01";
mockRandomUUID.mockReturnValueOnce(uuid);

jest.mock("node:crypto", () => {
return {
randomUUID: () => mockRandomUUID(),
};
});

const mockCommand = jest.fn();
jest.mock(`../../../src/common/command`, () => {
return {
command: (program, args) => mockCommand(program, args),
};
});

describe("Run Python Use Case", () => {
it("should be able to run Python code", async () => {
const template = "# @code-here";
mockGetTemplateByLanguage.mockResolvedValueOnce(template);

const inputs = [
{
value: 5,
},
];

const code = "def run(value):\n return pow(value, 2)\n";

const value = inputs[0].value;
const expected = [{ input: value, output: value ** 2 }];
mockCommand.mockResolvedValueOnce(JSON.stringify(expected));

const response = await main({ code, inputs });

expect(mockGetTemplateByLanguage).toHaveBeenCalledWith({
language: LANGUAGE.PYTHON,
});

const fileName = `/tmp/${uuid}.py`;
expect(mockWriteFile).toHaveBeenCalledWith(
expect.stringMatching(fileName),
code
);
expect(mockCommand).toHaveBeenCalledWith("python", [
fileName,
JSON.stringify(inputs),
]);
expect(response).toStrictEqual(expected);
});
});

0 comments on commit 077a66e

Please sign in to comment.