python-plexapi/tests/test_utils.py

85 lines
2.5 KiB
Python
Raw Normal View History

2017-02-02 04:47:22 +00:00
# -*- coding: utf-8 -*-
2020-04-14 20:13:30 +00:00
import time
2017-01-09 14:21:54 +00:00
import plexapi.utils as utils
2020-04-14 20:13:30 +00:00
import pytest
2017-01-09 14:21:54 +00:00
from plexapi.exceptions import NotFound
def test_utils_toDatetime():
2020-04-29 21:23:22 +00:00
assert (
str(utils.toDatetime("2006-03-03", format="%Y-%m-%d")) == "2006-03-03 00:00:00"
)
# assert str(utils.toDatetime('0'))[:-9] in ['1970-01-01', '1969-12-31']
2017-01-09 14:21:54 +00:00
2017-02-02 04:47:22 +00:00
2017-04-18 03:32:08 +00:00
def test_utils_threaded():
2018-09-14 22:29:14 +00:00
def _squared(num, results, i, job_is_done_event=None):
2017-04-18 03:32:08 +00:00
time.sleep(0.5)
results[i] = num * num
2020-04-29 21:23:22 +00:00
2017-04-18 03:32:08 +00:00
starttime = time.time()
results = utils.threaded(_squared, [[1], [2], [3], [4], [5]])
assert results == [1, 4, 9, 16, 25]
assert (time.time() - starttime) < 1
2017-01-09 14:21:54 +00:00
2017-02-02 04:47:22 +00:00
2017-04-18 03:32:08 +00:00
@pytest.mark.req_client
def test_utils_downloadSessionImages():
# TODO: Implement test_utils_downloadSessionImages()
pass
2017-01-09 14:21:54 +00:00
def test_utils_searchType():
2020-04-29 21:23:22 +00:00
st = utils.searchType("movie")
2017-01-09 14:21:54 +00:00
assert st == 1
2017-01-29 21:22:48 +00:00
movie = utils.searchType(1)
2020-04-29 21:23:22 +00:00
assert movie == "1"
2017-01-09 14:21:54 +00:00
with pytest.raises(NotFound):
2020-04-29 21:23:22 +00:00
utils.searchType("kekekekeke")
2017-01-09 14:21:54 +00:00
def test_utils_joinArgs():
2020-04-29 21:23:22 +00:00
test_dict = {"genre": "action", "type": 1337}
assert utils.joinArgs(test_dict) == "?genre=action&type=1337"
2017-01-09 14:21:54 +00:00
def test_utils_cast():
2017-04-18 03:32:08 +00:00
int_int = utils.cast(int, 1)
2020-04-29 21:23:22 +00:00
int_str = utils.cast(int, "1")
bool_str = utils.cast(bool, "1")
2017-04-18 03:32:08 +00:00
bool_int = utils.cast(bool, 1)
float_int = utils.cast(float, 1)
float_float = utils.cast(float, 1.0)
2020-04-29 21:23:22 +00:00
float_str = utils.cast(float, "1.2")
float_nan = utils.cast(float, "wut?")
2017-04-18 03:32:08 +00:00
assert int_int == 1 and isinstance(int_int, int)
assert int_str == 1 and isinstance(int_str, int)
assert bool_str is True
assert bool_int is True
assert float_int == 1.0 and isinstance(float_int, float)
assert float_float == 1.0 and isinstance(float_float, float)
assert float_str == 1.2 and isinstance(float_str, float)
assert float_nan != float_nan # nan is never equal
2017-01-09 14:21:54 +00:00
with pytest.raises(ValueError):
2020-04-29 21:23:22 +00:00
bool_str = utils.cast(bool, "kek")
2017-04-18 03:32:08 +00:00
def test_utils_download(plex, episode):
2017-04-18 03:32:08 +00:00
url = episode.getStreamURL()
locations = episode.locations[0]
session = episode._server._session
assert utils.download(url, plex._token, filename=locations, mocked=True)
2020-04-29 21:23:22 +00:00
assert utils.download(
url, plex._token, filename=locations, session=session, mocked=True
)
assert utils.download(
episode.thumbUrl, plex._token, filename=episode.title, mocked=True
)
2020-06-14 18:21:46 +00:00
def test_millisecondToHumanstr():
res = utils.millisecondToHumanstr(1000)
assert res == "00:00:01.0000"