From 17812a5070a5ddcea722ccd210a1d5c9a44154e5 Mon Sep 17 00:00:00 2001 From: Thad Kerosky Date: Wed, 22 Nov 2023 10:32:39 -0500 Subject: [PATCH] add pyodide rules_engine whl test with vitest --- heat-stack/app/utils/pyodide.test.ts | 32 ++++++++++++++++++++++++++++ heat-stack/package.json | 2 +- heat-stack/tests/mocks/index.ts | 4 ++-- 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 heat-stack/app/utils/pyodide.test.ts diff --git a/heat-stack/app/utils/pyodide.test.ts b/heat-stack/app/utils/pyodide.test.ts new file mode 100644 index 00000000..adc4dee9 --- /dev/null +++ b/heat-stack/app/utils/pyodide.test.ts @@ -0,0 +1,32 @@ +import { expect, test } from 'vitest' +import * as pyodideModule from 'pyodide' + +/* For this to pass, you must run + `pushd ../rules-engine && python3 -m venv venv && source venv/bin/activate && pip install -q build && python3 -m build && popd` */ + +/* Referenced https://github.com/epicweb-dev/full-stack-testing/blob/main/exercises/04.unit-test/02.solution.spies/app/utils/misc.error-message.test.ts of https://www.epicweb.dev/workshops/web-application-testing*/ +test('pyodide loads', async () => { + const getPyodide = async () => { + // public folder: + return await pyodideModule.loadPyodide({ + indexURL: 'public/pyodide-env/', + }) + } + const runPythonScript = async () => { + const pyodide: any = await getPyodide() + // console.log(engine); + await pyodide.loadPackage('numpy') + await pyodide.loadPackage( + '../rules-engine/dist/rules_engine-0.0.1-py3-none-any.whl', + ) + return pyodide + } + // consider running https://github.com/codeforboston/home-energy-analysis-tool/blob/main/rules-engine/tests/test_rules_engine/test_engine.py + const pyodide: any = await runPythonScript() + const result = await pyodide.runPythonAsync(` + from rules_engine import engine + + out = engine.hdd(57, 60) + out`) + expect(result).toBe(3) +}) diff --git a/heat-stack/package.json b/heat-stack/package.json index ed44b2b9..4ae9cabc 100644 --- a/heat-stack/package.json +++ b/heat-stack/package.json @@ -20,7 +20,7 @@ "setup": "npm run build && prisma generate && prisma migrate deploy && prisma db seed && playwright install", "start": "cross-env NODE_ENV=production node .", "start:mocks": "cross-env NODE_ENV=production MOCKS=true tsx .", - "test": "vitest", + "test": "pushd ../rules-engine && python3 -m venv venv && source venv/bin/activate && pip install -q build && python3 -m build && popd && vitest", "coverage": "vitest run --coverage", "test:e2e": "npm run test:e2e:dev --silent", "test:e2e:dev": "playwright test --ui", diff --git a/heat-stack/tests/mocks/index.ts b/heat-stack/tests/mocks/index.ts index 62a4786a..85145fde 100644 --- a/heat-stack/tests/mocks/index.ts +++ b/heat-stack/tests/mocks/index.ts @@ -1,7 +1,7 @@ import closeWithGrace from 'close-with-grace' import { passthrough, http } from 'msw' import { setupServer } from 'msw/node' -import { handlers as githubHandlers } from './github.ts' +// import { handlers as githubHandlers } from './github.ts' import { handlers as resendHandlers } from './resend.ts' const miscHandlers = [ @@ -13,7 +13,7 @@ const miscHandlers = [ export const server = setupServer( ...miscHandlers, ...resendHandlers, - ...githubHandlers, + // ...githubHandlers, /* commenting out to enable app/utils/pyodide.test.ts ) server.listen({ onUnhandledRequest: 'warn' })