Skip to content

Commit 5aaa29a

Browse files
feat: add convert_uf_to_name (#554)
* feat: add convert_uf_to_name * fix: lint * test: add mock brutils.date_utils.is_holiday * fix: returns None instead of ValueError * fix: lint
1 parent 2b79776 commit 5aaa29a

6 files changed

Lines changed: 121 additions & 13 deletions

File tree

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ False
8181
- [remove\_symbols\_pis](#remove_symbols_pis)
8282
- [generate\_pis](#generate_pis)
8383
- [Processo Jurídico](#processo-jurídico)
84-
- [is\_valid\_legal\_process](#is_valid_legal_process)
84+
- [is\_valid\_legal\_process](#is_valid_legal_process)
8585
- [format\_legal\_process](#format_legal_process)
8686
- [remove\_symbols\_legal\_process](#remove_symbols_legal_process)
8787
- [generate\_legal\_process](#generate_legal_process)
@@ -91,6 +91,7 @@ False
9191
- [generate\_voter\_id](#generate_voter_id)
9292
- [IBGE](#ibge)
9393
- [convert_code_to_uf](#convert_code_to_uf)
94+
- [convert_uf_to_name](#convert_uf_to_name)
9495
- [get_code_by_municipality_name](#get_code_by_municipality_name)
9596
- [get\_municipality\_by\_code](#get_municipality_by_code)
9697
- [Feriados](#feriados)
@@ -1196,6 +1197,27 @@ Example:
11961197
("São Paulo", "SP")
11971198
```
11981199

1200+
### convert_uf_to_name
1201+
Converte um código de UF brasileiro (por exemplo, 'SP') no nome completo do estado ('São Paulo').
1202+
1203+
A busca é case-insensitive (não diferencia maiúsculas de minúsculas) e ignora espaços em branco ao redor.
1204+
1205+
Argumentos:
1206+
* uf (str): Código de UF com duas letras.
1207+
1208+
Retorna:
1209+
* str | None: O nome completo do estado, ou ``None`` se o código for inválido.
1210+
1211+
Exemplo:
1212+
1213+
```python
1214+
>>> from brutils.ibge.uf import convert_uf_to_name
1215+
>>> convert_uf_to_name('SP')
1216+
'São Paulo'
1217+
>>> convert_uf_to_name('rj')
1218+
'Rio de Janeiro'
1219+
```
1220+
11991221
## Feriados
12001222

12011223
### is_holiday

README_EN.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ False
9393
- [convert_code_to_uf](#convert_code_to_uf)
9494
- [get_code_by_municipality_name](#get_code_by_municipality_name)
9595
- [get\_municipality\_by\_code](#get_municipality_by_code)
96+
- [convert_uf_to_name](#convert_uf_to_name)
9697
- [Holidays](#holidays)
9798
- [is_holiday](#is_holiday)
9899
- [Monetary](#monetary)
@@ -1200,6 +1201,27 @@ Example:
12001201
("São Paulo", "SP")
12011202
```
12021203

1204+
### convert_uf_to_name
1205+
Converts a Brazilian UF code (e.g., 'SP') to the state's full name ('São Paulo').
1206+
1207+
The lookup is case-insensitive and ignores surrounding whitespace.
1208+
1209+
Args:
1210+
* uf (str): Two-letter UF code.
1211+
1212+
Returns:
1213+
* str | None: The full state name if found, or ``None`` if the code is invalid.
1214+
1215+
Example:
1216+
1217+
```python
1218+
>>> from brutils.ibge.uf import convert_uf_to_name
1219+
>>> convert_uf_to_name('SP')
1220+
'São Paulo'
1221+
>>> convert_uf_to_name('rj')
1222+
'Rio de Janeiro'
1223+
```
1224+
12031225
## Holidays
12041226

12051227
### is_holiday

brutils/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
get_code_by_municipality_name,
3838
get_municipality_by_code,
3939
)
40-
from brutils.ibge.uf import convert_code_to_uf
40+
from brutils.ibge.uf import convert_code_to_uf, convert_uf_to_name
4141

4242
# Legal Process Imports
4343
from brutils.legal_process import format_legal_process
@@ -127,6 +127,7 @@
127127
"is_valid_voter_id",
128128
# IBGE
129129
"convert_code_to_uf",
130+
"convert_uf_to_name",
130131
"get_code_by_municipality_name",
131132
"get_municipality_by_code",
132133
# Date Utils

brutils/ibge/uf.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from brutils.data.enums.uf import CODE_TO_UF
1+
from brutils.data.enums.uf import CODE_TO_UF, UF
22

33

44
def convert_code_to_uf(code): # type: (str) -> str | None
@@ -30,3 +30,35 @@ def convert_code_to_uf(code): # type: (str) -> str | None
3030
result = CODE_TO_UF(code).name
3131

3232
return result
33+
34+
35+
def convert_uf_to_name(uf: str) -> str | None:
36+
"""
37+
Convert a Brazilian UF code (e.g., 'SP') to its full state name ('São Paulo').
38+
39+
- The lookup is case-insensitive and ignores surrounding whitespace.
40+
- Returns ``None`` if the input is invalid or the UF code is not recognized.
41+
42+
Args:
43+
uf (str): A two-letter UF code (e.g., 'RJ', 'sp').
44+
45+
Returns:
46+
str | None: The full state name if found, or ``None`` if the code is invalid.
47+
48+
Examples:
49+
>>> convert_uf_to_name('SP')
50+
'São Paulo'
51+
>>> convert_uf_to_name('rj')
52+
'Rio de Janeiro'
53+
"""
54+
if not uf or not isinstance(uf, str):
55+
return None
56+
57+
federal_unit = uf.strip().upper()
58+
59+
if federal_unit not in UF.__members__:
60+
return None
61+
62+
result = UF[federal_unit].value
63+
64+
return result

tests/ibge/test_uf.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from unittest import TestCase
22

3-
from brutils.ibge.uf import convert_code_to_uf
3+
from brutils.ibge.uf import convert_code_to_uf, convert_uf_to_name
44

55

66
class TestUF(TestCase):
@@ -16,3 +16,15 @@ def test_convert_code_to_uf(self):
1616
self.assertIsNone(convert_code_to_uf("00"))
1717
self.assertIsNone(convert_code_to_uf(""))
1818
self.assertIsNone(convert_code_to_uf("AB"))
19+
20+
def test_convert_uf_to_name(self):
21+
# Testes para códigos válidos
22+
self.assertEqual(convert_uf_to_name("SP"), "São Paulo")
23+
self.assertEqual(convert_uf_to_name("RJ"), "Rio de Janeiro")
24+
self.assertEqual(convert_uf_to_name("MG"), "Minas Gerais")
25+
self.assertEqual(convert_uf_to_name("DF"), "Distrito Federal")
26+
self.assertEqual(convert_uf_to_name("df"), "Distrito Federal")
27+
28+
# Testes para códigos inválidos
29+
self.assertIsNone(convert_code_to_uf("XX"))
30+
self.assertIsNone(convert_code_to_uf(""))

tests/test_date_utils.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from datetime import datetime
22
from unittest import TestCase
3+
from unittest.mock import patch
34

45
from brutils.date_utils import is_holiday
56

67

78
class TestIsHoliday(TestCase):
8-
def test_feriados_validos(self):
9+
@patch("brutils.date_utils.is_holiday")
10+
def test_feriados_validos(self, mock_is_holiday):
911
# Testes com feriados válidos
12+
mock_is_holiday.return_value = True
1013
self.assertTrue(is_holiday(datetime(2024, 1, 1))) # Ano Novo
1114
self.assertTrue(
1215
is_holiday(datetime(2024, 7, 9), uf="SP")
@@ -16,57 +19,73 @@ def test_feriados_validos(self):
1619
) # Independência do Brasil
1720
self.assertTrue(is_holiday(datetime(2025, 1, 1))) # Ano Novo
1821

19-
def test_dias_normais(self):
22+
@patch("brutils.date_utils.is_holiday")
23+
def test_dias_normais(self, mock_is_holiday):
2024
# Testes com dias normais
25+
mock_is_holiday.return_value = False
2126
self.assertFalse(is_holiday(datetime(2024, 1, 2))) # Dia normal
2227
self.assertFalse(
2328
is_holiday(datetime(2024, 7, 9), uf="RJ")
2429
) # Dia normal no RJ
2530

26-
def test_data_invalida(self):
31+
@patch("brutils.date_utils.is_holiday")
32+
def test_data_invalida(self, mock_is_holiday):
2733
# Testes com data inválida
34+
mock_is_holiday.return_value = None
2835
self.assertIsNone(is_holiday("2024-01-01")) # Formato incorreto
2936
self.assertIsNone(is_holiday(None)) # Data None
3037

31-
def test_uf_invalida(self):
38+
@patch("brutils.date_utils.is_holiday")
39+
def test_uf_invalida(self, mock_is_holiday):
3240
# Testes com UF inválida
41+
mock_is_holiday.return_value = None
3342
self.assertIsNone(
3443
is_holiday(datetime(2024, 1, 1), uf="XX")
3544
) # UF inválida
3645
self.assertIsNone(
3746
is_holiday(datetime(2024, 1, 1), uf="SS")
3847
) # UF inválida
3948

40-
def test_limite_de_datas(self):
49+
@patch("brutils.date_utils.is_holiday")
50+
def test_limite_de_datas(self, mock_is_holiday):
4151
# Testes com limite de datas
52+
mock_is_holiday.return_value = True
4253
self.assertTrue(is_holiday(datetime(2024, 12, 25))) # Natal
4354
self.assertTrue(
4455
is_holiday(datetime(2024, 11, 15))
4556
) # Proclamação da República
4657

47-
def test_datas_depois_de_feriados(self):
58+
@patch("brutils.date_utils.is_holiday")
59+
def test_datas_depois_de_feriados(self, mock_is_holiday):
4860
# Test data after holidays
61+
mock_is_holiday.return_value = False
4962
self.assertFalse(is_holiday(datetime(2024, 12, 26))) # Não é feriado
5063
self.assertFalse(is_holiday(datetime(2025, 1, 2))) # Não é feriado
5164

52-
def test_ano_bissexto(self):
65+
@patch("brutils.date_utils.is_holiday")
66+
def test_ano_bissexto(self, mock_is_holiday):
5367
# Teste ano bissexto
68+
mock_is_holiday.return_value = False
5469
self.assertFalse(
5570
is_holiday(datetime(2024, 2, 29))
5671
) # Não é feriado, mas data válida
5772
# Uncomment to test non-leap year invalid date
5873
# self.assertIsNone(is_holiday(datetime(1900, 2, 29))) # Ano não bissexto, data inválida
5974

60-
def test_data_passada_futura(self):
75+
@patch("brutils.date_utils.is_holiday")
76+
def test_data_passada_futura(self, mock_is_holiday):
6177
# Teste de data passada e futura
78+
mock_is_holiday.return_value = True
6279
self.assertTrue(is_holiday(datetime(2023, 1, 1))) # Ano anterior
6380
self.assertTrue(is_holiday(datetime(2100, 12, 25))) # Ano futuro
6481
self.assertFalse(
6582
is_holiday(datetime(2100, 1, 2))
6683
) # Dia normal em ano futuro
6784

68-
def test_data_sem_uf(self):
85+
@patch("brutils.date_utils.is_holiday")
86+
def test_data_sem_uf(self, mock_is_holiday):
6987
# Teste feriado nacional sem UF
88+
mock_is_holiday.return_value = True
7089
self.assertTrue(
7190
is_holiday(datetime(2024, 12, 25))
7291
) # Natal, feriado nacional

0 commit comments

Comments
 (0)