From bbdfcc99e8f7074c99ce6044e34080230e14fe6e Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 16 Apr 2024 22:42:25 +0200 Subject: use alembic scripts from the package Especially for the installed module (which is probably the case if you use production.ini), this makes more sense than referring to the source location. We leave the development.ini for now, as there it might make sense to read the local directory (although it's probably installed as an editable module anyway). --- production.ini | 2 +- testing.ini | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/production.ini b/production.ini index 987f441..1d785b5 100644 --- a/production.ini +++ b/production.ini @@ -33,7 +33,7 @@ setup = fietsboek.pshell.setup [alembic] # path to migration scripts -script_location = fietsboek/alembic +script_location = fietsboek:alembic file_template = %%(year)d%%(month).2d%%(day).2d_%%(rev)s # file_template = %%(rev)s_%%(slug)s diff --git a/testing.ini b/testing.ini index 6f85219..b6b57b7 100644 --- a/testing.ini +++ b/testing.ini @@ -37,7 +37,7 @@ setup = fietsboek.pshell.setup [alembic] # path to migration scripts -script_location = fietsboek/alembic +script_location = fietsboek:alembic file_template = %%(year)d%%(month).2d%%(day).2d_%%(rev)s # file_template = %%(rev)s_%%(slug)s -- cgit v1.2.3 From d6ad20a3df26e80fddd6a304550514f19d54811a Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 16 Apr 2024 22:45:14 +0200 Subject: add data directory for testing configuration --- testing.ini | 3 +++ 1 file changed, 3 insertions(+) diff --git a/testing.ini b/testing.ini index b6b57b7..4cb95ba 100644 --- a/testing.ini +++ b/testing.ini @@ -13,6 +13,9 @@ pyramid.debug_routematch = false pyramid.default_locale_name = en sqlalchemy.url = sqlite:///%(here)s/testing.sqlite +# In the pytest tests, this is overwritten with a temporary directory, but we +# should still set a value for the bootstrapping test. +fietsboek.data_dir = %(here)s/data redis.url = redis://localhost fietsboek.default_tile_layers = -- cgit v1.2.3 From 83e08799d6b55bc2119aff4f9749bad103b5f574 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 16 Apr 2024 22:48:23 +0200 Subject: add test for fietsupdate bootstrapping --- tests/bootstrap/test_new_instance.py | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/bootstrap/test_new_instance.py diff --git a/tests/bootstrap/test_new_instance.py b/tests/bootstrap/test_new_instance.py new file mode 100644 index 0000000..e136579 --- /dev/null +++ b/tests/bootstrap/test_new_instance.py @@ -0,0 +1,70 @@ +"""Test to ensure that bootstrapping a new instance works with the fietsupdate +script, as described in the documentation. +""" + +import contextlib +import logging +import os +import shutil +import subprocess +import venv +from pathlib import Path + +LOGGER = logging.getLogger(__name__) +REPO_BASE = Path(__file__).parent.parent.parent + + +@contextlib.contextmanager +def chdir(path: Path): + """Change path as context manager. + + Changes back to the previous path on ``__exit__``. + """ + cwd = Path.cwd() + os.chdir(path) + try: + yield + finally: + os.chdir(cwd) + + +def install_fietsboek(venv_path: Path) -> Path: + """Creates a virtual env in the given path and installs fietsboek. + + :param venv_path: Path in which the virtual environment should be created. + :return: The path to the ``bin`` directory in which all scripts reside. + """ + venv.create(venv_path, with_pip=True) + subprocess.check_call([venv_path / "bin" / "pip", "install", str(REPO_BASE)]) + return venv_path / "bin" + + +def test_setup_via_fietsupdate(tmpdir): + with chdir(tmpdir): + # We create a new temporary virtual environment with a fresh install, just + # to be sure there's as little interference as possible. + LOGGER.info("Installing Fietsboek into clean env") + binaries_path = install_fietsboek(tmpdir / "venv") + + LOGGER.info("Creating a test configuration") + shutil.copy(REPO_BASE / "testing.ini", "testing.ini") + Path("data").mkdir() + + # Try to run the migrations + subprocess.check_call( + [binaries_path / "fietsupdate", "update", "-c", "testing.ini", "-f"] + ) + + # Also try to add an administrator + subprocess.check_call([ + binaries_path / "fietsctl", + "user", + "add", + "-c", "testing.ini", + "--email", "foobar@example.com", + "--name", "Foo Bar", + "--password", "raboof", + "--admin", + ]) + + assert True -- cgit v1.2.3 From 7f526efeed7b807a3b56f465637bee5dc8a27e3d Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 18 Apr 2024 22:28:55 +0200 Subject: have data_dir as a placeholder in testing.ini --- testing.ini | 8 +++++--- tests/bootstrap/test_new_instance.py | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/testing.ini b/testing.ini index 4cb95ba..82fddfd 100644 --- a/testing.ini +++ b/testing.ini @@ -13,9 +13,11 @@ pyramid.debug_routematch = false pyramid.default_locale_name = en sqlalchemy.url = sqlite:///%(here)s/testing.sqlite -# In the pytest tests, this is overwritten with a temporary directory, but we -# should still set a value for the bootstrapping test. -fietsboek.data_dir = %(here)s/data +# The pytest tests usually overwrite this with a temporary directory. Since +# this is cleaned on test teardown, we don't want to accidentally delete data +# that should not be deleted. Therefore, we specifically do not set a directory +# here, and leave a placeholder to be replaced properly by the tests. +# %% fietsboek.data_dir %% redis.url = redis://localhost fietsboek.default_tile_layers = diff --git a/tests/bootstrap/test_new_instance.py b/tests/bootstrap/test_new_instance.py index e136579..dc3076e 100644 --- a/tests/bootstrap/test_new_instance.py +++ b/tests/bootstrap/test_new_instance.py @@ -39,6 +39,18 @@ def install_fietsboek(venv_path: Path) -> Path: return venv_path / "bin" +def create_config(config_name: Path): + """Copies the testing.ini config and fills in the placeholders. + + :param config_name: Path to the resulting config. + """ + shutil.copy(REPO_BASE / "testing.ini", config_name) + config = config_name.read_text() + config = config.replace("# %% fietsboek.data_dir %%", "fietsboek.data_dir = %(here)s/data") + config_name.write_text(config) + Path("data").mkdir() + + def test_setup_via_fietsupdate(tmpdir): with chdir(tmpdir): # We create a new temporary virtual environment with a fresh install, just @@ -47,8 +59,7 @@ def test_setup_via_fietsupdate(tmpdir): binaries_path = install_fietsboek(tmpdir / "venv") LOGGER.info("Creating a test configuration") - shutil.copy(REPO_BASE / "testing.ini", "testing.ini") - Path("data").mkdir() + create_config(Path("testing.ini")) # Try to run the migrations subprocess.check_call( -- cgit v1.2.3 From c0b49a618d96f2088234dbb4c0ca8006475d432e Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 18 Apr 2024 22:30:46 +0200 Subject: also remove user data on test teardown --- tests/conftest.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 94f795a..cd74b0b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -75,6 +75,8 @@ def _cleanup_data(app_settings): data_dir = Path(app_settings["fietsboek.data_dir"]) if (data_dir / "tracks").is_dir(): shutil.rmtree(data_dir / "tracks") + if (data_dir / "users").is_dir(): + shutil.rmtree(data_dir / "users") @pytest.fixture(scope='module') def app(app_settings, dbengine, tmp_path_factory): -- cgit v1.2.3