Skip to content

Commit

Permalink
Format with black
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Powers committed Sep 23, 2024
1 parent 170c61d commit 7f6ef24
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
27 changes: 17 additions & 10 deletions balance_service/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
from django.contrib.auth import get_user_model
from unittest.mock import patch

from .utils.su_calculators import get_used_sus, get_total_sus, get_active_allocation, project_balances, calculate_user_total_su_usage
from .utils.su_calculators import (
get_used_sus,
get_total_sus,
get_active_allocation,
project_balances,
calculate_user_total_su_usage,
)


class SUCalculatorsTest(TestCase):
def setUp(self):
Expand All @@ -28,7 +35,7 @@ def setUp(self):
pi=self.test_requestor,
title="Test Project",
nickname="test_project",
charge_code="TEST123"
charge_code="TEST123",
)
self.allocation = self.project.allocations.create(
project=self.project,
Expand Down Expand Up @@ -78,7 +85,7 @@ def setUp(self):
hourly_cost=5.0,
)

@patch('django.utils.timezone.now') # Mocking timezone.now()
@patch("django.utils.timezone.now") # Mocking timezone.now()
def test_get_used_sus(self, mock_now):
mock_now.return_value = self.now
# Test the get_used_sus function
Expand All @@ -96,20 +103,20 @@ def test_get_active_allocation(self):
self.assertIsNotNone(active_allocation)
self.assertEqual(active_allocation.status, "active")

@patch('django.utils.timezone.now') # Mocking timezone.now()
@patch("django.utils.timezone.now") # Mocking timezone.now()
def test_project_balances(self, mock_now):
mock_now.return_value = self.now
# Test the project_balances function
balances = project_balances([self.project.id])
self.assertEqual(len(balances), 1)
balance = balances[0]
self.assertEqual(balance['charge_code'], 'TEST123')
self.assertAlmostEqual(balance['used'], 16.0, places=2)
self.assertAlmostEqual(balance['total'], 41.0, places=2)
self.assertAlmostEqual(balance['allocated'], 50)
self.assertAlmostEqual(balance['encumbered'], 25.0, places=2)
self.assertEqual(balance["charge_code"], "TEST123")
self.assertAlmostEqual(balance["used"], 16.0, places=2)
self.assertAlmostEqual(balance["total"], 41.0, places=2)
self.assertAlmostEqual(balance["allocated"], 50)
self.assertAlmostEqual(balance["encumbered"], 25.0, places=2)

@patch('django.utils.timezone.now') # Mocking timezone.now()
@patch("django.utils.timezone.now") # Mocking timezone.now()
def test_calculate_user_total_su_usage(self, mock_now):
mock_now.return_value = self.now
# Test calculate_user_total_su_usage function
Expand Down
16 changes: 9 additions & 7 deletions balance_service/utils/su_calculators.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def project_balances(project_ids) -> "list[dict]":
{
"id": project.id,
"charge_code": project.charge_code,
"used": used_sus, # how much they have used
"total": total_sus, # how much they have used + pending use
"encumbered": total_sus - used_sus, # pending use
"used": used_sus, # how much they have used
"total": total_sus, # how much they have used + pending use
"encumbered": total_sus - used_sus, # pending use
"allocated": allocated_sus,
}
)
Expand All @@ -100,16 +100,18 @@ def calculate_user_total_su_usage(user, project):
# Extract requires native DurationField database support.
charges_with_duration_in_ms = charges.annotate(
charge_duration=ExpressionWrapper(
F('end_time') - F('start_time'), output_field=models.FloatField()
F("end_time") - F("start_time"), output_field=models.FloatField()
)
)
# Calculate cost of each charge in SUs by converting ms to hours
charges_with_actual_cost = charges_with_duration_in_ms.annotate(
charge_cost=F('charge_duration') / microseconds_per_hour * F('hourly_cost')
charge_cost=F("charge_duration") / microseconds_per_hour * F("hourly_cost")
)
# calculates the total cost of charges for the user on the project
# by summing up the charge_cost values calculated for each charge.
# If there are no charges, it returns 0.0
return charges_with_actual_cost.aggregate(
total_cost=functions.Coalesce(Sum('charge_cost'), 0.0, output_field=models.IntegerField())
)['total_cost']
total_cost=functions.Coalesce(
Sum("charge_cost"), 0.0, output_field=models.IntegerField()
)
)["total_cost"]

0 comments on commit 7f6ef24

Please sign in to comment.