From 11d3c86cf113e9d1d1097f9008d569b39a4e82fd Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Mon, 13 May 2024 15:26:39 -0700 Subject: [PATCH] test /spend/report --- tests/test_spend_logs.py | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tests/test_spend_logs.py b/tests/test_spend_logs.py index 477fdb86f..1a3373c9d 100644 --- a/tests/test_spend_logs.py +++ b/tests/test_spend_logs.py @@ -138,6 +138,23 @@ async def get_predict_spend_logs(session): return await response.json() +async def get_spend_report(session, start_date, end_date): + url = "http://0.0.0.0:4000/global/spend/report" + headers = {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"} + async with session.get( + url, headers=headers, params={"start_date": start_date, "end_date": end_date} + ) as response: + status = response.status + response_text = await response.text() + + print(response_text) + print() + + if status != 200: + raise Exception(f"Request did not return a 200 status code: {status}") + return await response.json() + + @pytest.mark.asyncio async def test_get_predicted_spend_logs(): """ @@ -205,3 +222,39 @@ async def test_spend_logs_high_traffic(): except: print(n, time.time() - start, 0) raise Exception("it worked!") + + +@pytest.mark.asyncio +async def test_spend_report_endpoint(): + async with aiohttp.ClientSession( + timeout=aiohttp.ClientTimeout(total=600) + ) as session: + import datetime + + todays_date = datetime.date.today() + datetime.timedelta(days=1) + todays_date = todays_date.strftime("%Y-%m-%d") + + print("todays_date", todays_date) + thirty_days_ago = ( + datetime.date.today() - datetime.timedelta(days=30) + ).strftime("%Y-%m-%d") + spend_report = await get_spend_report( + session=session, start_date=thirty_days_ago, end_date=todays_date + ) + print("spend report", spend_report) + + for row in spend_report: + date = row["group_by_day"] + teams = row["teams"] + for team in teams: + team_name = team["team_name"] + total_spend = team["total_spend"] + metadata = team["metadata"] + + assert team_name is not None + + print(f"Date: {date}") + print(f"Team: {team_name}") + print(f"Total Spend: {total_spend}") + print("Metadata: ", metadata) + print()