jrrribeiro commited on
Commit
f5d6b69
·
1 Parent(s): 8d52438

test(core): cover pagination and filtering queue behavior

Browse files
tests/unit/test_detection_queue_service.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ from src.domain.models import Detection
4
+ from src.repositories.in_memory_detection_repository import InMemoryDetectionRepository
5
+ from src.services.detection_queue_service import DetectionQueueService
6
+
7
+
8
+ def _sample(project: str = "ppbio-rabeca") -> list[Detection]:
9
+ return [
10
+ Detection(
11
+ detection_key="0000000000000001",
12
+ audio_id="audio_001",
13
+ scientific_name="Species A",
14
+ confidence=0.91,
15
+ start_time=0.0,
16
+ end_time=1.0,
17
+ ),
18
+ Detection(
19
+ detection_key="0000000000000002",
20
+ audio_id="audio_002",
21
+ scientific_name="Species B",
22
+ confidence=0.61,
23
+ start_time=0.0,
24
+ end_time=1.0,
25
+ ),
26
+ Detection(
27
+ detection_key="0000000000000003",
28
+ audio_id="audio_003",
29
+ scientific_name="Species A",
30
+ confidence=0.75,
31
+ start_time=0.0,
32
+ end_time=1.0,
33
+ ),
34
+ Detection(
35
+ detection_key="0000000000000004",
36
+ audio_id="audio_004",
37
+ scientific_name="Species C",
38
+ confidence=0.40,
39
+ start_time=0.0,
40
+ end_time=1.0,
41
+ ),
42
+ ]
43
+
44
+
45
+ def test_get_page_basic_pagination() -> None:
46
+ repo = InMemoryDetectionRepository()
47
+ repo.seed("ppbio-rabeca", _sample())
48
+ service = DetectionQueueService(repo)
49
+
50
+ page = service.get_page(project_slug="ppbio-rabeca", page=1, page_size=2)
51
+
52
+ assert page.total_items == 4
53
+ assert page.total_pages == 2
54
+ assert page.has_next is True
55
+ assert page.has_previous is False
56
+ assert len(page.items) == 2
57
+
58
+
59
+ def test_get_page_caps_requested_page_to_last_page() -> None:
60
+ repo = InMemoryDetectionRepository()
61
+ repo.seed("ppbio-rabeca", _sample())
62
+ service = DetectionQueueService(repo)
63
+
64
+ page = service.get_page(project_slug="ppbio-rabeca", page=99, page_size=3)
65
+
66
+ assert page.page == 2
67
+ assert page.total_pages == 2
68
+ assert len(page.items) == 1
69
+
70
+
71
+ def test_get_page_with_filters() -> None:
72
+ repo = InMemoryDetectionRepository()
73
+ repo.seed("ppbio-rabeca", _sample())
74
+ service = DetectionQueueService(repo)
75
+
76
+ page = service.get_page(
77
+ project_slug="ppbio-rabeca",
78
+ page=1,
79
+ page_size=10,
80
+ scientific_name="Species A",
81
+ min_confidence=0.8,
82
+ )
83
+
84
+ assert page.total_items == 1
85
+ assert len(page.items) == 1
86
+ assert page.items[0].detection_key == "0000000000000001"
87
+
88
+
89
+ def test_get_page_requires_positive_page_and_page_size() -> None:
90
+ repo = InMemoryDetectionRepository()
91
+ repo.seed("ppbio-rabeca", _sample())
92
+ service = DetectionQueueService(repo)
93
+
94
+ with pytest.raises(ValueError):
95
+ _ = service.get_page(project_slug="ppbio-rabeca", page=0, page_size=10)
96
+
97
+ with pytest.raises(ValueError):
98
+ _ = service.get_page(project_slug="ppbio-rabeca", page=1, page_size=0)