Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ckanext-odsh
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Open-Data
ckanext-odsh
Commits
65ebc5cd
Commit
65ebc5cd
authored
1 year ago
by
Thorge Petersen
Browse files
Options
Downloads
Patches
Plain Diff
Add email extraction helper function with tests
parent
f4403634
No related branches found
No related tags found
1 merge request
!51
Display contact information on dataset page and use text-ellipsis for long description texts
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
ckanext/odsh/helpers.py
+11
-0
11 additions, 0 deletions
ckanext/odsh/helpers.py
ckanext/odsh/tests/test_odsh_helpers.py
+52
-1
52 additions, 1 deletion
ckanext/odsh/tests/test_odsh_helpers.py
with
63 additions
and
1 deletion
ckanext/odsh/helpers.py
+
11
−
0
View file @
65ebc5cd
...
...
@@ -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
This diff is collapsed.
Click to expand it.
ckanext/odsh/tests/test_odsh_helpers.py
+
52
−
1
View file @
65ebc5cd
...
...
@@ -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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment