|
| 1 | +import gzip |
| 2 | +from json import JSONDecodeError |
| 3 | +from unittest import TestCase, main |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | +from urllib.error import HTTPError |
| 6 | + |
| 7 | +from brutils.ibge.municipality import get_municipality_by_code |
| 8 | + |
| 9 | + |
| 10 | +class TestIBGE(TestCase): |
| 11 | + def test_get_municipality_by_code(self): |
| 12 | + self.assertEqual( |
| 13 | + get_municipality_by_code("3550308"), ("São Paulo", "SP") |
| 14 | + ) |
| 15 | + self.assertEqual( |
| 16 | + get_municipality_by_code("3304557"), ("Rio de Janeiro", "RJ") |
| 17 | + ) |
| 18 | + self.assertEqual(get_municipality_by_code("5208707"), ("Goiânia", "GO")) |
| 19 | + self.assertIsNone(get_municipality_by_code("1234567")) |
| 20 | + |
| 21 | + @patch("brutils.ibge.municipality.urlopen") |
| 22 | + def test_get_municipality_http_error(self, mock): |
| 23 | + mock.side_effect = HTTPError( |
| 24 | + "http://fakeurl.com", 404, "Not Found", None, None |
| 25 | + ) |
| 26 | + result = get_municipality_by_code("342432") |
| 27 | + self.assertIsNone(result) |
| 28 | + |
| 29 | + @patch("brutils.ibge.municipality.urlopen") |
| 30 | + def test_get_municipality_http_error_1(self, mock): |
| 31 | + mock.side_effect = HTTPError( |
| 32 | + "http://fakeurl.com", 401, "Denied", None, None |
| 33 | + ) |
| 34 | + result = get_municipality_by_code("342432") |
| 35 | + self.assertIsNone(result) |
| 36 | + |
| 37 | + @patch("brutils.ibge.municipality.urlopen") |
| 38 | + def test_get_municipality_excpetion(self, mock): |
| 39 | + mock.side_effect = Exception("Erro desconhecido") |
| 40 | + result = get_municipality_by_code("342432") |
| 41 | + self.assertIsNone(result) |
| 42 | + |
| 43 | + @patch("brutils.ibge.municipality.urlopen") |
| 44 | + def test_successfull_decompression(self, mock_urlopen): |
| 45 | + valid_json = '{"nome":"São Paulo","microrregiao":{"mesorregiao":{"UF":{"sigla":"SP"}}}}' |
| 46 | + compressed_data = gzip.compress(valid_json.encode("utf-8")) |
| 47 | + mock_response = MagicMock() |
| 48 | + mock_response.read.return_value = compressed_data |
| 49 | + mock_response.info.return_value.get.return_value = "gzip" |
| 50 | + mock_urlopen.return_value.__enter__.return_value = mock_response |
| 51 | + |
| 52 | + result = get_municipality_by_code("3550308") |
| 53 | + self.assertEqual(result, ("São Paulo", "SP")) |
| 54 | + |
| 55 | + @patch("brutils.ibge.municipality.urlopen") |
| 56 | + def test_successful_json_without_compression(self, mock_urlopen): |
| 57 | + valid_json = '{"nome":"São Paulo","microrregiao":{"mesorregiao":{"UF":{"sigla":"SP"}}}}' |
| 58 | + mock_response = MagicMock() |
| 59 | + mock_response.read.return_value = valid_json |
| 60 | + mock_urlopen.return_value.__enter__.return_value = mock_response |
| 61 | + |
| 62 | + result = get_municipality_by_code("3550308") |
| 63 | + self.assertEqual(result, ("São Paulo", "SP")) |
| 64 | + |
| 65 | + @patch("gzip.GzipFile.read", side_effect=OSError("Erro na descompressão")) |
| 66 | + def test_error_decompression(self, mock_gzip_read): |
| 67 | + result = get_municipality_by_code("3550308") |
| 68 | + self.assertIsNone(result) |
| 69 | + |
| 70 | + @patch( |
| 71 | + "gzip.GzipFile.read", |
| 72 | + side_effect=Exception("Erro desconhecido na descompressão"), |
| 73 | + ) |
| 74 | + def test_error_decompression_generic_exception(self, mock_gzip_read): |
| 75 | + result = get_municipality_by_code("3550308") |
| 76 | + self.assertIsNone(result) |
| 77 | + |
| 78 | + @patch("json.loads", side_effect=JSONDecodeError("error", "city.json", 1)) |
| 79 | + def test_error_json_load(self, mock_json_loads): |
| 80 | + result = get_municipality_by_code("3550308") |
| 81 | + self.assertIsNone(result) |
| 82 | + |
| 83 | + @patch("json.loads", side_effect=KeyError) |
| 84 | + def test_error_json_key_error(self, mock_json_loads): |
| 85 | + result = get_municipality_by_code("3550308") |
| 86 | + self.assertIsNone(result) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + main() |
0 commit comments