Skip to content

Commit

Permalink
refactor: linting
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgarel committed Dec 17, 2024
1 parent 95f0d69 commit b201aa9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
4 changes: 3 additions & 1 deletion backend/editor/controllers/project_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
11 changes: 8 additions & 3 deletions backend/editor/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
"""
Expand Down
27 changes: 12 additions & 15 deletions backend/tests/test_export.py
Original file line number Diff line number Diff line change
@@ -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
"""

Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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"
Expand All @@ -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"]))
Expand All @@ -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"]))
Expand All @@ -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]
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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"]))
Expand Down
14 changes: 11 additions & 3 deletions parser/openfoodfacts_taxonomy_parser/patcher.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand Down

0 comments on commit b201aa9

Please sign in to comment.