Skip to content

Commit

Permalink
Add missing unit test coverage for personperson merge (#1500)
Browse files Browse the repository at this point in the history
  • Loading branch information
blms committed Jan 22, 2025
1 parent b7bb7a2 commit b2a8829
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
59 changes: 58 additions & 1 deletion geniza/entities/tests/test_entities_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@
from geniza.corpus.forms import FacetChoiceField
from geniza.entities.forms import (
PersonChoiceField,
PersonDocumentRelationTypeChoiceField,
PersonDocumentRelationTypeMergeForm,
PersonListForm,
PersonMergeForm,
PersonPersonRelationTypeChoiceField,
PlaceListForm,
)
from geniza.entities.models import Name, Person, PersonDocumentRelationType, PersonRole
from geniza.entities.models import (
Name,
Person,
PersonDocumentRelation,
PersonDocumentRelationType,
PersonPersonRelation,
PersonPersonRelationType,
PersonRole,
)


class TestPersonChoiceField:
Expand Down Expand Up @@ -52,6 +62,28 @@ def test_init(self):
assert people.last() not in mergeform.fields["primary_person"].queryset


class TestPersonDocumentRelationTypeChoiceField:
@pytest.mark.django_db
def test_label_from_instance(self, person, document):
# adapted from TestDocumentChoiceField
choicefield = PersonDocumentRelationTypeChoiceField(Mock())

# Should not error on a relation with the most minimal information
minimal = PersonDocumentRelationType.objects.create()
label = choicefield.label_from_instance(minimal)
assert str(minimal.id) in label

# Check that the necessary information is in the label
reltype = PersonDocumentRelationType.objects.create(name="test")
PersonDocumentRelation.objects.create(
person=person, document=document, type=reltype
)
label = choicefield.label_from_instance(reltype)
assert "test relation" in label
assert str(person) in label
assert str(document) in label


class TestPersonDocumentRelationTypeMergeForm:
@pytest.mark.django_db
def test_init(self):
Expand All @@ -77,6 +109,31 @@ def test_init(self):
assert types.last() not in mergeform.fields["primary_relation_type"].queryset


class TestPersonPersonRelationTypeChoiceField:
@pytest.mark.django_db
def test_label_from_instance(self, person, person_multiname):
# adapted from TestDocumentChoiceField
choicefield = PersonPersonRelationTypeChoiceField(Mock())

# Should not error on a relation with the most minimal information
minimal = PersonPersonRelationType.objects.create()
label = choicefield.label_from_instance(minimal)
assert str(minimal.id) in label

# Check that the necessary information is in the label
reltype = PersonPersonRelationType.objects.create(
name="test", converse_name="converse"
)
label = choicefield.label_from_instance(reltype)
assert "test" in label
assert "converse" not in label
PersonPersonRelation.objects.create(
type=reltype, from_person=person, to_person=person_multiname
)
label = choicefield.label_from_instance(reltype)
assert "converse" in label


@pytest.mark.django_db
class TestPersonListForm:
def test_set_choices_from_facets(self, person, person_diacritic):
Expand Down
30 changes: 30 additions & 0 deletions geniza/entities/tests/test_entities_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,36 @@ def test_merge_with(self, person, person_multiname, document, join):
in rel_type.log_entries.all()[2].change_message
)

@pytest.mark.django_db
def test_objects_by_label(self):
"""Should return dict of PersonDocumentRelationType objects keyed on English label"""
# invalidate cached property (it is computed in other tests in the suite)
if "objects_by_label" in PersonDocumentRelationType.__dict__:
# __dict__["objects_by_label"] returns a classmethod
# __func__ returns a property
# fget returns the actual cached function
PersonDocumentRelationType.__dict__[
"objects_by_label"
].__func__.fget.cache_clear()
# add some new relation types
rel_type = PersonDocumentRelationType(name="Some kind of official")
rel_type.save()
rel_type_2 = PersonDocumentRelationType(name="Example")
rel_type_2.save()
# should be able to get a relation type by label
assert isinstance(
PersonDocumentRelationType.objects_by_label.get("Some kind of official"),
PersonDocumentRelationType,
)
assert (
PersonDocumentRelationType.objects_by_label.get("Some kind of official").pk
== rel_type.pk
)
assert (
PersonDocumentRelationType.objects_by_label.get("Example").pk
== rel_type_2.pk
)


@pytest.mark.django_db
class TestPersonPersonRelationType:
Expand Down
15 changes: 15 additions & 0 deletions geniza/entities/tests/test_entities_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
PersonDocumentRelationTypeMerge,
PersonListView,
PersonMerge,
PersonPersonRelationTypeMerge,
PlaceAutocompleteView,
PlaceListView,
)
Expand Down Expand Up @@ -196,6 +197,20 @@ def test_person_document_relation_type_merge(self, admin_client, client):
assert "test message" in messages


class TestPersonPersonRelationTypeMergeView:
# adapted from TestPersonMergeView
@pytest.mark.django_db
def test_get_success_url(self):
rel_type = PersonPersonRelationType.objects.create(name="test")
merge_view = PersonPersonRelationTypeMerge()
merge_view.primary_relation_type = rel_type

resolved_url = resolve(merge_view.get_success_url())
assert "admin" in resolved_url.app_names
assert resolved_url.url_name == "entities_personpersonrelationtype_change"
assert resolved_url.kwargs["object_id"] == str(rel_type.pk)


class TestPersonAutocompleteView:
@pytest.mark.django_db
def test_get_queryset(self):
Expand Down

0 comments on commit b2a8829

Please sign in to comment.