Skip to content

Commit

Permalink
Ensure rules are consistently named
Browse files Browse the repository at this point in the history
Change-Id: Ic0c21c03b5ada188b8e80a7063c6a3711d194a94
  • Loading branch information
achembarpu committed Jan 24, 2023
1 parent bf95935 commit 8cde60f
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Options per Parser, with possible arguments:
* `is_phone_number`: Checks if the string is possibly a phone number.
* `fully_matches_regex`: Checks if the string fully matches a given regular expression.
* `regex`: [Python-compatible regex](https://docs.python.org/3/howto/regex.html)
* `search_regex`: Checks if the string contains some part of a given regular
* `contains_regex`: Checks if the string contains some part of a given regular
expression.
* `regex`: [Python-compatible regex](https://docs.python.org/3/howto/regex.html)

Expand Down
2 changes: 1 addition & 1 deletion rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def func_mapper(funcs: List[Callable]) -> Dict[str, Callable]:

TextRules: RulesMap[str] = func_mapper([
text.contains_at_sign, text.fully_matches_regex, text.is_email,
text.is_phone_number, text.search_regex
text.is_phone_number, text.contains_regex
])


Expand Down
31 changes: 15 additions & 16 deletions rules/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def is_email() -> RuleChecker[str]:
pattern = re.compile(regex)

def _checker(string: str) -> RuleOutput:
if (pattern.search(string)):
return 'String contains a possible email address.'
else:
if pattern.search(string):
return None
else:
return 'String does not resemble an email address.'

return _checker

Expand All @@ -59,16 +59,15 @@ def contains_at_sign() -> RuleChecker[str]:
"""

def _checker(string: str) -> RuleOutput:

if '@' in string:
return 'String contains the @ character.'
else:
return None
else:
return 'String does not contain the @ character.'

return _checker


def search_regex(regex: str) -> RuleChecker[str]:
def contains_regex(regex: str) -> RuleChecker[str]:
"""
Checks if the string contains some part of a given regular
expression, i.e. string contains the word "Approved".
Expand All @@ -77,10 +76,10 @@ def search_regex(regex: str) -> RuleChecker[str]:

def _checker(string: str) -> RuleOutput:
#
if (pattern.search(string)):
return 'String contains a match for the given pattern.'
else:
if pattern.search(string):
return None
else:
return 'String does not contain a match for the given pattern.'

return _checker

Expand All @@ -93,10 +92,10 @@ def fully_matches_regex(regex: str) -> RuleChecker[str]:
pattern = re.compile(regex)

def _checker(string: str) -> RuleOutput:
if (pattern.fullmatch(string)):
return 'String is a full match for the given pattern.'
else:
if pattern.fullmatch(string):
return None
else:
return 'String is not a full match for the given pattern.'

return _checker

Expand All @@ -112,10 +111,10 @@ def _checker(string: str) -> RuleOutput:
try:
int_number = int(string)
if 9 < len(string) < 14 and int_number > 999999:
return 'String contains a possible phone number.'
else:
return None
else:
raise ValueError
except ValueError:
return None
return 'String does not resemble a phone number.'

return _checker
10 changes: 5 additions & 5 deletions test/core/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_invalid_rule(self):
def test_mismatched_rule(self):
rules: RulesMap = NumericRules
with self.assertRaises(ValueError):
generate_selected_rules([RuleConfig(rule="search_regex")], rules)
generate_selected_rules([RuleConfig(rule="contains_regex")], rules)

def test_numeric_rule(self):
rules: RulesMap = NumericRules
Expand Down Expand Up @@ -79,11 +79,11 @@ def test_text_rule(self):
def test_text_rule_with_args(self):
rules: RulesMap = TextRules
generated = generate_selected_rules(
[RuleConfig(rule="search_regex", args={"regex": "testpattern"})],
[RuleConfig(rule="contains_regex", args={"regex": "testpattern"})],
rules)
generated_rule = generated[0]

self.assertIn('search_regex', generated_rule.__qualname__)
self.assertIn('contains_regex', generated_rule.__qualname__)

self.assertIsNone(generated_rule("otherpattern"))
self.assertIsNotNone(generated_rule("testpattern"))
self.assertIsNotNone(generated_rule("otherpattern"))
self.assertIsNone(generated_rule("testpattern"))
12 changes: 6 additions & 6 deletions test/text/test_contains_at_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from rules.text import contains_at_sign


class ContainsAtSign(unittest.TestCase):
class ContainsAtSignTest(unittest.TestCase):

def setUp(self) -> None:
self.rule_checker = contains_at_sign()
Expand All @@ -29,10 +29,10 @@ def test_returns_callable(self):
self.assertTrue(callable(self.rule_checker))

def test_string_with_character(self):
self.assertIsNotNone(self.rule_checker("@"))
self.assertIsNotNone(self.rule_checker("[email protected]"))
self.assertIsNotNone(self.rule_checker("john@@"))
self.assertIsNone(self.rule_checker("@"))
self.assertIsNone(self.rule_checker("[email protected]"))
self.assertIsNone(self.rule_checker("john@@"))

def test_string_without_character(self):
self.assertIsNone(self.rule_checker("johndoe.com"))
self.assertIsNone(self.rule_checker(""))
self.assertIsNotNone(self.rule_checker("johndoe.com"))
self.assertIsNotNone(self.rule_checker(""))
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@
import re
import unittest

from rules.text import search_regex
from rules.text import contains_regex


class searchRegex(unittest.TestCase):
class ContainsRegexTest(unittest.TestCase):

def setUp(self) -> None:
test_regex = r'test'
self.rule_checker = search_regex(test_regex)
self.rule_checker = contains_regex(test_regex)
return super().setUp()

def test_returns_callable(self):
self.assertTrue(callable(self.rule_checker))

def test_contains_regex(self):
self.assertIsNotNone(self.rule_checker('test'))
self.assertIsNotNone(self.rule_checker('test123'))
self.assertIsNone(self.rule_checker('test'))
self.assertIsNone(self.rule_checker('test123'))

def test_not_contains_regex(self):
self.assertIsNone(self.rule_checker('tes'))
self.assertIsNone(self.rule_checker('tes1t123'))
self.assertIsNone(self.rule_checker('123'))
self.assertIsNotNone(self.rule_checker('tes'))
self.assertIsNotNone(self.rule_checker('tes1t123'))
self.assertIsNotNone(self.rule_checker('123'))

def test_invalid_regex_behaviour(self):
# pass invalid regex
test_regex = '['

# assert that error is raised
with self.assertRaises(re.error):
rule_checker = search_regex(test_regex)
rule_checker = contains_regex(test_regex)
rule_checker('123')
10 changes: 5 additions & 5 deletions test/text/test_fully_matches_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from rules.text import fully_matches_regex


class fullyMatchesRegex(unittest.TestCase):
class FullyMatchesRegexTest(unittest.TestCase):

def setUp(self) -> None:
test_regex = r'test'
Expand All @@ -30,12 +30,12 @@ def test_returns_callable(self):
self.assertTrue(callable(self.rule_checker))

def test_contains_regex(self):
self.assertIsNotNone(self.rule_checker('test'))
self.assertIsNone(self.rule_checker('test'))

def test_non_matching_regex(self):
self.assertIsNone(self.rule_checker('tes'))
self.assertIsNone(self.rule_checker('tes1t123'))
self.assertIsNone(self.rule_checker('123'))
self.assertIsNotNone(self.rule_checker('tes'))
self.assertIsNotNone(self.rule_checker('tes1t123'))
self.assertIsNotNone(self.rule_checker('123'))

def test_invalid_regex_behaviour(self):
# pass invalid regex
Expand Down
54 changes: 27 additions & 27 deletions test/text/test_is_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from rules.text import is_email


class IsEmail(unittest.TestCase):
class IsEmailTest(unittest.TestCase):

def setUp(self) -> None:
self.rule_checker = is_email()
Expand All @@ -30,69 +30,69 @@ def test_returns_callable(self):

def test_valid_emails(self):
# standard email addresses
self.assertIsNotNone(self.rule_checker("[email protected]"))
self.assertIsNotNone(self.rule_checker("[email protected]"))
self.assertIsNotNone(self.rule_checker("[email protected]"))
self.assertIsNone(self.rule_checker("[email protected]"))
self.assertIsNone(self.rule_checker("[email protected]"))
self.assertIsNone(self.rule_checker("[email protected]"))

# dot seperated email name
self.assertIsNotNone(self.rule_checker("[email protected]"))
self.assertIsNone(self.rule_checker("[email protected]"))

# email with number
self.assertIsNotNone(self.rule_checker("[email protected]"))
self.assertIsNone(self.rule_checker("[email protected]"))

# email with capitals
self.assertIsNotNone(self.rule_checker("[email protected]"))
self.assertIsNone(self.rule_checker("[email protected]"))

# leading space
self.assertIsNotNone(self.rule_checker(" [email protected]"))
self.assertIsNone(self.rule_checker(" [email protected]"))

# trailing space
self.assertIsNotNone(self.rule_checker("[email protected] "))
self.assertIsNone(self.rule_checker("[email protected] "))

# email in random string
self.assertIsNotNone(
self.assertIsNone(
self.rule_checker("asdfasdfsdf [email protected]"))

def test_invalid_characters(self):
# quotes
self.assertIsNone(self.rule_checker("john\"@doe.com"))
self.assertIsNone(self.rule_checker("john'@doe.com"))
self.assertIsNone(self.rule_checker("john@doe\".com"))
self.assertIsNone(self.rule_checker("john@do'e.com"))
self.assertIsNotNone(self.rule_checker("john\"@doe.com"))
self.assertIsNotNone(self.rule_checker("john'@doe.com"))
self.assertIsNotNone(self.rule_checker("john@doe\".com"))
self.assertIsNotNone(self.rule_checker("john@do'e.com"))

# asterisk
self.assertIsNone(self.rule_checker("john**@doe.com"))
self.assertIsNone(self.rule_checker("john@do*e.com"))
self.assertIsNotNone(self.rule_checker("john**@doe.com"))
self.assertIsNotNone(self.rule_checker("john@do*e.com"))

# backslash
# self.assertIsNone(self.rule_checker("jo\[email protected]"))
self.assertIsNone(self.rule_checker(r"john@do\e.com"))
# self.assertIsNotNone(self.rule_checker("jo\[email protected]"))
self.assertIsNotNone(self.rule_checker(r"john@do\e.com"))

def test_invalid_email_structure(self):
# missing address sign
self.assertIsNone(self.rule_checker("john.com"))
self.assertIsNotNone(self.rule_checker("john.com"))

# double at sign
self.assertIsNone(self.rule_checker("john@@com"))
self.assertIsNotNone(self.rule_checker("john@@com"))

# wrongly placed at sign
self.assertIsNone(self.rule_checker("johndoe.co@m"))
self.assertIsNotNone(self.rule_checker("johndoe.co@m"))

# leading at sign
self.assertIsNone(self.rule_checker("@john.com"))
self.assertIsNotNone(self.rule_checker("@john.com"))

# trailing at sign
self.assertIsNone(self.rule_checker("john.com@"))
self.assertIsNotNone(self.rule_checker("john.com@"))

# missing dot
self.assertIsNone(self.rule_checker("john@doecom"))
self.assertIsNotNone(self.rule_checker("john@doecom"))

# double dot in domain
self.assertIsNone(self.rule_checker("[email protected]"))
self.assertIsNotNone(self.rule_checker("[email protected]"))

def test_invalid_input(self):
# number string
self.assertIsNone(self.rule_checker('252342'))
self.assertIsNotNone(self.rule_checker('252342'))

# character string
self.assertIsNone(self.rule_checker('*#^$*(@($&'))
self.assertIsNotNone(self.rule_checker('*#^$*(@($&'))
10 changes: 5 additions & 5 deletions test/text/test_is_phone_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
# yapf: enable


class IsPhoneNumber(unittest.TestCase):
class IsPhoneNumberTest(unittest.TestCase):

def setUp(self) -> None:
self.rule_checker = is_phone_number()
Expand All @@ -138,10 +138,10 @@ def test_returns_callable(self):
def test_phone_numbers(self):
# standard phone number
for number in random_phone_numbers:
self.assertIsNotNone(self.rule_checker(number))
self.assertIsNone(self.rule_checker(number))

def test_non_phone_numbers(self):
# standard phone number
self.assertIsNone(self.rule_checker('test'))
self.assertIsNone(self.rule_checker('23598734928057034978'))
self.assertIsNone(self.rule_checker('00000000000'))
self.assertIsNotNone(self.rule_checker('test'))
self.assertIsNotNone(self.rule_checker('23598734928057034978'))
self.assertIsNotNone(self.rule_checker('00000000000'))

0 comments on commit 8cde60f

Please sign in to comment.