From 65ebc5cd5564b7396c0719e6be3705140d5cef91 Mon Sep 17 00:00:00 2001
From: Thorge Petersen <petersen@rz.uni-kiel.de>
Date: Fri, 13 Oct 2023 10:08:33 +0200
Subject: [PATCH] Add email extraction helper function with tests

---
 ckanext/odsh/helpers.py                 | 11 +++++
 ckanext/odsh/tests/test_odsh_helpers.py | 53 ++++++++++++++++++++++++-
 2 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/ckanext/odsh/helpers.py b/ckanext/odsh/helpers.py
index 968b901f..2324b01a 100644
--- a/ckanext/odsh/helpers.py
+++ b/ckanext/odsh/helpers.py
@@ -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
diff --git a/ckanext/odsh/tests/test_odsh_helpers.py b/ckanext/odsh/tests/test_odsh_helpers.py
index fba58f02..8fca26af 100644
--- a/ckanext/odsh/tests/test_odsh_helpers.py
+++ b/ckanext/odsh/tests/test_odsh_helpers.py
@@ -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
-- 
GitLab