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