バックエンドAPI
This commit is contained in:
80
verify_possible_orders.py
Normal file
80
verify_possible_orders.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import requests
|
||||
import json
|
||||
from diplomacy import Game
|
||||
|
||||
BASE_URL = "http://127.0.0.1:8000"
|
||||
|
||||
def get_initial_state():
|
||||
resp = requests.get(f"{BASE_URL}/game/initial-state")
|
||||
return resp.json()
|
||||
|
||||
def test_possible_orders_default():
|
||||
print("--- Testing Default ---")
|
||||
state = get_initial_state()
|
||||
resp = requests.post(f"{BASE_URL}/calculate/possible-orders", json=state)
|
||||
data = resp.json()
|
||||
# Expect loc -> list
|
||||
if "possible_orders" in data and isinstance(data["possible_orders"], dict):
|
||||
first_key = list(data["possible_orders"].keys())[0]
|
||||
if isinstance(data["possible_orders"][first_key], list):
|
||||
print("OK: Default format is loc -> list")
|
||||
else:
|
||||
print(f"FAILED: Expected list for {first_key}, got {type(data['possible_orders'][first_key])}")
|
||||
else:
|
||||
print("FAILED: Response structure incorrect")
|
||||
|
||||
def test_possible_orders_power():
|
||||
print("--- Testing Power Name (FRANCE) ---")
|
||||
state = get_initial_state()
|
||||
resp = requests.post(f"{BASE_URL}/calculate/possible-orders?power_name=FRANCE", json=state)
|
||||
data = resp.json()
|
||||
if data.get("power") == "FRANCE" and "possible_orders" in data:
|
||||
# Check if only France units are there
|
||||
locs = list(data["possible_orders"].keys())
|
||||
# Initial France units are at BRE, MAR, PAR
|
||||
expected = {"BRE", "MAR", "PAR"}
|
||||
if set(locs) == expected:
|
||||
print("OK: Filtered by power correctly")
|
||||
else:
|
||||
print(f"FAILED: Expected locs {expected}, got {locs}")
|
||||
else:
|
||||
print(f"FAILED: Response structure incorrect: {data}")
|
||||
|
||||
def test_possible_orders_by_power():
|
||||
print("--- Testing By Power ---")
|
||||
state = get_initial_state()
|
||||
resp = requests.post(f"{BASE_URL}/calculate/possible-orders?by_power=true", json=state)
|
||||
data = resp.json()
|
||||
if "possible_orders" in data and "FRANCE" in data["possible_orders"]:
|
||||
france_orders = data["possible_orders"]["FRANCE"]
|
||||
if isinstance(france_orders, dict) and "PAR" in france_orders:
|
||||
print("OK: Grouped by power correctly")
|
||||
else:
|
||||
print(f"FAILED: France orders structure incorrect: {france_orders}")
|
||||
else:
|
||||
print(f"FAILED: Response structure incorrect or FRANCE missing: {data.keys()}")
|
||||
|
||||
def test_possible_orders_both():
|
||||
print("--- Testing Both (By Power + FRANCE) ---")
|
||||
state = get_initial_state()
|
||||
resp = requests.post(f"{BASE_URL}/calculate/possible-orders?by_power=true&power_name=FRANCE", json=state)
|
||||
data = resp.json()
|
||||
# My implementation returns just that power in filtered format if both provided?
|
||||
# Actually, my code:
|
||||
# if by_power:
|
||||
# ...
|
||||
# if power_name:
|
||||
# return {"power": power_name, "possible_orders": results[power_name]}
|
||||
if data.get("power") == "FRANCE" and "possible_orders" in data:
|
||||
print("OK: Both parameters handled correctly (returned filtered power)")
|
||||
else:
|
||||
print(f"FAILED: Response structure incorrect: {data}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
test_possible_orders_default()
|
||||
test_possible_orders_power()
|
||||
test_possible_orders_by_power()
|
||||
test_possible_orders_both()
|
||||
except Exception as e:
|
||||
print(f"Error during testing: {e}")
|
||||
Reference in New Issue
Block a user