Skip to content
Snippets Groups Projects
Commit 65ebc5cd authored by Thorge Petersen's avatar Thorge Petersen
Browse files

Add email extraction helper function with tests

parent f4403634
No related branches found
No related tags found
1 merge request!51Display contact information on dataset page and use text-ellipsis for long description texts
......@@ -791,3 +791,14 @@ def format_resource_format(format_str):
return format_str[:-5]
else:
return format_str
def extract_email(text):
# Regular expression pattern to match email addresses
email_pattern = r'[\w\.-]+@[a-zA-Z\d\.-]+\.[a-zA-Z]{2,}'
# Find all email addresses in the input text using the regular expression pattern
matches = re.findall(email_pattern, text)
# If there are matches, return the first email address found, else return None
return matches[0] if matches else None
\ No newline at end of file
......@@ -2,7 +2,7 @@ import datetime
from mock import patch
import unittest
from ckanext.odsh.helpers import is_within_last_month
from ckanext.odsh.helpers import is_within_last_month, extract_email
class Test_is_within_last_month(unittest.TestCase):
def test_it_returns_true_for_simple_query(self):
......@@ -33,3 +33,54 @@ class Test_is_within_last_month(unittest.TestCase):
date = datetime.date(2018, 6, 8)
date_ref = datetime.date(2018, 7, 10)
assert is_within_last_month(date, date_ref)==False
class TestHelpers(unittest.TestCase):
# is_within_last_month
def test_within_last_month_with_same_month(self):
date = datetime.date(2019, 4, 15)
date_ref = datetime.date(2019, 4, 29)
self.assertTrue(is_within_last_month(date, date_ref))
def test_within_last_month_with_different_years(self):
date = datetime.date(2018, 12, 16)
date_ref = datetime.date(2019, 1, 15)
self.assertTrue(is_within_last_month(date, date_ref))
def test_within_last_month_with_different_months(self):
date = datetime.date(2018, 6, 16)
date_ref = datetime.date(2018, 7, 10)
self.assertTrue(is_within_last_month(date, date_ref))
def test_within_last_month_returns_false_for_invalid_dates(self):
date = datetime.date(2018, 12, 15)
date_ref = datetime.date(2019, 1, 15)
self.assertFalse(is_within_last_month(date, date_ref))
def test_within_last_month_returns_false_for_dates_in_different_months(self):
date = datetime.date(2018, 6, 8)
date_ref = datetime.date(2018, 7, 10)
self.assertFalse(is_within_last_month(date, date_ref))
def test_within_last_month_uses_today_if_date_ref_missing(self):
date = datetime.date.today() - datetime.timedelta(days=20)
self.assertTrue(is_within_last_month(date))
# extract_email
def test_extract_email_with_mailto_prefix(self):
email = extract_email("mailto:user1@example.com")
self.assertEqual(email, "user1@example.com")
def test_extract_email_without_mailto_prefix(self):
email = extract_email("user1@example.com")
self.assertEqual(email, "user1@example.com")
def test_extract_email_invalid_input(self):
email = extract_email("thisisnomail")
self.assertIsNone(email)
def test_extract_email_multiple_emails(self):
email = extract_email("Emails: user1@example.com, user2@example.com")
self.assertEqual(email, "user1@example.com")
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment