1
1
import pytest
2
2
from policyengine_api .services .user_service import UserService
3
+ from datetime import datetime
3
4
4
5
user_service = UserService ()
5
6
6
7
7
8
class TestCreateProfile :
8
9
10
+ # Test creating a valid user profile
9
11
def test_create_profile_valid (self , test_db ):
10
- # Use a more explicit garbage ID to not imply any formatting
11
12
garbage_auth0_id = "test_garbage_auth0_id_123"
12
- primary_country = "us" # Use correct country format
13
+ primary_country = "us"
13
14
username = "test_username"
14
- user_since = 20250101 # Use BIGINT as expected by the database
15
+ user_since = int ( datetime . now (). timestamp ())
15
16
16
- # Create the profile
17
17
result = user_service .create_profile (
18
18
primary_country = primary_country ,
19
19
auth0_id = garbage_auth0_id ,
20
20
username = username ,
21
21
user_since = user_since ,
22
22
)
23
23
24
- # Verify the result from the service
25
24
assert result [0 ] is True
26
25
27
- # Query the database directly to verify the record was created
28
26
created_record = test_db .query (
29
27
"SELECT * FROM user_profiles WHERE auth0_id = ?" ,
30
28
(garbage_auth0_id ,),
31
29
).fetchone ()
32
30
33
- # Verify the record was created with the correct values
34
31
assert created_record is not None
35
32
assert created_record ["auth0_id" ] == garbage_auth0_id
36
33
assert created_record ["primary_country" ] == primary_country
37
34
assert created_record ["username" ] == username
38
35
assert created_record ["user_since" ] == user_since
39
36
37
+ # Test that creating a profile without auth0_id raises an exception
40
38
def test_create_profile_missing_auth0_id (self , test_db ):
41
- # More descriptive test name for this specific invalid case
42
39
primary_country = "us"
43
40
username = "test_username"
44
- user_since = 20250101
41
+ user_since = int ( datetime . now (). timestamp ())
45
42
46
- # Test that we get an error when auth0_id is missing
47
43
with pytest .raises (
48
44
Exception ,
49
45
match = r"UserService.create_profile\(\) missing 1 required positional argument: 'auth0_id'" ,
@@ -54,21 +50,20 @@ def test_create_profile_missing_auth0_id(self, test_db):
54
50
user_since = user_since ,
55
51
)
56
52
57
- # Verify that no record was created in the database
58
53
records = test_db .query (
59
54
"SELECT COUNT(*) as count FROM user_profiles WHERE username = ?" ,
60
55
(username ,),
61
56
).fetchone ()
62
57
63
58
assert records ["count" ] == 0
64
59
60
+ # Test that creating a duplicate profile returns False
65
61
def test_create_profile_duplicate (self , test_db ):
66
62
garbage_auth0_id = "duplicate_test_id_456"
67
63
primary_country = "us"
68
64
username = "duplicate_test_username"
69
- user_since = 20250101
65
+ user_since = int ( datetime . now (). timestamp ())
70
66
71
- # Create the first profile and verify it was created
72
67
result1 = user_service .create_profile (
73
68
primary_country = primary_country ,
74
69
auth0_id = garbage_auth0_id ,
@@ -78,26 +73,22 @@ def test_create_profile_duplicate(self, test_db):
78
73
79
74
assert result1 [0 ] is True
80
75
81
- # Verify the record exists in the database
82
76
record_count_before = test_db .query (
83
77
"SELECT COUNT(*) as count FROM user_profiles WHERE auth0_id = ?" ,
84
78
(garbage_auth0_id ,),
85
79
).fetchone ()
86
80
87
81
assert record_count_before ["count" ] == 1
88
82
89
- # Attempt to create a duplicate profile
90
83
result2 = user_service .create_profile (
91
84
primary_country = primary_country ,
92
85
auth0_id = garbage_auth0_id ,
93
86
username = username ,
94
87
user_since = user_since ,
95
88
)
96
89
97
- # Verify that the second attempt returns False
98
90
assert result2 [0 ] is False
99
91
100
- # Verify that no additional record was created in the database
101
92
record_count_after = test_db .query (
102
93
"SELECT COUNT(*) as count FROM user_profiles WHERE auth0_id = ?" ,
103
94
(garbage_auth0_id ,),
0 commit comments