@@ -158,8 +158,43 @@ def test_get_municipality_by_code_json_key_error(self, mock_json_loads):
158158 result = get_municipality_by_code ("3550308" )
159159 self .assertIsNone (result )
160160
161- def test_get_code_by_municipality_name_success (self ):
161+ @patch ("brutils.ibge.municipality.urlopen" )
162+ def test_get_code_by_municipality_name_success (self , mock_urlopen ):
162163 """Test successful municipality code retrieval by name and UF."""
164+
165+ def mock_response (url ):
166+ mock_responses = {
167+ f"{ IBGE_BASE_URL } /estados/SC/municipios" : [
168+ {"nome" : "Florianópolis" , "id" : 4205407 },
169+ {"nome" : "Aurora" , "id" : 4201901 },
170+ {"nome" : "Lauro Müller" , "id" : 4209607 },
171+ ],
172+ f"{ IBGE_BASE_URL } /estados/SP/municipios" : [
173+ {"nome" : "São Paulo" , "id" : 3550308 },
174+ ],
175+ f"{ IBGE_BASE_URL } /estados/RJ/municipios" : [
176+ {"nome" : "Rio de Janeiro" , "id" : 3304557 },
177+ ],
178+ f"{ IBGE_BASE_URL } /estados/GO/municipios" : [
179+ {"nome" : "Goiânia" , "id" : 5208707 },
180+ ],
181+ f"{ IBGE_BASE_URL } /estados/BA/municipios" : [
182+ {"nome" : "Conceição do Coité" , "id" : 2908408 },
183+ ],
184+ f"{ IBGE_BASE_URL } /estados/RS/municipios" : [
185+ {"nome" : "Tôrres" , "id" : 4321501 },
186+ ],
187+ f"{ IBGE_BASE_URL } /estados/CE/municipios" : [
188+ {"nome" : "Aurora" , "id" : 2301703 },
189+ ],
190+ }
191+
192+ response_data = mock_responses .get (url , [])
193+ json_data = dumps (response_data ).encode ("utf-8" )
194+ return self ._create_mock_response (json_data )
195+
196+ mock_urlopen .side_effect = mock_response
197+
163198 self .assertEqual (
164199 get_code_by_municipality_name ("Florianópolis" , "sc" ), "4205407"
165200 )
0 commit comments