"""
Provide Betty's default Jinja2 tests.
"""
from __future__ import annotations
from typing import Any, TYPE_CHECKING, Self
from typing_extensions import override
from betty.ancestry.event_type.event_types import (
StartOfLifeEventType,
EndOfLifeEventType,
)
from betty.ancestry.has_file_references import HasFileReferences
from betty.ancestry.link import HasLinks
from betty.ancestry.presence_role.presence_roles import Subject, Witness
from betty.date import DateRange
from betty.factory import IndependentFactory
from betty.json.linked_data import LinkedDataDumpable
from betty.model import (
Entity,
UserFacingEntity,
ENTITY_TYPE_REPOSITORY,
has_generated_entity_id,
)
from betty.privacy import is_private, is_public
from betty.typing import internal
if TYPE_CHECKING:
from betty.machine_name import MachineName
from collections.abc import Mapping, Callable
from betty.ancestry.event import Event
from betty.plugin import PluginIdToTypeMap
[docs]
def test_linked_data_dumpable(value: Any) -> bool:
"""
Test if a value can be dumped to Linked Data.
"""
return isinstance(value, LinkedDataDumpable)
[docs]
class TestEntity(IndependentFactory):
"""
Test if a value is an entity.
"""
def __call__(
self, value: Any, entity_type_identifier: MachineName | None = None
) -> bool:
"""
:param entity_type_id: If given, additionally ensure the value is an entity of this type.
"""
if entity_type_identifier is not None:
entity_type = self._entity_type_id_to_type_map[entity_type_identifier]
else:
entity_type = Entity # type: ignore[type-abstract]
return isinstance(value, entity_type)
[docs]
def test_user_facing_entity(value: Any) -> bool:
"""
Test if a value is an entity of a user-facing type.
"""
return isinstance(value, UserFacingEntity)
[docs]
def test_has_links(value: Any) -> bool:
"""
Test if a value has external links associated with it.
"""
return isinstance(value, HasLinks)
[docs]
def test_has_file_references(value: Any) -> bool:
"""
Test if a value has :py:class:`betty.ancestry.file_reference.FileReference` entities associated with it.
"""
return isinstance(value, HasFileReferences)
[docs]
def test_subject_role(value: Any) -> bool:
"""
Test if a presence role is that of Subject.
"""
return isinstance(value, Subject)
[docs]
def test_witness_role(value: Any) -> bool:
"""
Test if a presence role is that of Witness.
"""
return isinstance(value, Witness)
[docs]
def test_date_range(value: Any) -> bool:
"""
Test if a value is a date range.
"""
return isinstance(value, DateRange)
[docs]
def test_start_of_life_event(event: Event) -> bool:
"""
Test if an event is a start-of-life event.
"""
return isinstance(event.event_type, StartOfLifeEventType)
[docs]
def test_end_of_life_event(event: Event) -> bool:
"""
Test if an event is an end-of-life event.
"""
return isinstance(event.event_type, EndOfLifeEventType)