-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
116 lines (105 loc) · 5.13 KB
/
test.py
File metadata and controls
116 lines (105 loc) · 5.13 KB
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
import unittest
from services.github import get_user_profile, get_user_repos
from unittest.mock import Mock, patch
# from unittest.mock import Mock, patch
class TestGithubService(unittest.TestCase):
# @patch('services.github.get_user_profile.requests.get')
def test_get_user_profile_when_github_response_is_ok(self):
"""
Test getting user profile
"""
data_to_return = {
"id": 6036370,
"login": "mayara-melo",
"name": "",
"avatar_url": "https://avatars3.githubusercontent.com/u/6036370?v=4",
"html_url": "https://github.com/mayara-melo"
}
full_github_response = dict({
"node_id": "MDQ6VXNlcjYwMzYzNzA=",
"gravatar_id": "",
"url": "https://api.github.com/users/mayara-melo",
"followers_url": "https://api.github.com/users/mayara-melo/followers",
"following_url": "https://api.github.com/users/mayara-melo/following{/other_user}",
"gists_url": "https://api.github.com/users/mayara-melo/gists{/gist_id}",
"starred_url": "https://api.github.com/users/mayara-melo/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/mayara-melo/subscriptions",
"organizations_url": "https://api.github.com/users/mayara-melo/orgs",
"repos_url": "https://api.github.com/users/mayara-melo/repos",
"events_url": "https://api.github.com/users/mayara-melo/events{/privacy}",
"received_events_url": "https://api.github.com/users/mayara-melo/received_events",
"type": "User",
"public_repos": 4,
"public_gists": 0,
"followers": 5,
"following": 0,
"created_at": "2013-11-26T01:36:56Z",
"updated_at": "2019-07-31T22:06:06Z"
}, **data_to_return)
with patch('services.github.requests.get') as mock_get:
mock_get.return_value.ok = True
mock_get.return_value = Mock(ok=True)
mock_get.return_value.json.return_value = full_github_response
user_profile = get_user_profile(
username='mayara-melo')
self.assertDictEqual(user_profile, data_to_return)
def test_get_user_profile_when_github_response_is_not_ok(self):
"""
Test getting user profile when github request fails
"""
with patch('services.github.requests.get') as mock_get:
mock_get.return_value.ok = False
self.assertRaises(Exception, get_user_profile,
'mayara-melo')
def test_get_valid_user_repos_when_github_response_ok(self):
"""
Test getting user repos when github response is ok
"""
with patch('services.github.requests.get') as mock_get:
resumed_repos = [
{
"id": 31819396,
"name": "analise-juridica",
"html_url": "https://github.com/mayara-melo/analise-juridica",
"description": ""
}, {
"id": 199935279,
"name": "test-python-webservice",
"html_url": "https://github.com/mayara-melo/test-python-webservice",
"description": ""
}
]
full_data_repos = [
{
"id": 31819396,
"name": "analise-juridica",
"html_url": "https://github.com/mayara-melo/analise-juridica",
"description": "",
"forks_url": "https://api.github.com/repos/mayara-melo/analise-juridica/forks",
"collaborators_url": "https://api.github.com/repos/mayara-melo/analise-juridica/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/mayara-melo/analise-juridica/teams",
}, {
"id": 199935279,
"name": "test-python-webservice",
"html_url": "https://github.com/mayara-melo/test-python-webservice",
"description": "",
"forks_url": "https://api.github.com/repos/mayara-melo/analise-juridica/forks",
"collaborators_url": "https://api.github.com/repos/mayara-melo/analise-juridica/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/mayara-melo/analise-juridica/teams",
}
]
mock_get.return_value.ok = True
mock_get.return_value = Mock(ok=True)
mock_get.return_value.json.return_value = full_data_repos
user_repos = get_user_repos(
username='mayara-melo')
self.assertCountEqual(user_repos, resumed_repos)
def test_get_valid_user_repos_when_github_response_is_not_ok(self):
"""
Test getting user repos when github request fails
"""
with patch('services.github.requests.get') as mock_get:
mock_get.return_value.ok = False
self.assertRaises(Exception, get_user_repos, 'mayara-melo')
if __name__ == '__main__':
unittest.main()