Envirolytics_backend / test_endpoints.py
Ayush Modi
Clean deploy with LFS
e4667e2
import requests
import json
BASE_URL = "http://localhost:8000"
def print_result(name, input_data, response, expected=None):
print(f"\n--- TEST: {name} ---")
if input_data:
try:
print(f"Input: {json.dumps(input_data, indent=2)}")
except:
print(f"Input: {input_data}")
status = response.status_code
try:
data = response.json()
except:
data = response.text
print(f"Output ({status}): {json.dumps(data, indent=2)}")
if expected:
if isinstance(expected, dict):
matched = all(data.get(k) == v for k, v in expected.items() if k in data)
elif isinstance(expected, str):
matched = expected in str(data)
else:
matched = True
print(f"Result: {'PASS' if matched else 'FAIL'} (Expected: {expected})")
def test_live_status():
resp = requests.get(f"{BASE_URL}/api/live-status")
print_result("Live Status (Monitoring)", None, resp, {"status": "ONLINE"})
def test_source_attribution():
resp = requests.get(f"{BASE_URL}/api/analysis/source-attribution")
print_result("Source Attribution (AI)", None, resp, {"analysis_type": "SOURCE_ATTRIBUTION"})
def test_green_qr():
# Metro
payload = {
"user_gps": [28.6139, 77.2090],
"station_gps": [28.6140, 77.2090],
"transport_mode": "METRO"
}
resp = requests.post(f"{BASE_URL}/api/enforce/green-qr", json=payload)
print_result("Green QR (Metro)", payload, resp, {"credits_earned": 50})
# Carpool
payload["transport_mode"] = "CARPOOL"
resp = requests.post(f"{BASE_URL}/api/enforce/green-qr", json=payload)
print_result("Green QR (Carpool)", payload, resp, {"credits_earned": 20})
def test_marketplace():
listing = {
"farmer_id": "F_009",
"biomass_type": "STUBBLE",
"quantity_kg": 1000,
"price_per_kg": 2.0,
"gps": [30.5, 76.5],
"contact_info": "Phone: 9999999999"
}
resp = requests.post(f"{BASE_URL}/api/marketplace/list-biomass", json=listing)
print_result("Marketplace List", listing, resp, {"status": "LISTED"})
resp2 = requests.get(f"{BASE_URL}/api/marketplace/browse")
print_result("Marketplace Browse", None, resp2)
def test_farmer_subsidy():
# Fraud Test
payload = {
"farmer_id": "F_FRAUD",
"gps": [30.1, 75.2],
"claimed_biomass_sold_kg": 100,
"debug_fire_co": 5.0
}
resp = requests.post(f"{BASE_URL}/api/enforce/farmer-subsidy", json=payload)
print_result("Farmer (Fraud)", payload, resp, {"status": "FRAUD_DETECTED"})
if __name__ == "__main__":
print("Testing Updated Features...")
try:
test_live_status()
test_source_attribution()
test_green_qr()
test_marketplace()
test_farmer_subsidy()
except Exception as e:
print(f"Error: {e}")