diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 3ba51099..a05be4b0 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -32,7 +32,7 @@ def anyio_backend(): @pytest.fixture(scope="session") -def neo4j(): +def wait_neo4j(): """waiting for neo4j to be ready""" uri = os.environ.get("NEO4J_URI", "bolt://localhost:7687") with GraphDatabase.driver(uri) as driver: @@ -45,11 +45,10 @@ def neo4j(): time.sleep(1) else: connected = True - yield driver @pytest.fixture(scope="session") -async def database_lifespan(neo4j): +async def database_lifespan(wait_neo4j, anyio_backend): async with graph_db.database_lifespan() as driver: yield driver diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index 95c1d5e2..c38f43df 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -8,15 +8,15 @@ @pytest.fixture(autouse=True) -def test_setup(neo4j): +async def test_setup(database_lifespan, anyio_backend): # delete all the nodes and relations in the database - with neo4j.session() as session: + async with database_lifespan.session() as session: query = "MATCH (n) DETACH DELETE n" - session.run(query) + await session.run(query) query = "DROP INDEX p_test_branch_SearchIds IF EXISTS" - session.run(query) + await session.run(query) query = "DROP INDEX p_test_branch_SearchTagsIds IF EXISTS" - session.run(query) + await session.run(query) def test_hello(client): diff --git a/backend/tests/test_export.py b/backend/tests/test_export.py index 6dca8f6e..b53d9655 100644 --- a/backend/tests/test_export.py +++ b/backend/tests/test_export.py @@ -17,7 +17,7 @@ @pytest.fixture() -async def taxonomy_test(database_lifespan): +async def taxonomy_test(database_lifespan, anyio_backend): """We will import a project to work with We cache the project by fully duplicating it so that setup is faster @@ -53,7 +53,7 @@ async def test_no_modification(taxonomy_test): file_path = taxonomy_test.dump_taxonomy(background_tasks) assert open(file_path).read() == open("tests/data/test.txt").read() # clean files - background_tasks.run() + await background_tasks.run() @pytest.mark.anyio @@ -75,7 +75,7 @@ async def test_remove_parent(taxonomy_test): del expected[45] assert result == expected # clean files - background_tasks.run() + await background_tasks.run() @pytest.mark.anyio @@ -98,7 +98,7 @@ async def test_add_parent(taxonomy_test): expected.insert(46, "< en:fake-stuff\n") assert result == expected # clean files - background_tasks.run() + await background_tasks.run() @pytest.mark.anyio @@ -123,7 +123,7 @@ async def test_add_synonym(taxonomy_test): expected[46] = "fr: yaourts au fruit de la passion allégés, yaourts allégé aux grenadilles\n" assert result == expected # clean files - background_tasks.run() + await background_tasks.run() @pytest.mark.anyio @@ -145,7 +145,7 @@ async def test_remove_synonym(taxonomy_test): expected.insert(11, expected.pop(13)) assert result == expected # clean files - background_tasks.run() + await background_tasks.run() @pytest.mark.anyio @@ -164,7 +164,7 @@ async def test_no_comment_repeat(taxonomy_test): expected = list(open("tests/data/test.txt")) assert result == expected # clean files - background_tasks.run() + await background_tasks.run() @pytest.mark.anyio @@ -188,7 +188,7 @@ async def test_add_bare_child(taxonomy_test): ] assert result == expected # clean files - background_tasks.run() + await background_tasks.run() @pytest.mark.anyio @@ -240,7 +240,7 @@ async def test_add_new_entry_as_child(taxonomy_test): ] assert result == expected # clean files - background_tasks.run() + await background_tasks.run() @pytest.mark.anyio @@ -295,7 +295,7 @@ async def test_add_new_root_entries(taxonomy_test): assert result == expected # clean files - background_tasks.run() + await background_tasks.run() @pytest.mark.anyio @@ -321,4 +321,4 @@ async def test_change_entry_id(taxonomy_test): expected.insert(39, expected.pop(41)) assert result == expected # clean files - background_tasks.run() + await background_tasks.run() diff --git a/backend/tests/utils.py b/backend/tests/utils.py index e2577630..6fa25fc6 100644 --- a/backend/tests/utils.py +++ b/backend/tests/utils.py @@ -1,5 +1,7 @@ """Some utils related to testing""" +import inspect + class FakeBackgroundTask: """A fake background task to avoid running background tasks during tests""" @@ -13,7 +15,10 @@ def add_task(self, fn, *args, **kwargs): async def run(self): while self.tasks: fn, args, kwargs = self.tasks.pop() - await fn(*args, **kwargs) + if inspect.iscoroutinefunction(fn): + await fn(*args, **kwargs) + else: + fn(*args, **kwargs) async def clean_neo4j_db(neo4j):