Skip to content

Commit

Permalink
🐛 Bugfix/computation of credits (ITISFoundation#5151)
Browse files Browse the repository at this point in the history
  • Loading branch information
matusdrobuliak66 authored Dec 11, 2023
1 parent 4cfa318 commit 3f92d39
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ async def compute_service_run_credit_costs(
) -> Decimal:
if start <= stop:
time_delta = stop - start
return round(Decimal(time_delta.seconds / 3600) * cost_per_unit, 2)
return round(Decimal(time_delta.total_seconds() / 3600) * cost_per_unit, 2)
msg = f"Stop {stop} is smaller then {start} this should not happen. Investigate."
raise ValueError(msg)
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# pylint: disable=redefined-outer-name
# pylint: disable=unused-argument
# pylint: disable=unused-variable
# pylint: disable=too-many-arguments

from datetime import datetime, timedelta, timezone
from decimal import Decimal

import pytest
from simcore_service_resource_usage_tracker.resource_tracker_utils import (
compute_service_run_credit_costs,
)


@pytest.mark.parametrize(
"stop,start,cost_per_unit,expected_cost",
[
(
datetime.now(tz=timezone.utc),
datetime.now(tz=timezone.utc) - timedelta(days=1),
Decimal(25),
Decimal(600),
),
(
datetime.now(tz=timezone.utc),
datetime.now(tz=timezone.utc) - timedelta(days=2.5),
Decimal(40),
Decimal(2400),
),
(
datetime.now(tz=timezone.utc),
datetime.now(tz=timezone.utc) - timedelta(days=25),
Decimal(12),
Decimal(7200),
),
(
datetime.now(tz=timezone.utc),
datetime.now(tz=timezone.utc) - timedelta(days=45),
Decimal(13.5),
Decimal(14580),
),
(
datetime.now(tz=timezone.utc),
datetime.now(tz=timezone.utc) - timedelta(minutes=37),
Decimal(25),
round(Decimal(15.42), 2),
),
],
)
async def test_credit_computation(stop, start, cost_per_unit, expected_cost):
computed_credits = await compute_service_run_credit_costs(
start, stop, cost_per_unit
)
assert computed_credits == expected_cost


async def test_invalid_dates_in_credit_computation():
start = datetime.now(tz=timezone.utc)
stop = datetime.now(tz=timezone.utc) - timedelta(minutes=3)
cost_per_unit = Decimal(25)

with pytest.raises(ValueError):
await compute_service_run_credit_costs(start, stop, cost_per_unit)

0 comments on commit 3f92d39

Please sign in to comment.