Add models and routes for costs, consumption, equipment, maintenance, and production; implement CRUD operations and unit tests
This commit is contained in:
42
tests/unit/test_consumption.py
Normal file
42
tests/unit/test_consumption.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from config.database import Base, engine
|
||||
|
||||
# Setup and teardown
|
||||
|
||||
|
||||
def setup_module(module):
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
def teardown_module(module):
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_create_and_list_consumption():
|
||||
# Create a scenario to attach consumption
|
||||
resp = client.post(
|
||||
"/api/scenarios/", json={"name": "ConsScenario", "description": "consumption scenario"}
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
scenario = resp.json()
|
||||
sid = scenario["id"]
|
||||
|
||||
# Create Consumption item
|
||||
cons_payload = {"scenario_id": sid, "amount": 250.0,
|
||||
"description": "Monthly consumption"}
|
||||
resp2 = client.post("/api/consumption/", json=cons_payload)
|
||||
assert resp2.status_code == 200
|
||||
cons = resp2.json()
|
||||
assert cons["scenario_id"] == sid
|
||||
assert cons["amount"] == 250.0
|
||||
|
||||
# List Consumption items
|
||||
resp3 = client.get("/api/consumption/")
|
||||
assert resp3.status_code == 200
|
||||
data = resp3.json()
|
||||
assert any(item["amount"] == 250.0 and item["scenario_id"]
|
||||
== sid for item in data)
|
||||
58
tests/unit/test_costs.py
Normal file
58
tests/unit/test_costs.py
Normal file
@@ -0,0 +1,58 @@
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from config.database import Base, engine
|
||||
|
||||
# Setup and teardown
|
||||
|
||||
|
||||
def setup_module(module):
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
def teardown_module(module):
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_create_and_list_capex_and_opex():
|
||||
# Create a scenario to attach costs
|
||||
resp = client.post(
|
||||
"/api/scenarios/", json={"name": "CostScenario", "description": "cost scenario"}
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
scenario = resp.json()
|
||||
sid = scenario["id"]
|
||||
|
||||
# Create Capex item
|
||||
capex_payload = {"scenario_id": sid,
|
||||
"amount": 1000.0, "description": "Initial capex"}
|
||||
resp2 = client.post("/api/costs/capex", json=capex_payload)
|
||||
assert resp2.status_code == 200
|
||||
capex = resp2.json()
|
||||
assert capex["scenario_id"] == sid
|
||||
assert capex["amount"] == 1000.0
|
||||
|
||||
# List Capex items
|
||||
resp3 = client.get("/api/costs/capex")
|
||||
assert resp3.status_code == 200
|
||||
data = resp3.json()
|
||||
assert any(item["amount"] == 1000.0 and item["scenario_id"]
|
||||
== sid for item in data)
|
||||
|
||||
# Create Opex item
|
||||
opex_payload = {"scenario_id": sid, "amount": 500.0,
|
||||
"description": "Recurring opex"}
|
||||
resp4 = client.post("/api/costs/opex", json=opex_payload)
|
||||
assert resp4.status_code == 200
|
||||
opex = resp4.json()
|
||||
assert opex["scenario_id"] == sid
|
||||
assert opex["amount"] == 500.0
|
||||
|
||||
# List Opex items
|
||||
resp5 = client.get("/api/costs/opex")
|
||||
assert resp5.status_code == 200
|
||||
data_o = resp5.json()
|
||||
assert any(item["amount"] == 500.0 and item["scenario_id"]
|
||||
== sid for item in data_o)
|
||||
42
tests/unit/test_equipment.py
Normal file
42
tests/unit/test_equipment.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from config.database import Base, engine
|
||||
|
||||
# Setup and teardown
|
||||
|
||||
|
||||
def setup_module(module):
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
def teardown_module(module):
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_create_and_list_equipment():
|
||||
# Create a scenario to attach equipment
|
||||
resp = client.post(
|
||||
"/api/scenarios/", json={"name": "EquipScenario", "description": "equipment scenario"}
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
scenario = resp.json()
|
||||
sid = scenario["id"]
|
||||
|
||||
# Create Equipment item
|
||||
eq_payload = {"scenario_id": sid, "name": "Excavator",
|
||||
"description": "Heavy machinery"}
|
||||
resp2 = client.post("/api/equipment/", json=eq_payload)
|
||||
assert resp2.status_code == 200
|
||||
eq = resp2.json()
|
||||
assert eq["scenario_id"] == sid
|
||||
assert eq["name"] == "Excavator"
|
||||
|
||||
# List Equipment items
|
||||
resp3 = client.get("/api/equipment/")
|
||||
assert resp3.status_code == 200
|
||||
data = resp3.json()
|
||||
assert any(item["name"] == "Excavator" and item["scenario_id"]
|
||||
== sid for item in data)
|
||||
41
tests/unit/test_maintenance.py
Normal file
41
tests/unit/test_maintenance.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from config.database import Base, engine
|
||||
|
||||
# Setup and teardown
|
||||
|
||||
|
||||
def setup_module(module):
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
def teardown_module(module):
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_create_and_list_maintenance():
|
||||
# Create a scenario to attach maintenance
|
||||
resp = client.post(
|
||||
"/api/scenarios/", json={"name": "MaintScenario", "description": "maintenance scenario"}
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
scenario = resp.json()
|
||||
sid = scenario["id"]
|
||||
|
||||
# Create Maintenance record
|
||||
maint_payload = {"scenario_id": sid, "details": "Routine check"}
|
||||
resp2 = client.post("/api/maintenance/", json=maint_payload)
|
||||
assert resp2.status_code == 200
|
||||
maint = resp2.json()
|
||||
assert maint["scenario_id"] == sid
|
||||
assert maint["details"] == "Routine check"
|
||||
|
||||
# List Maintenance records
|
||||
resp3 = client.get("/api/maintenance/")
|
||||
assert resp3.status_code == 200
|
||||
data = resp3.json()
|
||||
assert any(item["details"] ==
|
||||
"Routine check" and item["scenario_id"] == sid for item in data)
|
||||
35
tests/unit/test_production.py
Normal file
35
tests/unit/test_production.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from fastapi.testclient import TestClient
|
||||
from main import app
|
||||
from config.database import Base, engine
|
||||
|
||||
# Setup and teardown
|
||||
def setup_module(module):
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
def teardown_module(module):
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
def test_create_and_list_production_output():
|
||||
# Create a scenario to attach production output
|
||||
resp = client.post(
|
||||
"/api/scenarios/", json={"name": "ProdScenario", "description": "production scenario"}
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
scenario = resp.json()
|
||||
sid = scenario["id"]
|
||||
|
||||
# Create Production Output item
|
||||
prod_payload = {"scenario_id": sid, "amount": 300.0, "description": "Daily output"}
|
||||
resp2 = client.post("/api/production/", json=prod_payload)
|
||||
assert resp2.status_code == 200
|
||||
prod = resp2.json()
|
||||
assert prod["scenario_id"] == sid
|
||||
assert prod["amount"] == 300.0
|
||||
|
||||
# List Production Output items
|
||||
resp3 = client.get("/api/production/")
|
||||
assert resp3.status_code == 200
|
||||
data = resp3.json()
|
||||
assert any(item["amount"] == 300.0 and item["scenario_id"] == sid for item in data)
|
||||
Reference in New Issue
Block a user