Skip to content

Commit 59219ef

Browse files
authored
Add integration test for usergroup costs (#96)
* Add output data to run assertion against * Add integration test
1 parent 06814b4 commit 59219ef

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

tests/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ def output_cost_per_user():
5555
return data
5656

5757

58+
@pytest.fixture(scope="function")
59+
def output_cost_per_group():
60+
"""
61+
Output to assert against query_total_costs_per_group function.
62+
"""
63+
with open("tests/data/test_output_cost_per_group.json") as f:
64+
data = json.load(f)
65+
return data
66+
67+
5868
@pytest.fixture(autouse=True)
5969
def env_vars(monkeypatch):
6070
"""
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
"cost": 9.804900000000002,
4+
"date": "2025-09-01",
5+
"usergroup": "none"
6+
},
7+
{
8+
"cost": 1.9541,
9+
"date": "2025-09-01",
10+
"usergroup": "group_1"
11+
},
12+
{
13+
"cost": 4.3109,
14+
"date": "2025-09-01",
15+
"usergroup": "group_2"
16+
},
17+
{
18+
"cost": 14.564999999999998,
19+
"date": "2025-09-02",
20+
"usergroup": "none"
21+
},
22+
{
23+
"cost": 3.8275,
24+
"date": "2025-09-02",
25+
"usergroup": "group_1"
26+
},
27+
{
28+
"cost": 2.6275,
29+
"date": "2025-09-02",
30+
"usergroup": "group_2"
31+
}
32+
]

tests/test_integration.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,32 @@ def test_costs_per_user(
120120
assert r["value"] == lookup[key]
121121

122122

123+
@pytest.mark.parametrize("mock_prometheus_usage", [None], indirect=True)
124+
def test_costs_per_group(
125+
mock_prometheus_usage,
126+
mock_prometheus_usage_share,
127+
mock_prometheus_user_group_info,
128+
mock_ce,
129+
output_cost_per_group,
130+
):
131+
"""
132+
Test cost logic for costs per group.
133+
"""
134+
from src.jupyterhub_cost_monitoring.query_cost_aws import (
135+
query_total_costs_per_group,
136+
)
137+
138+
result = query_total_costs_per_group(date_range)
139+
logger.info(f"Cost per group: {result}")
140+
141+
lookup = {(o["date"], o["usergroup"]): o["cost"] for o in output_cost_per_group}
142+
143+
for r in result:
144+
key = (r["date"], r["usergroup"])
145+
if key in lookup:
146+
assert r["cost"] == lookup[key]
147+
148+
123149
@pytest.mark.parametrize("mock_prometheus_usage", [None], indirect=True)
124150
def test_costs_per_user_limit(
125151
mock_ce,

0 commit comments

Comments
 (0)