Skip to content

Commit

Permalink
test: better transaction handling
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgarel committed Dec 17, 2024
1 parent c623857 commit f0b4c43
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
5 changes: 2 additions & 3 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
10 changes: 5 additions & 5 deletions backend/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
22 changes: 11 additions & 11 deletions backend/tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
7 changes: 6 additions & 1 deletion backend/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Some utils related to testing"""

import inspect


class FakeBackgroundTask:
"""A fake background task to avoid running background tasks during tests"""
Expand All @@ -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):
Expand Down

0 comments on commit f0b4c43

Please sign in to comment.