Skip to content

Commit 600f456

Browse files
committed
two more post usage tests and updated a deprec fn
1 parent 89ab300 commit 600f456

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

rctab/routers/accounting/usage.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ async def insert_usage(all_usage: AllUsage) -> None:
9090
await executemany(
9191
database,
9292
on_duplicate_key_stmt,
93-
values=[i.dict() for i in all_usage.usage_list],
93+
values=[i.model_dump() for i in all_usage.usage_list],
9494
)
9595
logger.info("Inserting usage data took %s", datetime.datetime.now() - insert_start)
9696
refresh_start = datetime.datetime.now()

tests/test_routes/test_usage.py

+87-3
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,94 @@ def test_post_usage(
8888
)
8989

9090

91-
def test_post_usage2(
92-
mocker: pytest_mock.MockerFixture,
91+
@pytest.mark.asyncio
92+
async def test_post_usage2(
93+
test_db: Database, # pylint: disable=redefined-outer-name
9394
) -> None:
94-
del mocker
95+
sub1 = await create_subscription(test_db)
96+
# create some usage data across 2 or more dates
97+
usage_items = AllUsage(
98+
usage_list=[
99+
Usage(
100+
id=str(UUID(int=0)),
101+
subscription_id=sub1,
102+
date="2024-04-01",
103+
total_cost=1.0,
104+
invoice_section="-",
105+
),
106+
Usage(
107+
id=str(UUID(int=1)),
108+
subscription_id=sub1,
109+
date="2024-04-02",
110+
total_cost=2.0,
111+
invoice_section="-",
112+
),
113+
Usage(
114+
id=str(UUID(int=2)),
115+
subscription_id=sub1,
116+
date="2024-04-03",
117+
total_cost=4.0,
118+
invoice_section="-",
119+
),
120+
],
121+
start_date="2024-04-01",
122+
end_date="2024-04-03",
123+
)
124+
await post_usage(usage_items, {"mock": "authentication"})
125+
all_usage = await get_usage()
126+
assert len(all_usage) == 3
127+
128+
# upload some usage data for some subset of the dates
129+
usage_list = [
130+
Usage(
131+
id=str(UUID(int=0)),
132+
subscription_id=sub1,
133+
date="2024-04-01",
134+
total_cost=4.0,
135+
invoice_section="-",
136+
),
137+
]
138+
usage_items = AllUsage(
139+
usage_list=usage_list,
140+
start_date="2024-04-01",
141+
end_date="2024-04-03",
142+
)
143+
await post_usage(usage_items, {"mock": "authentication"})
144+
145+
# check that some usage data was left alone and some was replaced
146+
all_usage = await get_usage()
147+
assert all_usage == usage_list
148+
149+
150+
@pytest.mark.asyncio
151+
async def test_post_usage3(
152+
test_db: Database, # pylint: disable=redefined-outer-name
153+
) -> None:
154+
sub1 = await create_subscription(test_db)
155+
# create some usage data across 2 or more dates
156+
usage_items = AllUsage(
157+
usage_list=[
158+
Usage(
159+
id=str(UUID(int=0)),
160+
subscription_id=sub1,
161+
date="2024-04-01",
162+
total_cost=1.0,
163+
invoice_section="A",
164+
),
165+
Usage(
166+
id=str(UUID(int=0)),
167+
subscription_id=sub1,
168+
date="2024-04-02",
169+
total_cost=1.0,
170+
invoice_section="B",
171+
),
172+
],
173+
start_date="2024-04-01",
174+
end_date="2024-04-02",
175+
)
176+
await post_usage(usage_items, {"mock": "authentication"})
177+
all_usage = await get_usage()
178+
assert len(all_usage) == 2
95179

96180

97181
def test_write_usage(

0 commit comments

Comments
 (0)