Coverage for geoparser/db/models/context.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, UniqueConstraint 

5from sqlmodel import Field, Relationship, SQLModel 

6 

7from geoparser.db.models.base import GeoparserModel 

8 

9if t.TYPE_CHECKING: 

10 from geoparser.db.models.project import Project 

11 from geoparser.db.models.recognizer import Recognizer 

12 from geoparser.db.models.resolver import Resolver 

13 

14 

15class ContextBase(GeoparserModel): 

16 """Base model for context data.""" 

17 

18 tag: str 

19 

20 

21class Context(ContextBase, table=True): 

22 """ 

23 Represents a tagged context within a project. 

24 

25 A context record associates a tag with a specific recognizer and resolver 

26 combination within a project. This allows users to manage multiple result 

27 sets (e.g., from different pipeline runs or annotation sources) using 

28 human-friendly tags instead of module IDs. 

29 """ 

30 

31 __table_args__ = (UniqueConstraint("project_id", "tag"),) 

32 

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

34 project_id: uuid.UUID = Field( 

35 sa_column=Column( 

36 UUID, ForeignKey("project.id", ondelete="CASCADE"), nullable=False 

37 ) 

38 ) 

39 recognizer_id: t.Optional[str] = Field( 

40 default=None, 

41 sa_column=Column( 

42 String, ForeignKey("recognizer.id", ondelete="SET NULL"), nullable=True 

43 ), 

44 ) 

45 resolver_id: t.Optional[str] = Field( 

46 default=None, 

47 sa_column=Column( 

48 String, ForeignKey("resolver.id", ondelete="SET NULL"), nullable=True 

49 ), 

50 ) 

51 project: "Project" = Relationship() 

52 recognizer: t.Optional["Recognizer"] = Relationship() 

53 resolver: t.Optional["Resolver"] = Relationship() 

54 

55 

56class ContextCreate(ContextBase): 

57 """Model for creating a new context record.""" 

58 

59 project_id: uuid.UUID 

60 recognizer_id: t.Optional[str] = None 

61 resolver_id: t.Optional[str] = None 

62 

63 

64class ContextUpdate(SQLModel): 

65 """Model for updating an existing context record.""" 

66 

67 id: uuid.UUID 

68 tag: t.Optional[str] = None 

69 recognizer_id: t.Optional[str] = None 

70 resolver_id: t.Optional[str] = None