Skip to content

Fix issues in test_create_profile and changelog #2348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .coverage
Binary file not shown.
4 changes: 0 additions & 4 deletions .github/publish-git-tag.sh

This file was deleted.

Empty file removed changelog_entry.yaml
Empty file.
97 changes: 97 additions & 0 deletions tests/unit/services/test_create_profile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import pytest
from policyengine_api.services.user_service import UserService
from datetime import datetime

user_service = UserService()


class TestCreateProfile:

# Test creating a valid user profile
def test_create_profile_valid(self, test_db):
garbage_auth0_id = "test_garbage_auth0_id_123"
primary_country = "us"
username = "test_username"
user_since = int(datetime.now().timestamp())

result = user_service.create_profile(
primary_country=primary_country,
auth0_id=garbage_auth0_id,
username=username,
user_since=user_since,
)

assert result[0] is True

created_record = test_db.query(
"SELECT * FROM user_profiles WHERE auth0_id = ?",
(garbage_auth0_id,),
).fetchone()

assert created_record is not None
assert created_record["auth0_id"] == garbage_auth0_id
assert created_record["primary_country"] == primary_country
assert created_record["username"] == username
assert created_record["user_since"] == user_since

# Test that creating a profile without auth0_id raises an exception
def test_create_profile_missing_auth0_id(self, test_db):
primary_country = "us"
username = "test_username"
user_since = int(datetime.now().timestamp())

with pytest.raises(
Exception,
match=r"UserService.create_profile\(\) missing 1 required positional argument: 'auth0_id'",
):
user_service.create_profile(
primary_country=primary_country,
username=username,
user_since=user_since,
)

records = test_db.query(
"SELECT COUNT(*) as count FROM user_profiles WHERE username = ?",
(username,),
).fetchone()

assert records["count"] == 0

# Test that creating a duplicate profile returns False
def test_create_profile_duplicate(self, test_db):
garbage_auth0_id = "duplicate_test_id_456"
primary_country = "us"
username = "duplicate_test_username"
user_since = int(datetime.now().timestamp())

result1 = user_service.create_profile(
primary_country=primary_country,
auth0_id=garbage_auth0_id,
username=username,
user_since=user_since,
)

assert result1[0] is True

record_count_before = test_db.query(
"SELECT COUNT(*) as count FROM user_profiles WHERE auth0_id = ?",
(garbage_auth0_id,),
).fetchone()

assert record_count_before["count"] == 1

result2 = user_service.create_profile(
primary_country=primary_country,
auth0_id=garbage_auth0_id,
username=username,
user_since=user_since,
)

assert result2[0] is False

record_count_after = test_db.query(
"SELECT COUNT(*) as count FROM user_profiles WHERE auth0_id = ?",
(garbage_auth0_id,),
).fetchone()

assert record_count_after["count"] == 1
Loading