Skip to content

Commit 935870f

Browse files
committed
Fixed implementation of create_test_profile.py and changelog entry format
1 parent 6b31886 commit 935870f

File tree

3 files changed

+93
-40
lines changed

3 files changed

+93
-40
lines changed

.coverage

52 KB
Binary file not shown.

changelog_entry.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
- bump: patch
22
changes:
3-
changed:
4-
- Create test_create_profile
5-
date: 2025-03-10 12:55:10
3+
fixed:
4+
- Fixed tests for user profile creation to test database interactions directly
5+
date: 2025-03-10 12:55:10
Lines changed: 90 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,106 @@
11
import pytest
2-
import unittest.mock as mock
3-
import time
42
from policyengine_api.services.user_service import UserService
53

6-
userService = UserService()
4+
user_service = UserService()
5+
76

87
class TestCreateProfile:
98

10-
def test_create_profile_valid(self):
11-
auth0_id = 'test-auth-id'
12-
primary_country = 'United States'
13-
username = 'test_username'
14-
user_since = int(time.time() * 1000)
15-
16-
result = userService.create_profile(
17-
primary_country=primary_country, auth0_id=auth0_id, username=username, user_since=user_since
9+
def test_create_profile_valid(self, test_db):
10+
# Use a more explicit garbage ID to not imply any formatting
11+
garbage_auth0_id = "test_garbage_auth0_id_123"
12+
primary_country = "us" # Use correct country format
13+
username = "test_username"
14+
user_since = 20250101 # Use BIGINT as expected by the database
15+
16+
# Create the profile
17+
result = user_service.create_profile(
18+
primary_country=primary_country,
19+
auth0_id=garbage_auth0_id,
20+
username=username,
21+
user_since=user_since,
1822
)
19-
23+
24+
# Verify the result from the service
2025
assert result[0] is True
21-
user_record = userService.get_profile(auth0_id)
22-
assert user_record is not None
23-
assert user_record['auth0_id'] == auth0_id
24-
assert user_record['primary_country'] == primary_country
25-
assert user_record['username'] == username
26-
assert user_record['user_since'] == user_since
27-
28-
def test_create_profile_invalid(self):
29-
primary_country = 'United States'
30-
username = 'test_username'
31-
user_since = int(time.time() * 1000)
26+
27+
# Query the database directly to verify the record was created
28+
created_record = test_db.query(
29+
"SELECT * FROM user_profiles WHERE auth0_id = ?",
30+
(garbage_auth0_id,),
31+
).fetchone()
32+
33+
# Verify the record was created with the correct values
34+
assert created_record is not None
35+
assert created_record["auth0_id"] == garbage_auth0_id
36+
assert created_record["primary_country"] == primary_country
37+
assert created_record["username"] == username
38+
assert created_record["user_since"] == user_since
39+
40+
def test_create_profile_missing_auth0_id(self, test_db):
41+
# More descriptive test name for this specific invalid case
42+
primary_country = "us"
43+
username = "test_username"
44+
user_since = 20250101
45+
46+
# Test that we get an error when auth0_id is missing
3247
with pytest.raises(
3348
Exception,
3449
match=r"UserService.create_profile\(\) missing 1 required positional argument: 'auth0_id'",
3550
):
36-
userService.create_profile(
37-
primary_country=primary_country, username=username, user_since=user_since
51+
user_service.create_profile(
52+
primary_country=primary_country,
53+
username=username,
54+
user_since=user_since,
3855
)
3956

40-
def test_create_profile_duplicate(self):
41-
auth0_id = 'test-auth-id'
42-
primary_country = 'United States'
43-
username = 'test_username'
44-
user_since = int(time.time() * 1000)
45-
result1 = userService.create_profile(
46-
primary_country=primary_country, auth0_id=auth0_id, username=username, user_since=user_since
57+
# Verify that no record was created in the database
58+
records = test_db.query(
59+
"SELECT COUNT(*) as count FROM user_profiles WHERE username = ?",
60+
(username,),
61+
).fetchone()
62+
63+
assert records["count"] == 0
64+
65+
def test_create_profile_duplicate(self, test_db):
66+
garbage_auth0_id = "duplicate_test_id_456"
67+
primary_country = "us"
68+
username = "duplicate_test_username"
69+
user_since = 20250101
70+
71+
# Create the first profile and verify it was created
72+
result1 = user_service.create_profile(
73+
primary_country=primary_country,
74+
auth0_id=garbage_auth0_id,
75+
username=username,
76+
user_since=user_since,
4777
)
48-
49-
50-
result2 = userService.create_profile(
51-
primary_country=primary_country, auth0_id=auth0_id, username=username, user_since=user_since
78+
79+
assert result1[0] is True
80+
81+
# Verify the record exists in the database
82+
record_count_before = test_db.query(
83+
"SELECT COUNT(*) as count FROM user_profiles WHERE auth0_id = ?",
84+
(garbage_auth0_id,),
85+
).fetchone()
86+
87+
assert record_count_before["count"] == 1
88+
89+
# Attempt to create a duplicate profile
90+
result2 = user_service.create_profile(
91+
primary_country=primary_country,
92+
auth0_id=garbage_auth0_id,
93+
username=username,
94+
user_since=user_since,
5295
)
53-
assert result2[0] == False
96+
97+
# Verify that the second attempt returns False
98+
assert result2[0] is False
99+
100+
# Verify that no additional record was created in the database
101+
record_count_after = test_db.query(
102+
"SELECT COUNT(*) as count FROM user_profiles WHERE auth0_id = ?",
103+
(garbage_auth0_id,),
104+
).fetchone()
105+
106+
assert record_count_after["count"] == 1

0 commit comments

Comments
 (0)