From dc265604cb82957cb1c70fcc3b2ec277231c9490 Mon Sep 17 00:00:00 2001 From: L0u Date: Sun, 6 Oct 2024 10:11:02 -0300 Subject: [PATCH] =?UTF-8?q?Adicionando=20utilit=C3=A1rio=20convert=5Fuf=5F?= =?UTF-8?q?to=5Ftext?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- brutils/__init__.py | 7 +++++++ brutils/ibge/uf.py | 29 +++++++++++++++++++++++++++++ poetry.lock | 40 ++++++++++++++++++++-------------------- pyproject.toml | 1 + tests/ibge/test_uf.py | 24 ++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 20 deletions(-) create mode 100644 brutils/ibge/uf.py create mode 100644 tests/ibge/test_uf.py diff --git a/brutils/__init__.py b/brutils/__init__.py index cbfa194c..168da324 100644 --- a/brutils/__init__.py +++ b/brutils/__init__.py @@ -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, @@ -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", diff --git a/brutils/ibge/uf.py b/brutils/ibge/uf.py new file mode 100644 index 00000000..7c01ba5d --- /dev/null +++ b/brutils/ibge/uf.py @@ -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 diff --git a/poetry.lock b/poetry.lock index f7212dd2..8e096a72 100644 --- a/poetry.lock +++ b/poetry.lock @@ -86,32 +86,32 @@ toml = ["tomli"] [[package]] name = "ruff" -version = "0.6.5" +version = "0.6.9" description = "An extremely fast Python linter and code formatter, written in Rust." optional = false python-versions = ">=3.7" files = [ - {file = "ruff-0.6.5-py3-none-linux_armv6l.whl", hash = "sha256:7e4e308f16e07c95fc7753fc1aaac690a323b2bb9f4ec5e844a97bb7fbebd748"}, - {file = "ruff-0.6.5-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:932cd69eefe4daf8c7d92bd6689f7e8182571cb934ea720af218929da7bd7d69"}, - {file = "ruff-0.6.5-py3-none-macosx_11_0_arm64.whl", hash = "sha256:3a8d42d11fff8d3143ff4da41742a98f8f233bf8890e9fe23077826818f8d680"}, - {file = "ruff-0.6.5-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a50af6e828ee692fb10ff2dfe53f05caecf077f4210fae9677e06a808275754f"}, - {file = "ruff-0.6.5-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:794ada3400a0d0b89e3015f1a7e01f4c97320ac665b7bc3ade24b50b54cb2972"}, - {file = "ruff-0.6.5-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:381413ec47f71ce1d1c614f7779d88886f406f1fd53d289c77e4e533dc6ea200"}, - {file = "ruff-0.6.5-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:52e75a82bbc9b42e63c08d22ad0ac525117e72aee9729a069d7c4f235fc4d276"}, - {file = "ruff-0.6.5-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:09c72a833fd3551135ceddcba5ebdb68ff89225d30758027280968c9acdc7810"}, - {file = "ruff-0.6.5-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:800c50371bdcb99b3c1551d5691e14d16d6f07063a518770254227f7f6e8c178"}, - {file = "ruff-0.6.5-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8e25ddd9cd63ba1f3bd51c1f09903904a6adf8429df34f17d728a8fa11174253"}, - {file = "ruff-0.6.5-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:7291e64d7129f24d1b0c947ec3ec4c0076e958d1475c61202497c6aced35dd19"}, - {file = "ruff-0.6.5-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:9ad7dfbd138d09d9a7e6931e6a7e797651ce29becd688be8a0d4d5f8177b4b0c"}, - {file = "ruff-0.6.5-py3-none-musllinux_1_2_i686.whl", hash = "sha256:005256d977021790cc52aa23d78f06bb5090dc0bfbd42de46d49c201533982ae"}, - {file = "ruff-0.6.5-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:482c1e6bfeb615eafc5899127b805d28e387bd87db38b2c0c41d271f5e58d8cc"}, - {file = "ruff-0.6.5-py3-none-win32.whl", hash = "sha256:cf4d3fa53644137f6a4a27a2b397381d16454a1566ae5335855c187fbf67e4f5"}, - {file = "ruff-0.6.5-py3-none-win_amd64.whl", hash = "sha256:3e42a57b58e3612051a636bc1ac4e6b838679530235520e8f095f7c44f706ff9"}, - {file = "ruff-0.6.5-py3-none-win_arm64.whl", hash = "sha256:51935067740773afdf97493ba9b8231279e9beef0f2a8079188c4776c25688e0"}, - {file = "ruff-0.6.5.tar.gz", hash = "sha256:4d32d87fab433c0cf285c3683dd4dae63be05fd7a1d65b3f5bf7cdd05a6b96fb"}, + {file = "ruff-0.6.9-py3-none-linux_armv6l.whl", hash = "sha256:064df58d84ccc0ac0fcd63bc3090b251d90e2a372558c0f057c3f75ed73e1ccd"}, + {file = "ruff-0.6.9-py3-none-macosx_10_12_x86_64.whl", hash = "sha256:140d4b5c9f5fc7a7b074908a78ab8d384dd7f6510402267bc76c37195c02a7ec"}, + {file = "ruff-0.6.9-py3-none-macosx_11_0_arm64.whl", hash = "sha256:53fd8ca5e82bdee8da7f506d7b03a261f24cd43d090ea9db9a1dc59d9313914c"}, + {file = "ruff-0.6.9-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:645d7d8761f915e48a00d4ecc3686969761df69fb561dd914a773c1a8266e14e"}, + {file = "ruff-0.6.9-py3-none-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:eae02b700763e3847595b9d2891488989cac00214da7f845f4bcf2989007d577"}, + {file = "ruff-0.6.9-py3-none-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:7d5ccc9e58112441de8ad4b29dcb7a86dc25c5f770e3c06a9d57e0e5eba48829"}, + {file = "ruff-0.6.9-py3-none-manylinux_2_17_ppc64.manylinux2014_ppc64.whl", hash = "sha256:417b81aa1c9b60b2f8edc463c58363075412866ae4e2b9ab0f690dc1e87ac1b5"}, + {file = "ruff-0.6.9-py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:3c866b631f5fbce896a74a6e4383407ba7507b815ccc52bcedabb6810fdb3ef7"}, + {file = "ruff-0.6.9-py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:7b118afbb3202f5911486ad52da86d1d52305b59e7ef2031cea3425142b97d6f"}, + {file = "ruff-0.6.9-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a67267654edc23c97335586774790cde402fb6bbdb3c2314f1fc087dee320bfa"}, + {file = "ruff-0.6.9-py3-none-musllinux_1_2_aarch64.whl", hash = "sha256:3ef0cc774b00fec123f635ce5c547dac263f6ee9fb9cc83437c5904183b55ceb"}, + {file = "ruff-0.6.9-py3-none-musllinux_1_2_armv7l.whl", hash = "sha256:12edd2af0c60fa61ff31cefb90aef4288ac4d372b4962c2864aeea3a1a2460c0"}, + {file = "ruff-0.6.9-py3-none-musllinux_1_2_i686.whl", hash = "sha256:55bb01caeaf3a60b2b2bba07308a02fca6ab56233302406ed5245180a05c5625"}, + {file = "ruff-0.6.9-py3-none-musllinux_1_2_x86_64.whl", hash = "sha256:925d26471fa24b0ce5a6cdfab1bb526fb4159952385f386bdcc643813d472039"}, + {file = "ruff-0.6.9-py3-none-win32.whl", hash = "sha256:eb61ec9bdb2506cffd492e05ac40e5bc6284873aceb605503d8494180d6fc84d"}, + {file = "ruff-0.6.9-py3-none-win_amd64.whl", hash = "sha256:785d31851c1ae91f45b3d8fe23b8ae4b5170089021fbb42402d811135f0b7117"}, + {file = "ruff-0.6.9-py3-none-win_arm64.whl", hash = "sha256:a9641e31476d601f83cd602608739a0840e348bda93fec9f1ee816f8b6798b93"}, + {file = "ruff-0.6.9.tar.gz", hash = "sha256:b076ef717a8e5bc819514ee1d602bbdca5b4420ae13a9cf61a0c0a4f53a2baa2"}, ] [metadata] lock-version = "2.0" python-versions = "^3.8.1" -content-hash = "989d447f5ac999811a4e25ed6b1fc0749aa127fb45290a675b17e652d85b4123" +content-hash = "d8b14f498f22110c956599439555c577506734f885bad5b50b2ea6ba8b64596b" diff --git a/pyproject.toml b/pyproject.toml index e70382e8..a9435a86 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/tests/ibge/test_uf.py b/tests/ibge/test_uf.py new file mode 100644 index 00000000..208a9ade --- /dev/null +++ b/tests/ibge/test_uf.py @@ -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