From f7b43af1513168a161e199da25576746ec8ce568 Mon Sep 17 00:00:00 2001 From: Diogo Zani Date: Sat, 28 Nov 2026 12:58:54 -0300 Subject: [PATCH] fix: mocka urlopen nos testes de erro gzip do ibge que quebravam o CI --- CHANGELOG.md | 1 + tests/ibge/test_municipality.py | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74c1db3f..312d73e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Testes de erro de descompressão gzip do `ibge` faziam requisição HTTP real à API do IBGE e quebravam o CI de todos os PRs [#811](https://github.com/brazilian-utils/python/issues/811) - Cálculo do dígito verificador em `is_valid_legal_process` e `generate_legal_process`, que rejeitava números de processo reais e gerava números inválidos [#785](https://github.com/brazilian-utils/python/pull/785) ## [2.5.0] - 2026-06-30 diff --git a/tests/ibge/test_municipality.py b/tests/ibge/test_municipality.py index 7e0beab5..73466c6c 100644 --- a/tests/ibge/test_municipality.py +++ b/tests/ibge/test_municipality.py @@ -131,18 +131,36 @@ def test_get_municipality_by_code_no_compression(self, mock_urlopen): result = get_municipality_by_code("3550308") self.assertEqual(result, ("São Paulo", "SP")) + @patch("brutils.ibge.municipality.urlopen") @patch("gzip.GzipFile.read", side_effect=OSError("Erro na descompressão")) - def test_get_municipality_by_code_gzip_error(self, mock_gzip_read): + def test_get_municipality_by_code_gzip_error( + self, mock_gzip_read, mock_urlopen + ): """Test gzip decompression error handling.""" + valid_json = dumps(VALID_MUNICIPALITY_DATA["SAO_PAULO"]) + compressed_data = gzip.compress(valid_json.encode("utf-8")) + mock_urlopen.return_value = self._create_mock_response( + compressed_data, "gzip" + ) + result = get_municipality_by_code("3550308") self.assertIsNone(result) + @patch("brutils.ibge.municipality.urlopen") @patch( "gzip.GzipFile.read", side_effect=Exception("Erro desconhecido na descompressão"), ) - def test_get_municipality_by_code_gzip_generic_error(self, mock_gzip_read): + def test_get_municipality_by_code_gzip_generic_error( + self, mock_gzip_read, mock_urlopen + ): """Test generic gzip error handling.""" + valid_json = dumps(VALID_MUNICIPALITY_DATA["SAO_PAULO"]) + compressed_data = gzip.compress(valid_json.encode("utf-8")) + mock_urlopen.return_value = self._create_mock_response( + compressed_data, "gzip" + ) + result = get_municipality_by_code("3550308") self.assertIsNone(result)