68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
import base64
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
|
|
import app as rollcalc_app
|
|
|
|
|
|
class CalculationApiTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.temp_dir = tempfile.TemporaryDirectory()
|
|
self.original_log_file = rollcalc_app.LOG_FILE
|
|
rollcalc_app.LOG_FILE = os.path.join(self.temp_dir.name, "access.json")
|
|
rollcalc_app.app.config.update(TESTING=True)
|
|
self.client = rollcalc_app.app.test_client()
|
|
credentials = base64.b64encode(b"mtazl:rollcalc").decode("ascii")
|
|
self.headers = {"Authorization": f"Basic {credentials}"}
|
|
|
|
def tearDown(self):
|
|
rollcalc_app.LOG_FILE = self.original_log_file
|
|
self.temp_dir.cleanup()
|
|
|
|
def test_calculation_endpoint_returns_machine_readable_authoritative_result(self):
|
|
response = self.client.post(
|
|
"/api/calculations/roll",
|
|
json={
|
|
"article_number": "180205",
|
|
"roll_length_m": 65,
|
|
"core_diameter_mm": 150,
|
|
},
|
|
headers=self.headers,
|
|
)
|
|
result = response.get_json()
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertEqual(result["status"], "success")
|
|
self.assertEqual(result["resolved_article"]["number"], "180205")
|
|
self.assertEqual(
|
|
result["provenance"]["calculator"],
|
|
"roll_calculation.calculate_roll",
|
|
)
|
|
|
|
def test_modification_endpoint_changes_state_and_recalculates(self):
|
|
response = self.client.post(
|
|
"/api/calculations/roll/modify",
|
|
json={
|
|
"state": {
|
|
"request": {
|
|
"article_number": "180205",
|
|
"roll_length_m": 65,
|
|
"core_diameter_mm": 150,
|
|
}
|
|
},
|
|
"changes": {"roll_length_m": 80},
|
|
},
|
|
headers=self.headers,
|
|
)
|
|
result = response.get_json()
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
self.assertEqual(result["status"], "success")
|
|
self.assertEqual(result["state"]["request"]["roll_length_m"], 80.0)
|
|
self.assertEqual(result["calculation"]["effective_roll_length_m"], 80.0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|