forked from sigmavirus24/github3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_models.py
More file actions
217 lines (175 loc) · 7.15 KB
/
Copy pathtest_models.py
File metadata and controls
217 lines (175 loc) · 7.15 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import io
import json
import pytest
import requests
from datetime import datetime, timedelta
from github3 import exceptions, GitHubError
from github3.models import GitHubCore
from unittest import TestCase
from . import helper
class MyTestRefreshClass(GitHubCore):
"""Subclass for testing refresh on GitHubCore."""
def __init__(self, example_data, session=None):
super(MyTestRefreshClass, self).__init__(example_data, session)
self._api = example_data['url']
self.last_modified = example_data['last_modified']
self.etag = example_data['etag']
class TestGitHubError(TestCase):
"""Test methods on GitHubError class."""
def setUp(self):
response = requests.Response()
response.status_code = 400
message = b'{"message": "m", "errors": ["e"]}'
response.raw = io.BytesIO(message)
self.instance = GitHubError(response)
def test_message_is_empty(self):
"""Verify instance message is correct."""
response = requests.Response()
response.status_code = 400
response.raw = io.BytesIO()
error = GitHubError(response)
assert error.message == '[No message]'
def test_message(self):
"""Verify instance message is correct."""
assert self.instance.msg == self.instance.message
def test_str(self):
"""Verify instance string is formatted correctly."""
assert str(self.instance) == '400 m'
class TestGitHubCore(helper.UnitHelper):
described_class = MyTestRefreshClass
last_modified = datetime.now().strftime(
'%a, %d %b %Y %H:%M:%S GMT'
)
url = 'https://api.github.com/foo'
etag = '644b5b0155e6404a9cc4bd9d8b1ae730'
example_data = {
'url': url,
'last_modified': last_modified,
'etag': etag,
'fake_attr': 'foo',
}
def test_boolean(self):
"""Verify boolean tests for response codes correctly."""
response = requests.Response()
response.status_code = 200
boolean = self.instance._boolean(response=response,
true_code=200,
false_code=204)
assert boolean is True
def test_boolean_raises_exception(self):
"""Verify boolean tests for response codes correctly."""
response = requests.Response()
response.status_code = 512
with pytest.raises(exceptions.GitHubError):
self.instance._boolean(response=response,
true_code=200,
false_code=204)
def test_boolean_false_code(self):
"""Verify boolean tests for response codes correctly."""
response = requests.Response()
response.status_code = 204
boolean = self.instance._boolean(response=response,
true_code=200,
false_code=204)
assert boolean is False
def test_boolean_empty_response(self):
"""Verify boolean tests for response codes correctly."""
boolean = self.instance._boolean(response=None,
true_code=200,
false_code=204)
assert boolean is False
def test_exposes_attributes(self):
"""Verify JSON attributes are exposed even if not explicitly set."""
assert self.instance.fake_attr == 'foo'
def test_from_json(self):
"""Verify that method returns GitHubObject from json."""
github_core = GitHubCore.from_json('{}')
assert isinstance(github_core, GitHubCore)
def test_instance_or_null(self):
"""Verify method raises exception when json is not a dict."""
with pytest.raises(exceptions.UnprocessableResponseBody):
self.instance._instance_or_null(GitHubCore, [])
def test_json(self):
"""Verify JSON information is retrieved correctly."""
response = requests.Response()
response.headers['Last-Modified'] = 'foo'
response.headers['ETag'] = 'bar'
response.raw = io.BytesIO(b'{}')
response.status_code = 200
json = self.instance._json(response, 200)
assert json['Last-Modified'] == 'foo'
assert json['ETag'] == 'bar'
def test_json_status_code_does_not_match(self):
"""Verify JSON information is retrieved correctly."""
response = requests.Response()
response.status_code = 204
json = self.instance._json(response, 200)
assert json is None
def test_missingattribute(self):
"""Test AttributeError is raised when attribute is not in JSON."""
with pytest.raises(AttributeError):
self.instance.missingattribute
def test_refresh(self):
"""Verify the request of refreshing an object."""
instance = self.instance.refresh()
assert isinstance(instance, MyTestRefreshClass)
expected_headers = None
self.session.get.assert_called_once_with(
self.url,
headers=expected_headers,
)
def test_refresh_custom_headers(self):
"""Verify the request of refreshing an object."""
self.instance.CUSTOM_HEADERS = {
'Accept': 'application/vnd.github.drax-preview+json'
}
expected_headers = {
'Accept': 'application/vnd.github.drax-preview+json'
}
self.instance.refresh()
self.session.get.assert_called_once_with(
self.url,
headers=expected_headers,
)
def test_refresh_last_modified(self):
"""Verify the request of refreshing an object."""
expected_headers = {
'If-Modified-Since': self.last_modified
}
self.instance.refresh(conditional=True)
self.session.get.assert_called_once_with(
self.url,
headers=expected_headers,
)
def test_refresh_etag(self):
"""Verify the request of refreshing an object."""
self.instance.last_modified = None
expected_headers = {
'If-None-Match': self.etag
}
self.instance.refresh(conditional=True)
self.session.get.assert_called_once_with(
self.url,
headers=expected_headers,
)
def test_refresh_json(self):
"""Verify refreshing an object updates stored json data."""
expected_data = {
'changed_files': 4
}
response = requests.Response()
response.status_code = 200
response.raw = io.BytesIO(json.dumps(expected_data).encode('utf8'))
self.session.get.return_value = response
self.instance.refresh()
assert 'changed_files' in self.instance.as_dict()
assert self.instance.changed_files == 4
def test_strptime(self):
"""Verify that method converts ISO 8601 formatted string."""
dt = self.instance._strptime('2015-06-18T19:53:04Z')
assert dt.tzname() == 'UTC'
assert dt.dst() == timedelta(0)
assert dt.utcoffset() == timedelta(0)
def test_strptime_time_str_required(self):
"""Verify that method converts ISO 8601 formatted string."""
assert self.instance._strptime('') is None