python-plexapi/tests/test_collection.py

288 lines
9.4 KiB
Python
Raw Normal View History

2021-02-15 06:33:38 +00:00
# -*- coding: utf-8 -*-
2021-02-15 06:54:31 +00:00
import pytest
2021-05-30 02:26:12 +00:00
from plexapi.exceptions import BadRequest, NotFound
2021-02-15 06:54:31 +00:00
2021-02-15 06:33:38 +00:00
from . import conftest as utils
from . import test_mixins
def test_Collection_attrs(collection):
assert utils.is_datetime(collection.addedAt)
assert collection.art is None
assert collection.artBlurHash is None
assert collection.childCount == 1
assert collection.collectionMode == -1
2021-04-23 20:59:02 +00:00
assert collection.collectionPublished is False
2021-02-15 06:33:38 +00:00
assert collection.collectionSort == 0
2021-04-23 20:59:02 +00:00
assert collection.content is None
2021-05-28 05:39:00 +00:00
assert collection.contentRating is None
2021-02-15 06:33:38 +00:00
assert not collection.fields
assert collection.guid.startswith("collection://")
assert utils.is_int(collection.index)
assert collection.key.startswith("/library/collections/")
assert not collection.labels
assert utils.is_int(collection.librarySectionID)
assert collection.librarySectionKey == "/library/sections/%s" % collection.librarySectionID
assert collection.librarySectionTitle == "Movies"
2021-02-15 06:43:44 +00:00
assert utils.is_int(collection.maxYear)
assert utils.is_int(collection.minYear)
2021-04-23 20:59:02 +00:00
assert utils.is_int(collection.ratingCount)
2021-02-15 06:33:38 +00:00
assert utils.is_int(collection.ratingKey)
2021-04-23 20:59:02 +00:00
assert collection.smart is False
2021-02-15 06:33:38 +00:00
assert collection.subtype == "movie"
assert collection.summary == ""
assert collection.thumb.startswith("/library/collections/%s/composite" % collection.ratingKey)
assert collection.thumbBlurHash is None
2021-05-28 05:39:00 +00:00
assert collection.title == "Test Collection"
2021-02-15 06:33:38 +00:00
assert collection.titleSort == collection.title
assert collection.type == "collection"
assert utils.is_datetime(collection.updatedAt)
2021-05-30 02:26:12 +00:00
assert collection.listType == "video"
assert collection.metadataType == collection.subtype
assert collection.isVideo is True
assert collection.isAudio is False
assert collection.isPhoto is False
2021-02-15 06:33:38 +00:00
2021-05-29 22:17:47 +00:00
def test_Collection_section(collection, movies):
assert collection.section() == movies
def test_Collection_item(collection):
item1 = collection.item("Elephants Dream")
assert item1.title == "Elephants Dream"
item2 = collection.get("Elephants Dream")
assert item2.title == "Elephants Dream"
assert item1 == item2
2021-05-30 02:26:12 +00:00
with pytest.raises(NotFound):
collection.item("Does not exist")
2021-05-29 22:17:47 +00:00
def test_Collection_items(collection):
items = collection.items()
assert len(items) == 1
2021-02-15 06:33:38 +00:00
def test_Collection_modeUpdate(collection):
2021-02-15 06:43:44 +00:00
mode_dict = {"default": -1, "hide": 0, "hideItems": 1, "showItems": 2}
2021-02-15 06:33:38 +00:00
for key, value in mode_dict.items():
2021-02-15 06:54:31 +00:00
collection.modeUpdate(mode=key)
2021-02-15 06:33:38 +00:00
collection.reload()
assert collection.collectionMode == value
2021-02-15 06:54:31 +00:00
with pytest.raises(BadRequest):
collection.modeUpdate(mode="bad-mode")
collection.modeUpdate("default")
2021-02-15 06:33:38 +00:00
def test_Collection_sortUpdate(collection):
2021-05-30 02:26:12 +00:00
sort_dict = {"release": 0, "alpha": 1}
2021-02-15 06:54:31 +00:00
for key, value in sort_dict.items():
2021-02-15 07:06:34 +00:00
collection.sortUpdate(sort=key)
2021-02-15 06:54:31 +00:00
collection.reload()
assert collection.collectionSort == value
with pytest.raises(BadRequest):
collection.sortUpdate(sort="bad-sort")
collection.sortUpdate("release")
2021-02-15 06:33:38 +00:00
@pytest.mark.authenticated
def test_Collection_sortUpdate_custom(collection):
collection.sortUpdate(sort="custom")
collection.reload()
assert collection.collectionSort == 2
collection.sortUpdate("release")
def test_Collection_add_move_remove(collection, movies):
2021-05-29 22:17:47 +00:00
movie = movies.get("Big Buck Bunny")
assert movie not in collection
collection.addItems(movie)
2021-02-15 06:33:38 +00:00
collection.reload()
2021-05-29 22:17:47 +00:00
assert movie in collection
items = collection.items()
collection.moveItem(items[1])
items_moved = collection.reload().items()
assert items_moved[0] == items[1]
assert items_moved[1] == items[0]
collection.moveItem(items[1], after=items[0])
items_moved = collection.reload().items()
assert items_moved[0] == items[0]
assert items_moved[1] == items[1]
2021-05-29 22:17:47 +00:00
collection.removeItems(movie)
collection.reload()
assert movie not in collection
# Reset collection sort due to bug with corrupted XML response
# for movies that have been moved in a collection and have
# progress (updateProgress) or marked as played (markWatched)
collection.sortUpdate("release")
2021-05-29 22:17:47 +00:00
def test_Collection_edit(collection, movies):
fields = {"title", "titleSort", "contentRating", "summary"}
title = collection.title
titleSort = collection.titleSort
contentRating = collection.contentRating
summary = collection.summary
newTitle = "New Title"
newTitleSort = "New Title Sort"
newContentRating = "New Content Rating"
newSummary = "New Summary"
collection.edit(
title=newTitle,
titleSort=newTitleSort,
contentRating=newContentRating,
summary=newSummary
)
collection.reload()
assert collection.title == newTitle
assert collection.titleSort == newTitleSort
assert collection.contentRating == newContentRating
assert collection.summary == newSummary
lockedFields = [f.locked for f in collection.fields if f.name in fields]
assert all(lockedFields)
collection.edit(
title=title,
titleSort=titleSort,
contentRating=contentRating or "",
summary=summary,
**{
"title.locked": 0,
"titleSort.locked": 0,
"contentRating.locked": 0,
"summary.locked": 0
}
)
2021-06-06 21:38:36 +00:00
collection.reload()
2021-05-29 22:17:47 +00:00
assert collection.title == title
assert collection.titleSort == titleSort
assert collection.contentRating is None
assert collection.summary == summary
lockedFields = [f.locked for f in collection.fields if f.name in fields]
assert not any(lockedFields)
def test_Collection_create(plex, tvshows):
title = "test_Collection_create"
try:
collection = plex.createCollection(
title=title,
section=tvshows,
items=tvshows.all()
)
assert collection in tvshows.collections()
assert collection.smart is False
finally:
collection.delete()
def test_Collection_createSmart(plex, tvshows):
title = "test_Collection_createSmart"
try:
collection = plex.createCollection(
title=title,
section=tvshows,
smart=True,
limit=3,
libtype="episode",
sort="episode.index:desc",
filters={"show.title": "Game of Thrones"}
)
assert collection in tvshows.collections()
assert collection.smart is True
assert len(collection.items()) == 3
assert all([e.type == "episode" for e in collection.items()])
assert all([e.grandparentTitle == "Game of Thrones" for e in collection.items()])
assert collection.items() == sorted(collection.items(), key=lambda e: e.index, reverse=True)
collection.updateFilters(limit=5, libtype="episode", filters={"show.title": "The 100"})
collection.reload()
assert len(collection.items()) == 5
assert all([e.grandparentTitle == "The 100" for e in collection.items()])
finally:
collection.delete()
2021-06-18 22:26:24 +00:00
def test_Collection_smartFilters(plex, movies):
title = "test_Collection_smartFilters"
2021-06-18 22:26:24 +00:00
advancedFilters = {
'and': [
{
'or': [
{'title': 'elephant'},
{'title=': 'Big Buck Bunny'}
]
},
{'year>>': 1990},
{'unwatched': True}
]
}
try:
collection = plex.createCollection(
title=title,
2021-06-18 22:26:24 +00:00
section=movies,
smart=True,
limit=5,
2021-06-18 22:26:24 +00:00
sort="year",
filters=advancedFilters
)
filters = collection.filters()
2021-06-18 22:26:24 +00:00
assert movies.search(**filters) == collection.items()
finally:
collection.delete()
2021-05-29 22:17:47 +00:00
def test_Collection_exceptions(plex, movies, movie, artist):
title = 'test_Collection_exceptions'
try:
2021-05-30 02:26:12 +00:00
collection = plex.createCollection(title, section=movies.title, items=movie)
2021-05-29 22:17:47 +00:00
with pytest.raises(BadRequest):
collection.updateFilters()
with pytest.raises(BadRequest):
collection.addItems(artist)
finally:
collection.delete()
2021-02-15 06:33:38 +00:00
2021-05-29 22:17:47 +00:00
with pytest.raises(BadRequest):
2021-05-30 01:05:42 +00:00
plex.createCollection(title, section=movies, items=[])
2021-05-29 22:17:47 +00:00
with pytest.raises(BadRequest):
2021-05-30 01:05:42 +00:00
plex.createCollection(title, section=movies, items=[movie, artist])
2021-02-15 06:33:38 +00:00
2021-05-29 22:17:47 +00:00
try:
2021-05-30 02:26:12 +00:00
collection = plex.createCollection(title, smart=True, section=movies.title, **{'year>>': 2000})
2021-05-29 22:17:47 +00:00
with pytest.raises(BadRequest):
collection.addItems(movie)
with pytest.raises(BadRequest):
collection.removeItems(movie)
with pytest.raises(BadRequest):
collection.moveItem(movie)
2021-05-29 22:17:47 +00:00
finally:
collection.delete()
2021-02-15 06:33:38 +00:00
2021-02-24 17:07:40 +00:00
def test_Collection_posters(collection):
2021-02-15 06:33:38 +00:00
posters = collection.posters()
assert posters
2021-02-24 17:07:40 +00:00
def test_Collection_art(collection):
2021-02-15 06:33:38 +00:00
arts = collection.arts()
assert not arts # Collection has no default art
2021-02-24 17:07:40 +00:00
def test_Collection_mixins_images(collection):
test_mixins.lock_art(collection)
test_mixins.lock_poster(collection)
test_mixins.edit_art(collection)
test_mixins.edit_poster(collection)
test_mixins.attr_artUrl(collection)
test_mixins.attr_posterUrl(collection)
2021-05-30 22:49:28 +00:00
def test_Collection_mixins_rating(collection):
test_mixins.edit_rating(collection)
2021-02-24 17:07:40 +00:00
def test_Collection_mixins_tags(collection):
2021-02-15 06:33:38 +00:00
test_mixins.edit_label(collection)