Skip to content

Commit

Permalink
two more post usage tests and updated a deprec fn
Browse files Browse the repository at this point in the history
  • Loading branch information
dlpbc committed Oct 7, 2024
1 parent 89ab300 commit 600f456
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
2 changes: 1 addition & 1 deletion rctab/routers/accounting/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async def insert_usage(all_usage: AllUsage) -> None:
await executemany(
database,
on_duplicate_key_stmt,
values=[i.dict() for i in all_usage.usage_list],
values=[i.model_dump() for i in all_usage.usage_list],
)
logger.info("Inserting usage data took %s", datetime.datetime.now() - insert_start)
refresh_start = datetime.datetime.now()
Expand Down
90 changes: 87 additions & 3 deletions tests/test_routes/test_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,94 @@ def test_post_usage(
)


def test_post_usage2(
mocker: pytest_mock.MockerFixture,
@pytest.mark.asyncio
async def test_post_usage2(
test_db: Database, # pylint: disable=redefined-outer-name
) -> None:
del mocker
sub1 = await create_subscription(test_db)
# create some usage data across 2 or more dates
usage_items = AllUsage(
usage_list=[
Usage(
id=str(UUID(int=0)),
subscription_id=sub1,
date="2024-04-01",
total_cost=1.0,
invoice_section="-",
),
Usage(
id=str(UUID(int=1)),
subscription_id=sub1,
date="2024-04-02",
total_cost=2.0,
invoice_section="-",
),
Usage(
id=str(UUID(int=2)),
subscription_id=sub1,
date="2024-04-03",
total_cost=4.0,
invoice_section="-",
),
],
start_date="2024-04-01",
end_date="2024-04-03",
)
await post_usage(usage_items, {"mock": "authentication"})
all_usage = await get_usage()
assert len(all_usage) == 3

# upload some usage data for some subset of the dates
usage_list = [
Usage(
id=str(UUID(int=0)),
subscription_id=sub1,
date="2024-04-01",
total_cost=4.0,
invoice_section="-",
),
]
usage_items = AllUsage(
usage_list=usage_list,
start_date="2024-04-01",
end_date="2024-04-03",
)
await post_usage(usage_items, {"mock": "authentication"})

# check that some usage data was left alone and some was replaced
all_usage = await get_usage()
assert all_usage == usage_list


@pytest.mark.asyncio
async def test_post_usage3(
test_db: Database, # pylint: disable=redefined-outer-name
) -> None:
sub1 = await create_subscription(test_db)
# create some usage data across 2 or more dates
usage_items = AllUsage(
usage_list=[
Usage(
id=str(UUID(int=0)),
subscription_id=sub1,
date="2024-04-01",
total_cost=1.0,
invoice_section="A",
),
Usage(
id=str(UUID(int=0)),
subscription_id=sub1,
date="2024-04-02",
total_cost=1.0,
invoice_section="B",
),
],
start_date="2024-04-01",
end_date="2024-04-02",
)
await post_usage(usage_items, {"mock": "authentication"})
all_usage = await get_usage()
assert len(all_usage) == 2


def test_write_usage(
Expand Down

0 comments on commit 600f456

Please sign in to comment.