You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dsg_lib/async_database_functions/database_operations.py
+4Lines changed: 4 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -19,6 +19,10 @@
19
19
Each method is tested to ensure it performs the expected operation and handles errors correctly. The tests use the pytest-asyncio plugin to run the asynchronous methods in an event loop, and the unittest.mock library to mock the database session and simulate errors.
20
20
21
21
The tests are organized into a single class, TestDatabaseOperations, which contains one test method for each method in the DatabaseOperations class. Each test method follows the Arrange-Act-Assert pattern: it sets up the necessary objects and state (Arrange), calls the method being tested (Act), and checks that the results are as expected (Assert).
This module provides functionality for validating email addresses.
4
+
5
+
The main function in this module is `validate_email_address`, which takes an email address and a set of optional parameters to control the validation process. It uses the `email_validator` library to perform the validation and returns a dictionary containing the validation result and other information about the email address.
6
+
7
+
The module also defines a `DNSType` enum for specifying the type of DNS resolver to use during the validation process.
8
+
9
+
The `validate_email_address` function supports the following optional parameters:
10
+
- `check_deliverability`: If True, the function checks whether the email address is deliverable.
11
+
- `test_environment`: If True, the function operates in test mode and does not actually send any emails.
12
+
- `allow_smtputf8`: If True, the function allows non-ASCII characters in the email address.
13
+
- `allow_empty_local`: If True, the function allows email addresses with an empty local part.
14
+
- `allow_quoted_local`: If True, the function allows email addresses with a quoted local part.
15
+
- `allow_display_name`: If True, the function allows email addresses with a display name.
16
+
- `allow_domain_literal`: If True, the function allows email addresses with a domain literal.
17
+
- `globally_deliverable`: If True, the function checks whether the email address is globally deliverable.
18
+
- `timeout`: The timeout for the DNS resolver, in seconds.
19
+
- `dns_type`: The type of DNS resolver to use, either 'dns' or 'timeout'.
20
+
21
+
Example:
22
+
To use the `validate_email` function in this module, you can do the following:
23
+
24
+
```python
25
+
from email_validation import validate_email
26
+
27
+
email = "test@example.com"
28
+
if validate_email(email):
29
+
print(f"{email} is valid.")
30
+
else:
31
+
print(f"{email} is not valid.")
32
+
```
33
+
See example for more use or bottom of module for more use examples.
34
+
35
+
Author: Mike Ryan
36
+
Date: 2024/05/16
37
+
License: MIT
38
+
"""
2
39
fromenumimportEnum
3
40
fromtypingimportDict, List, Union
4
41
fromloguruimportlogger
@@ -11,6 +48,15 @@
11
48
12
49
13
50
classDNSType(Enum):
51
+
"""
52
+
Enum representing the type of DNS resolver to use during email validation.
53
+
54
+
This enum is used in the `validate_email_address` function to specify the type of DNS resolver to use when checking the deliverability of an email address. The `DNS` option uses a standard DNS resolver, while the `TIMEOUT` option uses a DNS resolver with a specified timeout.
55
+
56
+
Attributes:
57
+
DNS (str): Represents a standard DNS resolver.
58
+
TIMEOUT (str): Represents a DNS resolver with a specified timeout.
Validates an email address and returns a dictionary with the validation result and other information.
79
+
80
+
This function uses the `email_validator` library to validate the email address. It supports a variety of optional parameters to control the validation process, such as whether to check deliverability, whether to allow non-ASCII characters, and the type of DNS resolver to use.
31
81
82
+
Args:
83
+
email (str): The email address to validate.
84
+
check_deliverability (bool, optional): If True, checks whether the email address is deliverable. Defaults to True.
85
+
test_environment (bool, optional): If True, operates in test mode and does not actually send any emails. Defaults to False.
86
+
allow_smtputf8 (bool, optional): If True, allows non-ASCII characters in the email address. Defaults to False.
87
+
allow_empty_local (bool, optional): If True, allows email addresses with an empty local part. Defaults to False.
88
+
allow_quoted_local (bool, optional): If True, allows email addresses with a quoted local part. Defaults to False.
89
+
allow_display_name (bool, optional): If True, allows email addresses with a display name. Defaults to False.
90
+
allow_domain_literal (bool, optional): If True, allows email addresses with a domain literal. Defaults to False.
91
+
globally_deliverable (bool, optional): If True, checks whether the email address is globally deliverable. Defaults to None.
92
+
timeout (int, optional): The timeout for the DNS resolver, in seconds. Defaults to 10.
93
+
dns_type (str, optional): The type of DNS resolver to use, either 'dns' or 'timeout'. Defaults to 'dns'.
94
+
95
+
Returns:
96
+
Dict[str, Union[str, bool, Dict[str, Union[str, bool, List[str]]]]]: A dictionary containing the validation result and other information about the email address.
97
+
98
+
Raises:
99
+
ValueError: If `dns_type` is not 'dns' or 'timeout'.
100
+
EmailUndeliverableError: If the email address is not deliverable.
101
+
EmailNotValidError: If the email address is not valid according to the `email_validator` library.
102
+
Exception: If any other error occurs during the validation process.
103
+
"""
104
+
# Log the function call with the provided parameters
32
105
logger.debug(f"validate_email_address: {email} with params: {locals()}")
106
+
107
+
# Initialize the valid flag to False
33
108
valid: bool=False
34
109
35
-
dns_type=dns_type.lower()
110
+
# Convert the dns_type to a DNSType enum
111
+
try:
112
+
dns_type=DNSType(dns_type.lower())
113
+
exceptValueError:
114
+
raiseValueError("dns_type must be either 'dns' or 'timeout'. Default is 'dns' if not provided or input is None.")
'this is"not\\allowed@example.com', # spaces, quotes, and backslashes may only exist when within quoted strings and preceded by a backslash
117
223
'this\\ still\\"not\\\\allowed@example.com', # even if escaped (preceded by a backslash), spaces, quotes, and backslashes must still be contained by quotes
118
224
"1234567890123456789012345678901234567890123456789012345678901234+x@example.com", # local part is longer than 64 characters
119
-
"mike@google.com",
225
+
120
226
# Emails with empty local part
121
227
"@example.com", # only valid if allow_empty_local is True
122
228
@@ -142,11 +248,14 @@ def validate_email_address(
142
248
"john@doe@example.com", # only one @ is allowed
143
249
"john.doe@.com", # domain can't start with a dot
144
250
"john.doe@example..com", # domain can't have two consecutive dots
0 commit comments