Skip to content

Commit 05efebe

Browse files
committed
Update test_create_profile.py
1 parent 935870f commit 05efebe

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

tests/unit/services/test_create_profile.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,45 @@
11
import pytest
22
from policyengine_api.services.user_service import UserService
3+
from datetime import datetime
34

45
user_service = UserService()
56

67

78
class TestCreateProfile:
89

10+
# Test creating a valid user profile
911
def test_create_profile_valid(self, test_db):
10-
# Use a more explicit garbage ID to not imply any formatting
1112
garbage_auth0_id = "test_garbage_auth0_id_123"
12-
primary_country = "us" # Use correct country format
13+
primary_country = "us"
1314
username = "test_username"
14-
user_since = 20250101 # Use BIGINT as expected by the database
15+
user_since = int(datetime.now().timestamp())
1516

16-
# Create the profile
1717
result = user_service.create_profile(
1818
primary_country=primary_country,
1919
auth0_id=garbage_auth0_id,
2020
username=username,
2121
user_since=user_since,
2222
)
2323

24-
# Verify the result from the service
2524
assert result[0] is True
2625

27-
# Query the database directly to verify the record was created
2826
created_record = test_db.query(
2927
"SELECT * FROM user_profiles WHERE auth0_id = ?",
3028
(garbage_auth0_id,),
3129
).fetchone()
3230

33-
# Verify the record was created with the correct values
3431
assert created_record is not None
3532
assert created_record["auth0_id"] == garbage_auth0_id
3633
assert created_record["primary_country"] == primary_country
3734
assert created_record["username"] == username
3835
assert created_record["user_since"] == user_since
3936

37+
# Test that creating a profile without auth0_id raises an exception
4038
def test_create_profile_missing_auth0_id(self, test_db):
41-
# More descriptive test name for this specific invalid case
4239
primary_country = "us"
4340
username = "test_username"
44-
user_since = 20250101
41+
user_since = int(datetime.now().timestamp())
4542

46-
# Test that we get an error when auth0_id is missing
4743
with pytest.raises(
4844
Exception,
4945
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):
5450
user_since=user_since,
5551
)
5652

57-
# Verify that no record was created in the database
5853
records = test_db.query(
5954
"SELECT COUNT(*) as count FROM user_profiles WHERE username = ?",
6055
(username,),
6156
).fetchone()
6257

6358
assert records["count"] == 0
6459

60+
# Test that creating a duplicate profile returns False
6561
def test_create_profile_duplicate(self, test_db):
6662
garbage_auth0_id = "duplicate_test_id_456"
6763
primary_country = "us"
6864
username = "duplicate_test_username"
69-
user_since = 20250101
65+
user_since = int(datetime.now().timestamp())
7066

71-
# Create the first profile and verify it was created
7267
result1 = user_service.create_profile(
7368
primary_country=primary_country,
7469
auth0_id=garbage_auth0_id,
@@ -78,26 +73,22 @@ def test_create_profile_duplicate(self, test_db):
7873

7974
assert result1[0] is True
8075

81-
# Verify the record exists in the database
8276
record_count_before = test_db.query(
8377
"SELECT COUNT(*) as count FROM user_profiles WHERE auth0_id = ?",
8478
(garbage_auth0_id,),
8579
).fetchone()
8680

8781
assert record_count_before["count"] == 1
8882

89-
# Attempt to create a duplicate profile
9083
result2 = user_service.create_profile(
9184
primary_country=primary_country,
9285
auth0_id=garbage_auth0_id,
9386
username=username,
9487
user_since=user_since,
9588
)
9689

97-
# Verify that the second attempt returns False
9890
assert result2[0] is False
9991

100-
# Verify that no additional record was created in the database
10192
record_count_after = test_db.query(
10293
"SELECT COUNT(*) as count FROM user_profiles WHERE auth0_id = ?",
10394
(garbage_auth0_id,),

0 commit comments

Comments
 (0)