Coverage for geoparser/db/models/referent.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-20 18:08 +0000

1import typing as t 

2import uuid 

3 

4from sqlalchemy import UUID, Column, ForeignKey, String 

5from sqlmodel import Field, Relationship, SQLModel 

6 

7from geoparser.db.models.base import GeoparserModel 

8 

9if t.TYPE_CHECKING: 

10 from geoparser.db.models.reference import Reference 

11 from geoparser.db.models.resolver import Resolver 

12 from geoparser.gazetteer.feature import Feature 

13 

14 

15class ReferentBase(GeoparserModel): 

16 """Base model for referent data.""" 

17 

18 gazetteer_name: str 

19 feature_identifier: str 

20 

21 

22class Referent(ReferentBase, table=True): 

23 """ 

24 Represents a resolved referent for a reference. 

25 

26 A referent is a specific place in a gazetteer that a reference refers to, 

27 stored as a (gazetteer name, feature identifier) pair pointing into the 

28 gazetteer's installed artifact. Each reference can have multiple potential 

29 referents, reflecting ambiguity in the text. 

30 """ 

31 

32 id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) 

33 reference_id: uuid.UUID = Field( 

34 sa_column=Column( 

35 UUID, ForeignKey("reference.id", ondelete="CASCADE"), nullable=False 

36 ) 

37 ) 

38 resolver_id: str = Field( 

39 sa_column=Column( 

40 String, ForeignKey("resolver.id", ondelete="CASCADE"), nullable=False 

41 ) 

42 ) 

43 reference: "Reference" = Relationship(back_populates="referents") 

44 resolver: "Resolver" = Relationship( 

45 back_populates="referents", sa_relationship_kwargs={"lazy": "joined"} 

46 ) 

47 

48 @property 

49 def feature(self) -> t.Optional["Feature"]: 

50 """ 

51 The gazetteer feature this referent points to. 

52 

53 Returns: 

54 Feature object from the installed gazetteer, or None if the 

55 feature no longer exists in the gazetteer 

56 

57 Raises: 

58 ValueError: If the referent's gazetteer is not installed 

59 """ 

60 # Lazy import to avoid circular dependency 

61 from geoparser.gazetteer.gazetteer import Gazetteer 

62 

63 return Gazetteer(self.gazetteer_name).find(self.feature_identifier) 

64 

65 

66class ReferentCreate(ReferentBase): 

67 """ 

68 Model for creating a new referent. 

69 

70 Includes the reference_id and resolver_id to associate the referent. 

71 """ 

72 

73 reference_id: uuid.UUID 

74 resolver_id: str 

75 

76 

77class ReferentUpdate(SQLModel): 

78 """Model for updating an existing referent.""" 

79 

80 id: uuid.UUID 

81 reference_id: t.Optional[uuid.UUID] = None 

82 gazetteer_name: t.Optional[str] = None 

83 feature_identifier: t.Optional[str] = None 

84 resolver_id: t.Optional[str] = None