Coverage for geoparser/db/crud/recognition.py: 100%

24 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 not_ 

5from sqlmodel import Session, select 

6 

7from geoparser.db.crud.base import BaseRepository 

8from geoparser.db.models import Document, Recognition 

9 

10 

11class RecognitionRepository(BaseRepository[Recognition]): 

12 """ 

13 Repository for Recognition model operations. 

14 """ 

15 

16 model = Recognition 

17 

18 @classmethod 

19 def get_by_document( 

20 cls, db: Session, document_id: uuid.UUID 

21 ) -> t.List[Recognition]: 

22 """ 

23 Get all recognitions for a document. 

24 

25 Args: 

26 db: Database session 

27 document_id: ID of the document 

28 

29 Returns: 

30 List of recognitions 

31 """ 

32 statement = select(Recognition).where(Recognition.document_id == document_id) 

33 return db.exec(statement).unique().all() 

34 

35 @classmethod 

36 def get_by_recognizer(cls, db: Session, recognizer_id: str) -> t.List[Recognition]: 

37 """ 

38 Get all recognitions for a recognizer. 

39 

40 Args: 

41 db: Database session 

42 recognizer_id: ID of the recognizer 

43 

44 Returns: 

45 List of recognitions 

46 """ 

47 statement = select(Recognition).where( 

48 Recognition.recognizer_id == recognizer_id 

49 ) 

50 return db.exec(statement).unique().all() 

51 

52 @classmethod 

53 def get_by_document_and_recognizer( 

54 cls, db: Session, document_id: uuid.UUID, recognizer_id: str 

55 ) -> t.Optional[Recognition]: 

56 """ 

57 Get a recognition for a specific document and recognizer. 

58 

59 Args: 

60 db: Database session 

61 document_id: ID of the document 

62 recognizer_id: ID of the recognizer 

63 

64 Returns: 

65 Recognition if found, None otherwise 

66 """ 

67 statement = select(Recognition).where( 

68 Recognition.document_id == document_id, 

69 Recognition.recognizer_id == recognizer_id, 

70 ) 

71 return db.exec(statement).unique().first() 

72 

73 @classmethod 

74 def get_unprocessed_documents( 

75 cls, db: Session, project_id: uuid.UUID, recognizer_id: str 

76 ) -> t.List[Document]: 

77 """ 

78 Get all documents from a project that have not been processed by a specific recognizer. 

79 

80 This is done by retrieving all documents for the project and excluding those 

81 that have a corresponding recognition record for the given recognizer. 

82 

83 Args: 

84 db: Database session 

85 project_id: ID of the project containing the documents 

86 recognizer_id: ID of the recognizer 

87 

88 Returns: 

89 List of unprocessed Document objects 

90 """ 

91 # This query selects all documents from the project where there is no 

92 # corresponding entry in the recognition table for the given recognizer 

93 statement = select(Document).where( 

94 Document.project_id == project_id, 

95 not_( 

96 Document.id.in_( 

97 select(Recognition.document_id).where( 

98 Recognition.recognizer_id == recognizer_id 

99 ) 

100 ) 

101 ), 

102 ) 

103 return db.exec(statement).unique().all()