diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/constants.py b/tests/constants.py deleted file mode 100644 index 6932dd2..0000000 --- a/tests/constants.py +++ /dev/null @@ -1,27 +0,0 @@ -PY_MODULE = """from spylt import require_svelte - -app = require_svelte("{tempdir}/App.svelte") - - -@app.backend() -def say_hello(name: str) -> str: - '''Says hello to the user''' - return f"Hello {{name}}" -""".format - -SVELTE_MODULE = """ - -
- - -

{greeting}

-
""" diff --git a/tests/test_builder.py b/tests/test_builder.py deleted file mode 100644 index ba4e408..0000000 --- a/tests/test_builder.py +++ /dev/null @@ -1,108 +0,0 @@ -"""Test building functions that are used for modules""" -from tempfile import TemporaryDirectory -from pathlib import Path -import subprocess -import runpy -import ast -import os - -import spylt -from spylt.module import Module -from spylt import builder -from spylt.cli import REQUIREMENTS - -from tests.constants import PY_MODULE, SVELTE_MODULE - - -def write_mock_file(path, data): - with open(path, "w", encoding="utf-8") as fh: - fh.write(data) - - -def test_create_link(): - """Test if `create_link()` creates a proper App initializer""" - with TemporaryDirectory() as _tempdir: - tempdir = Path(_tempdir) - write_mock_file(tempdir / "App.py", PY_MODULE(tempdir=tempdir)) - write_mock_file(tempdir / "App.svelte", SVELTE_MODULE) - linker_code = builder.create_link(f"{tempdir / 'App.py'}:app") - - error_output = None - try: - subprocess.run( - ["node", "--input-type=module"], - input=linker_code, - capture_output=True, - text=True, - check=True, - ) - except subprocess.CalledProcessError as err: - error_output = err.stderr - # Check if Node complains about .svelte file extension in linker - # which indicates a syntactically correct JS file - assert ( - "node:internal/errors:490" in error_output - or "node:internal/errors:478" in error_output - ) - - -def test_create_html(): - """Test if `create_html()` correctly compiles Svelte and bundles to HTML/CSS/JS""" - with TemporaryDirectory() as _tempdir: - tempdir = Path(_tempdir) - write_mock_file(tempdir / "App.py", PY_MODULE(tempdir=tempdir)) - write_mock_file(tempdir / "App.svelte", SVELTE_MODULE) - linker_code = builder.create_link(f"{tempdir / 'App.py'}:app") - rollup_config = ( - Path(spylt.__file__).parent / "rollup.config.js.txt" - ).read_text("utf-8") - with open(tempdir / "rollup.config.js", "w", encoding="utf-8") as fh: - fh.write(rollup_config) - assert (tempdir / "rollup.config.js").exists() - os.chdir(tempdir) - - subprocess.call(["npm", "init", "--y"]) - subprocess.call(["npm", "install", *REQUIREMENTS]) - - html = builder.create_html(linker_code) - # Check if JavaScript compiled correctly - assert "var app=function()" in html - - -def test__create_api(): - """Test if inner `create_api()` returns correct function information and converts code""" - with TemporaryDirectory() as _tempdir: - tempdir = Path(_tempdir) - write_mock_file(tempdir / "App.py", PY_MODULE(tempdir=tempdir)) - # Required to import the module with runpy - write_mock_file(tempdir / "App.svelte", SVELTE_MODULE) - module: Module = runpy.run_path(str(tempdir / "App.py"))["app"] - apis = module._apis # pylint:disable=W0212 - - _, source, _, imports, docs = builder._create_api( # pylint:disable=W0212 - apis, tempdir / "App.py" - ) - assert len(imports) == 0 - assert len(source.values()) == 1 - assert docs[0] == apis[0].__doc__ - - # Check if the generated code has the same return value - # should convert something like f"Hello {name}" to {"response": f"Hello {name}"} - return_obj = ( - list(source.values())[0][0].strip().split(" ", 1)[1].split(" ", 1)[1][:-1] - ) - assert return_obj in PY_MODULE(tempdir=tempdir) - - -def test_create_api(): - """Test if `create_api()` produces a syntactically correct API""" - with TemporaryDirectory() as _tempdir: - tempdir = Path(_tempdir) - write_mock_file(tempdir / "App.py", PY_MODULE(tempdir=tempdir)) - # Required to import the module with runpy - write_mock_file(tempdir / "App.svelte", SVELTE_MODULE) - module: Module = runpy.run_path(str(tempdir / "App.py"))["app"] - api_string = builder.create_api( - module._apis, tempdir / "App.py" # pylint:disable=W0212 - ) - assert ast.parse(api_string)