mirror of
https://git.vectorsigma.ru/public/tubearchivist.git
synced 2026-08-05 00:39:28 +00:00
add tests for video query building
This commit is contained in:
@@ -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")
|
||||
|
||||
0
tubearchivist/video/tests/__init__.py
Normal file
0
tubearchivist/video/tests/__init__.py
Normal file
0
tubearchivist/video/tests/test_src/__init__.py
Normal file
0
tubearchivist/video/tests/test_src/__init__.py
Normal file
68
tubearchivist/video/tests/test_src/test_query_building.py
Normal file
68
tubearchivist/video/tests/test_src/test_query_building.py
Normal 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()
|
||||
Reference in New Issue
Block a user