Skip to content

Create test_create_profile.py #2250

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

Closed
wants to merge 3 commits into from

Conversation

SongmingFan123
Copy link
Collaborator

No description provided.

Copy link

codecov bot commented Mar 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 76.16%. Comparing base (39fb610) to head (b5288ef).
Report is 62 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2250      +/-   ##
==========================================
+ Coverage   75.84%   76.16%   +0.31%     
==========================================
  Files          83       84       +1     
  Lines        3192     3226      +34     
  Branches      348      349       +1     
==========================================
+ Hits         2421     2457      +36     
+ Misses        702      701       -1     
+ Partials       69       68       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@anth-volk anth-volk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this @SongmingFan123. I had some requested changes. Please feel free to reach out with any questions, etc.


class TestCreateProfile:

def fetch_created_record(self, auth0_id):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue, blocking: This as a method used in testing could be more brittle and error-prone than fetching data back out of the database itself via an ORM

The issue here is twofold:

  1. You're using one method from this service to validate the other. If that first method works well and never changes, then there'll be no problem, but if the method is ever changed or contains bugs, that will have unanticipated side effects for this test.
  2. By using a method, you move away from imagining the test as a black box that takes inputs and validates their intended outputs. create_profile is written to emit something to a database, and thus the most direct way to test this would be to check that it emitted it to the database.

To move forward here, I'd write a new fixture to fetch records out of the database, then save that fixture in fixtures/services/create_profile.py. Feel free to add any questions here.

return userService.get_profile(auth0_id)

def test_create_profile_valid(self):
auth0_id = 'test-auth-id'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd be a bit more explicit here and name this something like garbage_auth0_id so as to not imply any formatting to match any internal structure auth0 might provide

import unittest.mock as mock
from policyengine_api.services.user_service import UserService

userService = UserService()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Python typically uses snake_case, not camelCase.

primary_country=primary_country, auth0_id=auth0_id, username=username, user_since=user_since
)

assert result[0] is True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment: If fetching directly from the db, this line would be removed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: What does this line do now?

assert user_record['username'] == username
assert user_record['user_since'] == user_since

def test_create_profile_invalid(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Make this title a bit more descriptive, as there are a few different "invalid" sets of arguments

assert user_record['user_since'] == user_since

def test_create_profile_invalid(self):
primary_country = 'United States'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: This isn't clear anywhere in the repo, but this is actually formatted as "us" for the US, "uk" for the UK, etc.; could you change this and the other one above to be of that format?

primary_country=primary_country, username=username, user_since=user_since
)

def test_create_profile_duplicate(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue, blocking: I'm not sure this test correctly tests the feature

The way I'd do this is to involve the database directly - emit the first one, check that it's present, then emit the second one and make sure that only one record remains

More broadly, what is the expected behavior when a duplicate is emitted? Maybe this should be raising an error or something else?

@SongmingFan123
Copy link
Collaborator Author

Thank you very much Anthony for your detailed instructions and issues with the code. I have just created another PR for the updated code.

@SongmingFan123
Copy link
Collaborator Author

For the database, I used the test_db fixture in the conftest.py within tests/unit.

@anth-volk
Copy link
Collaborator

@SongmingFan Before I review, could you run make format and add a changelog entry, following the instructions in the README?

@SongmingFan123
Copy link
Collaborator Author

@SongmingFan Before I review, could you run make format and add a changelog entry, following the instructions in the README?

I’ll fix that, thanks for the feedback

@SongmingFan123
Copy link
Collaborator Author

Just pushed the changed I made, thanks

Comment on lines +7 to +17
## [3.12.11] - 2025-03-22 13:20:22

### Added

- Create test_create_profile

## [3.12.10] - 2025-03-10 12:55:10

### Changed

- Update PolicyEngine US to 1.208.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue, blocking: Code should not be added to CHANGELOG.md; please remove

Apologies if the instructions in our PR chat were unclear, but only changelog_entry.yaml should be modified when submitting a PR. The information from this file will automatically be added to CHANGELOG.md.

@@ -5367,6 +5378,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0



[3.12.10]: https://github.com/PolicyEngine/policyengine-api/compare/3.12.9...3.12.10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue, blocking: This code should be removed

This is the same as above

Comment on lines +4474 to +4478
- bump: patch
changes:
changed:
- Update PolicyEngine US to 1.208.0
date: 2025-03-10 12:55:10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue, blocking: This code should be added to changelog_entry.yaml, not changelog.yaml

The entry will automatically be added to changelog_entry.yaml as part of the GitHub Actions scripts and should not be included here.

@@ -6,7 +6,7 @@
POST = "POST"
UPDATE = "UPDATE"
LIST = "LIST"
VERSION = "3.12.9"
VERSION = "3.12.10"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue, blocking: This code should not be updated

This will be updated automatically as part of the merge process and should not be modified

Comment on lines +27 to +28
"policyengine_us==1.208.0",
"policyengine_core>=3.12.10",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Did you modify this?

If not and all of my comments seem unexpected (I'm starting to suspect they are), I'd recommend switching to your local master branch, running git pull origin master, then switching back to your feature branch and running git rebase master to fix this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It likely has to do with my local branch. Sorry about that.

primary_country=primary_country, auth0_id=auth0_id, username=username, user_since=user_since
)

assert result[0] is True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: What does this line do now?

username = "test_username"
user_since = 2025

def fetch_created_record():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, non-blocking: I'd probably not write this as a separate function and just fetch the record back out using this code

assert user_record is not None
assert (
user_record["auth0_id"] == auth0_id
) # Ensure column name is correct
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: What is the purpose of this comment?

auth0_id = "test_auth0_id"
primary_country = "us"
username = "test_username"
user_since = 2025
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue, blocking: Here, too, this should be a BIGINT value

@anth-volk
Copy link
Collaborator

Superseded by #2348.

@anth-volk anth-volk closed this Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants