-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathtest_validators.py
More file actions
69 lines (63 loc) · 1.98 KB
/
test_validators.py
File metadata and controls
69 lines (63 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import pytest
from .validators import validate_email, validate_username
def test_valid_emails():
"""Test validation of properly formatted email addresses"""
valid_emails = [
"test@example.com",
"user.name@domain.com",
"user+tag@example.co.uk",
"123@domain.com",
"user@sub.domain.com"
]
for email in valid_emails:
assert validate_email(email) is True
def test_invalid_emails():
"""Test validation of improperly formatted email addresses"""
invalid_emails = [
"test@.com",
"@domain.com",
"test@domain",
"test@domain.",
"test.domain.com",
"test@domain@.com",
"test space@domain.com"
]
for email in invalid_emails:
assert validate_email(email) is False
def test_edge_cases():
"""Test validation with edge cases"""
assert validate_email("") is False
with pytest.raises(TypeError):
validate_email(None)
def test_valid_usernames():
"""Test validation of properly formatted usernames"""
valid_usernames = [
"user123",
"_user",
"john_doe",
"a123",
"Developer42",
"code_master",
"Alice_Bob_123"
]
for username in valid_usernames:
assert validate_username(username) is True
def test_invalid_usernames():
"""Test validation of improperly formatted usernames"""
invalid_usernames = [
"ab", # too short
"a" * 17, # too long
"123user", # starts with number
"__user", # multiple underscores at start
"user name", # contains space
"user@name", # special character
"-user", # starts with hyphen
"user-", # ends with hyphen
]
for username in invalid_usernames:
assert validate_username(username) is False
def test_username_edge_cases():
"""Test username validation with edge cases"""
assert validate_username("") is False
with pytest.raises(TypeError):
validate_username(None)