Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimenunes committed Sep 15, 2024
1 parent e94a47a commit 8970f19
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions tests/ibge/municipality.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import gzip
from unittest import TestCase, main
from unittest.mock import MagicMock, patch

from requests import HTTPError

from brutils.ibge.municipality import get_municipality_by_code

Expand All @@ -14,6 +18,63 @@ def test_get_municipality_by_code(self):
self.assertEqual(get_municipality_by_code("5208707"), ("Goiânia", "GO"))
self.assertIsNone(get_municipality_by_code("1234567"))

from unittest import TestCase, main

@patch("brutils.ibge.municipality.urlopen")
def test_get_municipality_by_code_success(self, mock_urlopen):
# Mock data for a valid municipality code
valid_json = '{"nome":"São Paulo","microrregiao":{"mesorregiao":{"UF":{"sigla":"SP"}}}'
compressed_data = gzip.compress(valid_json.encode("utf-8"))

mock_response = MagicMock()
mock_response.read.return_value = compressed_data
mock_urlopen.return_value = mock_response

self.assertEqual(
get_municipality_by_code("3550308"), ("São Paulo", "SP")
)
self.assertEqual(
get_municipality_by_code("3304557"), ("Rio de Janeiro", "RJ")
)
self.assertEqual(get_municipality_by_code("5208707"), ("Goiânia", "GO"))

@patch("brutils.ibge.municipality.urlopen")
def test_get_municipality_by_code_empty_data(self, mock_urlopen):
# Mock data for an empty response
empty_json = "[]"
compressed_data = gzip.compress(empty_json.encode("utf-8"))

mock_response = MagicMock()
mock_response.read.return_value = compressed_data
mock_urlopen.return_value = mock_response

self.assertIsNone(get_municipality_by_code("1234567"))

@patch("brutils.ibge.municipality.urlopen")
def test_get_municipality_by_code_http_error(self, mock_urlopen):
# Mock HTTPError
mock_urlopen.side_effect = HTTPError(
url="https://servicodados.ibge.gov.br/api/v1/localidades/municipios/1234567",
code=404,
msg="Not Found",
hdrs=None,
fp=None,
)

self.assertIsNone(get_municipality_by_code("1234567"))

@patch("brutils.ibge.municipality.urlopen")
def test_get_municipality_by_code_unexpected_error(self, mock_urlopen):
# Mock an unexpected error during URL open
mock_urlopen.side_effect = Exception("Some unexpected error")

with self.assertRaises(Exception):
get_municipality_by_code("1234567")


if __name__ == "__main__":
main()


if __name__ == "__main__":
main()

0 comments on commit 8970f19

Please sign in to comment.