Repository: pyscript/pyscript

Issue:
Create infrastructure and write the first Python unit test



Discussion:

Resolving patch:
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


Test patch:
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
--- a/pyscriptjs/tests/support.py
+++ b/pyscriptjs/tests/integration/support.py
@@ -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")
 
 
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
diff --git a/pyscriptjs/tests/py-unit/conftest.py b/pyscriptjs/tests/py-unit/conftest.py
new file mode 100644
--- /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
--- /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
--- /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
--- /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