-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch @validate_country to decorator
- Loading branch information
ldorner
authored and
ldorner
committed
Nov 12, 2024
1 parent
1e42f63
commit f4ea3f7
Showing
10 changed files
with
72 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
- bump: patch | ||
changes: | ||
changed | ||
- validate_country to decorator for validation on applicable functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
from policyengine_api.country import COUNTRIES, validate_country | ||
|
||
|
||
@validate_country | ||
def get_metadata(country_id: str) -> dict: | ||
"""Get metadata for a country. | ||
Args: | ||
country_id (str): The country ID. | ||
""" | ||
invalid_country = validate_country(country_id) | ||
if invalid_country: | ||
return invalid_country | ||
|
||
return COUNTRIES.get(country_id).metadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from flask import Response | ||
from policyengine_api.country import validate_country | ||
|
||
|
||
@validate_country | ||
def foo(country_id, other): | ||
""" | ||
A simple dummy test method for validation testing. Must be defined outside of the class (or within | ||
the test functions themselves) due to complications with the `self` parameter for class methods. | ||
""" | ||
return "bar" | ||
|
||
class TestValidateCountry: | ||
""" | ||
Test that the @validate_country decorator returns 404 if the country does not exist, otherwise | ||
continues execution of the function. | ||
""" | ||
|
||
def test_valid_country(self): | ||
result = foo("us", "extra_arg") | ||
assert result == "bar" | ||
|
||
def test_invalid_country(self): | ||
result = foo("baz", "extra_arg") | ||
assert isinstance(result, Response) | ||
assert result.status_code == 404 | ||
|
Oops, something went wrong.