forked from sigma67/spotify_to_ytmusic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cli.py
133 lines (117 loc) · 4.39 KB
/
test_cli.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
import time
import unittest
from io import StringIO
from unittest import mock
from spotify_to_ytmusic import settings as settings_module
from spotify_to_ytmusic import setup
from spotify_to_ytmusic.main import get_args, main
from spotify_to_ytmusic.settings import DEFAULT_PATH, EXAMPLE_PATH, CACHE_DIR, Settings
TEST_PLAYLIST = "https://open.spotify.com/playlist/4UzyZJfSQ4584FaWGwepfL"
TEST_SONG = "https://open.spotify.com/track/7bnczC5ATlZaZX0MHjX7KU?si=5a07bffaf6324717"
class TestCli(unittest.TestCase):
@classmethod
def setUpClass(cls):
Settings()
def test_get_args(self):
args = get_args(["all", "user"])
self.assertEqual(len(vars(args)), 6)
args = get_args(["create", "playlist-link"])
self.assertEqual(len(vars(args)), 10)
args = get_args(["update", "playlist-link", "playlist-name"])
self.assertEqual(len(vars(args)), 7)
args = get_args(["liked"])
self.assertEqual(len(vars(args)), 9)
args = get_args(["search", "link"])
self.assertEqual(len(vars(args)), 5)
args = get_args(["setup"])
self.assertEqual(len(vars(args)), 4)
def test_liked(self):
with mock.patch(
"sys.argv",
["", "liked", "-n", "spotify_to_ytmusic", "-d", "-i", "test liked"],
):
main()
def test_create(self):
with mock.patch("sys.argv", ["", "all", "sigmatics"]):
main()
with mock.patch(
"sys.argv",
[
"",
"create",
TEST_PLAYLIST,
"-n",
"spotify_to_ytmusic",
"-i",
"test-playlist",
"-d",
],
):
main()
time.sleep(2)
with mock.patch(
"sys.argv", ["", "update", TEST_PLAYLIST, "spotify_to_ytmusic"]
):
main()
time.sleep(2)
with (
mock.patch("sys.argv", ["", "remove", "spotify\_to\_ytmusic"]),
mock.patch("sys.stdout", new=StringIO()) as fakeOutput,
mock.patch("builtins.input", side_effect="y"),
):
main()
assert (
int(fakeOutput.getvalue().splitlines()[-1][0]) >= 2
) # assert number of lines deleted
def test_search(self):
with mock.patch("sys.argv", ["", "search", TEST_SONG]):
main()
def test_search_with_use_cached_flag(self):
cache_file = CACHE_DIR / "lookup.json"
# Ensure the cache file doesn't exist before running the test
if cache_file.exists():
cache_file.unlink()
with mock.patch("sys.argv", ["", "search", TEST_SONG, "--use-cached"]):
main()
self.assertTrue(cache_file.exists(), "Cache file was not created.")
def test_setup(self):
tmp_path = DEFAULT_PATH.with_suffix(".tmp")
settings = Settings()
with (
mock.patch("sys.argv", ["", "setup"]),
mock.patch(
"builtins.input",
side_effect=[
"4",
settings["youtube"]["client_id"],
settings["youtube"]["client_secret"],
"yes",
"a",
"b",
"",
],
),
mock.patch(
"ytmusicapi.auth.oauth.credentials.OAuthCredentials.token_from_code",
return_value=json.loads(settings["youtube"]["headers"]),
),
mock.patch.object(setup, "DEFAULT_PATH", tmp_path),
mock.patch("spotify_to_ytmusic.setup.has_browser", return_value=False),
mock.patch.object(settings_module, "DEFAULT_PATH", tmp_path),
mock.patch.object(Settings, "filepath", tmp_path),
):
main()
assert tmp_path.is_file()
settings = Settings() # reload settings
assert settings["spotify"]["client_id"] == "a"
assert settings["spotify"]["client_secret"] == "b"
tmp_path.unlink()
with (
mock.patch("sys.argv", ["", "setup", "--file", EXAMPLE_PATH.as_posix()]),
mock.patch.object(setup, "DEFAULT_PATH", tmp_path),
mock.patch.object(settings_module, "DEFAULT_PATH", tmp_path),
):
main()
assert tmp_path.is_file()
tmp_path.unlink()