File size: 3,317 Bytes
f5d6b69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37a6299
 
 
 
 
 
 
 
 
 
f5d6b69
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import pytest

from src.domain.models import Detection
from src.repositories.in_memory_detection_repository import InMemoryDetectionRepository
from src.services.detection_queue_service import DetectionQueueService


def _sample(project: str = "ppbio-rabeca") -> list[Detection]:
    return [
        Detection(
            detection_key="0000000000000001",
            audio_id="audio_001",
            scientific_name="Species A",
            confidence=0.91,
            start_time=0.0,
            end_time=1.0,
        ),
        Detection(
            detection_key="0000000000000002",
            audio_id="audio_002",
            scientific_name="Species B",
            confidence=0.61,
            start_time=0.0,
            end_time=1.0,
        ),
        Detection(
            detection_key="0000000000000003",
            audio_id="audio_003",
            scientific_name="Species A",
            confidence=0.75,
            start_time=0.0,
            end_time=1.0,
        ),
        Detection(
            detection_key="0000000000000004",
            audio_id="audio_004",
            scientific_name="Species C",
            confidence=0.40,
            start_time=0.0,
            end_time=1.0,
        ),
    ]


def test_get_page_basic_pagination() -> None:
    repo = InMemoryDetectionRepository()
    repo.seed("ppbio-rabeca", _sample())
    service = DetectionQueueService(repo)

    page = service.get_page(project_slug="ppbio-rabeca", page=1, page_size=2)

    assert page.total_items == 4
    assert page.total_pages == 2
    assert page.has_next is True
    assert page.has_previous is False
    assert len(page.items) == 2


def test_get_page_caps_requested_page_to_last_page() -> None:
    repo = InMemoryDetectionRepository()
    repo.seed("ppbio-rabeca", _sample())
    service = DetectionQueueService(repo)

    page = service.get_page(project_slug="ppbio-rabeca", page=99, page_size=3)

    assert page.page == 2
    assert page.total_pages == 2
    assert len(page.items) == 1


def test_get_page_with_filters() -> None:
    repo = InMemoryDetectionRepository()
    repo.seed("ppbio-rabeca", _sample())
    service = DetectionQueueService(repo)

    page = service.get_page(
        project_slug="ppbio-rabeca",
        page=1,
        page_size=10,
        scientific_name="Species A",
        min_confidence=0.8,
    )

    assert page.total_items == 1
    assert len(page.items) == 1
    assert page.items[0].detection_key == "0000000000000001"


def test_list_all_detections_returns_filtered_project_items_without_paging() -> None:
    repo = InMemoryDetectionRepository()
    repo.seed("ppbio-rabeca", _sample())
    service = DetectionQueueService(repo)

    items = service.list_all_detections(project_slug="ppbio-rabeca", scientific_name="Species A")

    assert [item.detection_key for item in items] == ["0000000000000001", "0000000000000003"]


def test_get_page_requires_positive_page_and_page_size() -> None:
    repo = InMemoryDetectionRepository()
    repo.seed("ppbio-rabeca", _sample())
    service = DetectionQueueService(repo)

    with pytest.raises(ValueError):
        _ = service.get_page(project_slug="ppbio-rabeca", page=0, page_size=10)

    with pytest.raises(ValueError):
        _ = service.get_page(project_slug="ppbio-rabeca", page=1, page_size=0)