Skip to content

Commit 5c2e9c1

Browse files
committed
Refactor around the fileRead in test files & add unit test cases
1 parent 66b009e commit 5c2e9c1

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

__tests__/code-excution/python.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,35 @@ describe("Python transform into executable", () => {
1414

1515

1616
describe("Python code excution test", () => {
17-
let code: string
17+
let fileJobs: Promise<any>[] = []
18+
let codes: string[]
19+
fileJobs.push(fs.promises.readFile(`${__dirname}/../test-code/python/counter.py`))
20+
fileJobs.push(fs.promises.readFile(`${__dirname}/../test-code/python/helloWorld.py`))
1821
beforeAll((done) => {
19-
fs.readFile(`${__dirname}/../test-code/python/counter.py`, 'utf8', (err, data) => {
20-
code = data
22+
Promise.all(fileJobs)
23+
.then(outputs => {
24+
codes = outputs
2125
done()
2226
})
2327
})
2428
it('should calculate a frequency in string', async () => {
2529
const codeContext: CodeContext = {
26-
code: code,
30+
code: codes[0],
2731
functionName: "calculate"
2832
}
2933
const worker = new JobWorker("python3", codeContext);
3034
const response = await worker.startContainer()
3135
expect(response.codeOutput).toBe("Counter({'a': 2, 's': 2, 'w': 2, 'e': 2, 'l': 1, 'd': 1, 'k': 1})")
3236
await worker.cleanupJob();
3337
})
38+
it('should return string "Hello World"', async () => {
39+
const codeContext: CodeContext = {
40+
code: codes[1],
41+
functionName: "run"
42+
}
43+
const worker = new JobWorker("python3", codeContext);
44+
const response = await worker.startContainer()
45+
expect(response.codeOutput).toBe("Hello World!")
46+
await worker.cleanupJob();
47+
});
3448
})
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Execute:
2+
def run(self):
3+
return "Hello World!"
4+

dockerfiles/python3.Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
FROM python:3.8-slim-buster
2+
23
WORKDIR /src
4+
35
CMD ["python3", "target.py"]

src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { CodeContext } from "./types/worker";
2+
import JobWorker from "./worker";
3+
import fs from "fs"
4+
5+
fs.readFile(`${__dirname}/../__tests__/test-code/python/counter.py`, 'utf8', (err, data) => {
6+
const codeContext: CodeContext = {
7+
code: data,
8+
functionName: "calculate"
9+
}
10+
const worker = new JobWorker("python3", codeContext);
11+
worker.startContainer()
12+
.then((output) => {
13+
console.log(output)
14+
}).catch(e => {
15+
console.log(e)
16+
})
17+
})

0 commit comments

Comments
 (0)