|
1 | 1 | import pytest
|
2 |
| -import unittest.mock as mock |
3 |
| -import time |
4 | 2 | from policyengine_api.services.user_service import UserService
|
5 | 3 |
|
6 |
| -userService = UserService() |
| 4 | +user_service = UserService() |
| 5 | + |
7 | 6 |
|
8 | 7 | class TestCreateProfile:
|
9 | 8 |
|
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, |
18 | 22 | )
|
19 |
| - |
| 23 | + |
| 24 | + # Verify the result from the service |
20 | 25 | 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 |
32 | 47 | with pytest.raises(
|
33 | 48 | Exception,
|
34 | 49 | match=r"UserService.create_profile\(\) missing 1 required positional argument: 'auth0_id'",
|
35 | 50 | ):
|
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, |
38 | 55 | )
|
39 | 56 |
|
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, |
47 | 77 | )
|
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, |
52 | 95 | )
|
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