File size: 2,206 Bytes
e7964fa 9172cb2 e7964fa cf801d3 e7964fa 3e182e5 e7964fa 3e182e5 e7964fa 82321d6 9172cb2 9e52f7b 9172cb2 9e52f7b b2b9291 cf801d3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# -*- coding: utf-8 -*-
from unittest import mock
import pytest
from pytube import helpers
from pytube.exceptions import RegexMatchError
from pytube.helpers import deprecated, cache, target_directory, setup_logger
def test_regex_search_no_match():
with pytest.raises(RegexMatchError):
helpers.regex_search("^a$", "", group=0)
def test_regex_search():
assert helpers.regex_search("^a$", "a", group=0) == "a"
def test_safe_filename():
"""Unsafe characters get stripped from generated filename"""
assert helpers.safe_filename("abc1245$$") == "abc1245"
assert helpers.safe_filename("abc##") == "abc"
@mock.patch("warnings.warn")
def test_deprecated(warn):
@deprecated("oh no")
def deprecated_function():
return None
deprecated_function()
warn.assert_called_with(
"Call to deprecated function deprecated_function (oh no).",
category=DeprecationWarning,
stacklevel=2,
)
def test_cache():
call_count = 0
@cache
def cached_func(stuff):
nonlocal call_count
call_count += 1
return stuff
cached_func("hi")
cached_func("hi")
cached_func("bye")
cached_func("bye")
assert call_count == 2
@mock.patch("os.path.isabs", return_value=False)
@mock.patch("os.getcwd", return_value="/cwd")
@mock.patch("os.makedirs")
def test_target_directory_with_relative_path(_, __, makedirs):
assert target_directory("test") == "/cwd/test"
makedirs.assert_called()
@mock.patch("os.path.isabs", return_value=True)
@mock.patch("os.makedirs")
def test_target_directory_with_absolute_path(_, makedirs):
assert target_directory("/test") == "/test"
makedirs.assert_called()
@mock.patch("os.getcwd", return_value="/cwd")
@mock.patch("os.makedirs")
def test_target_directory_with_no_path(_, makedirs):
assert target_directory() == "/cwd"
makedirs.assert_called()
@mock.patch("pytube.helpers.logging")
def test_setup_logger(logging):
# Given
logger = logging.getLogger.return_value
# When
setup_logger(20)
# Then
logging.getLogger.assert_called_with("pytube")
logger.addHandler.assert_called()
logger.setLevel.assert_called_with(20)
|