test_models.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from django.test import TestCase
  2. from django.core.exceptions import ValidationError
  3. from ..models import Service, Cost, CostUse, Document
  4. class AbstractUseTests(TestCase):
  5. """ Testing AbstractUseTests through CostUse
  6. """
  7. def setUp(self):
  8. self.doc = Document.objects.create(name='budget')
  9. self.hosting_service = Service.objects.create(
  10. name='Physical hosting', document=self.doc)
  11. self.mailbox_service = Service.objects.create(
  12. name='Mailbox', document=self.doc)
  13. self.electricity_cost = Cost.objects.create(
  14. name='electricity',
  15. price=10,
  16. document=self.doc,
  17. capacity_unit='A',
  18. total_capacity=4,
  19. )
  20. def test_can_add_service_share(self):
  21. use = CostUse(
  22. service=self.hosting_service,
  23. resource=self.electricity_cost,
  24. share=0.4)
  25. use.full_clean()
  26. use.save()
  27. def test_can_add_service_share_with_custom_unity(self):
  28. use = CostUse(
  29. service=self.hosting_service,
  30. resource=self.electricity_cost,
  31. share=2) # means 2 Amps
  32. self.hosting_service.subscriptions_count = 2
  33. self.hosting_service.save()
  34. use.full_clean()
  35. use.save()
  36. self.assertEqual(use.share, 2.0)
  37. self.assertEqual(use.real_share(), 4.0)
  38. self.assertEqual(use.cost_share(), 10)
  39. self.assertEqual(use.unit_cost_share(), 5)
  40. def test_cannot_add_excess_share_one(self):
  41. use = CostUse(
  42. service=self.hosting_service,
  43. resource=self.electricity_cost,
  44. share=40.1)
  45. with self.assertRaises(ValidationError):
  46. use.full_clean()
  47. use.save()
  48. def test_add_several_service_share(self):
  49. u1 = CostUse(
  50. service=self.hosting_service,
  51. resource=self.electricity_cost,
  52. share=0.4)
  53. u1.full_clean()
  54. u1.save()
  55. u2 = CostUse(
  56. service=self.mailbox_service,
  57. resource=self.electricity_cost,
  58. share=0.6)
  59. u2.full_clean()
  60. u2.save()
  61. def test_add_several_service_share_excessive_sum(self):
  62. u1 = CostUse(
  63. service=self.hosting_service,
  64. resource=self.electricity_cost,
  65. share=3)
  66. u1.full_clean()
  67. u1.save()
  68. u2 = CostUse(
  69. service=self.mailbox_service,
  70. resource=self.electricity_cost,
  71. share=1.1)
  72. # Would be 4.1 out of 4 amp...
  73. with self.assertRaises(ValidationError):
  74. u2.full_clean()
  75. u2.save()
  76. def test_modify_service_share_no_error(self):
  77. u1 = CostUse(
  78. service=self.hosting_service,
  79. resource=self.electricity_cost,
  80. share=1)
  81. u1.full_clean()
  82. u1.save()
  83. u1.full_clean()
  84. u1.save()
  85. def test_real_shares(self):
  86. u1 = CostUse.objects.create(
  87. service=self.hosting_service,
  88. resource=self.electricity_cost,
  89. share=1.6)
  90. u2 = CostUse.objects.create(
  91. service=self.hosting_service,
  92. resource=self.electricity_cost,
  93. share=0.6)
  94. self.assertEqual(u1.real_share() + u2.real_share(), 4)
  95. self.assertEqual(u1.share/u2.share, u1.real_share()/u2.real_share())
  96. def test_unit_value_share(self):
  97. self.mailbox_service.subscriptions_count = 2
  98. self.mailbox_service.share = 0.5
  99. self.hosting_service.save()
  100. self.hosting_service.subscriptions_count = 1
  101. self.hosting_service.share = 0.5
  102. self.hosting_service.save()
  103. mailbox_use = CostUse.objects.create(
  104. service=self.mailbox_service,
  105. resource=self.electricity_cost,
  106. share=2)
  107. hosting_use = CostUse.objects.create(
  108. service=self.hosting_service,
  109. resource=self.electricity_cost,
  110. share=2)
  111. self.assertEqual(mailbox_use.value_share(), 5)
  112. self.assertEqual(mailbox_use.unit_value_share(), 2.5)
  113. self.assertEqual(hosting_use.value_share(), 5)
  114. self.assertEqual(hosting_use.unit_value_share(), 5)
  115. def test_used(self):
  116. CostUse.objects.create(
  117. service=self.mailbox_service,
  118. resource=self.electricity_cost,
  119. share=0.5)
  120. self.assertEqual(self.electricity_cost.used(), 0.5)
  121. self.assertEqual(self.electricity_cost.unused(), 3.5)