From 44cfb15e0c2d072426dc335efda0c36f3895fdc5 Mon Sep 17 00:00:00 2001 From: Simon Date: Fri, 2 Aug 2024 20:20:12 +0200 Subject: [PATCH] add tests for video query building --- tubearchivist/video/src/query_building.py | 8 +-- tubearchivist/video/tests/__init__.py | 0 .../video/tests/test_src/__init__.py | 0 .../tests/test_src/test_query_building.py | 68 +++++++++++++++++++ 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 tubearchivist/video/tests/__init__.py create mode 100644 tubearchivist/video/tests/test_src/__init__.py create mode 100644 tubearchivist/video/tests/test_src/test_query_building.py diff --git a/tubearchivist/video/src/query_building.py b/tubearchivist/video/src/query_building.py index a4c5ff65..da98cd34 100644 --- a/tubearchivist/video/src/query_building.py +++ b/tubearchivist/video/src/query_building.py @@ -35,19 +35,19 @@ class QueryBuilder: watch = self.request_params.get("watch") if watch: - watch_must_list = self._parse_watch(watch[0]) + watch_must_list = self.parse_watch(watch[0]) must_list.append(watch_must_list) video_type = self.request_params.get("type") if video_type: - type_list_list = self._parse_type(video_type[0]) + type_list_list = self.parse_type(video_type[0]) must_list.append(type_list_list) query = {"bool": {"must": must_list}} return query - def _parse_watch(self, watch: str) -> dict: + def parse_watch(self, watch: str) -> dict: """build query""" if watch not in self.WATCH_OPTIONS: raise ValueError(f"'{watch}' not in {self.WATCH_OPTIONS}") @@ -68,7 +68,7 @@ class QueryBuilder: return continue_ids - def _parse_type(self, video_type: str): + def parse_type(self, video_type: str): """parse video type""" if not hasattr(VideoTypeEnum, video_type.upper()): raise ValueError(f"'{video_type}' not in VideoTypeEnum") diff --git a/tubearchivist/video/tests/__init__.py b/tubearchivist/video/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tubearchivist/video/tests/test_src/__init__.py b/tubearchivist/video/tests/test_src/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tubearchivist/video/tests/test_src/test_query_building.py b/tubearchivist/video/tests/test_src/test_query_building.py new file mode 100644 index 00000000..aeb2c781 --- /dev/null +++ b/tubearchivist/video/tests/test_src/test_query_building.py @@ -0,0 +1,68 @@ +"""test video query building""" + +import pytest +from video.src.query_building import QueryBuilder + + +def test_initialization(): + """init constructor""" + qb = QueryBuilder(user_id=1) + assert qb.user_id == 1 + assert not qb.request_params + + +def test_build_data(): + """test for correct key building""" + qb = QueryBuilder( + user_id=1, + channel=["test_channel"], + playlist=["test_playlist"], + watch=["watched"], + type=["videos"], + sort=["published"], + order=["desc"], + ) + result = qb.build_data() + assert "query" in result + assert "sort" in result + assert result["sort"] == [{"published": {"order": "desc"}}] + + +def test_parse_watch(): + """watched query building""" + qb = QueryBuilder(user_id=1, watch=["watched"]) + result = qb.parse_watch("watched") + assert result == {"match": {"player.watched": True}} + + result = qb.parse_watch("unwatched") + assert result == {"match": {"player.watched": False}} + + with pytest.raises(ValueError): + qb.parse_watch("invalid") + + +def test_parse_type(): + """test type is parsed""" + qb = QueryBuilder(user_id=1, type=["videos"]) + with pytest.raises(ValueError): + qb.parse_type("invalid") + + result = qb.parse_type("videos") + assert result == {"match": {"vid_type": "videos"}} + + +def test_parse_sort(): + """test sort and order""" + qb = QueryBuilder(user_id=1, sort=["views"], order=["desc"]) + result = qb.parse_sort() + assert result == {"sort": [{"stats.view_count": {"order": "desc"}}]} + + with pytest.raises(ValueError): + qb = QueryBuilder(user_id=1, sort=["invalid"]) + qb.parse_sort() + + with pytest.raises(ValueError): + qb = QueryBuilder( + user_id=1, sort=["stats.view_count"], order=["invalid"] + ) + qb.parse_sort()