codeforboston / home-energy-analysis-tool

https://www.codeforboston.org/projects/
MIT License
8 stars 25 forks source link

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

Closed thadk closed 7 months ago

thadk commented 7 months ago

From the heat-stack folder, run npm test app/utils/pyodide.test.ts or see the new whole heat-stack Checks below.

Full output:

``` ➜ npm test app/utils/pyodide.test.ts zsh: correct 'test' to 'tests' [nyae]? n > test > pushd ../rules-engine && python3 -m venv venv && source venv/bin/activate && pip install -q build && python3 -m build && popd && vitest app/utils/pyodide.test.ts ~/gitrepos/home-energy-analysis-tool/rules-engine ~/gitrepos/home-energy-analysis-tool/heat-stack [notice] A new release of pip is available: 23.2.1 -> 23.3.1 [notice] To update, run: pip install --upgrade pip * Creating venv isolated environment... * Installing packages in isolated environment... (setuptools) * Getting build dependencies for sdist... running egg_info writing src/rules_engine.egg-info/PKG-INFO writing dependency_links to src/rules_engine.egg-info/dependency_links.txt writing requirements to src/rules_engine.egg-info/requires.txt writing top-level names to src/rules_engine.egg-info/top_level.txt reading manifest file 'src/rules_engine.egg-info/SOURCES.txt' writing manifest file 'src/rules_engine.egg-info/SOURCES.txt' * Building sdist... running sdist running egg_info writing src/rules_engine.egg-info/PKG-INFO writing dependency_links to src/rules_engine.egg-info/dependency_links.txt writing requirements to src/rules_engine.egg-info/requires.txt writing top-level names to src/rules_engine.egg-info/top_level.txt reading manifest file 'src/rules_engine.egg-info/SOURCES.txt' writing manifest file 'src/rules_engine.egg-info/SOURCES.txt' running check creating rules-engine-0.0.1 creating rules-engine-0.0.1/src creating rules-engine-0.0.1/src/rules_engine creating rules-engine-0.0.1/src/rules_engine.egg-info copying files to rules-engine-0.0.1... copying README.md -> rules-engine-0.0.1 copying pyproject.toml -> rules-engine-0.0.1 copying src/rules_engine/__init__.py -> rules-engine-0.0.1/src/rules_engine copying src/rules_engine/engine.py -> rules-engine-0.0.1/src/rules_engine copying src/rules_engine.egg-info/PKG-INFO -> rules-engine-0.0.1/src/rules_engine.egg-info copying src/rules_engine.egg-info/SOURCES.txt -> rules-engine-0.0.1/src/rules_engine.egg-info copying src/rules_engine.egg-info/dependency_links.txt -> rules-engine-0.0.1/src/rules_engine.egg-info copying src/rules_engine.egg-info/requires.txt -> rules-engine-0.0.1/src/rules_engine.egg-info copying src/rules_engine.egg-info/top_level.txt -> rules-engine-0.0.1/src/rules_engine.egg-info copying src/rules_engine.egg-info/SOURCES.txt -> rules-engine-0.0.1/src/rules_engine.egg-info Writing rules-engine-0.0.1/setup.cfg Creating tar archive removing 'rules-engine-0.0.1' (and everything under it) * Building wheel from sdist * Creating venv isolated environment... * Installing packages in isolated environment... (setuptools) * Getting build dependencies for wheel... running egg_info writing src/rules_engine.egg-info/PKG-INFO writing dependency_links to src/rules_engine.egg-info/dependency_links.txt writing requirements to src/rules_engine.egg-info/requires.txt writing top-level names to src/rules_engine.egg-info/top_level.txt reading manifest file 'src/rules_engine.egg-info/SOURCES.txt' writing manifest file 'src/rules_engine.egg-info/SOURCES.txt' * Installing packages in isolated environment... (wheel) * Building wheel... running bdist_wheel running build running build_py creating build creating build/lib creating build/lib/rules_engine copying src/rules_engine/__init__.py -> build/lib/rules_engine copying src/rules_engine/engine.py -> build/lib/rules_engine running egg_info writing src/rules_engine.egg-info/PKG-INFO writing dependency_links to src/rules_engine.egg-info/dependency_links.txt writing requirements to src/rules_engine.egg-info/requires.txt writing top-level names to src/rules_engine.egg-info/top_level.txt reading manifest file 'src/rules_engine.egg-info/SOURCES.txt' writing manifest file 'src/rules_engine.egg-info/SOURCES.txt' installing to build/bdist.macosx-13-arm64/wheel running install running install_lib creating build/bdist.macosx-13-arm64 creating build/bdist.macosx-13-arm64/wheel creating build/bdist.macosx-13-arm64/wheel/rules_engine copying build/lib/rules_engine/__init__.py -> build/bdist.macosx-13-arm64/wheel/rules_engine copying build/lib/rules_engine/engine.py -> build/bdist.macosx-13-arm64/wheel/rules_engine running install_egg_info Copying src/rules_engine.egg-info to build/bdist.macosx-13-arm64/wheel/rules_engine-0.0.1-py3.11.egg-info running install_scripts creating build/bdist.macosx-13-arm64/wheel/rules_engine-0.0.1.dist-info/WHEEL creating '/Users/thadk/gitrepos/home-energy-analysis-tool/rules-engine/dist/.tmp-k18bytp9/rules_engine-0.0.1-py3-none-any.whl' and adding 'build/bdist.macosx-13-arm64/wheel' to it adding 'rules_engine/__init__.py' adding 'rules_engine/engine.py' adding 'rules_engine-0.0.1.dist-info/METADATA' adding 'rules_engine-0.0.1.dist-info/WHEEL' adding 'rules_engine-0.0.1.dist-info/top_level.txt' adding 'rules_engine-0.0.1.dist-info/RECORD' removing build/bdist.macosx-13-arm64/wheel Successfully built rules-engine-0.0.1.tar.gz and rules_engine-0.0.1-py3-none-any.whl ~/gitrepos/home-energy-analysis-tool/heat-stack DEV v0.34.4 /Users/thadk/gitrepos/home-energy-analysis-tool/heat-stack stdout | app/utils/pyodide.test.ts > pyodide loads Loading numpy stdout | app/utils/pyodide.test.ts > pyodide loads Loaded numpy Loading rules_engine stdout | app/utils/pyodide.test.ts > pyodide loads Loaded rules_engine ✓ app/utils/pyodide.test.ts (1) 1667ms ✓ pyodide loads 1665ms Test Files 1 passed (1) Tests 1 passed (1) Start at 01:59:23 Duration 2.21s (transform 70ms, setup 387ms, collect 4ms, tests 1.67s, environment 0ms, prepare 41ms) ```

Test output:

Intentional fail:

image

Pass:

image

thadk commented 7 months ago

Making this a draft PR: Oops, I made the branch off an outdated branch so the readme would be altered. I'll recreate this PR branch correctly tomorrow.

thadk commented 7 months ago

@alanisaac Welcome to review. I also changed the Github Action python target version to 3.11.3 which is the current pyodide release's version .

The main next step I can think of is moving the rules_engine-0.0.1-py3-none-any.whl file out of the rules-engine/dist folder into the heat-stack/public/pyodide-env folder so it can be used by the frontend too and not just in the test, but we'll get to that soon. It's just a little tweak.

thadk commented 7 months ago

@alanisaac Yep, that folder re-hosts locally:

Both of these binaries have to be fetched at Javascript run-time and aren't bundleable along with the Pyodide Javascript library itself.

thadk commented 7 months ago

used engine.hdd(57, 60) but we may ultimately want to use an alternative version of the test_rules_engine/*.py files

I tried to run pytest and the test_engine.py from within the Pyodide environment. I executed that file contents in python within Pyodide, then ran pytest.main() but python crashed. Pytest seems very file oriented and pyodide is not too much.

Based on that, If we want an internal Pyodide test like test_engine.py, we might need to wrap those assertions with some other library than pytest. On ChatGPT or on StackOverflow, I saw unittest suggested but I'm not at all sure if that would be the right choice.

More broadly, if we want to run test_examples.py in Pyodide, it might need to be made into a whl or part of the main whl because of its directory structure.

jkoren commented 7 months ago

Thad will:

plocket commented 7 months ago

Also, just to be sure, close the block comment at https://github.com/codeforboston/home-energy-analysis-tool/pull/109/files#diff-ae03e02fefef517ad9aa21f08290622cf90b06ccfb95f94362b273a43a70aa9eR16. [It's fine for now, but if we remove the in-line commenting, it'll be a problem.]