2020-09-11 21:23:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from plexapi.exceptions import BadRequest
|
2020-11-30 22:04:51 +00:00
|
|
|
from plexapi.playqueue import PlayQueue
|
2020-09-11 21:23:27 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_playqueue(plex, show):
|
|
|
|
# create the playlist
|
|
|
|
episodes = show.episodes()
|
|
|
|
pq = plex.createPlayQueue(episodes[:3])
|
|
|
|
assert len(pq) == 3, "PlayQueue does not contain 3 items."
|
|
|
|
assert pq.playQueueLastAddedItemID is None
|
|
|
|
assert pq.playQueueSelectedMetadataItemID == episodes[0].ratingKey
|
|
|
|
assert (
|
|
|
|
pq.items[0].ratingKey == episodes[0].ratingKey
|
|
|
|
), "Items not in proper order [0a]."
|
|
|
|
assert (
|
|
|
|
pq.items[1].ratingKey == episodes[1].ratingKey
|
|
|
|
), "Items not in proper order [1a]."
|
|
|
|
assert (
|
|
|
|
pq.items[2].ratingKey == episodes[2].ratingKey
|
|
|
|
), "Items not in proper order [2a]."
|
|
|
|
|
|
|
|
# Test move items around (b)
|
|
|
|
pq.moveItem(pq.items[1])
|
|
|
|
assert pq.playQueueLastAddedItemID is None
|
|
|
|
assert pq.playQueueSelectedMetadataItemID == episodes[0].ratingKey
|
|
|
|
assert (
|
|
|
|
pq.items[0].ratingKey == episodes[1].ratingKey
|
|
|
|
), "Items not in proper order [0b]."
|
|
|
|
assert (
|
|
|
|
pq.items[1].ratingKey == episodes[0].ratingKey
|
|
|
|
), "Items not in proper order [1b]."
|
|
|
|
assert (
|
|
|
|
pq.items[2].ratingKey == episodes[2].ratingKey
|
|
|
|
), "Items not in proper order [2b]."
|
|
|
|
|
|
|
|
# Test move items around (c)
|
|
|
|
pq.moveItem(pq.items[0], after=pq.items[1])
|
|
|
|
assert pq.playQueueLastAddedItemID == pq.items[1].playQueueItemID
|
|
|
|
assert pq.playQueueSelectedMetadataItemID == episodes[0].ratingKey
|
|
|
|
assert (
|
|
|
|
pq.items[0].ratingKey == episodes[0].ratingKey
|
|
|
|
), "Items not in proper order [0c]."
|
|
|
|
assert (
|
|
|
|
pq.items[1].ratingKey == episodes[1].ratingKey
|
|
|
|
), "Items not in proper order [1c]."
|
|
|
|
assert (
|
|
|
|
pq.items[2].ratingKey == episodes[2].ratingKey
|
|
|
|
), "Items not in proper order [2c]."
|
|
|
|
|
|
|
|
# Test adding an item to Up Next section
|
|
|
|
pq.addItem(episodes[3])
|
|
|
|
assert pq.playQueueLastAddedItemID == pq.items[2].playQueueItemID
|
|
|
|
assert pq.playQueueSelectedMetadataItemID == episodes[0].ratingKey
|
|
|
|
assert pq.items[2].ratingKey == episodes[3].ratingKey, (
|
2022-08-28 05:56:01 +00:00
|
|
|
f"Missing added item: {episodes[3]}"
|
2020-09-11 21:23:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Test adding an item to play next
|
|
|
|
pq.addItem(episodes[4], playNext=True)
|
|
|
|
assert pq.playQueueLastAddedItemID == pq.items[3].playQueueItemID
|
|
|
|
assert pq.playQueueSelectedMetadataItemID == episodes[0].ratingKey
|
|
|
|
assert pq.items[1].ratingKey == episodes[4].ratingKey, (
|
2022-08-28 05:56:01 +00:00
|
|
|
f"Missing added item: {episodes[4]}"
|
2020-09-11 21:23:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Test add another item into Up Next section
|
|
|
|
pq.addItem(episodes[5])
|
|
|
|
assert pq.playQueueLastAddedItemID == pq.items[4].playQueueItemID
|
|
|
|
assert pq.playQueueSelectedMetadataItemID == episodes[0].ratingKey
|
|
|
|
assert pq.items[4].ratingKey == episodes[5].ratingKey, (
|
2022-08-28 05:56:01 +00:00
|
|
|
f"Missing added item: {episodes[5]}"
|
2020-09-11 21:23:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Test removing an item
|
|
|
|
toremove = pq.items[3]
|
|
|
|
pq.removeItem(toremove)
|
|
|
|
assert pq.playQueueLastAddedItemID == pq.items[3].playQueueItemID
|
|
|
|
assert pq.playQueueSelectedMetadataItemID == episodes[0].ratingKey
|
2022-08-28 05:56:01 +00:00
|
|
|
assert toremove not in pq, f"Removed item still in PlayQueue: {toremove}"
|
|
|
|
assert len(pq) == 5, f"PlayQueue should have 5 items, {len(pq)} found"
|
2020-09-11 21:23:27 +00:00
|
|
|
|
|
|
|
# Test clearing the PlayQueue
|
|
|
|
pq.clear()
|
|
|
|
assert pq.playQueueSelectedMetadataItemID == episodes[0].ratingKey
|
2022-08-28 05:56:01 +00:00
|
|
|
assert len(pq) == 1, f"PlayQueue should have 1 item, {len(pq)} found"
|
2020-09-11 21:23:27 +00:00
|
|
|
|
|
|
|
# Test adding an item again
|
|
|
|
pq.addItem(episodes[7])
|
|
|
|
assert pq.playQueueLastAddedItemID == pq.items[1].playQueueItemID
|
|
|
|
assert pq.playQueueSelectedMetadataItemID == episodes[0].ratingKey
|
|
|
|
assert pq.items[1].ratingKey == episodes[7].ratingKey, (
|
2022-08-28 05:56:01 +00:00
|
|
|
f"Missing added item: {episodes[7]}"
|
2020-09-11 21:23:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_playqueue_with_single_item(plex, movie):
|
|
|
|
pq = plex.createPlayQueue(movie)
|
|
|
|
assert len(pq) == 1
|
|
|
|
assert pq.items[0].ratingKey == movie.ratingKey
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_playqueue_with_start_choice(plex, show):
|
|
|
|
episodes = show.episodes()
|
|
|
|
pq = plex.createPlayQueue(episodes[:3], startItem=episodes[1])
|
|
|
|
assert pq.playQueueSelectedMetadataItemID == pq.items[1].ratingKey
|
|
|
|
|
|
|
|
|
|
|
|
def test_modify_playqueue_with_library_media(plex, show):
|
|
|
|
episodes = show.episodes()
|
|
|
|
pq = plex.createPlayQueue(episodes[:3])
|
|
|
|
assert len(pq) == 3, "PlayQueue does not contain 3 items."
|
|
|
|
# Test move PlayQueue using library items
|
|
|
|
pq.moveItem(episodes[1], after=episodes[2])
|
|
|
|
assert pq.items[0].ratingKey == episodes[0].ratingKey, "Items not in proper order."
|
|
|
|
assert pq.items[2].ratingKey == episodes[1].ratingKey, "Items not in proper order."
|
|
|
|
assert pq.items[1].ratingKey == episodes[2].ratingKey, "Items not in proper order."
|
2022-02-27 03:26:08 +00:00
|
|
|
# Test too many matching library items
|
2020-09-11 21:23:27 +00:00
|
|
|
pq.addItem(episodes[0])
|
|
|
|
pq.addItem(episodes[0])
|
|
|
|
with pytest.raises(BadRequest):
|
|
|
|
pq.moveItem(episodes[2], after=episodes[0])
|
|
|
|
# Test items not in PlayQueue
|
|
|
|
with pytest.raises(BadRequest):
|
|
|
|
pq.moveItem(episodes[9], after=episodes[0])
|
|
|
|
with pytest.raises(BadRequest):
|
|
|
|
pq.removeItem(episodes[9])
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_playqueue_from_playlist(plex, album):
|
|
|
|
try:
|
2021-05-30 01:05:42 +00:00
|
|
|
playlist = plex.createPlaylist("test_playlist", items=album)
|
2020-09-11 21:23:27 +00:00
|
|
|
pq = playlist.playQueue(shuffle=1)
|
|
|
|
assert pq.playQueueShuffled is True
|
|
|
|
assert len(pq) == len(playlist)
|
|
|
|
pq.addItem(playlist)
|
|
|
|
assert len(pq) == 2 * len(playlist)
|
|
|
|
finally:
|
|
|
|
playlist.delete()
|
2020-11-30 22:04:51 +00:00
|
|
|
|
|
|
|
|
2021-05-29 22:17:47 +00:00
|
|
|
def test_create_playqueue_from_collection(plex, music, album):
|
|
|
|
try:
|
|
|
|
collection = plex.createCollection("test_collection", music, album)
|
|
|
|
pq = collection.playQueue(shuffle=1)
|
|
|
|
assert pq.playQueueShuffled is True
|
2022-12-21 19:43:26 +00:00
|
|
|
assert len(pq) == len(album.tracks())
|
2021-05-29 22:17:47 +00:00
|
|
|
pq.addItem(collection.items()[0])
|
|
|
|
assert len(pq) == len(collection) + 1
|
|
|
|
finally:
|
|
|
|
collection.delete()
|
|
|
|
|
|
|
|
|
2020-11-30 22:04:51 +00:00
|
|
|
def test_lookup_playqueue(plex, movie):
|
|
|
|
pq = PlayQueue.create(plex, movie)
|
|
|
|
pq_id = pq.playQueueID
|
|
|
|
pq2 = PlayQueue.get(plex, pq_id)
|
|
|
|
assert pq.playQueueID == pq2.playQueueID
|
|
|
|
assert pq.items == pq2.items
|