Setup
This commit is contained in:
+27
-3
@@ -18,7 +18,7 @@ def running_server(*arguments, stop_signal=signal.SIGTERM):
|
||||
with tempfile.TemporaryDirectory(prefix="fpol_test_") as directory:
|
||||
log_path = pathlib.Path(directory) / "server.log"
|
||||
with log_path.open("w") as log:
|
||||
process = subprocess.Popen([str(SERVER), "--port", "0", *map(str, arguments)], stdout=log, stderr=log)
|
||||
process = subprocess.Popen([str(SERVER), "--port", "0", "--database", str(pathlib.Path(directory) / "league.sqlite"), *map(str, arguments)], stdout=log, stderr=log)
|
||||
try:
|
||||
deadline = time.monotonic() + 10
|
||||
while time.monotonic() < deadline:
|
||||
@@ -44,10 +44,10 @@ def running_server(*arguments, stop_signal=signal.SIGTERM):
|
||||
if return_code != 0:
|
||||
raise AssertionError(f"Server exited with {return_code}:\n{log_path.read_text()}")
|
||||
|
||||
def request(port, path, method="GET"):
|
||||
def request(port, path, method="GET", body=None):
|
||||
connection = http.client.HTTPConnection("127.0.0.1", port, timeout=3)
|
||||
try:
|
||||
connection.request(method, path)
|
||||
connection.request(method, path, body=json.dumps(body) if body is not None else None, headers={"Content-Type": "application/json"})
|
||||
response = connection.getresponse()
|
||||
return response.status, response.getheader("Content-Type"), response.read()
|
||||
finally:
|
||||
@@ -66,6 +66,30 @@ class server_tests(unittest.TestCase):
|
||||
self.assertEqual(status, 404)
|
||||
self.assertEqual(json.loads(body)["code"], "not_found")
|
||||
self.assertEqual(request(port, "/api/health", "POST")[0], 404)
|
||||
def test_catalog_rules_and_validation_routes(self):
|
||||
with running_server() as port:
|
||||
self.assertEqual(json.loads(request(port, "/api/catalog")[2])["payload"], {"documents": []})
|
||||
self.assertEqual(len(json.loads(request(port, "/api/rules")[2])["payload"]["slots"]), 12)
|
||||
status, _, body = request(port, "/api/capacity", "POST", {"catalog_id": "missing", "players": 12})
|
||||
self.assertEqual(status, 400)
|
||||
self.assertEqual(json.loads(body)["code"], "invalid_argument")
|
||||
self.assertEqual(request(port, "/api/score-preview", "POST", {})[0], 400)
|
||||
self.assertEqual(request(port, "/api/capacity")[0], 404)
|
||||
def test_seeded_catalog_capacity_and_scoring(self):
|
||||
demo = pathlib.Path(__file__).resolve().parents[1] / "data" / "demo"
|
||||
with tempfile.TemporaryDirectory(prefix="fpol_api_") as directory:
|
||||
database = pathlib.Path(directory) / "league.sqlite"
|
||||
result = subprocess.run([str(SERVER.with_name("fpol_admin")), "--database", str(database), "import", str(demo / "catalog.json"), str(demo / "ratings.json"), str(demo / "results-3.json")], capture_output=True, text=True, timeout=10)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
with running_server("--database", database) as port:
|
||||
catalog = json.loads(request(port, "/api/catalog")[2])["payload"]
|
||||
self.assertEqual(len(catalog["documents"]), 3)
|
||||
status, _, body = request(port, "/api/capacity", "POST", json.loads((demo / "capacity.json").read_text()))
|
||||
self.assertEqual(status, 200)
|
||||
self.assertTrue(json.loads(body)["payload"]["sufficient_candidates"])
|
||||
status, _, body = request(port, "/api/score-preview", "POST", json.loads((demo / "preview.json").read_text()))
|
||||
self.assertEqual(status, 200)
|
||||
self.assertEqual(json.loads(body)["payload"]["total"], 528.24)
|
||||
def test_static_assets_and_route_boundaries(self):
|
||||
with tempfile.TemporaryDirectory(prefix="fpol_web_") as directory:
|
||||
root = pathlib.Path(directory)
|
||||
|
||||
Reference in New Issue
Block a user