-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from PolicyEngine/chore/create_tests
Create api-light calculate endpoint sync tests; merging despite known test failure, to be discussed with Nikhil at later stage
- Loading branch information
Showing
7 changed files
with
119 additions
and
74 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
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: | ||
- Added tests to ensure API-light in sync with API |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,29 @@ | ||
from tests.python.utils import client | ||
|
||
|
||
def test_calculate_liveness(client): | ||
"""This tests that, when passed relevant data, calculate endpoint functions properly""" | ||
response = client.post( | ||
"/us/calculate", | ||
headers={"Content-Type": "application/json"}, | ||
data=open( | ||
"./tests/python/data/calculate_us_1_data.json", | ||
"r", | ||
encoding="utf-8", | ||
), | ||
) | ||
assert response.status_code == 200, response.text | ||
|
||
|
||
def test_calculate_full_liveness(client): | ||
"""This tests that, when passed relevant data, calculate endpoint functions properly""" | ||
response = client.post( | ||
"/us/calculate-full", | ||
headers={"Content-Type": "application/json"}, | ||
data=open( | ||
"./tests/python/data/calculate_us_1_data.json", | ||
"r", | ||
encoding="utf-8", | ||
), | ||
) | ||
assert response.status_code == 200, response.text |
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,65 @@ | ||
import requests | ||
import json | ||
import sys | ||
|
||
from tests.python.utils import client, extract_json_from_file | ||
|
||
API_URL = "https://api.policyengine.org/" | ||
|
||
|
||
def test_calculate_sync(client): | ||
"""Confirm that the calculate endpoint outputs the same data as the main API""" | ||
|
||
country_id = "us" | ||
|
||
# Load the sample data | ||
input_data = extract_json_from_file( | ||
"./tests/python/data/calculate_us_1_data.json" | ||
) | ||
|
||
# Make a POST request to the API and store its output | ||
resAPI = requests.post( | ||
API_URL + "/" + country_id + "/calculate", json=input_data | ||
).json() | ||
|
||
# Mock a POST request to API-light | ||
resLight = client.post( | ||
"/" + country_id + "/calculate", | ||
headers={"Content-Type": "application/json"}, | ||
json=input_data, | ||
).get_json() | ||
|
||
# Compare the outputs | ||
assert resAPI == resLight | ||
|
||
|
||
def test_calculate_full_sync(client): | ||
"""Confirm that the calculate endpoint outputs the same data as the main API""" | ||
|
||
country_id = "us" | ||
|
||
# Load the sample data | ||
input_data = extract_json_from_file( | ||
"./tests/python/data/calculate_us_1_data.json" | ||
) | ||
|
||
# Make a POST request to the API and store its output | ||
resAPI = requests.post( | ||
API_URL + "/" + country_id + "/calculate-full", json=input_data | ||
).json() | ||
|
||
# Mock a POST request to API-light | ||
resLight = client.post( | ||
"/" + country_id + "/calculate-full", | ||
headers={"Content-Type": "application/json"}, | ||
json=input_data, | ||
).get_json() | ||
|
||
with open("resAPI.json", "w+") as outfile: | ||
outfile.write(json.dumps(resAPI)) | ||
|
||
with open("resLight.json", "w+") as outfile: | ||
outfile.write(json.dumps(resLight)) | ||
|
||
# Compare the outputs | ||
assert resAPI == resLight |
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,20 @@ | ||
import pytest | ||
import json | ||
import requests | ||
from policyengine_api_light.api import app | ||
|
||
|
||
@pytest.fixture | ||
def client(): | ||
app.config["TESTING"] = True | ||
with app.test_client() as client: | ||
yield client | ||
|
||
|
||
def extract_json_from_file(filepath): | ||
extracted_data = None | ||
|
||
with open(filepath, "r", encoding="utf-8") as file: | ||
extracted_data = json.load(file) | ||
|
||
return extracted_data |