From c92294350fb4353fa06e6eef334ccc9c204f7b7b Mon Sep 17 00:00:00 2001 From: inexcode Date: Sat, 20 Aug 2022 22:47:32 +0400 Subject: [PATCH] Make Huey run immediately when testing --- selfprivacy_api/utils/huey.py | 8 +++++++- tests/conftest.py | 10 ++++++---- tests/test_common.py | 10 ++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/selfprivacy_api/utils/huey.py b/selfprivacy_api/utils/huey.py index 7b39d5a..034f7ba 100644 --- a/selfprivacy_api/utils/huey.py +++ b/selfprivacy_api/utils/huey.py @@ -1,8 +1,14 @@ """MiniHuey singleton.""" +import os from huey import SqliteHuey HUEY_DATABASE = "/etc/nixos/userdata/tasks.db" # Singleton instance containing the huey database. -huey = SqliteHuey(HUEY_DATABASE) +test_mode = os.environ.get("TEST_MODE") + +huey = SqliteHuey( + HUEY_DATABASE, + immediate=test_mode == "true", +) diff --git a/tests/conftest.py b/tests/conftest.py index b4f812d..ea7a66a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,10 +1,15 @@ """Tests configuration.""" # pylint: disable=redefined-outer-name # pylint: disable=unused-argument +import os import pytest from fastapi.testclient import TestClient +def pytest_generate_tests(metafunc): + os.environ["TEST_MODE"] = "true" + + @pytest.fixture def tokens_file(mocker, shared_datadir): """Mock tokens file.""" @@ -17,10 +22,7 @@ def tokens_file(mocker, shared_datadir): @pytest.fixture def jobs_file(mocker, shared_datadir): """Mock tokens file.""" - mock = mocker.patch( - "selfprivacy_api.utils.JOBS_FILE", - shared_datadir / "jobs.json" - ) + mock = mocker.patch("selfprivacy_api.utils.JOBS_FILE", shared_datadir / "jobs.json") return mock diff --git a/tests/test_common.py b/tests/test_common.py index e581bd4..e5d3f62 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -1,6 +1,7 @@ # pylint: disable=redefined-outer-name # pylint: disable=unused-argument import json +import os import pytest from selfprivacy_api.utils import WriteUserData, ReadUserData @@ -28,3 +29,12 @@ def test_write_invalid_user_data(): with pytest.raises(ValueError): with WriteUserData("invalid") as user_data: pass + + +@pytest.fixture +def test_mode(): + return os.environ.get("TEST_MODE") + + +def test_the_test_mode(test_mode): + assert test_mode == "true"