|
@@ -4,7 +4,7 @@ from django.test import TestCase
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
|
from ..models import (
|
|
|
- Cost, CostUse, Document, Good, GoodUse, Service)
|
|
|
+ Cost, CostUse, Document, Good, GoodUse, Service, ServiceUse)
|
|
|
|
|
|
|
|
|
class ServiceTests(TestCase):
|
|
@@ -97,6 +97,12 @@ class AbstractUseTests(TestCase):
|
|
|
total_capacity=4,
|
|
|
)
|
|
|
|
|
|
+ self.carrier_connection = Cost.objects.create(
|
|
|
+ name='carrier connection',
|
|
|
+ price=100,
|
|
|
+ document=self.doc,
|
|
|
+ )
|
|
|
+
|
|
|
def test_can_add_service_share(self):
|
|
|
use = CostUse(
|
|
|
service=self.hosting_service,
|
|
@@ -227,3 +233,70 @@ class AbstractUseTests(TestCase):
|
|
|
|
|
|
self.assertEqual(self.electricity_cost.used(), 0.5)
|
|
|
self.assertEqual(self.electricity_cost.unused(), 3.5)
|
|
|
+
|
|
|
+ def test_service_using_service(self):
|
|
|
+ """
|
|
|
+ Wifi+VPN is a service, but using VPN access
|
|
|
+ So there is a service using another service
|
|
|
+ """
|
|
|
+
|
|
|
+ vpn_service = Service.objects.create(
|
|
|
+ name="VPN",
|
|
|
+ document=self.doc,
|
|
|
+ subscriptions_count=20, # includes wifi+vpn subscribers
|
|
|
+ reusable=True,
|
|
|
+ )
|
|
|
+ # both should be auto-set
|
|
|
+ self.assertEqual(vpn_service.capacity_unit, 'services')
|
|
|
+ self.assertEqual(vpn_service.total_capacity, 20)
|
|
|
+
|
|
|
+ wifi_service = Service.objects.create(
|
|
|
+ name="Wifi, via VPN",
|
|
|
+ document=self.doc,
|
|
|
+ subscriptions_count=2,
|
|
|
+ reusable=True,
|
|
|
+ )
|
|
|
+ self.assertEqual(vpn_service.capacity_unit, 'services')
|
|
|
+
|
|
|
+ # To simplify, VPN is only using electricity
|
|
|
+ CostUse.objects.create(
|
|
|
+ service=vpn_service,
|
|
|
+ resource=self.electricity_cost,
|
|
|
+ share=0.5, # Amp
|
|
|
+ )
|
|
|
+
|
|
|
+ # Wifi is using VPN + a carrier connection
|
|
|
+ wifi_vpn_use = ServiceUse.objects.create(
|
|
|
+ service=wifi_service,
|
|
|
+ resource=vpn_service,
|
|
|
+ share=2,
|
|
|
+ )
|
|
|
+ CostUse.objects.create(
|
|
|
+ service=wifi_service,
|
|
|
+ resource=self.carrier_connection,
|
|
|
+ share=1, # 100%
|
|
|
+ )
|
|
|
+
|
|
|
+ self.assertEqual(wifi_vpn_use.share, 0.5*4*10/20*2)
|
|
|
+ self.assertEqual(wifi_vpn_use.unit_share(), 0.5*4*10/20)
|
|
|
+
|
|
|
+ # VPN this is the only service using electricity
|
|
|
+ self.assertEqual(wifi_vpn_use.unit_real_share(), 10)
|
|
|
+
|
|
|
+ def test_service_using_service_edgecases(self):
|
|
|
+ serva = Service.objects.create(
|
|
|
+ name='A', document=self.doc,
|
|
|
+ subscriptions_count=4,
|
|
|
+ reusable=False,
|
|
|
+ )
|
|
|
+
|
|
|
+ # A default service is not reusable
|
|
|
+
|
|
|
+ with self.assertRaises(ValidationError):
|
|
|
+ su = ServiceUse(
|
|
|
+ service=self.mailbox_service,
|
|
|
+ resource=serva,
|
|
|
+ share=1,
|
|
|
+ )
|
|
|
+ su.full_clean()
|
|
|
+ su.save()
|