Skip to content
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

Switch @validate_country to decorator #1970

Merged
merged 5 commits into from
Nov 23, 2024

Conversation

ldorner
Copy link
Collaborator

@ldorner ldorner commented Nov 12, 2024

Overview

Currently, there is a repeated check and short-circuit in many functions to validate existence of the given country ID. This PR refactors validate_country as a decorator to DRY the code. We may consider transitioning this to a before_request hook to further reduce code repetition.

This is essentially a refactor. There should be no functional change.

Changes

  • Update validate_country to change it into a decorator/wrapper
  • Replace instances of validate_country() check & short circuit -> @validate_country decorator
  • Add unit tests for the validate_country function
  • Add a sanity test in test_policy.py to ensure that the decorator functionality works

@ldorner ldorner force-pushed the ldorner/use_validation_hook_check_country branch from f4ea3f7 to e277100 Compare November 12, 2024 17:26
@@ -50,9 +51,6 @@ def get_economic_impact(
dict: The economic impact.
"""
print(f"Got request for {country_id} {policy_id} {baseline_policy_id}")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

With this change, this line will no longer print for invalid countries. Is it important to preserve this behavior (ie: we want to print all requested countries)? If so, perhaps it's best to add this printing/logging elsewhere. Otherwise, perhaps it can be removed entirely?

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, I don't think it's important to maintain this logging. Removing is totally fine.

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 looks like this function moved to policyengine_api/routes/economy_routes.py. I've made the update there and removed his comment.

@anth-volk
Copy link
Collaborator

Thank you for this work @ldorner. I will review probably tomorrow, depending on progress on some things I need to catch up on from some time off.

@ldorner ldorner force-pushed the ldorner/use_validation_hook_check_country branch from b897af6 to 6030263 Compare November 20, 2024 08:01
@ldorner
Copy link
Collaborator Author

ldorner commented Nov 20, 2024

I rebased to pull in recent changes/resolve conflicts.

@anth-volk
Copy link
Collaborator

@ldorner Unfortunately, it looks like the added test still fails due to accessing the keys() attribute of a tuple, which does not possess them.

@ldorner
Copy link
Collaborator Author

ldorner commented Nov 21, 2024

@ldorner Unfortunately, it looks like the added test still fails due to accessing the keys() attribute of a tuple, which does not possess them.

Sorry! Bad merge. Fixed now :)

@anth-volk
Copy link
Collaborator

@ldorner Thanks again for this, will review early tomorrow

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.

@ldorner This is perfect! Thank you very much, this removes so many instances of us importing and running the old function.

@anth-volk anth-volk merged commit 9ab20da into master Nov 23, 2024
4 checks passed
@anth-volk anth-volk deleted the ldorner/use_validation_hook_check_country branch November 23, 2024 00:20
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.

2 participants