Add tests for library search

This commit is contained in:
JonnyWong16 2021-03-19 15:53:20 -07:00
parent 3422bb7fb8
commit 1674d08ab3
No known key found for this signature in database
GPG key ID: B1F1F9807184697A

View file

@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
from datetime import datetime
from collections import namedtuple
from datetime import datetime, timedelta
import pytest
from plexapi.exceptions import NotFound
from plexapi.exceptions import BadRequest, NotFound
from . import conftest as utils
@ -25,7 +26,7 @@ def test_library_sectionByID_with_attrs(plex, movies):
assert movies.agent == "tv.plex.agents.movie"
# This seems to fail for some reason.
# my account alloew of sync, didnt find any about settings about the library.
# assert movies.allowSync is ('sync' in plex.ownerFeatures)
# assert movies.allowSync is ("sync" in plex.ownerFeatures)
assert movies.art == "/:/resources/movie-fanart.jpg"
assert utils.is_metadata(
movies.composite, prefix="/library/sections/", contains="/composite/"
@ -244,12 +245,12 @@ def test_library_settings(movies):
def test_library_editAdvanced_default(movies):
movies.editAdvanced(hidden=2)
for setting in movies.settings():
if setting.id == 'hidden':
if setting.id == "hidden":
assert int(setting.value) == 2
movies.editAdvanced(collectionMode=0)
for setting in movies.settings():
if setting.id == 'collectionMode':
if setting.id == "collectionMode":
assert int(setting.value) == 0
movies.reload()
@ -302,3 +303,167 @@ def test_library_section_timeline(plex, movies):
assert utils.is_int(tl.updateQueueSize, gte=0)
assert tl.viewGroup == "secondary"
assert tl.viewMode == 65592
def test_library_MovieSection_search(movies, movie):
movie.addLabel("test_search")
movie.addCollection("test_search")
_test_library_search(movies, movie)
movie.removeLabel("test_search", locked=False)
movie.removeCollection("test_search", locked=False)
def test_library_ShowSection_search(tvshows, show):
show.addLabel("test_search")
show.addCollection("test_search")
_test_library_search(tvshows, show)
show.removeLabel("test_search", locked=False)
show.removeCollection("test_search", locked=False)
season = show.season(season=1)
_test_library_search(tvshows, season)
episode = season.episode(episode=1)
_test_library_search(tvshows, episode)
def test_library_MusicSection_search(music, artist):
artist.addGenre("test_search")
artist.addStyle("test_search")
artist.addMood("test_search")
artist.addCollection("test_search")
_test_library_search(music, artist)
artist.removeGenre("test_search", locked=False)
artist.removeStyle("test_search", locked=False)
artist.removeMood("test_search", locked=False)
artist.removeCollection("test_search", locked=False)
album = artist.album("Layers")
album.addGenre("test_search")
album.addStyle("test_search")
album.addMood("test_search")
album.addCollection("test_search")
album.addLabel("test_search")
_test_library_search(music, album)
album.removeGenre("test_search", locked=False)
album.removeStyle("test_search", locked=False)
album.removeMood("test_search", locked=False)
album.removeCollection("test_search", locked=False)
album.removeLabel("test_search", locked=False)
track = album.track(track=1)
track.addMood("test_search")
_test_library_search(music, track)
track.removeMood("test_search", locked=False)
def test_library_PhotoSection_search(photos, photoalbum):
photo = photoalbum.photo("photo1")
photo.addTag("test_search")
_test_library_search(photos, photo)
photo.removeTag("test_search")
def test_library_MovieSection_search_sort(movies):
results = movies.search(sort="titleSort")
titleSort = [r.titleSort for r in results]
assert titleSort == sorted(titleSort)
results_asc = movies.search(sort="titleSort:asc")
titleSort_asc = [r.titleSort for r in results_asc]
assert titleSort == titleSort_asc
results_desc = movies.search(sort="titleSort:desc")
titleSort_desc = [r.titleSort for r in results_desc]
assert titleSort_desc == sorted(titleSort_desc, reverse=True)
def test_library_search_exceptions(movies):
with pytest.raises(BadRequest):
movies.listFilterChoices(field="123abc.title")
with pytest.raises(BadRequest):
movies.search(**{"123abc": True})
with pytest.raises(BadRequest):
movies.search(year="123abc")
with pytest.raises(BadRequest):
movies.search(sort="123abc")
with pytest.raises(NotFound):
movies.getFilterType(libtype='show')
with pytest.raises(NotFound):
movies.getFieldType(fieldType="unknown")
with pytest.raises(NotFound):
movies.listFilterChoices(field="unknown")
with pytest.raises(NotFound):
movies.search(**{"episode.title": "unknown"})
with pytest.raises(NotFound):
movies.search(**{"title<>!=": "unknown"})
with pytest.raises(NotFound):
movies.search(sort="unknown")
with pytest.raises(NotFound):
movies.search(sort="titleSort:bad")
def _test_library_search(library, obj):
# Create & operator
AndOperator = namedtuple('AndOperator', ['key', 'title'])
andOp = AndOperator('&=', 'and')
fields = library.listFields(obj.type)
for field in fields:
fieldAttr = field.key.split(".")[-1]
operators = library.listOperators(field.type)
if field.type in {'tag', 'string'}:
operators += [andOp]
for operator in operators:
if fieldAttr == "unmatched" and operator.key == "!=" or fieldAttr == 'userRating':
continue
value = getattr(obj, fieldAttr, None)
if field.type == "boolean" and value is None:
value = fieldAttr.startswith("unwatched")
if field.type == "tag" and isinstance(value, list) and value and operator.title != 'and':
value = value[0]
elif value is None:
continue
if operator.title == "begins with":
searchValue = value[:3]
elif operator.title == "ends with":
searchValue = value[-3:]
elif "contain" in operator.title:
searchValue = value.split(" ")[0]
elif operator.title == "is less than":
searchValue = value + 1
elif operator.title == "is greater than":
searchValue = max(value - 1, 0)
elif operator.title == "is before":
searchValue = value + timedelta(days=1)
elif operator.title == "is after":
searchValue = value - timedelta(days=1)
else:
searchValue = value
searchFilter = {field.key + operator.key[:-1]: searchValue}
results = library.search(libtype=obj.type, **searchFilter)
if operator.key.startswith("!") or operator.key.startswith(">>") and searchValue == 0:
assert obj not in results
else:
assert obj in results
# Test search again using string tag and date
if field.type in {"tag", "date"}:
if field.type == "tag" and fieldAttr != 'contentRating':
if not isinstance(searchValue, list):
searchValue = [searchValue]
searchValue = [v.tag for v in searchValue]
elif field.type == "date":
searchValue = searchValue.strftime("%Y-%m-%d")
searchFilter = {field.key + operator.key[:-1]: searchValue}
results = library.search(libtype=obj.type, **searchFilter)
if operator.key.startswith("!") or operator.key.startswith(">>") and searchValue == 0:
assert obj not in results
else:
assert obj in results