Skip to content

Commit

Permalink
fix: entry removal fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgarel committed Dec 17, 2024
1 parent f0b4c43 commit d8fafcc
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ config_quality: ## Run quality checks on configuration files

tests: backend_tests ## Run all tests

backend_tests: ## Run python tests, you might provide additional arguments witr args="…"
backend_tests: ## Run python tests, you might provide additional arguments with args="…"
@echo "🍜 Running python tests"
${DOCKER_COMPOSE_TEST} up -d neo4j
${DOCKER_COMPOSE_TEST} run --rm taxonomy_api pytest /parser ${args}
Expand Down
20 changes: 15 additions & 5 deletions backend/editor/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,26 +418,36 @@ async def delete_node(self, label: NodeType, entry):
// Find node to be deleted using node ID
MATCH (deleted_node:{self.project_name}:{label.value})-[:is_before]->(next_node)
WHERE deleted_node.id = $id
MATCH (previous_node)-[:is_before]->(deleted_node)
MATCH (previous_node)-[r:is_before]->(deleted_node)
// Remove node
DETACH (deleted_node)
DELETE r
// Rebuild relationships after deletion
CREATE (previous_node)-[:is_before]->(next_node)
"""
await get_current_transaction().run(query, {"id": entry})
# transfert child parent relations, and mark child nodes as modified
query = f"""
// Find relations to be removed using node ID
MATCH (child_node)-[:is_child_of]->(deleted_node:{self.project_name}:{label.value})
MATCH (child_node)-[r:is_child_of]->(deleted_node:{self.project_name}:{label.value})
WHERE deleted_node.id = $id
MATCH (deleted_node)-[:is_child_of]->(parent_node)
DETACH (deleted_node)
DELETE r
// transfer child
CREATE (child_node) -[:is_child_of]->(parent_node)
// mark modified
SET child_node.modified = $modified
"""
await get_current_transaction().run(query, {"id": entry, "modified": modified})
# or if no transfer is needed, just mark modified
query = f"""
// Find relations to be removed using node ID
MATCH (child_node)-[r:is_child_of]->(deleted_node:{self.project_name}:{label.value})
WHERE deleted_node.id = $id
DELETE r
// mark children as modified
SET child_node.modified = $modified
"""
await get_current_transaction().run(query, {"id": entry, "modified": modified})
# change label of node to be deleted
query = f"""
MATCH (deleted_node:{self.project_name}:{label.value})
Expand All @@ -447,7 +457,7 @@ async def delete_node(self, label: NodeType, entry):
// and mark modification date also
SET deleted_node.modified = $modified
"""
result = await get_current_transaction().run(query, {"id": entry})
result = await get_current_transaction().run(query, {"id": entry, "modified": modified})
return await async_list(result)

async def get_all_nodes(self, label: Optional[NodeType] = None, removed: bool = False):
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/data/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ en: roast-beef

<en: meat
en: fake-meat
vegan:en: yes
# undef will stop parents from transmitting a value
carbon_footprint_fr_foodges_value:fr: undef
vegan:en: yes

en: fake-stuff

Expand Down
38 changes: 38 additions & 0 deletions backend/tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,41 @@ async def test_change_entry_id(taxonomy_test):
assert result == expected
# clean files
await background_tasks.run()


@pytest.mark.anyio
async def test_remove_entry(taxonomy_test):
async with graph_db.TransactionCtx():
# remove "yaourts allégés"
await taxonomy_test.delete_node(NodeType.ENTRY, "fr:yaourts-alleges")
# remove meat
await taxonomy_test.delete_node(NodeType.ENTRY, "en:meat")
background_tasks = FakeBackgroundTask()
file_path = taxonomy_test.dump_taxonomy(background_tasks)
result = list(open(file_path))
# expected output
expected = list(open("tests/data/test.txt"))
# entry removed
to_remove = [
32,
33,
34, # yahourts alleges
62,
63,
64,
65, # meat
66,
73, # < en:meat
]
# parent changed to "en:yogurts" for "yahourts alleges"
expected[45] = expected[51] = "< en:yogurts\n"
# parent normalized
expected[44] = "< en:Passion fruit yogurts\n"
expected[50] = "< en:lemon yogurts\n"
# properties reordered
# expected.insert(39, expected.pop(41))
for i in sorted(to_remove, reverse=True):
del expected[i]
assert result == expected
# clean files
await background_tasks.run()
12 changes: 10 additions & 2 deletions parser/openfoodfacts_taxonomy_parser/patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ def is_removed(self, node):
def get_all_nodes(self, project_label):
"""Get modified and removed nodes, in the start line order"""
query = f"""
MATCH (n:({project_label}|REMOVED_{project_label}))
MATCH (n:{project_label})
WHERE
// no external node
(n.is_external = false OR n.is_external IS NULL)
AND (
// modified nodes
((n:TEXT OR n:SYNONYMS OR n:STOPWORDS OR n:ENTRY) AND n.modified IS NOT NULL)
(
(
n:TEXT OR n:SYNONYMS OR n:STOPWORDS OR n:ENTRY OR
n:REMOVED_TEXT OR n:REMOVED_SYNONYMS OR
n:REMOVED_STOPWORDS OR n:REMOVED_ENTRY
)
AND
n.modified IS NOT NULL
)
)
// optional match for node might not have parents
OPTIONAL
Expand Down

0 comments on commit d8fafcc

Please sign in to comment.