forked from sagnik/Project_Velocity
feat: Complete code integration of modules (#18)
The complete code integration is done. Co-authored-by: Sagnik <sagnik7896@gmail.com> Reviewed-on: sagnik/Project_Velocity#18
This commit is contained in:
94
backend/tests/test_catalyst_routes.py
Normal file
94
backend/tests/test_catalyst_routes.py
Normal file
@@ -0,0 +1,94 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from backend.api.routes_catalyst import router
|
||||
from backend.services.ad_network_service import BidAction, Platform
|
||||
|
||||
|
||||
def _build_client() -> TestClient:
|
||||
app = FastAPI()
|
||||
|
||||
async def _broadcast_live_event(*_args, **_kwargs):
|
||||
return None
|
||||
|
||||
app.state.broadcast_live_event = _broadcast_live_event
|
||||
app.include_router(router, prefix="/api/catalyst")
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
def test_catalyst_campaigns_and_google_budget_routes(monkeypatch) -> None:
|
||||
client = _build_client()
|
||||
|
||||
async def fake_list_campaigns(platform=None):
|
||||
return [
|
||||
type(
|
||||
"Campaign",
|
||||
(),
|
||||
{
|
||||
"id": "google-camp-001",
|
||||
"name": "Search Investors",
|
||||
"platform": Platform.GOOGLE,
|
||||
"status": type("Status", (), {"value": "active"})(),
|
||||
"daily_budget": 5000,
|
||||
"spent": 0,
|
||||
"objective": "SEARCH",
|
||||
"bid_strategy": "TARGET_ROAS",
|
||||
},
|
||||
)()
|
||||
]
|
||||
|
||||
async def fake_get_insights(**_kwargs):
|
||||
return [
|
||||
{
|
||||
"campaign_id": "google-camp-001",
|
||||
"spend": 2400,
|
||||
"impressions": 120000,
|
||||
"clicks": 4200,
|
||||
"conversions": 38,
|
||||
}
|
||||
]
|
||||
|
||||
async def fake_update_budget(_payload):
|
||||
return {"status": "ok", "platform": "google", "mode": "simulated"}
|
||||
|
||||
async def fake_update_bid_strategy(_payload):
|
||||
return BidAction(
|
||||
action_id="act-1",
|
||||
campaign_id="google-camp-001",
|
||||
platform=Platform.GOOGLE,
|
||||
old_strategy="TARGET_CPA",
|
||||
new_strategy="TARGET_ROAS",
|
||||
target_value=8.5,
|
||||
executed_at="2026-04-12T00:00:00Z",
|
||||
)
|
||||
|
||||
monkeypatch.setattr("backend.api.routes_catalyst.ad_network_service.list_campaigns", fake_list_campaigns)
|
||||
monkeypatch.setattr("backend.api.routes_catalyst.ad_network_service.get_insights", fake_get_insights)
|
||||
monkeypatch.setattr("backend.api.routes_catalyst.ad_network_service.update_budget", fake_update_budget)
|
||||
monkeypatch.setattr("backend.api.routes_catalyst.ad_network_service.update_bid_strategy", fake_update_bid_strategy)
|
||||
|
||||
campaigns = client.get("/api/catalyst/campaigns")
|
||||
assert campaigns.status_code == 200
|
||||
assert campaigns.json()["data"][0]["platform"] == "google"
|
||||
assert campaigns.json()["data"][0]["conversions"] == 38
|
||||
|
||||
budget = client.put(
|
||||
"/api/catalyst/budget",
|
||||
json={"campaign_id": "google-camp-001", "platform": "google", "daily_budget": 6500},
|
||||
)
|
||||
assert budget.status_code == 200
|
||||
assert budget.json()["data"]["platform"] == "google"
|
||||
|
||||
bid = client.put(
|
||||
"/api/catalyst/bid-strategy",
|
||||
json={
|
||||
"campaign_id": "google-camp-001",
|
||||
"platform": "google",
|
||||
"strategy": "TARGET_ROAS",
|
||||
"target_value": 8.5,
|
||||
},
|
||||
)
|
||||
assert bid.status_code == 200
|
||||
assert bid.json()["data"]["new_strategy"] == "TARGET_ROAS"
|
||||
Reference in New Issue
Block a user