Skip to content

Commit

Permalink
fix(elasticsearch): Restored use of partial for update_es_document task
Browse files Browse the repository at this point in the history
- Fixed failing tests.
  • Loading branch information
albertisfu committed Oct 19, 2024
1 parent b1f2d13 commit b043965
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 77 deletions.
54 changes: 31 additions & 23 deletions cl/lib/es_signal_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ def update_es_documents(
# Update main document in ES, including fields to be
# extracted from a related instance.
transaction.on_commit(
lambda: update_es_document.si(
partial(
update_es_document.delay,
es_document.__name__,
fields_to_update,
(
Expand All @@ -319,7 +320,7 @@ def update_es_documents(
),
(compose_app_label(instance), instance.pk),
fields_map,
).delay()
)
)


Expand Down Expand Up @@ -367,15 +368,16 @@ def update_m2m_field_in_es_document(
:return: None
"""
transaction.on_commit(
lambda: update_es_document.si(
partial(
update_es_document.delay,
es_document.__name__,
[
affected_field,
],
(compose_app_label(instance), instance.pk),
None,
None,
).delay()
)
)

if es_document is OpinionClusterDocument and isinstance(
Expand Down Expand Up @@ -430,17 +432,19 @@ def update_reverse_related_documents(
if isinstance(main_object, Person) and not main_object.is_judge:
continue
transaction.on_commit(
lambda: chain(
update_es_document.si(
es_document.__name__,
affected_fields,
(compose_app_label(main_object), main_object.pk),
related_instance,
fields_map_to_pass,
),
send_or_schedule_search_alerts.s(),
percolator_response_processing.s(),
).apply_async()
partial(
chain(
update_es_document.si(
es_document.__name__,
affected_fields,
(compose_app_label(main_object), main_object.pk),
related_instance,
fields_map_to_pass,
),
send_or_schedule_search_alerts.s(),
percolator_response_processing.s(),
).apply_async
)
)

match instance:
Expand Down Expand Up @@ -511,13 +515,14 @@ def delete_reverse_related_documents(
# Update the Person document after the reverse instanced is deleted
# Update parent document in ES.
transaction.on_commit(
lambda: update_es_document.si(
partial(
update_es_document.delay,
es_document.__name__,
affected_fields,
(compose_app_label(instance), instance.pk),
None,
None,
).delay()
)
)
# Avoid calling update_children_docs_by_query if the Person
# doesn't have any positions or is not a Judge.
Expand All @@ -537,13 +542,14 @@ def delete_reverse_related_documents(

# Update parent document in ES.
transaction.on_commit(
lambda: update_es_document.si(
partial(
update_es_document.delay,
es_document.__name__,
affected_fields,
(compose_app_label(instance), instance.pk),
None,
None,
).delay()
)
)
# Avoid calling update_children_docs_by_query if the Docket
# doesn't have any entries.
Expand All @@ -561,13 +567,14 @@ def delete_reverse_related_documents(
case OpinionCluster() if es_document is OpinionClusterDocument: # type: ignore
# Update parent document in ES.
transaction.on_commit(
lambda: update_es_document.si(
partial(
update_es_document.delay,
es_document.__name__,
affected_fields,
(compose_app_label(instance), instance.pk),
None,
None,
).delay()
)
)
# Then update all their child documents (Positions)
transaction.on_commit(
Expand All @@ -585,13 +592,14 @@ def delete_reverse_related_documents(
for main_object in main_objects:
# Update main document in ES.
transaction.on_commit(
lambda: update_es_document.si(
partial(
update_es_document.delay,
es_document.__name__,
affected_fields,
(compose_app_label(main_object), main_object.pk),
None,
None,
).delay()
)
)


Expand Down
24 changes: 12 additions & 12 deletions cl/search/tests/tests_es_opinion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3072,7 +3072,7 @@ def test_child_document_update_properly(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.es_save_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
es_save_document, *args, **kwargs
es_save_document, True, *args, **kwargs
),
):
opinion_cluster = OpinionClusterFactory.create(
Expand Down Expand Up @@ -3106,7 +3106,7 @@ def test_child_document_update_properly(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
# Update the author field in the opinion record.
Expand All @@ -3119,7 +3119,7 @@ def test_child_document_update_properly(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
opinion.joined_by_str = "Joined Lorem"
Expand Down Expand Up @@ -3194,7 +3194,7 @@ def test_child_document_update_properly(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
# Add OpinionsCited using save() as in add_manual_citations command
Expand All @@ -3211,7 +3211,7 @@ def test_child_document_update_properly(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
# Add OpinionsCited using bulk_create as in store_opinion_citations_and_update_parentheticals
Expand Down Expand Up @@ -3295,9 +3295,9 @@ def test_parent_document_update_fields_properly(self) -> None:
)

with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
"cl.lib.es_signal_processor.update_es_document.delay",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, False, *args, **kwargs
),
):
# Update the court field in the docket record.
Expand All @@ -3311,9 +3311,9 @@ def test_parent_document_update_fields_properly(self) -> None:

# Update a opinion_cluster untracked field.
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
"cl.lib.es_signal_processor.update_es_document.delay",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, False, *args, **kwargs
),
):
opinion_cluster.other_dates = "January 12"
Expand Down Expand Up @@ -3386,9 +3386,9 @@ def test_update_shared_fields_related_documents(self) -> None:
)

with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
"cl.lib.es_signal_processor.update_es_document.delay",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, False, *args, **kwargs
),
):
# update docket number in parent document
Expand All @@ -3411,7 +3411,7 @@ def test_update_shared_fields_related_documents(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_children_docs_by_query.delay",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_children_docs_by_query, *args, **kwargs
update_children_docs_by_query, False, *args, **kwargs
),
):
# update docket number in parent document
Expand Down
12 changes: 6 additions & 6 deletions cl/search/tests/tests_es_oral_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -2601,7 +2601,7 @@ def test_oa_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.es_save_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
es_save_document, *args, **kwargs
es_save_document, True, *args, **kwargs
),
):
audio = AudioFactory.create(
Expand All @@ -2617,7 +2617,7 @@ def test_oa_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.es_save_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
es_save_document, *args, **kwargs
es_save_document, True, *args, **kwargs
),
):
audio.save(
Expand All @@ -2633,7 +2633,7 @@ def test_oa_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
audio.save()
Expand All @@ -2644,7 +2644,7 @@ def test_oa_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
audio.case_name = "Bank vs America"
Expand All @@ -2657,7 +2657,7 @@ def test_oa_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
audio.docket = self.docket_2
Expand All @@ -2677,7 +2677,7 @@ def test_oa_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
audio.case_name = "Lorem Ipsum"
Expand Down
8 changes: 4 additions & 4 deletions cl/search/tests/tests_es_parenthetical.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ def test_parenthetical_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.es_save_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
es_save_document, *args, **kwargs
es_save_document, True, *args, **kwargs
),
):
cluster_1 = OpinionClusterFactory(
Expand Down Expand Up @@ -767,7 +767,7 @@ def test_parenthetical_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
pg_test.save()
Expand All @@ -778,7 +778,7 @@ def test_parenthetical_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
p6 = ParentheticalFactory(
Expand Down Expand Up @@ -809,7 +809,7 @@ def test_parenthetical_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
pg_test.opinion = o
Expand Down
16 changes: 8 additions & 8 deletions cl/search/tests/tests_es_person.py
Original file line number Diff line number Diff line change
Expand Up @@ -2664,7 +2664,7 @@ def test_person_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.es_save_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
es_save_document, *args, **kwargs
es_save_document, True, *args, **kwargs
),
):
person = PersonFactory.create(
Expand All @@ -2685,7 +2685,7 @@ def test_person_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.es_save_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
es_save_document, *args, **kwargs
es_save_document, True, *args, **kwargs
),
):
position = PositionFactory.create(
Expand Down Expand Up @@ -2713,7 +2713,7 @@ def test_person_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
person.save()
Expand All @@ -2724,7 +2724,7 @@ def test_person_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
position.save()
Expand All @@ -2735,7 +2735,7 @@ def test_person_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
person.name_first = "Barack"
Expand All @@ -2759,7 +2759,7 @@ def test_person_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
person.religion = "pr"
Expand All @@ -2774,7 +2774,7 @@ def test_person_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
position.nomination_process = "state_senate"
Expand All @@ -2789,7 +2789,7 @@ def test_person_indexing_and_tasks_count(self) -> None:
with mock.patch(
"cl.lib.es_signal_processor.update_es_document.si",
side_effect=lambda *args, **kwargs: self.count_task_calls(
update_es_document, *args, **kwargs
update_es_document, True, *args, **kwargs
),
):
position.predecessor = self.person_2
Expand Down
Loading

0 comments on commit b043965

Please sign in to comment.