From 314720f111c6a7258046f0c07e9cea5c4fb75575 Mon Sep 17 00:00:00 2001 From: Camila Maia Date: Wed, 18 Oct 2023 15:40:04 +0200 Subject: [PATCH 1/2] Arrumando checkboxes no template de PR --- .github/pull_request_template.md | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index e79e7f42..9b419a3c 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -8,21 +8,19 @@ - Exemplo 2: Descrição da segunda alteração. - ...--> - ## Checklist de Revisão -- [] Eu li o [Contributing.md](https://github.com/brazilian-utils/brutils-python/blob/main/CONTRIBUTING.md) -- [] Os testes foram adicionados ou atualizados para refletir as mudanças (se aplicável). -- [] Foi adicionada uma entrada no changelog / Meu PR não necessita de uma nova entrada no changelog. -- [] A [documentação](https://github.com/brazilian-utils/brutils-python/blob/main/README.md) em português foi atualizada ou criada, se necessário. -- [] Se feita a documentação, a atualização do [arquivo em inglês](https://github.com/brazilian-utils/brutils-python/blob/main/README_EN.md). -- [] Eu documentei as minhas mudanças no código, adicionando docstrings e comentários. [Instruções](https://github.com/brazilian-utils/brutils-python/blob/main/CONTRIBUTING.md#8-fa%C3%A7a-as-suas-altera%C3%A7%C3%B5es) -- [] O código segue as diretrizes de estilo e padrões de codificação do projeto. -- [] Todos os testes passam. [Instruções](https://github.com/brazilian-utils/brutils-python/blob/main/CONTRIBUTING.md#testes) -- [] O Pull Request foi testado localmente. [Instruções](https://github.com/brazilian-utils/brutils-python/blob/main/CONTRIBUTING.md#7-execute-o-brutils-localmente) -- [] Não há conflitos de mesclagem. - +- [ ] Eu li o [Contributing.md](https://github.com/brazilian-utils/brutils-python/blob/main/CONTRIBUTING.md) +- [ ] Os testes foram adicionados ou atualizados para refletir as mudanças (se aplicável). +- [ ] Foi adicionada uma entrada no changelog / Meu PR não necessita de uma nova entrada no changelog. +- [ ] A [documentação](https://github.com/brazilian-utils/brutils-python/blob/main/README.md) em português foi atualizada ou criada, se necessário. +- [ ] Se feita a documentação, a atualização do [arquivo em inglês](https://github.com/brazilian-utils/brutils-python/blob/main/README_EN.md). +- [ ] Eu documentei as minhas mudanças no código, adicionando docstrings e comentários. [Instruções](https://github.com/brazilian-utils/brutils-python/blob/main/CONTRIBUTING.md#8-fa%C3%A7a-as-suas-altera%C3%A7%C3%B5es) +- [ ] O código segue as diretrizes de estilo e padrões de codificação do projeto. +- [ ] Todos os testes passam. [Instruções](https://github.com/brazilian-utils/brutils-python/blob/main/CONTRIBUTING.md#testes) +- [ ] O Pull Request foi testado localmente. [Instruções](https://github.com/brazilian-utils/brutils-python/blob/main/CONTRIBUTING.md#7-execute-o-brutils-localmente) +- [ ] Não há conflitos de mesclagem. ## Comentários Adicionais (opcional) @@ -30,5 +28,4 @@ ## Issue Relacionada - Closes # From 072f6738d5d4792d1dc20e93f4c8b43c2fbd76f3 Mon Sep 17 00:00:00 2001 From: Camila Maia Date: Wed, 18 Oct 2023 16:21:16 +0200 Subject: [PATCH 2/2] Arrumando imports e agregando is_valid's para telefone --- README.md | 61 +++++----- README_EN.md | 49 ++++---- brutils/__init__.py | 24 ++-- brutils/license_plate.py | 12 +- brutils/phone.py | 91 ++++++++------ tests/test_license_plate.py | 24 ++-- tests/test_phone.py | 228 ++++++++++++++++++++++++------------ 7 files changed, 294 insertions(+), 195 deletions(-) diff --git a/README.md b/README.md index dad270c3..780395da 100644 --- a/README.md +++ b/README.md @@ -61,8 +61,6 @@ False - [Phone](#phone) - [format_phone](#format_phone) - [is_valid_phone](#is_valid_phone) - - [is_valid_mobile_phone](#is_valid_mobile_phone) - - [is_valid_landline_phone](#is_valid_landline_phone) - [remove_symbols_phone](#remove_symbols_phone) - [generate_mobile_phone](#generate_mobile_phone) - [generate_landline_phone](#generate_landline_phone) @@ -236,39 +234,40 @@ Formata um numero de telefone recebido para um formato apresentavel humanamente. ### is_valid_phone -Verifica se o número de telefone é valido, podendo ser telefone fixo ou celular. Apenas números, -com DDD e sem o prefixo internacional, formatados como string. -***Exemplo: +55 48 9999 9999 deve ser chamado como is_valid_phone('4899999999').*** Esta função -valida apenas números de telefone brasileiros e não verifica se o número realmente existe. +Retornar se um número de telefone brasileiro é valido. +Não verifica se o número realmente existe. -```python ->>> from brutils import is_valid_phone ->>> is_valid_phone('11994029275') -True +``` +is_valid_phone(phone_number, type) ``` -### is_valid_mobile_phone - -Verifica se o número de celular é valido. Apenas números, com DDD e sem o prefixo internacional, -formatados como string.***Exemplo: +55 48 9999 9999 ficaria '4899999999'.*** Esta função valida -apenas números de celular brasileiros e não verifica se o número realmente existe. +Argumentos + - phone_number (str): + * o número de telefone a ser validado + * obrigatório + * apenas dígitos, sem símbolos + * sem o código do país + * deve incluir o número de DDD com dois dígitos + * exemplo: '+55 48 9999 9999' deve ser utilizado como '4899999999' -```python ->>> from brutils import is_valid_mobile_phone ->>> is_valid_mobile_phone('11994029275') -True -``` + - type (str): + * 'mobile' para validar apenas números de celular + * 'landline' para validar apenas números de telefone fixo + * caso não especificado, valida para um para o outro. + * opcional -### is_valid_landline_phone +Retorno + - bool: True se o número é válido, False caso contrário. -Verifica se o número de telefone fixo é valido. Apenas números, com DDD e sem o prefixo -internacional, formatados como string. ***Exemplo: +55 48 3333 3333 ficaria '4833333333'.*** -Esta função valida apenas números de telefones fixos brasileiros e não verifica se o número -realmente existe. +Example ```python ->>> from brutils import is_valid_landline_phone ->>> is_valid_landline_phone('1938814933') +>>> from brutils import is_valid_phone +>>> is_valid_phone('11994029275') +True +>>> is_valid_mobile_phone('11994029275', 'mobile') +True +>>> is_valid_landline_phone('1938814933', 'landline') True ``` @@ -292,7 +291,7 @@ Gera um número de telefone móvel válido e aleatório '63996408441' >>> generate_mobile_phone() '78964850019' ->>> generate_mobile_phone() +>>> generate_mobile_phone() '53924997638' ``` @@ -317,7 +316,7 @@ Gera um número de telefone fixo válido ### is_valid_email -Verificar se uma string corresponde a um e-mail válido. As regras para validar um endereço de e-mail geralmente seguem as especificações definidas pelo RFC 5322 (atualizado pelo RFC 5322bis), que é o padrão amplamente aceito para formatos de endereços de e-mail. +Verificar se uma string corresponde a um e-mail válido. As regras para validar um endereço de e-mail geralmente seguem as especificações definidas pelo RFC 5322 (atualizado pelo RFC 5322bis), que é o padrão amplamente aceito para formatos de endereços de e-mail. ```python from brutils import is_valid_email @@ -368,7 +367,7 @@ False ### is_valid_license_plate_mercosul Verifica se uma string correspondente a um número da placa é válido, conforme as novas -normas do Mercosul, isto é, seguindo o padrão LLLNLNN. +normas do Mercosul, isto é, seguindo o padrão LLLNLNN. ***Exemplo: ABC4E67.*** ```python @@ -617,4 +616,4 @@ Vamos construir juntos! 🚀🚀 [github-discussions-doc]: https://docs.github.com/pt/discussions [github-discussions]: https://github.com/brazilian-utils/brutils-python/discussions [github-issues-doc]: https://docs.github.com/pt/issues/tracking-your-work-with-issues/creating-an-issue -[github-issues]: https://github.com/brazilian-utils/brutils-python/issues \ No newline at end of file +[github-issues]: https://github.com/brazilian-utils/brutils-python/issues diff --git a/README_EN.md b/README_EN.md index c3d38d01..11bf077b 100644 --- a/README_EN.md +++ b/README_EN.md @@ -60,8 +60,6 @@ False - [Phone](#phone) - [format_phone](#format_phone) - [is_valid_phone](#is_valid_phone) - - [is_valid_mobile_phone](#is_valid_mobile_phone) - - [is_valid_landline_phone](#is_valid_landline_phone) - [remove_symbols_phone](#remove_symbols_phone) - [generate_mobile_phone](#generate_mobile_phone) - [generate_landline_phone](#generate_landline_phone) @@ -239,31 +237,40 @@ Formats a given phone number to a human-presentable format. If it is not a valid ### is_valid_phone -Check if phone number is valid, can be landline or mobile phone. Numbers only, with area code (DDD) and without the international prefix, formatted as a string. ***For example: +55 48 9999 9999 would become '4899999999'*** This function validates only Brazilian phone numbers and does not verify if the number actually exists. +Return whether a Brazilian phone number is valid. +It does not verify if the number actually exists. -```python ->>> from brutils import is_valid_phone ->>> is_valid_phone('11994029275') -True +``` +is_valid_phone(phone_number, type) ``` -### is_valid_mobile_phone - -Check if mobile phone number is valid. Numbers only, with area code (DDD) and without the international prefix, formatted as a string. ***For example: +55 48 9999 9999 would become '4899999999'*** This function validates only Brazilian phone numbers and does not verify if the number actually exists. +Args + - phone_number: + * the phone number to be validated + * mandatory + * digits only, no symbols + * without the country code + * should include the area code (DDD) with two digits + * example: '+55 48 9999 9999' should be used as '4899999999' -```python ->>> from brutils import is_valid_mobile_phone ->>> is_valid_mobile_phone('11994029275') -True -``` + - type: + * 'mobile' to validate only mobile numbers + * 'landline' to validate only landline phone numbers + * if not specified, it validates for either. + * optional -### is_valid_landline_phone +Return + - bool: True if the phone number is valid. False otherwise. -Check if landline phone number is valid. Numbers only, with area code (DDD) and without the international prefix, formatted as a string. ***For example: +55 48 3333 3333 would become '4833333333'.*** This function validates only Brazilian phone numbers and does not verify if the number actually exists. +Example ```python ->>> from brutils import is_valid_landline_phone ->>> is_valid_landline_phone('1938814933') +>>> from brutils import is_valid_phone +>>> is_valid_phone('11994029275') +True +>>> is_valid_mobile_phone('11994029275', 'mobile') +True +>>> is_valid_landline_phone('1938814933', 'landline') True ``` @@ -287,7 +294,7 @@ Generates a valid and random mobile phone number '63996408441' >>> generate_mobile_phone() '78964850019' ->>> generate_mobile_phone() +>>> generate_mobile_phone() '53924997638' ``` @@ -625,4 +632,4 @@ Let's build it together 🚀🚀 [github-discussions-doc]: https://docs.github.com/en/discussions [github-discussions]: https://github.com/brazilian-utils/brutils-python/discussions [github-issues-doc]: https://docs.github.com/en/issues/tracking-your-work-with-issues/creating-an-issue -[github-issues]: https://github.com/brazilian-utils/brutils-python/issues \ No newline at end of file +[github-issues]: https://github.com/brazilian-utils/brutils-python/issues diff --git a/brutils/__init__.py b/brutils/__init__.py index 9efb6791..e5079494 100644 --- a/brutils/__init__.py +++ b/brutils/__init__.py @@ -8,45 +8,43 @@ ) from brutils.cnpj import ( - is_valid as is_valid_cnpj, format_cnpj, - remove_symbols as remove_symbols_cnpj, generate as generate_cnpj, + is_valid as is_valid_cnpj, + remove_symbols as remove_symbols_cnpj, ) from brutils.cep import ( - is_valid as is_valid_cep, format_cep, generate as generate_cep, + is_valid as is_valid_cep, remove_symbols as remove_symbols_cep, ) from brutils.phone import ( - is_valid_landline as is_valid_landline_phone, - is_valid_mobile as is_valid_mobile_phone, - is_valid as is_valid_phone, format_phone, - generate_mobile_phone, generate_landline_phone, + generate_mobile_phone, + is_valid as is_valid_phone, ) from brutils.license_plate import ( - is_valid_mercosul as is_valid_license_plate_mercosul, - is_valid_license_plate_old_format, - is_valid as is_valid_license_plate, convert_to_mercosul as convert_license_plate_to_mercosul, format as format_license_plate, - remove_symbols as remove_symbols_license_plate, get_format as get_license_plate_format, + is_valid as is_valid_license_plate, + is_valid_old_format as is_valid_license_plate_old_format, + is_valid_mercosul as is_valid_license_plate_mercosul, + remove_symbols as remove_symbols_license_plate, ) from brutils.email import is_valid as is_valid_email from brutils.pis import ( - is_valid as is_valid_pis, + format_pis, generate as generate_pis, + is_valid as is_valid_pis, remove_symbols as remove_symbols_pis, - format_pis, ) from brutils.legal_process import ( diff --git a/brutils/license_plate.py b/brutils/license_plate.py index 71f7b0ae..e97d286a 100644 --- a/brutils/license_plate.py +++ b/brutils/license_plate.py @@ -16,7 +16,7 @@ def convert_to_mercosul(license_plate: str) -> Optional[str]: Ex: ABC4567 - > ABC4F67 ABC4*67 - > 'None' """ - if not is_valid_license_plate_old_format(license_plate): + if not is_valid_old_format(license_plate): return None digits = [letter for letter in license_plate.upper()] @@ -34,7 +34,7 @@ def format(license_plate: str) -> Optional[str]: ABC123 - > 'None' """ license_plate = license_plate.upper() - if is_valid_license_plate_old_format(license_plate): + if is_valid_old_format(license_plate): return license_plate[0:3] + "-" + license_plate[3:] elif is_valid_mercosul(license_plate): return license_plate.upper() @@ -50,12 +50,12 @@ def is_valid(license_plate: str) -> bool: Checks wheter license plate is valid according to the old format and the Mercosul one. """ - return is_valid_license_plate_old_format( + return is_valid_old_format(license_plate) or is_valid_mercosul( license_plate - ) or is_valid_mercosul(license_plate) + ) -def is_valid_license_plate_old_format(license_plate: str) -> bool: +def is_valid_old_format(license_plate: str) -> bool: """ Checks whether a string matches the old format of Brazilian license plate. """ @@ -105,7 +105,7 @@ def get_format(license_plate: str) -> Optional[str]: value[None]: For invalid license plates """ - if is_valid_license_plate_old_format(license_plate): + if is_valid_old_format(license_plate): return "LLLNNNN" if is_valid_mercosul(license_plate): diff --git a/brutils/phone.py b/brutils/phone.py index 12f818fa..c376054f 100644 --- a/brutils/phone.py +++ b/brutils/phone.py @@ -35,45 +35,28 @@ def format_phone(phone): # type: (str) -> str ############ -def is_valid_landline(phone_number): # type: (str) -> bool +def is_valid(phone_number, type=None): # type: (str, str) -> bool """ - Returns whether or not the verifying first 3 digits are a - match.This function validates only Brazilian landline numbers - and does not verify if the number actually exists. - Input should be a digit string of proper length. + Returns if a Brazilian phone number is valid. + It does not verify if the number actually exists. - """ - pattern = re.compile(r"^[1-9][1-9][2-5]\d{7}$") - return ( - isinstance(phone_number, str) - and re.match(pattern, phone_number) is not None - ) - - -def is_valid_mobile(phone_number): # type: (str) -> bool - """ - Returns whether or not the verifying first 3 digits are a - match.This function validates only Brazilian mobile numbers - and does not verify if the number actually exists. - Input should be a digit string of proper length. + Args: + phone_number (str): The phone number to validate. + Only digits, without country code. + It should include two digits DDD. + type (str): "mobile" or "landline". + If not specified, checks for one or another. + Returns: + bool: True if the phone number is valid. False otherwise. """ - pattern = re.compile(r"^[1-9][1-9][9]\d{8}$") - return ( - isinstance(phone_number, str) - and re.match(pattern, phone_number) is not None - ) - -def is_valid(phone_number): # type: (str) -> bool - """ - Returns whether or not the verifying first 3 digits are a - match.This function validates only Brazilian phone numbers - and does not verify if the number actually exists. - Input should be a digit string of proper length. + if type == "landline": + return _is_valid_landline(phone_number) + if type == "mobile": + return _is_valid_mobile(phone_number) - """ - return is_valid_landline(phone_number) or is_valid_mobile(phone_number) + return _is_valid_landline(phone_number) or _is_valid_mobile(phone_number) def remove_symbols_phone(phone_number): # type: (str) -> str @@ -116,3 +99,45 @@ def generate_landline_phone(): # type () -> str """ ddd = _generate_ddd_number() return f"{ddd}{randint(2,5)}{str(randint(0,9999999)).zfill(7)}" + + +def _is_valid_landline(phone_number): # type: (str) -> bool + """ + Returns if a Brazilian landline number is valid. + It does not verify if the number actually exists. + + Args: + phone_number (str): The landline number to validate. + Only digits, without country code. + It should include two digits DDD. + + Returns: + bool: True if the phone number is valid. False otherwise. + """ + + pattern = re.compile(r"^[1-9][1-9][2-5]\d{7}$") + return ( + isinstance(phone_number, str) + and re.match(pattern, phone_number) is not None + ) + + +def _is_valid_mobile(phone_number): # type: (str) -> bool + """ + Returns if a Brazilian mobile number is valid. + It does not verify if the number actually exists. + + Args: + phone_number (str): The mobile number to validate. + Only digits, without country code. + It should include two digits DDD. + + Returns: + bool: True if the phone number is valid. False otherwise. + """ + + pattern = re.compile(r"^[1-9][1-9][9]\d{8}$") + return ( + isinstance(phone_number, str) + and re.match(pattern, phone_number) is not None + ) diff --git a/tests/test_license_plate.py b/tests/test_license_plate.py index 66a719f2..aea3d38f 100644 --- a/tests/test_license_plate.py +++ b/tests/test_license_plate.py @@ -1,6 +1,6 @@ from brutils.license_plate import ( remove_symbols, - is_valid_license_plate_old_format, + is_valid_old_format, is_valid_mercosul, is_valid, convert_to_mercosul, @@ -39,27 +39,27 @@ def test_is_valid(self): self.assertTrue(is_valid("ABC4E67")) self.assertTrue(is_valid("XXX9X99")) - def test_is_valid_license_plate_old_format(self): + def test_is_valid_old_format(self): # When license plate is valid, returns True - self.assertTrue(is_valid_license_plate_old_format("ABC1234")) - self.assertTrue(is_valid_license_plate_old_format("abc1234")) + self.assertTrue(is_valid_old_format("ABC1234")) + self.assertTrue(is_valid_old_format("abc1234")) # When license plate is valid with whitespaces, returns True - self.assertTrue(is_valid_license_plate_old_format(" ABC1234 ")) + self.assertTrue(is_valid_old_format(" ABC1234 ")) # When license plate is not string, returns False - self.assertFalse(is_valid_license_plate_old_format(123456)) + self.assertFalse(is_valid_old_format(123456)) # When license plate is invalid with special characters, returns False - self.assertFalse(is_valid_license_plate_old_format("ABC-1234")) + self.assertFalse(is_valid_old_format("ABC-1234")) # When license plate is invalid with numbers and letters out of order, # returns False - self.assertFalse(is_valid_license_plate_old_format("A1CA23W")) + self.assertFalse(is_valid_old_format("A1CA23W")) # When license plate is invalid with new format, returns False - self.assertFalse(is_valid_license_plate_old_format("ABC1D23")) - self.assertFalse(is_valid_license_plate_old_format("abcd123")) + self.assertFalse(is_valid_old_format("ABC1D23")) + self.assertFalse(is_valid_old_format("abcd123")) def test_is_valid_license_plate_mercosul(self): # When license plate is not string, returns False @@ -164,9 +164,7 @@ def test_generate_license_plate(self): self.assertTrue(is_valid_mercosul(generate(format="LLLNLNN"))) for _ in range(10_000): - self.assertTrue( - is_valid_license_plate_old_format(generate(format="LLLNNNN")) - ) + self.assertTrue(is_valid_old_format(generate(format="LLLNNNN"))) # When no format is provided, returns a valid Mercosul license plate self.assertTrue(is_valid_mercosul(generate())) diff --git a/tests/test_phone.py b/tests/test_phone.py index 9b21c8c2..a9e31db2 100644 --- a/tests/test_phone.py +++ b/tests/test_phone.py @@ -1,123 +1,195 @@ from unittest.mock import patch from brutils.phone import ( - is_valid_landline, - is_valid_mobile, - is_valid, - remove_symbols_phone, + _is_valid_landline, + _is_valid_mobile, format_phone, - generate_mobile_phone, generate_landline_phone, + generate_mobile_phone, + is_valid, + remove_symbols_phone, ) from unittest import TestCase, main class TestPhone(TestCase): - def test_is_valid_landline(self): - # When landline phone is not string, returns False - self.assertIs(is_valid_landline(1938814933), False) - - # When landline phone doesn't contain only digits, returns False - self.assertIs(is_valid_landline("(19)388149"), False) - - # When landline phone is an empty string, returns False - self.assertIs(is_valid_landline(""), False) - - # When landline phone's len is different of 10, returns False - self.assertIs(is_valid_landline("193881"), False) - - # When landline phone's first digit is 0, returns False - self.assertIs(is_valid_landline("0938814933"), False) + def test_is_valid_when_type_is_mobile(self): + # Mock _is_valid_mobile return True + with patch( + "brutils.phone._is_valid_landline" + ) as mock__is_valid_landline: + with patch( + "brutils.phone._is_valid_mobile", return_value=True + ) as mock__is_valid_mobile: + # When mobile phone is_valid, returns True + self.assertIs(is_valid("11994029275", "mobile"), True) - # When landline phone's second digit is 0, returns False - self.assertIs(is_valid_landline("1038814933"), False) + # Checks if function _is_valid_mobile is called with the + # correct argument + mock__is_valid_mobile.assert_called_once_with("11994029275") - # When landline phone's third digit is different of 2,3,4 or 5, - # returns False - self.assertIs(is_valid_landline("1998814933"), False) + # Checks if function _is_valid_mobile is not called + mock__is_valid_landline.assert_not_called() - # When landline phone is valid - self.assertIs(is_valid_landline("1928814933"), True) - self.assertIs(is_valid_landline("1938814933"), True) - self.assertIs(is_valid_landline("1948814933"), True) - self.assertIs(is_valid_landline("1958814933"), True) - self.assertIs(is_valid_landline("3333333333"), True) + # Mock _is_valid_mobile return False + with patch( + "brutils.phone._is_valid_landline" + ) as mock__is_valid_landline: + with patch( + "brutils.phone._is_valid_mobile", return_value=False + ) as mock__is_valid_mobile: + # When mobile phone is_valid, returns False + self.assertIs(is_valid("11994029275", "mobile"), False) - def test_is_valid_mobile(self): - # When mobile is not string, returns False - self.assertIs(is_valid_mobile(1), False) + # Checks if function _is_valid_mobile is called with the + # correct argument + mock__is_valid_mobile.assert_called_once_with("11994029275") - # When mobile doesn't contain only digits, returns False - self.assertIs(is_valid_mobile(119940 - 2927), False) + # Checks if function _is_valid_mobile is not called + mock__is_valid_landline.assert_not_called() - # When mobile is an empty string, returns False - self.assertIs(is_valid_mobile(""), False) + def test_is_valid_when_type_is_landline(self): + # Mock _is_valid_landline to return True + with patch( + "brutils.phone._is_valid_mobile", + ) as mock__is_valid_mobile: + with patch( + "brutils.phone._is_valid_landline", return_value=True + ) as mock__is_valid_landline: + # When mobile phone is_valid, returns True + self.assertIs(is_valid("11994029275", "landline"), True) - # When mobile's len is different of 11, returns False - self.assertIs(is_valid_mobile("119940"), False) + # Checks if function _is_valid_landline is called with the + # correct argument + mock__is_valid_landline.assert_called_once_with("11994029275") - # When mobile's first digit is 0, returns False - self.assertIs(is_valid_mobile("01994029275"), False) + # Checks if function _is_valid_mobile is not called + mock__is_valid_mobile.assert_not_called() - # When mobile's second digit is 0, returns False - self.assertIs(is_valid_mobile("90994029275"), False) + # Mock _is_valid_landline to return False + with patch( + "brutils.phone._is_valid_mobile", + ) as mock__is_valid_mobile: + with patch( + "brutils.phone._is_valid_landline", return_value=False + ) as mock__is_valid_landline: + # When mobile phone is_valid, returns False + self.assertIs(is_valid("11994029275", "landline"), False) - # When mobile's third digit is different of 9, returns False - self.assertIs(is_valid_mobile("11594029275"), False) + # Checks if function _is_valid_landline is called with the + # correct argument + mock__is_valid_landline.assert_called_once_with("11994029275") - # When mobile is valid - self.assertIs(is_valid_mobile("99999999999"), True) - self.assertIs(is_valid_mobile("11994029275"), True) + # Checks if function _is_valid_mobile is not called + mock__is_valid_mobile.assert_not_called() - def test_is_valid(self): - # Mock is_valid_landline to return True + def test_is_valid_when_type_is_none(self): + # Mock _is_valid_landline to return True with patch( - "brutils.phone.is_valid_landline", return_value=True - ) as mock_is_valid_landline: - with patch("brutils.phone.is_valid_mobile") as mock_is_valid_mobile: + "brutils.phone._is_valid_landline", return_value=True + ) as mock__is_valid_landline: + with patch( + "brutils.phone._is_valid_mobile" + ) as mock__is_valid_mobile: # When landline phone is_valid, returns True self.assertIs(is_valid("1958814933"), True) - # Checks if function is_valid_landline is called with the + # Checks if function _is_valid_landline is called with the # correct argument - mock_is_valid_landline.assert_called_once_with("1958814933") - # Checks if function is_valid_mobile is not called - mock_is_valid_mobile.assert_not_called() + mock__is_valid_landline.assert_called_once_with("1958814933") + # Checks if function _is_valid_mobile is not called + mock__is_valid_mobile.assert_not_called() - # Mock is_valid_landline to return False, is_valid_mobile return True + # Mock _is_valid_landline to return False, _is_valid_mobile return True with patch( - "brutils.phone.is_valid_landline", return_value=False - ) as mock_is_valid_landline: + "brutils.phone._is_valid_landline", return_value=False + ) as mock__is_valid_landline: with patch( - "brutils.phone.is_valid_mobile", return_value=True - ) as mock_is_valid_mobile: + "brutils.phone._is_valid_mobile", return_value=True + ) as mock__is_valid_mobile: # When mobile phone is_valid, returns True self.assertIs(is_valid("11994029275"), True) - # Checks if function is_valid_landline is called with the + # Checks if function _is_valid_landline is called with the # correct argument - mock_is_valid_landline.assert_called_once_with("11994029275") - # Checks if function is_valid_mobile is called with the + mock__is_valid_landline.assert_called_once_with("11994029275") + # Checks if function _is_valid_mobile is called with the # correct argument - mock_is_valid_mobile.assert_called_once_with("11994029275") + mock__is_valid_mobile.assert_called_once_with("11994029275") - # Mock is_valid_landline to return False, is_valid_mobile return False + # Mock _is_valid_landline to return False, _is_valid_mobile return False with patch( - "brutils.phone.is_valid_landline", return_value=False - ) as mock_is_valid_landline: + "brutils.phone._is_valid_landline", return_value=False + ) as mock__is_valid_landline: with patch( - "brutils.phone.is_valid_mobile", return_value=False - ) as mock_is_valid_mobile: + "brutils.phone._is_valid_mobile", return_value=False + ) as mock__is_valid_mobile: # When landline phone isn't valid, returns False self.assertIs(is_valid("11994029275"), False) - # Checks if function is_valid_landline is called with the + # Checks if function _is_valid_landline is called with the # correct argument - mock_is_valid_landline.assert_called_once_with("11994029275") - # Checks if function is_valid_mobile is called with the + mock__is_valid_landline.assert_called_once_with("11994029275") + # Checks if function _is_valid_mobile is called with the # correct argument - mock_is_valid_mobile.assert_called_once_with("11994029275") + mock__is_valid_mobile.assert_called_once_with("11994029275") + + def test__is_valid_landline(self): + # When landline phone is not string, returns False + self.assertIs(_is_valid_landline(1938814933), False) + + # When landline phone doesn't contain only digits, returns False + self.assertIs(_is_valid_landline("(19)388149"), False) + + # When landline phone is an empty string, returns False + self.assertIs(_is_valid_landline(""), False) + + # When landline phone's len is different of 10, returns False + self.assertIs(_is_valid_landline("193881"), False) + + # When landline phone's first digit is 0, returns False + self.assertIs(_is_valid_landline("0938814933"), False) + + # When landline phone's second digit is 0, returns False + self.assertIs(_is_valid_landline("1038814933"), False) + + # When landline phone's third digit is different of 2,3,4 or 5, + # returns False + self.assertIs(_is_valid_landline("1998814933"), False) + + # When landline phone is valid + self.assertIs(_is_valid_landline("1928814933"), True) + self.assertIs(_is_valid_landline("1938814933"), True) + self.assertIs(_is_valid_landline("1948814933"), True) + self.assertIs(_is_valid_landline("1958814933"), True) + self.assertIs(_is_valid_landline("3333333333"), True) + + def test__is_valid_mobile(self): + # When mobile is not string, returns False + self.assertIs(_is_valid_mobile(1), False) + + # When mobile doesn't contain only digits, returns False + self.assertIs(_is_valid_mobile(119940 - 2927), False) + + # When mobile is an empty string, returns False + self.assertIs(_is_valid_mobile(""), False) + + # When mobile's len is different of 11, returns False + self.assertIs(_is_valid_mobile("119940"), False) + + # When mobile's first digit is 0, returns False + self.assertIs(_is_valid_mobile("01994029275"), False) + + # When mobile's second digit is 0, returns False + self.assertIs(_is_valid_mobile("90994029275"), False) + + # When mobile's third digit is different of 9, returns False + self.assertIs(_is_valid_mobile("11594029275"), False) + + # When mobile is valid + self.assertIs(_is_valid_mobile("99999999999"), True) + self.assertIs(_is_valid_mobile("11994029275"), True) def test_remove_symbols_phone(self): # When the string empty, it returns an empty string @@ -178,13 +250,13 @@ def test_generate_mobile_phone(self): for _ in range(25): with self.subTest(): phone_generated = generate_mobile_phone() - self.assertTrue(is_valid_mobile(phone_generated)) + self.assertTrue(_is_valid_mobile(phone_generated)) def test_generate_landline_phone(self): for _ in range(25): with self.subTest(): number = generate_landline_phone() - self.assertTrue(is_valid_landline(number)) + self.assertTrue(_is_valid_landline(number)) self.assertEqual(len(generate_landline_phone()), 10) self.assertTrue(generate_landline_phone().isdigit()) self.assertTrue(isinstance(generate_landline_phone(), str))