-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
test_pylast.py
executable file
·133 lines (106 loc) · 3.78 KB
/
test_pylast.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
#!/usr/bin/env python
"""
Integration (not unit) tests for pylast.py
"""
import os
import sys
import time
import pylast
import pytest
from flaky import flaky
WRITE_TEST = sys.version_info[:2] == (3, 8)
def load_secrets():
secrets_file = "test_pylast.yaml"
if os.path.isfile(secrets_file):
import yaml # pip install pyyaml
with open(secrets_file) as f: # see example_test_pylast.yaml
doc = yaml.load(f)
else:
doc = {}
try:
doc["username"] = os.environ["PYLAST_USERNAME"].strip()
doc["password_hash"] = os.environ["PYLAST_PASSWORD_HASH"].strip()
doc["api_key"] = os.environ["PYLAST_API_KEY"].strip()
doc["api_secret"] = os.environ["PYLAST_API_SECRET"].strip()
except KeyError:
pytest.skip("Missing environment variables: PYLAST_USERNAME etc.")
return doc
class PyLastTestCase:
def assert_startswith(self, str, prefix, start=None, end=None):
assert str.startswith(prefix, start, end)
def assert_endswith(self, str, suffix, start=None, end=None):
assert str.endswith(suffix, start, end)
@flaky(max_runs=3, min_passes=1)
class TestPyLastWithLastFm(PyLastTestCase):
secrets = None
def unix_timestamp(self):
return int(time.time())
@classmethod
def setup_class(cls):
if cls.secrets is None:
cls.secrets = load_secrets()
cls.username = cls.secrets["username"]
password_hash = cls.secrets["password_hash"]
api_key = cls.secrets["api_key"]
api_secret = cls.secrets["api_secret"]
cls.network = pylast.LastFMNetwork(
api_key=api_key,
api_secret=api_secret,
username=cls.username,
password_hash=password_hash,
)
def helper_is_thing_hashable(self, thing):
# Arrange
things = set()
# Act
things.add(thing)
# Assert
assert thing is not None
assert len(things) == 1
def helper_validate_results(self, a, b, c):
# Assert
assert a is not None
assert b is not None
assert c is not None
assert len(a) >= 0
assert len(b) >= 0
assert len(c) >= 0
assert a == b
assert b == c
def helper_validate_cacheable(self, thing, function_name):
# Arrange
# get thing.function_name()
func = getattr(thing, function_name, None)
# Act
result1 = func(limit=1, cacheable=False)
result2 = func(limit=1, cacheable=True)
result3 = func(limit=1)
# Assert
self.helper_validate_results(result1, result2, result3)
def helper_at_least_one_thing_in_top_list(self, things, expected_type):
# Assert
assert len(things) > 1
assert isinstance(things, list)
assert isinstance(things[0], pylast.TopItem)
assert isinstance(things[0].item, expected_type)
def helper_only_one_thing_in_top_list(self, things, expected_type):
# Assert
assert len(things) == 1
assert isinstance(things, list)
assert isinstance(things[0], pylast.TopItem)
assert isinstance(things[0].item, expected_type)
def helper_only_one_thing_in_list(self, things, expected_type):
# Assert
assert len(things) == 1
assert isinstance(things, list)
assert isinstance(things[0], expected_type)
def helper_two_different_things_in_top_list(self, things, expected_type):
# Assert
assert len(things) == 2
thing1 = things[0]
thing2 = things[1]
assert isinstance(thing1, pylast.TopItem)
assert isinstance(thing2, pylast.TopItem)
assert isinstance(thing1.item, expected_type)
assert isinstance(thing2.item, expected_type)
assert thing1 != thing2