Skip to content

Commit 104c0bc

Browse files
committed
Code cleanup for tests.
1 parent f4a882b commit 104c0bc

File tree

2 files changed

+91
-156
lines changed

2 files changed

+91
-156
lines changed

tests/test_helix_user.py

Lines changed: 41 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import tracemalloc
22
import unittest
3+
from typing import Dict, Any
34

45
import responses
56

@@ -9,72 +10,56 @@
910

1011

1112
class TestHelixUser(unittest.TestCase):
12-
13-
@responses.activate
14-
def test_user(self):
13+
user: Dict[str, Dict[str, Any]] = {
14+
'zarlach': {
15+
'id': '24250859',
16+
'login': 'zarlach',
17+
'display_name': 'Zarlach',
18+
'type': '',
19+
'broadcaster_type': '',
20+
'description': '',
21+
'profile_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/zarlach-profile_image-1cb98e7eadb5918a-300x300.png',
22+
'offline_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/zarlach-channel_offline_image-f2d036ac9582d793-1920x1080.png',
23+
'view_count': 1664
24+
},
25+
'sodapoppin': {
26+
'id': '26301881',
27+
'login': 'sodapoppin',
28+
'display_name': 'sodapoppin',
29+
'type': '',
30+
'broadcaster_type': 'partner',
31+
'description': 'Wtf do i write here? Click my stream, or i scream.',
32+
'profile_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/sodapoppin-profile_image-10049b6200f90c14-300x300.png',
33+
'offline_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/7ed72b04-897e-4a85-a3c9-2a8ba74aeaa7-channel_offline_image-1920x1080.jpg',
34+
'view_count': 276164750
35+
}
36+
}
37+
38+
def setUp(self) -> None:
1539
responses.add(responses.GET, 'https://api.twitch.tv/helix/users?login=zarlach',
1640
match_querystring=True,
17-
json={
18-
'data': [{'id': '24250859',
19-
'login': 'zarlach',
20-
'display_name': 'Zarlach',
21-
'type': '',
22-
'broadcaster_type': '',
23-
'description': '',
24-
'profile_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/zarlach-profile_image-1cb98e7eadb5918a-300x300.png',
25-
'offline_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/zarlach-channel_offline_image-f2d036ac9582d793-1920x1080.png',
26-
27-
'view_count': 1664}]
28-
})
41+
json={'data': [TestHelixUser.user['zarlach']]})
42+
responses.add(responses.GET, 'https://api.twitch.tv/helix/users?login=sodapoppin',
43+
match_querystring=True,
44+
json={'data': [TestHelixUser.user['sodapoppin']]})
45+
46+
responses.add(responses.GET, 'https://api.twitch.tv/helix/users?id=24250859&login=sodapoppin',
47+
match_querystring=True,
48+
json={'data': [TestHelixUser.user['zarlach'], TestHelixUser.user['sodapoppin']]})
49+
50+
responses.add(responses.GET, 'https://api.twitch.tv/helix/users?login=sodapoppin&login=zarlach',
51+
match_querystring=True,
52+
json={'data': [TestHelixUser.user['zarlach'], TestHelixUser.user['sodapoppin']]})
2953

54+
@responses.activate
55+
def test_user(self):
3056
helix = twitch.Helix('client-id', use_cache=True)
3157

3258
# Get display name to display name
3359
self.assertEqual(helix.user('zarlach').display_name, 'Zarlach')
3460

3561
@responses.activate
3662
def test_users(self):
37-
responses.add(responses.GET, 'https://api.twitch.tv/helix/users?login=sodapoppin',
38-
match_querystring=True,
39-
json={
40-
'data': [
41-
{'id': '26301881',
42-
'login': 'sodapoppin',
43-
'display_name': 'sodapoppin',
44-
'type': '',
45-
'broadcaster_type': 'partner',
46-
'description': 'Wtf do i write here? Click my stream, or i scream.',
47-
'profile_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/sodapoppin-profile_image-10049b6200f90c14-300x300.png',
48-
'offline_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/7ed72b04-897e-4a85-a3c9-2a8ba74aeaa7-channel_offline_image-1920x1080.jpg',
49-
'view_count': 276164750}]
50-
}
51-
)
52-
53-
responses.add(responses.GET, 'https://api.twitch.tv/helix/users?id=24250859&login=sodapoppin',
54-
match_querystring=True,
55-
json={
56-
'data': [
57-
{'id': '24250859',
58-
'login': 'zarlach',
59-
'display_name': 'Zarlach',
60-
'type': '',
61-
'broadcaster_type': '',
62-
'description': '',
63-
'profile_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/zarlach-profile_image-1cb98e7eadb5918a-300x300.png',
64-
'offline_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/zarlach-channel_offline_image-f2d036ac9582d793-1920x1080.png',
65-
66-
'view_count': 1664},
67-
{'id': '26301881',
68-
'login': 'sodapoppin',
69-
'display_name': 'sodapoppin',
70-
'type': '',
71-
'broadcaster_type': 'partner',
72-
'description': 'Wtf do i write here? Click my stream, or i scream.',
73-
'profile_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/sodapoppin-profile_image-10049b6200f90c14-300x300.png',
74-
'offline_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/7ed72b04-897e-4a85-a3c9-2a8ba74aeaa7-channel_offline_image-1920x1080.jpg',
75-
'view_count': 276164750}]
76-
})
77-
7863
# Should returned cached data from above
7964
helix = twitch.Helix('client-id', use_cache=True)
8065

@@ -83,31 +68,6 @@ def test_users(self):
8368

8469
@responses.activate
8570
def test_custom_user_cache(self):
86-
responses.add(responses.GET, 'https://api.twitch.tv/helix/users?login=sodapoppin&login=zarlach',
87-
match_querystring=True,
88-
json={
89-
'data': [
90-
{'id': '24250859',
91-
'login': 'zarlach',
92-
'display_name': 'Zarlach',
93-
'type': '',
94-
'broadcaster_type': '',
95-
'description': '',
96-
'profile_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/zarlach-profile_image-1cb98e7eadb5918a-300x300.png',
97-
'offline_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/zarlach-channel_offline_image-f2d036ac9582d793-1920x1080.png',
98-
'view_count': 1664},
99-
{'id': '26301881',
100-
'login': 'sodapoppin',
101-
'display_name': 'sodapoppin',
102-
'type': '',
103-
'broadcaster_type': 'partner',
104-
'description': 'Wtf do i write here? Click my stream, or i scream.',
105-
'profile_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/sodapoppin-profile_image-10049b6200f90c14-300x300.png',
106-
'offline_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/7ed72b04-897e-4a85-a3c9-2a8ba74aeaa7-channel_offline_image-1920x1080.jpg',
107-
'view_count': 276164750}]
108-
}
109-
)
110-
11171
helix = twitch.Helix('client-id', use_cache=True)
11272
helix.users(['zarlach', 'sodapoppin'])
11373

tests/test_helix_video.py

Lines changed: 50 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,77 @@
11
import tracemalloc
22
import unittest
3+
from typing import Dict, Any
34

45
import responses
56

67
import twitch
8+
from .test_helix_user import TestHelixUser
79

810
tracemalloc.start()
911

1012

1113
class TestHelixVideo(unittest.TestCase):
12-
13-
@responses.activate
14-
def test_video(self):
14+
video: Dict[str, Dict[str, Any]] = {
15+
'471855782': {
16+
'id': '471855782',
17+
'user_id': '26301881',
18+
'user_name': 'sodapoppin',
19+
'title': '2 days til Classic, passing the time til then. ',
20+
'description': '',
21+
'created_at': '2019-08-24T21:34:03Z',
22+
'published_at': '2019-08-24T21:34:03Z',
23+
'url': 'https://www.twitch.tv/videos/471855782',
24+
'thumbnail_url': '',
25+
'viewable': 'public',
26+
'view_count': 329,
27+
'language': 'en',
28+
'type': 'archive',
29+
'duration': '2h14m55s'
30+
},
31+
'471295896': {
32+
'id': '471295896',
33+
'user_id': '26301881',
34+
'user_name': 'sodapoppin',
35+
'title': '3 days til Classic, passing the time til then. ',
36+
'description': '',
37+
'created_at': '2019-08-23T18:40:05Z',
38+
'published_at': '2019-08-23T18:40:05Z',
39+
'url': 'https://www.twitch.tv/videos/471295896',
40+
'thumbnail_url': 'https://static-cdn.jtvnw.net/s3_vods/7d5ae2c2918cf4ca8579_sodapoppin_35403289856_1281380185/thumb/thumb0-%{width}x%{height}.jpg',
41+
'viewable': 'public',
42+
'view_count': 5892,
43+
'language': 'en',
44+
'type': 'archive',
45+
'duration': '6h4m13s'}
46+
}
47+
48+
def setUp(self) -> None:
1549
responses.add(responses.GET, 'https://api.twitch.tv/helix/videos?id=471855782',
1650
match_querystring=True,
1751
json={
18-
'data': [{
19-
'id': '471855782',
20-
'user_id': '26301881',
21-
'user_name': 'sodapoppin',
22-
'title': '2 days til Classic, passing the time til then. ',
23-
'description': '',
24-
'created_at': '2019-08-24T21:34:03Z',
25-
'published_at': '2019-08-24T21:34:03Z',
26-
'url': 'https://www.twitch.tv/videos/471855782',
27-
'thumbnail_url': '',
28-
'viewable': 'public',
29-
'view_count': 329,
30-
'language': 'en',
31-
'type': 'archive',
32-
'duration': '2h14m55s'}],
52+
'data': [TestHelixVideo.video['471855782']],
3353
'pagination': {'cursor': 'eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MX19'}
3454
})
35-
36-
helix = twitch.Helix('client-id', use_cache=True)
37-
38-
# Get display name to display name
39-
self.assertEqual(helix.video('471855782').user_name, 'sodapoppin')
40-
41-
@responses.activate
42-
def test_first_videos(self):
4355
responses.add(responses.GET, 'https://api.twitch.tv/helix/users?login=sodapoppin',
4456
match_querystring=True,
45-
json={
46-
'data': [
47-
{
48-
'id': '26301881', 'login': 'sodapoppin', 'display_name': 'sodapoppin', 'type': '',
49-
'broadcaster_type': 'partner',
50-
'description': 'Wtf do i write here? Click my stream, or i scream.',
51-
'profile_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/sodapoppin-profile_image-10049b6200f90c14-300x300.png',
52-
'offline_image_url': 'https://static-cdn.jtvnw.net/jtv_user_pictures/7ed72b04-897e-4a85-a3c9-2a8ba74aeaa7-channel_offline_image-1920x1080.jpg',
53-
'view_count': 287171213
54-
}]
55-
})
57+
json={'data': [TestHelixUser.user['sodapoppin']]})
5658

5759
responses.add(responses.GET, 'https://api.twitch.tv/helix/videos?user_id=26301881&first=2',
5860
match_querystring=True,
5961
json={
60-
'data': [
61-
{
62-
'id': '471855782', 'user_id': '26301881', 'user_name': 'sodapoppin',
63-
'title': '2 days til Classic, passing the time til then. ', 'description': '',
64-
'created_at': '2019-08-24T21:34:03Z', 'published_at': '2019-08-24T21:34:03Z',
65-
'url': 'https://www.twitch.tv/videos/471855782', 'thumbnail_url': '',
66-
'viewable': 'public', 'view_count': 453, 'language': 'en', 'type': 'archive',
67-
'duration': '2h21m43s'
68-
},
69-
{
70-
'id': '471295896', 'user_id': '26301881', 'user_name': 'sodapoppin',
71-
'title': '3 days til Classic, passing the time til then. ', 'description': '',
72-
'created_at': '2019-08-23T18:40:05Z', 'published_at': '2019-08-23T18:40:05Z',
73-
'url': 'https://www.twitch.tv/videos/471295896',
74-
'thumbnail_url': 'https://static-cdn.jtvnw.net/s3_vods/7d5ae2c2918cf4ca8579_sodapoppin_35403289856_1281380185/thumb/thumb0-%{width}x%{height}.jpg',
75-
'viewable': 'public', 'view_count': 5892, 'language': 'en', 'type': 'archive',
76-
'duration': '6h4m13s'}],
62+
'data': [TestHelixVideo.video['471855782'], TestHelixVideo.video['471295896']],
7763
'pagination': {'cursor': 'eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6Mn19'}
7864
})
7965

66+
@responses.activate
67+
def test_video(self):
68+
helix = twitch.Helix('client-id', use_cache=True)
69+
70+
# Get display name to display name
71+
self.assertEqual(helix.video('471855782').user_name, 'sodapoppin')
72+
73+
@responses.activate
74+
def test_first_videos(self):
8075
# Should returned cached data from above
8176
helix = twitch.Helix('client-id', use_cache=True)
8277

@@ -85,28 +80,8 @@ def test_first_videos(self):
8580

8681
@responses.activate
8782
def test_custom_video_cache(self):
88-
responses.add(responses.GET, 'https://api.twitch.tv/helix/videos?id=471855782',
89-
match_querystring=True,
90-
json={
91-
'data': [
92-
{'id': '471855782',
93-
'user_id': '26301881',
94-
'user_name': 'sodapoppin',
95-
'title': '2 days til Classic, passing the time til then. ',
96-
'description': '',
97-
'created_at': '2019-08-24T21:34:03Z',
98-
'published_at': '2019-08-24T21:34:03Z',
99-
'url': 'https://www.twitch.tv/videos/471855782',
100-
'thumbnail_url': '',
101-
'viewable': 'public',
102-
'view_count': 329,
103-
'language': 'en',
104-
'type': 'archive',
105-
'duration': '2h14m55s'}],
106-
'pagination': {'cursor': 'eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MX19'}})
107-
10883
helix = twitch.Helix('client-id', use_cache=True)
109-
helix.video(471855782)
84+
_ = helix.video(471855782)
11085

11186
# Videos have custom caching, such that url should not be cached
11287
self.assertFalse(helix.api.SHARED_CACHE.has('GET:https://api.twitch.tv/helix/videos?id=471855782'))

0 commit comments

Comments
 (0)