diff --git a/test/conftest.py b/test/conftest.py index bd38fc08..5a103692 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,10 +1,38 @@ +import os +import sys +import pytest + +current_dir = os.path.dirname(os.path.abspath(__file__)) +sys.path.append(f"{current_dir}/../../green-metrics-tool/lib") + +## VERY IMPORTANT to override the config file here +## otherwise it will automatically connect to non-test DB and delete all your real data +from global_config import GlobalConfig +from db import DB +GlobalConfig().override_config(config_name='test-config.yml') + def pytest_addoption(parser): parser.addoption("--name", action="store", default="stress") - def pytest_generate_tests(metafunc): # This is called for every test. Only get/set command line arguments # if the argument is specified in the list of test "fixturenames". option_value = metafunc.config.option.name if 'name' in metafunc.fixturenames and option_value is not None: - metafunc.parametrize("name", [option_value]) \ No newline at end of file + metafunc.parametrize("name", [option_value]) + +# should we hardcode test-db here? +@pytest.fixture(autouse=True) +def cleanup_after_test(): + yield + tables = DB().fetch_all("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'") + for table in tables: + table_name = table[0] + DB().query(f'TRUNCATE TABLE "{table_name}" RESTART IDENTITY CASCADE') + +### If you wish to turn off the above auto-cleanup per test, include the following in your +### test module: +# from conftest import cleanup_after_test +# @pytest.fixture(autouse=False) # Set autouse to False to override the fixture +# def cleanup_after_test(): +# pass \ No newline at end of file diff --git a/test/smoke_test.py b/test/smoke_test.py index 8fc317c5..a70a22f0 100644 --- a/test/smoke_test.py +++ b/test/smoke_test.py @@ -35,15 +35,10 @@ def example_directories(): return example_dirs def run_test_on_directory(directory, capsys, skip_unsafe=False): - project_name = f"test_{utils.randomword(12)}" - - # Insert Project into testing DB - project_id = DB().fetch_one('INSERT INTO "projects" ("name","uri","email","last_run","created_at") \ - VALUES \ - (%s,%s,\'manual\',NULL,NOW()) RETURNING id;', params=(project_name, directory))[0] + name = f"test_{utils.randomword(12)}" # Run the application - runner = Runner(uri=directory, uri_type="folder", pid=project_id, skip_unsafe=skip_unsafe, skip_system_checks=True) + runner = Runner(name=name, uri=directory, uri_type="folder", dev_repeat_run=True, skip_unsafe=skip_unsafe, skip_system_checks=True) runner.run() # Capture Std.Out and Std.Err and make Assertions