diff --git a/tests/conftest.py b/tests/conftest.py index 7423b36c45dc8a17be1132cdaf0c63a879e7c324..248663bdc72e6ed97e5d099190ce017007a782aa 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,7 @@ # Reference: https://docs.pytest.org/en/latest/reference/fixtures.html#conftest-py-sharing-fixtures-across-multiple-files import os +import tempfile from flask import Flask import pytest from autosubmit_api.app import create_app @@ -11,17 +12,28 @@ from autosubmit_api import config FAKE_EXP_DIR = "./tests/experiments/" -#### FIXTURES #### @pytest.fixture(autouse=True) def fixture_disable_protection(monkeypatch: pytest.MonkeyPatch): + """ + This fixture disables the protection level for all the tests. + + Autouse is set, so, no need to put this fixture in the test function. + """ monkeypatch.setattr(config, "PROTECTION_LEVEL", "NONE") monkeypatch.setenv("PROTECTION_LEVEL", "NONE") -@pytest.fixture -def fixture_mock_basic_config(monkeypatch: pytest.MonkeyPatch): - # Get APIBasicConfig from file - monkeypatch.setenv("AUTOSUBMIT_CONFIGURATION", os.path.join(FAKE_EXP_DIR, ".autosubmitrc")) +@pytest.fixture( + params=[ + pytest.param("fixture_sqlite", marks=pytest.mark.sqlite), + ] +) +def fixture_mock_basic_config(request: pytest.FixtureRequest): + """ + Sets a mock basic config for the tests. + """ + request.getfixturevalue(request.param) + APIBasicConfig.read() yield APIBasicConfig @@ -44,3 +56,57 @@ def fixture_client(fixture_app: Flask): @pytest.fixture def fixture_runner(fixture_app: Flask): return fixture_app.test_cli_runner() + + +# Fixtures sqlite + + +@pytest.fixture(scope="session") +def fixture_temp_dir_copy(): + """ + Fixture that copies the contents of the FAKE_EXP_DIR to a temporary directory with rsync + """ + with tempfile.TemporaryDirectory() as tempdir: + # Copy all files recursively + os.system(f"rsync -r {FAKE_EXP_DIR} {tempdir}") + yield tempdir + + +@pytest.fixture(scope="session") +def fixture_gen_rc_sqlite(fixture_temp_dir_copy: str): + """ + Fixture that generates a .autosubmitrc file in the temporary directory + """ + rc_file = os.path.join(fixture_temp_dir_copy, ".autosubmitrc") + with open(rc_file, "w") as f: + f.write( + "\n".join( + [ + "[database]", + f"path = {fixture_temp_dir_copy}", + "filename = autosubmit.db", + "backend = sqlite", + "[local]", + f"path = {fixture_temp_dir_copy}", + "[globallogs]", + f"path = {fixture_temp_dir_copy}/logs", + "[historicdb]", + f"path = {fixture_temp_dir_copy}/metadata/data", + "[structures]", + f"path = {fixture_temp_dir_copy}/metadata/structures", + "[historiclog]", + f"path = {fixture_temp_dir_copy}/metadata/logs", + "[graph]", + f"path = {fixture_temp_dir_copy}/metadata/graph", + ] + ) + ) + yield fixture_temp_dir_copy + + +@pytest.fixture +def fixture_sqlite(fixture_gen_rc_sqlite: str, monkeypatch: pytest.MonkeyPatch): + monkeypatch.setenv( + "AUTOSUBMIT_CONFIGURATION", os.path.join(fixture_gen_rc_sqlite, ".autosubmitrc") + ) + yield fixture_gen_rc_sqlite diff --git a/tests/experiments/.autosubmitrc b/tests/experiments/.autosubmitrc deleted file mode 100644 index da7bbe7c1b55fa565084366bcb41f5026c2cb561..0000000000000000000000000000000000000000 --- a/tests/experiments/.autosubmitrc +++ /dev/null @@ -1,21 +0,0 @@ -[database] -path = ./tests/experiments/ -filename = autosubmit.db - -[local] -path = ./tests/experiments/ - -[globallogs] -path = ./tests/experiments/logs - -[historicdb] -path = ./tests/experiments/metadata/data - -[structures] -path = ./tests/experiments/metadata/structures - -[historiclog] -path = ./tests/experiments/metadata/logs - -[graph] -path = ./tests/experiments/metadata/graph \ No newline at end of file diff --git a/tests/experiments/metadata/data/job_data_a007.db b/tests/experiments/metadata/data/job_data_a007.db index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..7fcd6487e0efd128f66e1889e268c268079f8b1b 100755 Binary files a/tests/experiments/metadata/data/job_data_a007.db and b/tests/experiments/metadata/data/job_data_a007.db differ diff --git a/tests/experiments/metadata/graph/graph_data_a003.db b/tests/experiments/metadata/graph/graph_data_a003.db index 1862073cccbab88a43d010644c5c6316b4202aa1..736e381f49520ad0d6caa607f0a964faeffa80ce 100755 Binary files a/tests/experiments/metadata/graph/graph_data_a003.db and b/tests/experiments/metadata/graph/graph_data_a003.db differ diff --git a/tests/test_auth.py b/tests/test_auth.py index 8ecf3ed2e0fc483bdb39df4f18ba4a5f646a5cde..51f296d730a7d3b3df4bde8e0314810f87c4b639 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -6,7 +6,7 @@ from autosubmit_api import auth from autosubmit_api.auth.utils import validate_client from autosubmit_api.config.basicConfig import APIBasicConfig from autosubmit_api import config -from tests.custom_utils import custom_return_value, dummy_response +from tests.utils import custom_return_value, dummy_response class TestCommonAuth: diff --git a/tests/test_config.py b/tests/test_config.py index faefbada1963034f9555c4f85becd043127234a8..09f6c46e5b34d658cfd3a2ea2d8694514efaf60c 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -6,39 +6,10 @@ from autosubmit_api.config.basicConfig import APIBasicConfig from autosubmit_api.config.config_common import AutosubmitConfigResolver from autosubmit_api.config.ymlConfigStrategy import ymlConfigStrategy -from tests.conftest import FAKE_EXP_DIR -from tests.custom_utils import custom_return_value +from tests.utils import custom_return_value -class TestBasicConfig: - def test_api_basic_config(self, fixture_mock_basic_config): - APIBasicConfig.read() - - assert os.getenv("AUTOSUBMIT_CONFIGURATION") == os.path.join( - FAKE_EXP_DIR, ".autosubmitrc" - ) - assert APIBasicConfig.LOCAL_ROOT_DIR == FAKE_EXP_DIR - assert APIBasicConfig.DB_FILE == "autosubmit.db" - assert APIBasicConfig.DB_PATH == os.path.join( - FAKE_EXP_DIR, APIBasicConfig.DB_FILE - ) - assert APIBasicConfig.AS_TIMES_DB == "as_times.db" - assert APIBasicConfig.JOBDATA_DIR == os.path.join( - FAKE_EXP_DIR, "metadata", "data" - ) - assert APIBasicConfig.GLOBAL_LOG_DIR == os.path.join(FAKE_EXP_DIR, "logs") - assert APIBasicConfig.STRUCTURES_DIR == os.path.join( - FAKE_EXP_DIR, "metadata", "structures" - ) - assert APIBasicConfig.HISTORICAL_LOG_DIR == os.path.join( - FAKE_EXP_DIR, "metadata", "logs" - ) - - assert APIBasicConfig.GRAPHDATA_DIR == os.path.join( - FAKE_EXP_DIR, "metadata", "graph" - ) - class TestConfigResolver: def test_simple_init(self, monkeypatch: pytest.MonkeyPatch): # Conf test decision diff --git a/tests/test_endpoints_v4.py b/tests/test_endpoints_v4.py index 086108cdb5abf8018677ffc7d191d4ea1fe20971..28ffff26ce53ad5a543d96fbd78fa9089de1bda7 100644 --- a/tests/test_endpoints_v4.py +++ b/tests/test_endpoints_v4.py @@ -7,7 +7,7 @@ import jwt import pytest from autosubmit_api import config from autosubmit_api.views.v4 import PAGINATION_LIMIT_DEFAULT, ExperimentJobsViewOptEnum -from tests.custom_utils import custom_return_value +from tests.utils import custom_return_value class TestCASV2Login: diff --git a/tests/test_fixtures.py b/tests/test_fixtures.py new file mode 100644 index 0000000000000000000000000000000000000000..5f5426f0d8211a1c48879b1bdeba366b3828b8fb --- /dev/null +++ b/tests/test_fixtures.py @@ -0,0 +1,30 @@ +import os + + +class TestSQLiteFixtures: + def test_fixture_temp_dir_copy(self, fixture_temp_dir_copy: str): + """ + Test if all the files are copied from FAKEDIR to the temporary directory + """ + FILES_SHOULD_EXIST = [ + "a003/conf/minimal.yml", + "metadata/data/job_data_a007.db", + ] + for file in FILES_SHOULD_EXIST: + assert os.path.exists(os.path.join(fixture_temp_dir_copy, file)) + + def test_fixture_gen_rc_sqlite(self, fixture_gen_rc_sqlite: str): + """ + Test if the .autosubmitrc file is generated and the environment variable is set + """ + rc_file = os.path.join(fixture_gen_rc_sqlite, ".autosubmitrc") + + # File should exist + assert os.path.exists(rc_file) + + with open(rc_file, "r") as f: + content = f.read() + assert "[database]" in content + assert f"path = {fixture_gen_rc_sqlite}" in content + assert "filename = autosubmit.db" in content + assert "backend = sqlite" in content diff --git a/tests/custom_utils.py b/tests/utils.py similarity index 100% rename from tests/custom_utils.py rename to tests/utils.py