From 46daef9ddc6d564fcc260a76d1e7284378f3b865 Mon Sep 17 00:00:00 2001 From: Jose Tomas Robles Hahn Date: Thu, 10 Oct 2024 19:33:29 -0300 Subject: [PATCH 1/3] chore(Rut): Improve type annotation of `Rut(value)` `Rut.__init__()` is able to accept values of type `Rut` by converting them to `str`, so a more appropriate type annotation for `value` is `str | Rut` instead of just `str`. --- src/cl_sii/rut/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cl_sii/rut/__init__.py b/src/cl_sii/rut/__init__.py index 4b12b74e..ec787a29 100644 --- a/src/cl_sii/rut/__init__.py +++ b/src/cl_sii/rut/__init__.py @@ -10,6 +10,8 @@ """ +from __future__ import annotations + import itertools import random import re @@ -50,7 +52,7 @@ class Rut: """ - def __init__(self, value: str, validate_dv: bool = False) -> None: + def __init__(self, value: str | Rut, validate_dv: bool = False) -> None: """ Constructor. From 7fc3e8465d02011dde2881c640be943f19a669c5 Mon Sep 17 00:00:00 2001 From: Jose Tomas Robles Hahn Date: Thu, 10 Oct 2024 19:37:43 -0300 Subject: [PATCH 2/3] feat(rut): Add method `validate_dv()` to `Rut` This method is useful when we want to validate the DV of a `Rut` that was instantiated with `validate_dv=False`. --- src/cl_sii/rut/__init__.py | 16 ++++++++++++++++ src/tests/test_rut.py | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/cl_sii/rut/__init__.py b/src/cl_sii/rut/__init__.py index ec787a29..a63fdc0a 100644 --- a/src/cl_sii/rut/__init__.py +++ b/src/cl_sii/rut/__init__.py @@ -139,6 +139,22 @@ def __hash__(self) -> int: # Objects are hashable so they can be used in hashable collections. return hash(self.canonical) + ############################################################################ + # custom methods + ############################################################################ + + def validate_dv(self, raise_exception: bool = False) -> bool: + """ + Whether the "digito verificador" of the RUT is correct. + + :param raise_exception: Whether to raise an exception if validation fails. + :raises ValueError: + """ + is_valid = self.calc_dv(self._digits) == self._dv + if not is_valid and raise_exception: + raise ValueError("RUT's \"digito verificador\" is incorrect.", self.canonical) + return is_valid + ############################################################################ # class methods ############################################################################ diff --git a/src/tests/test_rut.py b/src/tests/test_rut.py index e7dd2e47..505264d3 100644 --- a/src/tests/test_rut.py +++ b/src/tests/test_rut.py @@ -261,6 +261,25 @@ def test__hash__(self) -> None: rut_hash = hash(self.valid_rut_instance.canonical) self.assertEqual(self.valid_rut_instance.__hash__(), rut_hash) + ############################################################################ + # custom methods + ############################################################################ + + def test_validate_dv(self) -> None: + self.assertIs(self.valid_rut_instance.validate_dv(), True) + self.assertIs(self.invalid_rut_instance.validate_dv(), False) + + def test_validate_dv_raises_exception(self) -> None: + try: + self.valid_rut_instance.validate_dv(raise_exception=True) + except ValueError as exc: + self.fail(f'{exc.__class__.__name__} raised') + + with self.assertRaisesRegex( + ValueError, r'''('RUT\\'s "digito verificador" is incorrect.', '6824160-0')''' + ): + self.invalid_rut_instance.validate_dv(raise_exception=True) + ############################################################################ # class methods ############################################################################ From 6321d3a54a420c11bb1a70328423a83c9655edaf Mon Sep 17 00:00:00 2001 From: Jose Tomas Robles Hahn Date: Thu, 10 Oct 2024 19:38:26 -0300 Subject: [PATCH 3/3] chore(rut): Refactor validation of DV in `Rut.__init__()` --- src/cl_sii/rut/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/cl_sii/rut/__init__.py b/src/cl_sii/rut/__init__.py index a63fdc0a..40345573 100644 --- a/src/cl_sii/rut/__init__.py +++ b/src/cl_sii/rut/__init__.py @@ -81,8 +81,7 @@ def __init__(self, value: str | Rut, validate_dv: bool = False) -> None: self._dv = match_groups['dv'] if validate_dv: - if Rut.calc_dv(self._digits) != self._dv: - raise ValueError("RUT's \"digito verificador\" is incorrect.", value) + self.validate_dv(raise_exception=True) ############################################################################ # properties