From b201aa99ac975242944c1173fe53ce77cbc4c111 Mon Sep 17 00:00:00 2001 From: Alex Garel Date: Tue, 17 Dec 2024 17:01:11 +0100 Subject: [PATCH] =?UTF-8?q?refactor:=C2=A0linting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/controllers/project_controller.py | 4 ++- backend/editor/entries.py | 11 +++++--- backend/tests/test_export.py | 27 +++++++++---------- .../openfoodfacts_taxonomy_parser/patcher.py | 14 +++++++--- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/backend/editor/controllers/project_controller.py b/backend/editor/controllers/project_controller.py index 3e2cd748..794da84f 100644 --- a/backend/editor/controllers/project_controller.py +++ b/backend/editor/controllers/project_controller.py @@ -99,7 +99,9 @@ async def clone_project(source_branch: str, taxonomy_name: str, target_branch: s CALL apoc.refactor.cloneNodes([p], true, ['id', 'branch'] ) YIELD output as new_node WITH new_node - SET new_node.created_at = datetime(), new_node.branch_name = $target_branch, new_node.id = $target_id + SET new_node.created_at = datetime(), + new_node.branch_name = $target_branch, + new_node.id = $target_id RETURN new_node """ params = { diff --git a/backend/editor/entries.py b/backend/editor/entries.py index 4f9a1ed2..324988df 100644 --- a/backend/editor/entries.py +++ b/backend/editor/entries.py @@ -369,8 +369,10 @@ async def add_node_to_end(self, label: NodeType, entry): # Rebuild relationships by inserting incoming node at the end query = [] query = f""" - MATCH (new_node:{self.project_name}:{label.value}) WHERE new_node.id = $id - MATCH (last_node:{self.project_name}:{end_node_label.value}) WHERE last_node.id = $endnodeid + MATCH (new_node:{self.project_name}:{label.value}) + WHERE new_node.id = $id + MATCH (last_node:{self.project_name}:{end_node_label.value}) + WHERE last_node.id = $endnodeid MATCH (footer:{self.project_name}:TEXT) WHERE footer.id = "__footer__" CREATE (last_node)-[:is_before]->(new_node) CREATE (new_node)-[:is_before]->(footer) @@ -601,7 +603,10 @@ async def update_node(self, label: NodeType, new_node: EntryNode): if id_changed: # mark children as modified because the parent id has changed query = f""" - MATCH (child:{self.project_name}:ENTRY) - [:is_child_of] -> (parent:{self.project_name}:ENTRY) + MATCH + (child:{self.project_name}:ENTRY) + - [:is_child_of] -> + (parent:{self.project_name}:ENTRY) WHERE parent.id = $id SET child.modified = $modified """ diff --git a/backend/tests/test_export.py b/backend/tests/test_export.py index 8f5dd4e8..dd4e49ce 100644 --- a/backend/tests/test_export.py +++ b/backend/tests/test_export.py @@ -1,6 +1,7 @@ """Test export feature -The idea is to see if changes made through the api are then correctly reflected in the exported file. +The idea is to see if changes made through the api +are then correctly reflected in the exported file. We use plain text export to avoid dealing with github """ @@ -21,12 +22,8 @@ async def taxonomy_test(database_lifespan): We cache the project by fully duplicating it so that setup is faster """ - from .utils import clean_neo4j_db - - # TEMPORARY use this to clean template in the db - # await clean_neo4j_db(database_lifespan) with open("tests/data/test.txt", "rb") as test_file: - async with graph_db.TransactionCtx() as session: + async with graph_db.TransactionCtx(): # clean the test project await project_controller.delete_project("p_test_branch") taxonomy = TaxonomyGraph("template", "test") @@ -44,7 +41,7 @@ async def taxonomy_test(database_lifespan): # this runs in its own transaction if background_tasks: await background_tasks.run() - async with graph_db.TransactionCtx() as session: + async with graph_db.TransactionCtx(): # clone template project as test project await project_controller.clone_project("template", "test", "branch") return TaxonomyGraph("branch", "test") @@ -61,7 +58,7 @@ async def test_no_modification(taxonomy_test): @pytest.mark.anyio async def test_remove_parent(taxonomy_test): - async with graph_db.TransactionCtx() as session: + async with graph_db.TransactionCtx(): # remove "yaourts allégés" for "yaourts au fruit de la passion allégés" children = await taxonomy_test.get_children("fr:yaourts-alleges") children_ids = [record["child.id"] for record in children] @@ -83,7 +80,7 @@ async def test_remove_parent(taxonomy_test): @pytest.mark.anyio async def test_add_parent(taxonomy_test): - async with graph_db.TransactionCtx() as session: + async with graph_db.TransactionCtx(): # add "en: fake-stuff" to "yaourts au fruit de la passion allégés" children = await taxonomy_test.get_children("en:fake-stuff") children_ids = [record["child.id"] for record in children] @@ -106,7 +103,7 @@ async def test_add_parent(taxonomy_test): @pytest.mark.anyio async def test_add_synonym(taxonomy_test): - async with graph_db.TransactionCtx() as session: + async with graph_db.TransactionCtx(): # add synonym to yaourts au fruit de la passion (node_data,) = await taxonomy_test.get_nodes( NodeType.ENTRY, "fr:yaourts-fruit-passion-alleges" @@ -131,7 +128,7 @@ async def test_add_synonym(taxonomy_test): @pytest.mark.anyio async def test_remove_synonym(taxonomy_test): - async with graph_db.TransactionCtx() as session: + async with graph_db.TransactionCtx(): # add synonym to yaourts au fruit de la passion (node_data,) = await taxonomy_test.get_nodes(NodeType.ENTRY, "en:yogurts") node = EntryNode(**dict(node_data["n"])) @@ -155,7 +152,7 @@ async def test_remove_synonym(taxonomy_test): async def test_no_comment_repeat(taxonomy_test): # we had a bug of repeating comments when modifying an entry # test it - async with graph_db.TransactionCtx() as session: + async with graph_db.TransactionCtx(): # just do a null edit on an entry with comments above it (node_data,) = await taxonomy_test.get_nodes(NodeType.ENTRY, "en:soup") node = EntryNode(**dict(node_data["n"])) @@ -172,7 +169,7 @@ async def test_no_comment_repeat(taxonomy_test): @pytest.mark.anyio async def test_add_new_entry_as_child(taxonomy_test): - async with graph_db.TransactionCtx() as session: + async with graph_db.TransactionCtx(): # add as children to "en:yogurts" children = await taxonomy_test.get_children("en:yogurts") children_ids = [record["child.id"] for record in children] @@ -224,7 +221,7 @@ async def test_add_new_entry_as_child(taxonomy_test): @pytest.mark.anyio async def test_add_new_root_entries(taxonomy_test): - async with graph_db.TransactionCtx() as session: + async with graph_db.TransactionCtx(): # add an entry potatoes await taxonomy_test.create_entry_node("Potatoes", "en") node = EntryNode( @@ -279,7 +276,7 @@ async def test_add_new_root_entries(taxonomy_test): @pytest.mark.anyio async def test_change_entry_id(taxonomy_test): - async with graph_db.TransactionCtx() as session: + async with graph_db.TransactionCtx(): # change id of entry yogurts (node_data,) = await taxonomy_test.get_nodes(NodeType.ENTRY, "en:yogurts") node = EntryNode(**dict(node_data["n"])) diff --git a/parser/openfoodfacts_taxonomy_parser/patcher.py b/parser/openfoodfacts_taxonomy_parser/patcher.py index 4498f393..6a60f0fa 100644 --- a/parser/openfoodfacts_taxonomy_parser/patcher.py +++ b/parser/openfoodfacts_taxonomy_parser/patcher.py @@ -1,7 +1,9 @@ """This module provide a function to dump a taxonomy from a neo4j database into a file, but taking the original taxonomy content and only modifying nodes that where modified or added """ + from collections import defaultdict + from .unparser import WriteTaxonomy from .utils import get_project_name, src_lines @@ -93,7 +95,9 @@ def iter_lines(self, branch_name, taxonomy_name): yield from self.iter_node_lines(dict(node), parents) yield "" - def nodes_by_lines(self, branch_name, taxonomy_name) -> dict[int, list[tuple[dict, list[dict]]]]: + def nodes_by_lines( + self, branch_name, taxonomy_name + ) -> dict[int, list[tuple[dict, list[dict]]]]: """Get the lines to replace in the original text :return: dict association each line number @@ -124,7 +128,11 @@ def nodes_by_lines(self, branch_name, taxonomy_name) -> dict[int, list[tuple[dic nodes_by_lines[node_position].append((node, parents)) # we now try to see iteratively to see in nodes_without position # if node parents have acquired a position - ids_positions = {node["id"]: node_position for node_position, nodes in nodes_by_lines.items() for node, _ in nodes} + ids_positions = { + node["id"]: node_position + for node_position, nodes in nodes_by_lines.items() + for node, _ in nodes + } while True: position_found = [] for i, (node, parents) in enumerate(nodes_without_position): @@ -136,7 +144,7 @@ def nodes_by_lines(self, branch_name, taxonomy_name) -> dict[int, list[tuple[dic if not position_found: break # no more progress, we are done for i in reversed(position_found): - del nodes_without_position[i] + del nodes_without_position[i] # put the rest at the end if nodes_without_position: nodes_by_lines[-1].extend(nodes_without_position)