Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janbaykara committed Dec 12, 2024
1 parent 7dac271 commit 401dabc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
31 changes: 31 additions & 0 deletions hub/tests/test_source_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from utils.py import parse_datetime

from hub.validation import validate_and_format_phone_number


class TestSourceParser(TestCase):
dates_that_should_work = [
Expand All @@ -16,3 +18,32 @@ class TestSourceParser(TestCase):
def test_dateparse(self):
for date in self.dates_that_should_work:
self.assertEqual(parse_datetime(date[0]), date[1])

class TestPhoneField(TestCase):
def test_invalid_phone_number(self):
phone = "123456789"
result = validate_and_format_phone_number(
phone, "GB"
)
self.assertIsNone(result)

def test_valid_phone_number_without_country_code(self):
phone = "07123456789"
result = validate_and_format_phone_number(
phone, "GB"
)
self.assertEqual(result, "+447123456789")

def test_valid_phone_number_with_country_code(self):
phone = "+447123456789"
result = validate_and_format_phone_number(
phone, ["GB"]
)
self.assertEqual(result, "+447123456789")

def test_valid_phone_number_for_usa(self):
phone = "4155552671"
result = validate_and_format_phone_number(
phone, ["US"]
)
self.assertEqual(result, "+14155552671")
6 changes: 5 additions & 1 deletion hub/validation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import phonenumbers
from phonenumbers.phonenumberutil import NumberParseException
from utils.py import ensure_list


def validate_and_format_phone_number(value, countries):
def validate_and_format_phone_number(value, countries = []):
"""
Validates and formats a phone number to E164 format if valid, otherwise returns None.
"""
countries = ensure_list(countries or [])
if len(countries) == 0:
countries = ["GB"]
try:
phone_number = phonenumbers.parse(value, countries[0])
if phonenumbers.is_valid_number(phone_number):
Expand Down

0 comments on commit 401dabc

Please sign in to comment.