From 8e91f8b802b84b05e4eb5ef0fa78ba04aeec5900 Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Mon, 2 Mar 2026 11:13:10 -0800 Subject: [PATCH] Cover the use_first branch cases --- .../tests/test_management_retagger.py | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/documents/tests/test_management_retagger.py b/src/documents/tests/test_management_retagger.py index c2e94e1cc..3f669c00e 100644 --- a/src/documents/tests/test_management_retagger.py +++ b/src/documents/tests/test_management_retagger.py @@ -220,6 +220,34 @@ class TestRetaggerDocumentType(DirectoriesMixin): assert d_first.document_type is None assert d_second.document_type is None + @pytest.mark.parametrize( + ("use_first_flag", "expects_assignment"), + [ + pytest.param(["--use-first"], True, id="use_first_assigns_first_match"), + pytest.param([], False, id="no_use_first_skips_ambiguous_match"), + ], + ) + def test_use_first_with_multiple_matches( + self, + use_first_flag: list[str], + *, + expects_assignment: bool, + ) -> None: + DocumentTypeFactory( + match="ambiguous", + matching_algorithm=MatchingModel.MATCH_ANY, + ) + DocumentTypeFactory( + match="ambiguous", + matching_algorithm=MatchingModel.MATCH_ANY, + ) + doc = DocumentFactory(content="ambiguous content") + + call_command("document_retagger", "--document_type", *use_first_flag) + + doc.refresh_from_db() + assert (doc.document_type is not None) is expects_assignment + # --------------------------------------------------------------------------- # Correspondent assignment @@ -253,6 +281,34 @@ class TestRetaggerCorrespondent(DirectoriesMixin): assert d_first.correspondent is None assert d_second.correspondent is None + @pytest.mark.parametrize( + ("use_first_flag", "expects_assignment"), + [ + pytest.param(["--use-first"], True, id="use_first_assigns_first_match"), + pytest.param([], False, id="no_use_first_skips_ambiguous_match"), + ], + ) + def test_use_first_with_multiple_matches( + self, + use_first_flag: list[str], + *, + expects_assignment: bool, + ) -> None: + CorrespondentFactory( + match="ambiguous", + matching_algorithm=MatchingModel.MATCH_ANY, + ) + CorrespondentFactory( + match="ambiguous", + matching_algorithm=MatchingModel.MATCH_ANY, + ) + doc = DocumentFactory(content="ambiguous content") + + call_command("document_retagger", "--correspondent", *use_first_flag) + + doc.refresh_from_db() + assert (doc.correspondent is not None) is expects_assignment + # --------------------------------------------------------------------------- # Storage path assignment @@ -294,6 +350,34 @@ class TestRetaggerStoragePath(DirectoriesMixin): assert d_second.storage_path is None assert d_unrelated.storage_path == sp2 + @pytest.mark.parametrize( + ("use_first_flag", "expects_assignment"), + [ + pytest.param(["--use-first"], True, id="use_first_assigns_first_match"), + pytest.param([], False, id="no_use_first_skips_ambiguous_match"), + ], + ) + def test_use_first_with_multiple_matches( + self, + use_first_flag: list[str], + *, + expects_assignment: bool, + ) -> None: + StoragePathFactory( + match="ambiguous", + matching_algorithm=MatchingModel.MATCH_ANY, + ) + StoragePathFactory( + match="ambiguous", + matching_algorithm=MatchingModel.MATCH_ANY, + ) + doc = DocumentFactory(content="ambiguous content") + + call_command("document_retagger", "--storage_path", *use_first_flag) + + doc.refresh_from_db() + assert (doc.storage_path is not None) is expects_assignment + # --------------------------------------------------------------------------- # ID range filtering