-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_postmon.py
299 lines (220 loc) · 7.92 KB
/
test_postmon.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import unittest
import json
from decimal import Decimal
import mock
import httpretty
import requests
import postmon
BASE_URL = postmon.PostmonModel.base_url
class TestCepCompleto(unittest.TestCase):
url = '%s/cep/11111111' % BASE_URL
response = {
"bairro": "Bairro B",
"cidade": "Cidade C",
"cep": "11111111",
"logradouro": "Logradouro L",
"complemento": "Complemento C",
"estado_info": {
"area_km2": "999.999,001",
"codigo_ibge": "35",
"nome": "Estado E"
},
"cidade_info": {
"area_km2": "1099,409",
"codigo_ibge": "3549904"
},
"estado": "SP"
}
@classmethod
def setUpClass(cls):
httpretty.enable()
httpretty.register_uri(httpretty.GET, cls.url,
body=json.dumps(cls.response))
@classmethod
def tearDownClass(cls):
httpretty.disable()
httpretty.reset()
def setUp(self):
self.endereco = postmon.endereco('11111111')
def test_cep(self):
self.assertEqual('11111111', self.endereco.cep)
def test_logradouro(self):
self.assertEqual('Logradouro L', self.endereco.logradouro)
def test_complemento(self):
self.assertEqual('Complemento C', self.endereco.complemento)
def test_bairro(self):
self.assertEqual('Bairro B', self.endereco.bairro)
def test_url(self):
self.assertEqual(self.url, self.endereco.url)
def test_str(self):
self.assertEqual('Logradouro L - Complemento C, Bairro B - Cidade C, '
'SP - CEP 11111111', str(self.endereco))
class TestCepIncompleto(unittest.TestCase):
response = {
"cidade": "Cidade C",
"cep": "22222222",
"estado": "SP"
}
@classmethod
def setUpClass(cls):
httpretty.enable()
url = '%s/cep/22222222' % BASE_URL
httpretty.register_uri(httpretty.GET, url,
body=json.dumps(cls.response))
@classmethod
def tearDownClass(cls):
httpretty.disable()
httpretty.reset()
def setUp(self):
self.endereco = postmon.endereco('22222222')
def test_cep(self):
self.assertEqual('22222222', self.endereco.cep)
def test_cidade(self):
self.assertEqual('Cidade C', self.endereco.cidade.nome)
def test_estado(self):
self.assertEqual('SP', self.endereco.estado.uf)
def test_bairro_eh_nulo(self):
self.assertTrue(self.endereco.bairro is None)
def test_str(self):
self.assertEqual('Cidade C, SP - CEP 22222222',
str(self.endereco))
class TestCidade(unittest.TestCase):
url = '%s/cidade/mg/Belo Horizonte' % BASE_URL
response = {
"area_km2": "331,401",
"codigo_ibge": "3106200"
}
@classmethod
def setUpClass(cls):
httpretty.enable()
httpretty.register_uri(httpretty.GET, cls.url,
body=json.dumps(cls.response))
@classmethod
def tearDownClass(cls):
httpretty.disable()
httpretty.reset()
def setUp(self):
self.cidade = postmon.cidade('mg', 'Belo Horizonte')
def test_area_km2(self):
self.assertEqual(Decimal('331.401'), self.cidade.area_km2)
def test_codigo_ibge(self):
self.assertEqual('3106200', self.cidade.codigo_ibge)
def test_url(self):
self.assertEqual(self.url, self.cidade.url)
def test_str(self):
self.assertEqual('Belo Horizonte - MG', str(self.cidade))
class TestEstado(unittest.TestCase):
url = '%s/uf/mg' % BASE_URL
response = {
"area_km2": "586.522,122",
"codigo_ibge": "31",
"nome": "Minas Gerais"
}
@classmethod
def setUpClass(cls):
httpretty.enable()
httpretty.register_uri(httpretty.GET, cls.url,
body=json.dumps(cls.response))
@classmethod
def tearDownClass(cls):
httpretty.disable()
httpretty.reset()
def setUp(self):
self.estado = postmon.estado('mg')
def test_area_km2(self):
self.assertEqual(Decimal('586522.122'), self.estado.area_km2)
def test_codigo_ibge(self):
self.assertEqual('31', self.estado.codigo_ibge)
def test_url(self):
self.assertEqual(self.url, self.estado.url)
class TestErrosEndereco(unittest.TestCase):
@httpretty.activate
def test_404(self):
url = '%s/cep/11111111' % BASE_URL
httpretty.register_uri(httpretty.GET, url, status=404)
r = postmon.endereco('11111111')
self.assertTrue(r is None)
@httpretty.activate
def test_503(self):
url = '%s/cep/22222222' % BASE_URL
httpretty.register_uri(httpretty.GET, url, status=503)
r = postmon.endereco('22222222')
self.assertTrue(r is None)
@mock.patch('postmon.requests.get')
def test_request_exception(self, mock_get):
mock_get.side_effect = requests.RequestException
r = postmon.endereco('11111111')
self.assertTrue(r is None)
def test_unloaded_entity_str(self):
e = postmon.Endereco('11111111')
self.assertEqual('CEP 11111111', str(e))
class TestErrosEstado(unittest.TestCase):
@httpretty.activate
def test_404(self):
url = '%s/uf/xx' % BASE_URL
httpretty.register_uri(httpretty.GET, url, status=404)
r = postmon.estado('xx')
self.assertTrue(r is None)
@httpretty.activate
def test_503(self):
url = '%s/uf/yy' % BASE_URL
httpretty.register_uri(httpretty.GET, url, status=503)
r = postmon.estado('yy')
self.assertTrue(r is None)
@mock.patch('postmon.requests.get')
def test_request_exception(self, mock_get):
mock_get.side_effect = requests.RequestException
r = postmon.estado('xx')
self.assertTrue(r is None)
class TestErrosCidade(unittest.TestCase):
@httpretty.activate
def test_404(self):
url = '%s/cidade/xx/yy' % BASE_URL
httpretty.register_uri(httpretty.GET, url, status=404)
r = postmon.cidade('xx', 'yy')
self.assertTrue(r is None)
@httpretty.activate
def test_503(self):
url = '%s/cidade/yy/zz' % BASE_URL
httpretty.register_uri(httpretty.GET, url, status=503)
r = postmon.cidade('yy', 'zz')
self.assertTrue(r is None)
@mock.patch('postmon.requests.get')
def test_request_exception(self, mock_get):
mock_get.side_effect = requests.RequestException
r = postmon.cidade('xx', 'yy')
self.assertTrue(r is None)
class TestParseAreaKm2(unittest.TestCase):
def f(self, value):
return postmon._parse_area_km2(value)
def assert_f(self, expected, value):
self.assertEqual(expected, self.f(value))
def test_int_value(self):
self.assert_f(Decimal('331.000'), '331')
def test_km2_exact(self):
self.assert_f(Decimal('331.000'), '331,000')
def test_grouping_separators(self):
self.assert_f(Decimal('1331.101'), '1.331,101')
def test_decimal_value(self):
self.assert_f(Decimal('1331.101'), Decimal('1331.101'))
def test_none_value(self):
self.assertTrue(self.f(None) is None)
class TestStatusBeforeFetch(unittest.TestCase):
def test_endereco(self):
e = postmon.Endereco('11111111')
self.assertTrue(e.status is None)
def test_cidade(self):
e = postmon.Cidade('MG', 'Belo Horizonte')
self.assertTrue(e.status is None)
def test_estado(self):
e = postmon.Estado('SP')
self.assertTrue(e.status is None)
class TestRequestClient(unittest.TestCase):
@httpretty.activate
def test_user_agent(self):
url = '%s/uf/xx' % BASE_URL
httpretty.register_uri(httpretty.GET, url, status=404)
e = postmon.Estado('xx')
e.buscar()
ua = e._response.request.headers['User-Agent'].split()
self.assertEqual(postmon.PostmonModel.base_user_agent, ua[0])