-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_client.py
executable file
·112 lines (99 loc) · 3.71 KB
/
test_client.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
#!/usr/bin/env python3
"""
MODULE TO TEST!
Parameterize and patch as decorators
"""
import unittest
from unittest.mock import patch, PropertyMock, Mock
from parameterized import parameterized, parameterized_class
from client import GithubOrgClient
class TestGithubOrgClient(unittest.TestCase):
"""
Tests for GithubOrgClient.
"""
@parameterized.expand([
("google", {"google": True}),
("abc", {"abc": True}),
])
@patch('client.get_json')
def test_org(self, org, expected, mock_get):
"""Test GithubOrgClient.org"""
mock_get.return_value = expected
goc = GithubOrgClient(org)
self.assertEqual(goc.org, expected)
mock_get.assert_called_once_with(f"https://api.github.com/orgs/{org}")
@parameterized.expand([
("my_license", {"license": {"key": "my_license"}}),
("other_license", {"license": {"key": "other_license"}})
])
def test_has_license(self, license_key, repo):
"""
Test GithubOrgClient.has_license method.
"""
with patch('client.GithubOrgClient.org',
new_callable=PropertyMock) as mock_org:
mock_org.return_value = {"repos_url": "test_repos_url"}
with patch('client.GithubOrgClient._public_repos_url',
new_callable=PropertyMock) as mock_public_repos_url:
mock_public_repos_url.return_value = [repo]
goc = GithubOrgClient("test")
self.assertEqual(goc.has_license(repo, license_key),
repo["license"]["key"] == license_key)
mock_public_repos_url.assert_called_once()
@patch('client.get_json')
def test_public_repos(self, mock_get_json):
"""
Test GithubOrgClient.public_repos method.
"""
mock_get_json.return_value = [{"name": "repo1"}, {"name": "repo2"}]
with patch('client.GithubOrgClient.org',
new_callable=PropertyMock) as mock_org:
mock_org.return_value = {"repos_url": "test_repos_url"}
goc = GithubOrgClient("test")
self.assertEqual(goc.public_repos(), ["repo1", "repo2"])
mock_org.assert_called_once()
mock_get_json.assert_called_once_with("test_repos_url")
@parameterized_class([
{"org_payload": {"login": "test"},
"repos_payload": [{"name": "test_repo"}],
"expected_repos": ["test_repo"],
"apache2_repos": [{"license": {"key": "apache-2.0"}}]}
])
class TestIntegrationGithubOrgClient(unittest.TestCase):
"""
Integration tests for GithubOrgClient.
"""
@classmethod
def setUpClass(cls):
"""
Set up class for integration tests.
"""
cls.get_patcher = patch('client.get_json')
cls.mock_get_json = cls.get_patcher.start()
cls.org_payload = {"login": "test"}
cls.repos_payload = [{"name": "test_repo"}]
cls.expected_repos = ["test_repo"]
cls.apache2_repos = [{"license": {"key": "apache-2.0"}}]
cls.mock_get_json.side_effect = [
cls.org_payload,
cls.repos_payload,
cls.apache2_repos
]
@classmethod
def tearDownClass(cls):
"""
Tear down class after tests.
"""
cls.get_patcher.stop()
def test_public_repos(self):
"""
Test GithubOrgClient.public_repos method.
"""
goc = GithubOrgClient("test")
self.assertEqual(goc.public_repos(), self.expected_repos)
def test_public_repos_with_license(self):
"""
Test GithubOrgClient.public_repos with license="apache-2.0".
"""
goc = GithubOrgClient("test")
self.assertEqual(goc.public_repos("apache-2.0"), self.apache2_repos)