-
Notifications
You must be signed in to change notification settings - Fork 35
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. 🚀 New features to boost your workflow:
|
There was a problem hiding this 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): |
There was a problem hiding this comment.
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:
- 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.
- 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' |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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' |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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?
Thank you very much Anthony for your detailed instructions and issues with the code. I have just created another PR for the updated code. |
For the database, I used the test_db fixture in the conftest.py within tests/unit. |
@SongmingFan Before I review, could you run |
I’ll fix that, thanks for the feedback |
Just pushed the changed I made, thanks |
## [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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
- bump: patch | ||
changes: | ||
changed: | ||
- Update PolicyEngine US to 1.208.0 | ||
date: 2025-03-10 12:55:10 |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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
"policyengine_us==1.208.0", | ||
"policyengine_core>=3.12.10", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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(): |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
Superseded by #2348. |
No description provided.