Skip to content

Commit

Permalink
Adicionando utilitário convert_uf_to_text
Browse files Browse the repository at this point in the history
  • Loading branch information
low-ise committed Oct 6, 2024
1 parent 9972d18 commit dc26560
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 20 deletions.
7 changes: 7 additions & 0 deletions brutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
# Email Import
from brutils.email import is_valid as is_valid_email

# UF Imports
from brutils.ibge.uf import (
convert_uf_to_text,
)

# Legal Process Imports
from brutils.legal_process import (
format_legal_process,
Expand Down Expand Up @@ -161,6 +166,8 @@
"generate_pis",
"is_valid_pis",
"remove_symbols_pis",
# UF
"convert_uf_to_text",
# Voter ID
"format_voter_id",
"generate_voter_id",
Expand Down
29 changes: 29 additions & 0 deletions brutils/ibge/uf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
def convert_uf_to_text(uf): # type: (str) -> str | None
"""
Converts a given Brazilian state code (UF) to its full state name.
This function takes a 2-letter UF code and returns the corresponding full state name.
It handles all Brazilian states and the Federal District.
Args:
uf (str): The 2-letter UF code to be converted.
Returns:
str or None: The full name of the state corresponding to the UF code,
or None if the UF code is invalid.
Example:
>>> convert_uf_to_text('SP')
"São Paulo"
>>> convert_uf_to_text('RJ')
"Rio de Janeiro"
>>> convert_uf_to_text('XX')
None
"""

from brutils.data.enums.uf import UF

try:
return UF[uf.upper()].value
except KeyError:
return None
40 changes: 20 additions & 20 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ classifiers = [

[tool.poetry.dependencies]
python = "^3.8.1"
ruff = "^0.6.9"

[tool.poetry.group.test.dependencies]
coverage = "^7.2.7"
Expand Down
24 changes: 24 additions & 0 deletions tests/ibge/test_uf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from unittest import TestCase

from brutils.ibge.uf import convert_uf_to_text


class TestUF(TestCase):
def test_convert_uf_to_text(self):
# Testes para códigos válidos
self.assertEqual(convert_uf_to_text("SP"), "São Paulo")
self.assertEqual(convert_uf_to_text("RJ"), "Rio de Janeiro")
self.assertEqual(convert_uf_to_text("MG"), "Minas Gerais")
self.assertEqual(convert_uf_to_text("DF"), "Distrito Federal")
self.assertEqual(convert_uf_to_text("df"), "Distrito Federal")

# Testes para códigos inválidos
self.assertIsNone(convert_uf_to_text("XX")) # Código não existe
self.assertIsNone(convert_uf_to_text("")) # Código vazio

# Implementar mais casos de teste aqui
# Testes com espaços em branco
self.assertIsNone(convert_uf_to_text(" SP ")) # UF com espaços ao redor
self.assertIsNone(convert_uf_to_text(" ")) # Apenas espaços
self.assertIsNone(convert_uf_to_text("S")) # Apenas 1 letra
self.assertIsNone(convert_uf_to_text("SPX")) # Mais de 2 letras

0 comments on commit dc26560

Please sign in to comment.