Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a vitest unit test for the Rules Engine Python whl #109

Merged
merged 12 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions .github/workflows/heat-stack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
name: whole heat-stack
on:
push:
branches:
- main
# - dev
pull_request: {}

env:
working-directory: heat-stack

defaults:
run:
working-directory: heat-stack

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions:
actions: write
contents: read

jobs:
lint:
name: ⬣ ESLint
runs-on: ubuntu-latest
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v3

- name: ⎔ Setup node
uses: actions/setup-node@v3
with:
node-version: 18

- name: 📥 Download deps
uses: bahmutov/npm-install@v1
with:
working-directory: ${{ env.working-directory }}

- name: 🖼 Build icons
working-directory: ${{ env.working-directory }}
run: npm run build:icons

- name: 🔬 Lint
working-directory: ${{ env.working-directory }}
run: npm run lint


typecheck:
name: ʦ TypeScript
runs-on: ubuntu-latest
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v3

- name: ⎔ Setup node
uses: actions/setup-node@v3
with:
node-version: 18

- name: 📥 Download deps
uses: bahmutov/npm-install@v1
with:
working-directory: ${{ env.working-directory }}

- name: 🖼 Build icons
run: npm run build:icons

- name: 🔎 Type check
run: npm run typecheck --if-present

vitest:
name: ⚡ Vitest pyodide.test.ts
runs-on: ubuntu-latest
steps:
- name: ⬇️ Checkout repo
uses: actions/checkout@v3

- name: ⎔ Setup node
uses: actions/setup-node@v3
with:
node-version: 18

- name: 📥 Download deps
uses: bahmutov/npm-install@v1
with:
working-directory: ${{ env.working-directory }}

- name: 🏄 Copy test env vars
run: cp .env.example .env

- name: 🖼 Build icons
run: npm run build:icons

- name: ⚡ Run vitest
run: npm run test app/utils/pyodide.test.ts -- --coverage

# playwright tests work great but slight jank/inconsistency passing, and not used yet, so disabling for now
# playwright:
# name: 🎭 Playwright
# runs-on: ubuntu-latest
# timeout-minutes: 60
# steps:
# - name: ⬇️ Checkout repo
# uses: actions/checkout@v3

# - name: 🏄 Copy test env vars
# run: cp .env.example .env

# - name: ⎔ Setup node
# uses: actions/setup-node@v3
# with:
# node-version: 18

# - name: 📥 Download deps
# uses: bahmutov/npm-install@v1
# with:
# working-directory: ${{ env.working-directory }}

# - name: 📥 Install Playwright Browsers
# run: npm run test:e2e:install

# - name: 🛠 Setup Database
# run: npx prisma migrate deploy

# - name: 🏦 Cache Database
# id: db-cache
# uses: actions/cache@v3
# with:
# path: prisma/data.db
# key:
# db-cache-schema_${{ hashFiles('./prisma/schema.prisma')
# }}-migrations_${{ hashFiles('./prisma/migrations/*/migration.sql')
# }}

# - name: 🌱 Seed Database
# if: steps.db-cache.outputs.cache-hit != 'true'
# run: npx prisma db seed
# env:
# MINIMAL_SEED: true

# - name: 🏗 Build
# run: npm run build

# - name: 🎭 Playwright tests
# run: npx playwright test

# - name: 📊 Upload report
# uses: actions/upload-artifact@v3
# if: always()
# with:
# name: playwright-report
# path: playwright-report/
# retention-days: 30

# deploy:
# name: 🚀 Deploy
# runs-on: ubuntu-latest
# needs: [lint, typecheck, vitest, playwright]
# # only build/deploy main branch on pushes
# if:
# ${{ (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev') &&
# github.event_name == 'push' }}

# steps:
# - name: ⬇️ Checkout repo
# uses: actions/checkout@v3

# - name: 👀 Read app name
# uses: SebRollen/[email protected]
# id: app_name
# with:
# file: 'fly.toml'
# field: 'app'

# # move Dockerfile to root
# - name: 🚚 Move Dockerfile
# run: |
# mv ./other/Dockerfile ./Dockerfile
# mv ./other/.dockerignore ./.dockerignore

# - name: 🎈 Setup Fly
# uses: superfly/flyctl-actions/[email protected]

# - name: 🚀 Deploy Staging
# if: ${{ github.ref == 'refs/heads/dev' }}
# run:
# flyctl deploy --remote-only --build-arg COMMIT_SHA=${{ github.sha }}
# --app ${{ steps.app_name.outputs.value }}-staging
# env:
# FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}

# - name: 🚀 Deploy Production
# if: ${{ github.ref == 'refs/heads/main' }}
# run:
# flyctl deploy --remote-only --build-arg COMMIT_SHA=${{ github.sha }}
# env:
# FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
8 changes: 4 additions & 4 deletions .github/workflows/test-rules-engine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]
python-version: ["3.11.3"] # Check python version at https://pyodide.org/en/stable/project/changelog.html

steps:
- uses: actions/checkout@v3
Expand All @@ -29,7 +29,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]
python-version: ["3.11.3"]

steps:
- uses: actions/checkout@v3
Expand All @@ -44,7 +44,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]
python-version: ["3.11.3"]

steps:
- uses: actions/checkout@v3
Expand All @@ -59,7 +59,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10"]
python-version: ["3.11.3"]

steps:
- uses: actions/checkout@v3
Expand Down
3 changes: 3 additions & 0 deletions heat-stack/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public/pyodide-env/
public/build
build/index.js
1 change: 1 addition & 0 deletions heat-stack/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
fixStyle: 'inline-type-imports',
},
],
'@typescript-eslint/no-unused-vars': ['warn'],
'import/no-duplicates': ['warn', { 'prefer-inline': true }],
'import/consistent-type-specifier-style': ['warn', 'prefer-inline'],
'import/order': [
Expand Down
71 changes: 71 additions & 0 deletions heat-stack/app/utils/pyodide.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as pyodideModule from 'pyodide'
import { expect, test } from 'vitest'

/* 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')

/* NOTES for pyodide-core:
need to be a special version from the release page, no pure whl: https://github.com/pydantic/pydantic-core/pull/128
get it from https://github.com/pydantic/pydantic-core/releases e.g. https://github.com/pydantic/pydantic-core/releases/download/v2.14.5/pydantic_core-2.14.5-cp311-cp311-emscripten_3_1_32_wasm32.whl
*/
await pyodide.loadPackage(
'public/pyodide-env/pydantic_core-2.14.5-cp311-cp311-emscripten_3_1_32_wasm32.whl',
)

/* NOTES for pydantic, typing-extensions, annotated_types:
pyodide should match pyodide-core somewhat.
typing-extensions needs specific version per https://github.com/pyodide/pyodide/issues/4234#issuecomment-1771735148
try getting it from
- https://pypi.org/project/pydantic/#files
- https://pypi.org/project/typing-extensions/
- https://pypi.org/project/annotated-types/#files
*/
await pyodide.loadPackage(
'public/pyodide-env/pydantic-2.5.2-py3-none-any.whl',
)
await pyodide.loadPackage(
'public/pyodide-env/typing_extensions-4.8.0-py3-none-any.whl',
)
await pyodide.loadPackage(
'public/pyodide-env/annotated_types-0.5.0-py3-none-any.whl',
)

/* NOTES FOR DEBUGGING new requirements.txt
and getting specific versions of pure whl */
// the below works but uses the internet/pypi content delivery network rather than localhost.
// await pyodide.loadPackage('micropip')
// const micropip = await pyodide.pyimport('micropip')
// await micropip.install([
// 'typing-extensions==4.8.0',
// 'pydantic_core==2.14.5',
// 'pydantic==2.5.2',
// ])
// await micropip.install(['annotated-types'])

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)
}, 6000)
8 changes: 4 additions & 4 deletions heat-stack/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions heat-stack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "cd ../rules-engine && python3 -m venv venv && . venv/bin/activate && pip install -q build && python3 -m build && cd ../heat-stack && vitest",
"coverage": "vitest run --coverage",
"test:e2e": "npm run test:e2e:dev --silent",
"test:e2e:dev": "playwright test --ui",
Expand Down Expand Up @@ -83,7 +83,7 @@
"lru-cache": "^10.0.1",
"morgan": "^1.10.0",
"prisma": "^5.3.1",
"pyodide": "^0.24",
"pyodide": "0.24.1",
"qrcode": "^1.5.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
Loading
Loading