forked from SatelliteQE/nailgun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_client.py
More file actions
144 lines (113 loc) · 5.25 KB
/
Copy pathtest_client.py
File metadata and controls
144 lines (113 loc) · 5.25 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
"""Unit tests for :mod:`nailgun.client`."""
import inspect
from unittest import TestCase, mock
from fauxfactory import gen_alpha
import requests
from nailgun import client
class ContentTypeIsJsonTestCase(TestCase):
"""Tests for function ``_content_type_is_json``."""
def test_true(self):
"""Assert ``True`` is returned when content-type is JSON."""
for kwargs in (
{'headers': {'content-type': 'application/json'}},
{'headers': {'content-type': 'appLICatiON/JSoN'}},
{'headers': {'content-type': 'APPLICATION/JSON'}},
):
self.assertTrue(client._content_type_is_json(kwargs))
def test_false(self):
"""Assert ``True`` is returned when content-type is not JSON."""
for kwargs in (
{'headers': {'content-type': ''}},
{'headers': {'content-type': 'application-json'}},
{'headers': {'content-type': 'application/pson'}},
):
self.assertFalse(client._content_type_is_json(kwargs))
def test_false_with_no_headers(self):
"""If no headers passed should return None."""
self.assertFalse(client._content_type_is_json({}))
class SetContentTypeTestCase(TestCase):
"""Tests for function ``_set_content_type``."""
def test_no_value(self):
"""Assert that a content-type is provided if none is set."""
kwargs = {'headers': {}}
client._set_content_type(kwargs)
self.assertEqual(
kwargs,
{'headers': {'content-type': 'application/json'}},
)
def test_existing_value(self):
"""Assert that no content-type is provided if one is set."""
kwargs = {'headers': {'content-type': ''}}
client._set_content_type(kwargs)
self.assertEqual(kwargs, {'headers': {'content-type': ''}})
def test_files_in_kwargs(self):
"""Assert that no content-type is provided if files are given."""
kwargs = {'files': None}
client._set_content_type(kwargs)
self.assertEqual(kwargs, {'files': None})
class ClientTestCase(TestCase):
"""Tests for functions in :mod:`nailgun.client`."""
def setUp(self):
"""Set up some common variables."""
self.bogus_url = gen_alpha()
self.mock_response = mock.Mock(status_code=200)
def test_clients(self):
"""Test all the wrappers except :func:`nailgun.client.request`.
The following functions are tested:
* :func:`nailgun.client.delete`
* :func:`nailgun.client.get`
* :func:`nailgun.client.head`
* :func:`nailgun.client.patch`
* :func:`nailgun.client.post`
* :func:`nailgun.client.put`
Assert that:
* The wrapper function passes the correct parameters to requests.
* The wrapper function returns whatever requests returns.
"""
for meth in ('delete', 'get', 'head', 'patch', 'post', 'put'):
with mock.patch.object(requests, meth) as requests_meth:
# Does the wrapper function return whatever requests returns?
requests_meth.return_value = self.mock_response
self.assertIs(getattr(client, meth)(self.bogus_url), self.mock_response)
# Did the wrapper function pass the correct params to requests?
if meth in ('delete', 'head'):
requests_meth.assert_called_once_with(
self.bogus_url, headers={'content-type': 'application/json'}
)
elif meth in ('get', 'patch', 'put'):
requests_meth.assert_called_once_with(
self.bogus_url, None, headers={'content-type': 'application/json'}
)
else: # meth is 'post'
requests_meth.assert_called_once_with(
self.bogus_url, None, None, headers={'content-type': 'application/json'}
)
def test_client_request(self):
"""Test :func:`nailgun.client.request`.
Make the same assertions as
:meth:`tests.test_client.ClientTestCase.test_clients`.
"""
with mock.patch.object(requests, 'request') as requests_request:
requests_request.return_value = self.mock_response
self.assertIs(
client.request('foo', self.bogus_url),
self.mock_response,
)
requests_request.assert_called_once_with(
'foo', self.bogus_url, headers={'content-type': 'application/json'}
)
def test_identical_args(self):
"""Check that the wrapper functions have the correct signatures.
For example, :func:`nailgun.client.delete` should have the same
signature as ``requests.delete``.
"""
def _strip_annotations(sig):
params = [
p.replace(annotation=inspect.Parameter.empty) for p in sig.parameters.values()
]
return sig.replace(parameters=params, return_annotation=inspect.Parameter.empty)
for meth in ('delete', 'get', 'head', 'patch', 'post', 'put'):
self.assertEqual(
_strip_annotations(inspect.signature(getattr(client, meth))),
_strip_annotations(inspect.signature(getattr(requests, meth))),
)