diff --git a/pyscriptjs/Makefile b/pyscriptjs/Makefile index fed7c837..050ffced 100644 --- a/pyscriptjs/Makefile +++ b/pyscriptjs/Makefile @@ -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/') @@ -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!" diff --git a/pyscriptjs/src/interpreter.ts b/pyscriptjs/src/interpreter.ts index 5ff662d0..0153ebd6 100644 --- a/pyscriptjs/src/interpreter.ts +++ b/pyscriptjs/src/interpreter.ts @@ -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; diff --git a/pyscriptjs/src/pyscript.py b/pyscriptjs/src/python/pyscript.py similarity index 100% rename from pyscriptjs/src/pyscript.py rename to pyscriptjs/src/python/pyscript.py diff --git a/pyscriptjs/tests/__init__.py b/pyscriptjs/tests/integration/__init__.py similarity index 100% rename from pyscriptjs/tests/__init__.py rename to pyscriptjs/tests/integration/__init__.py diff --git a/pyscriptjs/tests/conftest.py b/pyscriptjs/tests/integration/conftest.py similarity index 100% rename from pyscriptjs/tests/conftest.py rename to pyscriptjs/tests/integration/conftest.py diff --git a/pyscriptjs/tests/support.py b/pyscriptjs/tests/integration/support.py similarity index 99% rename from pyscriptjs/tests/support.py rename to pyscriptjs/tests/integration/support.py index 991151be..9dee415a 100644 --- a/pyscriptjs/tests/support.py +++ b/pyscriptjs/tests/integration/support.py @@ -3,7 +3,7 @@ import time import py import pytest -ROOT = py.path.local(__file__).dirpath("..", "..") +ROOT = py.path.local(__file__).dirpath("..", "..", "..") BUILD = ROOT.join("pyscriptjs", "build") diff --git a/pyscriptjs/tests/test_00_support.py b/pyscriptjs/tests/integration/test_00_support.py similarity index 100% rename from pyscriptjs/tests/test_00_support.py rename to pyscriptjs/tests/integration/test_00_support.py diff --git a/pyscriptjs/tests/test_01_basic.py b/pyscriptjs/tests/integration/test_01_basic.py similarity index 100% rename from pyscriptjs/tests/test_01_basic.py rename to pyscriptjs/tests/integration/test_01_basic.py diff --git a/pyscriptjs/tests/test_py_button.py b/pyscriptjs/tests/integration/test_py_button.py similarity index 100% rename from pyscriptjs/tests/test_py_button.py rename to pyscriptjs/tests/integration/test_py_button.py diff --git a/pyscriptjs/tests/test_zz_examples.py b/pyscriptjs/tests/integration/test_zz_examples.py similarity index 100% rename from pyscriptjs/tests/test_zz_examples.py rename to pyscriptjs/tests/integration/test_zz_examples.py diff --git a/pyscriptjs/tests/py-unit/__init__.py b/pyscriptjs/tests/py-unit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pyscriptjs/tests/py-unit/conftest.py b/pyscriptjs/tests/py-unit/conftest.py new file mode 100644 index 00000000..bac4bbfd --- /dev/null +++ b/pyscriptjs/tests/py-unit/conftest.py @@ -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)) diff --git a/pyscriptjs/tests/py-unit/js.py b/pyscriptjs/tests/py-unit/js.py new file mode 100644 index 00000000..d42a250e --- /dev/null +++ b/pyscriptjs/tests/py-unit/js.py @@ -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() diff --git a/pyscriptjs/tests/py-unit/micropip.py b/pyscriptjs/tests/py-unit/micropip.py new file mode 100644 index 00000000..ce3d762f --- /dev/null +++ b/pyscriptjs/tests/py-unit/micropip.py @@ -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() diff --git a/pyscriptjs/tests/py-unit/test_pyscript.py b/pyscriptjs/tests/py-unit/test_pyscript.py new file mode 100644 index 00000000..17ea9c48 --- /dev/null +++ b/pyscriptjs/tests/py-unit/test_pyscript.py @@ -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