Skip to content

Commit 81a9ff9

Browse files
committed
update DeltaTitleErrors to not create duplicates
1 parent caf0840 commit 81a9ff9

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

sde_collections/models/delta_patterns.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,9 @@ def apply(self) -> None:
454454
new_title, error = self.generate_title_for_url(curated_url)
455455

456456
if error:
457-
# Log error and continue to next URL
458-
DeltaResolvedTitleError.objects.create(title_pattern=self, delta_url=curated_url, error_string=error)
457+
DeltaResolvedTitleError.objects.update_or_create(
458+
delta_url=curated_url, defaults={"title_pattern": self, "error_string": error} # lookup field
459+
)
459460
continue
460461

461462
# Skip if the generated title matches existing or if Delta already exists
@@ -488,7 +489,9 @@ def apply(self) -> None:
488489
new_title, error = self.generate_title_for_url(delta_url)
489490

490491
if error:
491-
DeltaResolvedTitleError.objects.create(title_pattern=self, delta_url=delta_url, error_string=error)
492+
DeltaResolvedTitleError.objects.update_or_create(
493+
delta_url=delta_url, defaults={"title_pattern": self, "error_string": error} # lookup field
494+
)
492495
continue
493496

494497
# Update title and record resolution - key change here

sde_collections/tests/test_delta_patterns.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,46 @@ def test_pattern_reapplication_does_not_duplicate_delta_urls(self):
266266
# Ensure no new `DeltaUrl` is created after reapplying the pattern
267267
pattern.apply()
268268
assert DeltaUrl.objects.filter(url=curated_url.url).count() == 0
269+
270+
@pytest.mark.django_db
271+
def test_title_pattern_error_updates(self):
272+
"""
273+
Test that when a more specific pattern creates an error,
274+
it updates rather than duplicates the error record.
275+
"""
276+
# Create a collection and URL
277+
collection = CollectionFactory()
278+
url = DeltaUrlFactory(
279+
collection=collection, url="https://example.com/docs/specific/item.html", scraped_title="Original Title"
280+
)
281+
282+
# Create a general pattern first
283+
general_pattern = DeltaTitlePattern.objects.create(
284+
collection=collection,
285+
match_pattern="*docs*",
286+
# Use a different error-causing pattern
287+
title_pattern="{invalid}", # Invalid variable name will cause error
288+
match_pattern_type=2,
289+
)
290+
291+
# Verify initial error state
292+
error = url.deltaresolvedtitleerror
293+
assert error.title_pattern == general_pattern
294+
assert "Variable 'invalid' not allowed in f-string pattern" in error.error_string
295+
296+
# Create a more specific pattern
297+
specific_pattern = DeltaTitlePattern.objects.create(
298+
collection=collection,
299+
match_pattern="*docs/specific*",
300+
# Different invalid variable
301+
title_pattern="{another_invalid}",
302+
match_pattern_type=2,
303+
)
304+
305+
# Error should now be from specific pattern
306+
error = url.deltaresolvedtitleerror # Should still only be one
307+
assert error.title_pattern == specific_pattern
308+
assert "Variable 'another_invalid' not allowed in f-string pattern" in error.error_string
309+
310+
# Verify we still only have one error record
311+
assert DeltaResolvedTitleError.objects.filter(delta_url=url).count() == 1

0 commit comments

Comments
 (0)