Skip to content

Commit

Permalink
monthly usage: delete existing rows within dates
Browse files Browse the repository at this point in the history
logic and tests to delete existing rows within date range in
post monthly usage
  • Loading branch information
dlpbc committed Oct 23, 2024
1 parent 297576a commit ba29785
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
27 changes: 26 additions & 1 deletion rctab/routers/accounting/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
from fastapi.security import OAuth2PasswordBearer
from pydantic import BaseModel
from rctab_models.models import AllCMUsage, AllUsage, CMUsage, Usage, UserRBAC
from sqlalchemy import select
from sqlalchemy import delete, select

# require the postgre specific insert rather than the generic sqlachemy for;
# `post_cm_usage` fn where "excluded" and "on_conflict_do_update" are used.
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.inspection import inspect

Expand Down Expand Up @@ -96,6 +99,27 @@ async def insert_usage(all_usage: AllUsage) -> None:
)


async def delete_usage(start_date: datetime.date, end_date: datetime.date) -> None:
"""Deletes usage(s) within a date range from the database.
Args:
start_date: Defines the beginning of the date range.
end_date: Defines the end of the date range.
"""
usage_query = (
delete(accounting_models.usage)
.where(accounting_models.usage.c.date >= start_date)
.where(accounting_models.usage.c.date <= end_date)
)

logger.info("Delete usage data within a date range")
delete_start = datetime.datetime.now()

await database.execute(usage_query)

logger.info("Delete usage data took %s", datetime.datetime.now() - delete_start)


@router.post("/monthly-usage", response_model=TmpReturnStatus)
async def post_monthly_usage(
all_usage: AllUsage, _: Dict[str, str] = Depends(authenticate_usage_app)
Expand Down Expand Up @@ -180,6 +204,7 @@ async def post_usage(
unique_subscriptions = list(
{i.subscription_id for i in all_usage.usage_list}
)
await delete_usage(all_usage.start_date, all_usage.end_date)

await insert_subscriptions_if_not_exists(unique_subscriptions)

Expand Down
34 changes: 28 additions & 6 deletions tests/test_routes/test_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,45 @@ async def test_post_usage2(
# upload some usage data for some subset of the dates
usage_list = [
Usage(
id=str(UUID(int=0)),
id=str(UUID(int=3)),
subscription_id=sub1,
date="2024-04-01",
date="2024-04-02",
total_cost=4.0,
invoice_section="-",
),
]
usage_items = AllUsage(
usage_list=usage_list,
start_date="2024-04-01",
end_date="2024-04-03",
start_date="2024-04-02",
end_date="2024-04-02",
)
await post_usage(usage_items, {"mock": "authentication"})

# check that some usage data was left alone and some was replaced
# check that the uploaded usage data replaced the existing ones
all_usage = await get_usage()
assert all_usage == usage_list
assert set(all_usage) == {
Usage(
id=str(UUID(int=0)),
subscription_id=sub1,
date="2024-04-01",
total_cost=1.0,
invoice_section="-",
),
Usage(
id=str(UUID(int=3)),
subscription_id=sub1,
date="2024-04-02",
total_cost=4.0,
invoice_section="-",
),
Usage(
id=str(UUID(int=2)),
subscription_id=sub1,
date="2024-04-03",
total_cost=4.0,
invoice_section="-",
),
}


@pytest.mark.asyncio
Expand Down

0 comments on commit ba29785

Please sign in to comment.