Browse Source

Add Service.compare() method

To be able to compare figure variations between two services.
Jocelyn Delalande 7 years ago
parent
commit
45445a08f4
2 changed files with 51 additions and 0 deletions
  1. 27 0
      costs/models.py
  2. 24 0
      costs/tests/test_models.py

+ 27 - 0
costs/models.py

@@ -397,6 +397,33 @@ class Service(AbstractResource):
                 unit_staggered_goods_share + unit_recurring_price),
         }
 
+    def compare(self, other):
+        """ Compares figures between two services
+
+        Typically, same service type, but on two different reports.
+
+        :type other: Service
+        :rtype: dict
+        :returns: dict of deltas (from other to self).
+        """
+
+        self_prices = self.get_prices()
+        other_prices = other.get_prices()
+
+        comparated_prices = (
+            'total_recurring_price', 'total_goods_value_share',
+            'unit_goods_value_share', 'unit_recurring_price',
+            'unit_staggered_goods_share', 'unit_consolidated_cost',
+        )
+
+        comparated_vals = {
+            'subscriptions_count': self.subscriptions_count - other.subscriptions_count,
+        }
+        for i in comparated_prices:
+            comparated_vals[i] = self_prices[i] - other_prices[i]
+
+        return comparated_vals
+
 
 def validate_reusable_service(v):
     if not Service.objects.get(pk=v).reusable:

+ 24 - 0
costs/tests/test_models.py

@@ -92,6 +92,30 @@ class ServiceTests(TestCase):
         })
 
 
+    def test_compare_service(self):
+        s_first = Service.objects.create(
+            name='Foo', document=self.doc, subscriptions_count=3)
+
+        # The first service has no cost/goods associated
+
+        s_second = Service.objects.create(
+            name='Bar', document=self.doc, subscriptions_count=2)
+
+        GoodUse.objects.create(
+            service=s_second, resource=self.server, share=0.4)
+
+        total_recurring_price = 10/(365*3)*365.25/12
+        self.assertEqual(s_second.compare(s_first), {
+            'total_recurring_price': total_recurring_price,
+            'total_goods_value_share': 10,
+            'unit_recurring_price': total_recurring_price/2,
+            'unit_goods_value_share': 5,
+            'unit_staggered_goods_share': 5/36,
+            'unit_consolidated_cost': total_recurring_price/2 + 5/36,
+            'subscriptions_count': -1,
+        })
+
+
 class AbstractUseTests(TestCase):
     """ Testing AbstractUseTests through CostUse
     """