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

19 statements  

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

1import typing as t 

2 

3from sqlmodel import JSON, Field, Relationship, SQLModel 

4 

5from geoparser.db.models.base import GeoparserModel 

6 

7if t.TYPE_CHECKING: 

8 from geoparser.db.models.recognition import Recognition 

9 from geoparser.db.models.reference import Reference 

10 

11 

12class RecognizerBase(GeoparserModel): 

13 """Base model for recognizer metadata.""" 

14 

15 name: str = Field(index=True) 

16 config: t.Dict[str, t.Any] = Field(default_factory=dict, sa_type=JSON) 

17 

18 

19class Recognizer(RecognizerBase, table=True): 

20 """ 

21 Stores metadata about recognizers. 

22 

23 This includes configuration information and other details about specific 

24 recognizer instances. 

25 """ 

26 

27 id: str = Field(primary_key=True) 

28 references: list["Reference"] = Relationship( 

29 back_populates="recognizer", 

30 sa_relationship_kwargs={ 

31 "cascade": "all, delete-orphan", 

32 "passive_deletes": True, 

33 }, 

34 ) 

35 recognitions: list["Recognition"] = Relationship( 

36 back_populates="recognizer", 

37 sa_relationship_kwargs={ 

38 "cascade": "all, delete-orphan", 

39 "passive_deletes": True, 

40 }, 

41 ) 

42 

43 def __str__(self) -> str: 

44 """ 

45 Return a string representation of the recognizer. 

46 

47 Returns: 

48 String with recognizer name and config parameters 

49 """ 

50 config_str = ", ".join(f"{k}={repr(v)}" for k, v in self.config.items()) 

51 return f"{self.name}({config_str})" 

52 

53 def __repr__(self) -> str: 

54 """ 

55 Return a developer representation of the recognizer. 

56 

57 Returns: 

58 Same as __str__ method 

59 """ 

60 return self.__str__() 

61 

62 

63class RecognizerCreate(RecognizerBase): 

64 """Model for creating a new recognizer record.""" 

65 

66 id: str 

67 

68 

69class RecognizerUpdate(SQLModel): 

70 """Model for updating a recognizer record.""" 

71 

72 id: str 

73 name: t.Optional[str] = None 

74 config: t.Optional[t.Dict[str, t.Any]] = None