Skip to content

Commit

Permalink
Merge pull request #26 from PolicyEngine/chore/create_tests
Browse files Browse the repository at this point in the history
Create api-light calculate endpoint sync tests; merging despite known test failure, to be discussed with Nikhil at later stage
  • Loading branch information
anth-volk authored Dec 21, 2023
2 parents 62fbd47 + 736bd5f commit 4b3771b
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ debug:
FLASK_APP=policyengine_api_light.api FLASK_DEBUG=1 flask run --without-threads

test:
pytest -vv --durations=0 --timeout=150 -rP tests
pytest -vv --timeout=150 -rP tests

debug-test:
FLASK_DEBUG=1 pytest -vv --durations=0 --timeout=150 -rP tests
Expand Down
4 changes: 4 additions & 0 deletions changelog_entry.yaml
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
10 changes: 0 additions & 10 deletions tests/python/data/test_economy_1_policy_1.json

This file was deleted.

63 changes: 0 additions & 63 deletions tests/python/test_calculate_us_1.py

This file was deleted.

29 changes: 29 additions & 0 deletions tests/python/test_liveness.py
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
65 changes: 65 additions & 0 deletions tests/python/test_sync.py
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
20 changes: 20 additions & 0 deletions tests/python/utils.py
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

0 comments on commit 4b3771b

Please sign in to comment.