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

Bootstrap python tests #697

Merged
merged 9 commits into from
Aug 18, 2022
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
11 changes: 6 additions & 5 deletions pyscriptjs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ tag := latest
git_hash ?= $(shell git log -1 --pretty=format:%h)

base_dir ?= $(shell git rev-parse --show-toplevel)
src_dir ?= $(base_dir)/src
src_dir ?= $(base_dir)/pyscriptjs/src
examples ?= ../$(base_dir)/examples
app_dir ?= $(shell git rev-parse --show-prefix)

CONDA_EXE := conda
CONDA_ENV ?= ./env
CONDA_ENV ?= $(base_dir)/pyscriptjs/env
env := $(CONDA_ENV)
conda_run := $(CONDA_EXE) run -p $(env)
PYTEST_EXE := $(CONDA_ENV)/bin/pytest
GOOD_NODE_VER := 14
GOOD_NPM_VER := 6
NODE_VER := $(shell node -v | cut -d. -f1 | sed 's/^v\(.*\)/\1/')
Expand Down Expand Up @@ -74,11 +75,11 @@ test:
test-local:
make examples
npm run build
pytest -vvs $(ARGS) tests/ --log-cli-level=warning
pytest -vvs $(ARGS) tests/integration/ --log-cli-level=warning

test-py:
@echo "Tests are coming :( this is a placeholder and it's meant to fail!"
$(conda_run) pytest -vv $(ARGS) tests/ --log-cli-level=warning
@echo "Tests from $(src_dir)"
$(PYTEST_EXE) -vv $(ARGS) tests/py-unit/ --log-cli-level=warning

test-ts:
@echo "Tests are coming :( this is a placeholder and it's meant to fail!"
Expand Down
2 changes: 1 addition & 1 deletion pyscriptjs/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { PyodideInterface } from './pyodide';

// eslint-disable-next-line
// @ts-ignore
import pyscript from './pyscript.py';
import pyscript from './python/pyscript.py';

let pyodide: PyodideInterface;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import py
import pytest

ROOT = py.path.local(__file__).dirpath("..", "..")
ROOT = py.path.local(__file__).dirpath("..", "..", "..")
BUILD = ROOT.join("pyscriptjs", "build")


Expand Down
Empty file.
8 changes: 8 additions & 0 deletions pyscriptjs/tests/py-unit/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""All data required for testing examples"""
import pathlib
import sys

# current working directory
base_path = pathlib.Path().absolute()
python_source = base_path / "src" / "python"
sys.path.append(str(python_source))
5 changes: 5 additions & 0 deletions pyscriptjs/tests/py-unit/js.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Mock module that emulates some of the pyodide js module features for the sake of tests"""
from unittest.mock import Mock

document = Mock()
console = Mock()
Comment on lines +1 to +5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what this code is for (same for the micropip.py file).
The other parts of tests we're using console are here for example:

            <py-script>import js; js.console.info('one')</py-script>
            <py-script>js.console.info('two')</py-script>
            <py-script>js.console.info('three')</py-script>
            <py-script>js.console.info('four')</py-script>

But it's not using this object, as far as I understand.
Can you please explain it?
Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are fixtures for the the apis that are pyodide specific. The tests/integration tests run a web server that actually run the code in pyodide while the python tests are actually running python unit tests in local python. So the js and micropip modules are not there. We can potentially think of running the tests in pyodide at some point but even then, I think it makes sense for us to mock their API to make sure that we are calling them correctly...

In general, we'll be supporting other runtimes that are not pyodide so I think that it's important that we decouple our Python higher level API and test the interface itself.

Makes sense?

(given your comment, I think it actually makes sense for us to put js.py and micropip.py in a fixtures folder but we can do it in a separate follow up PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes total sense =)
Thanks for the explanation.

4 changes: 4 additions & 0 deletions pyscriptjs/tests/py-unit/micropip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""Mock module that emulates some of the pyodide js module features for the sake of tests"""
from unittest.mock import Mock

install = Mock()
22 changes: 22 additions & 0 deletions pyscriptjs/tests/py-unit/test_pyscript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from unittest.mock import Mock

import pyscript


class TestElement:
def test_id_is_correct(self):
el = pyscript.Element("something")
assert el.id == "something"

def test_element(self, monkeypatch):
el = pyscript.Element("something")
document_mock = Mock()
call_result = "some_result"
document_mock.querySelector = Mock(return_value=call_result)
monkeypatch.setattr(pyscript, "document", document_mock)
assert not el._element
real_element = el.element
assert real_element
assert pyscript.document.querySelector.call_count == 1
pyscript.document.querySelector.assert_called_with("#something")
assert real_element == call_result