From 3f92d3928be267fbfc90d651aece8110fad386bb Mon Sep 17 00:00:00 2001 From: matusdrobuliak66 <60785969+matusdrobuliak66@users.noreply.github.com> Date: Mon, 11 Dec 2023 14:03:29 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Bugfix/computation=20of=20credit?= =?UTF-8?q?s=20(#5151)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resource_tracker_utils.py | 2 +- .../tests/unit/test_computation_of_credits.py | 63 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 services/resource-usage-tracker/tests/unit/test_computation_of_credits.py diff --git a/services/resource-usage-tracker/src/simcore_service_resource_usage_tracker/resource_tracker_utils.py b/services/resource-usage-tracker/src/simcore_service_resource_usage_tracker/resource_tracker_utils.py index d2ffc4ad425..4466fc5e7de 100644 --- a/services/resource-usage-tracker/src/simcore_service_resource_usage_tracker/resource_tracker_utils.py +++ b/services/resource-usage-tracker/src/simcore_service_resource_usage_tracker/resource_tracker_utils.py @@ -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) diff --git a/services/resource-usage-tracker/tests/unit/test_computation_of_credits.py b/services/resource-usage-tracker/tests/unit/test_computation_of_credits.py new file mode 100644 index 00000000000..5264b799f33 --- /dev/null +++ b/services/resource-usage-tracker/tests/unit/test_computation_of_credits.py @@ -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)