Skip to content

Commit

Permalink
test: mockando chamada a API externa de busca de municípios (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
devid8642 authored Feb 7, 2025
1 parent b8e84bd commit bf7e281
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions tests/ibge/test_municipality.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gzip
from json import JSONDecodeError
from json import JSONDecodeError, dumps
from unittest import TestCase, main
from unittest.mock import MagicMock, patch
from urllib.error import HTTPError
Expand All @@ -11,7 +11,47 @@


class TestIBGE(TestCase):
def test_get_municipality_by_code(self):
@patch("brutils.ibge.municipality.urlopen")
def test_get_municipality_by_code(self, mock):
def mock_response(url):
responses = {
"https://servicodados.ibge.gov.br/api/v1/localidades/municipios/3550308": dumps(
{
"nome": "São Paulo",
"microrregiao": {
"mesorregiao": {"UF": {"sigla": "SP"}}
},
}
).encode("utf-8"),
"https://servicodados.ibge.gov.br/api/v1/localidades/municipios/3304557": dumps(
{
"nome": "Rio de Janeiro",
"microrregiao": {
"mesorregiao": {"UF": {"sigla": "RJ"}}
},
}
).encode("utf-8"),
"https://servicodados.ibge.gov.br/api/v1/localidades/municipios/5208707": dumps(
{
"nome": "Goiânia",
"microrregiao": {
"mesorregiao": {"UF": {"sigla": "GO"}}
},
}
).encode("utf-8"),
"https://servicodados.ibge.gov.br/api/v1/localidades/municipios/1234567": b"[]",
}

mock_file = MagicMock()
mock_file.__enter__.return_value = (
mock_file # mock_file is a context manager
)
mock_file.read.return_value = responses.get(url, b"[]")
mock_file.info.return_value = {"Content-Encoding": None}
return mock_file

mock.side_effect = mock_response

self.assertEqual(
get_municipality_by_code("3550308"), ("São Paulo", "SP")
)
Expand Down

0 comments on commit bf7e281

Please sign in to comment.