add tests for video query building

This commit is contained in:
Simon
2024-08-02 20:20:12 +02:00
parent 5afbc2e0a5
commit 44cfb15e0c
4 changed files with 72 additions and 4 deletions

View File

@@ -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")

View File

View File

@@ -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()