test_models.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import datetime
  2. from django.test import TestCase
  3. from django.core.exceptions import ValidationError
  4. from ..models import (
  5. Cost, CostUse, Document, Good, GoodUse, Service, ServiceUse)
  6. class ServiceTests(TestCase):
  7. def setUp(self):
  8. self.doc = Document.objects.create(name='budget')
  9. self.electricity_cost = Cost.objects.create(
  10. name='electricity',
  11. price=10,
  12. document=self.doc,
  13. )
  14. self.server = Good.objects.create(
  15. name="Computer",
  16. price=10,
  17. document=self.doc,
  18. provisioning_duration=datetime.timedelta(days=365*3),
  19. )
  20. def test_get_prices_zero(self):
  21. s = Service.objects.create(name='Foo', document=self.doc)
  22. self.assertEqual(s.get_prices(), {
  23. 'total_costs_price': 0,
  24. 'unit_costs_price': 0,
  25. 'unit_goods_value_share': 0,
  26. 'total_goods_value_share': 0,
  27. })
  28. def test_get_prices_w_costs(self):
  29. s = Service.objects.create(name='Foo', document=self.doc)
  30. CostUse.objects.create(
  31. service=s, resource=self.electricity_cost, share=0.4)
  32. self.assertEqual(s.get_prices(), {
  33. 'total_costs_price': 10,
  34. 'unit_costs_price': 0,
  35. 'unit_goods_value_share': 0,
  36. 'total_goods_value_share': 0,
  37. })
  38. s.subscriptions_count = 2
  39. s.save()
  40. self.assertEqual(s.get_prices(), {
  41. 'total_costs_price': 10,
  42. 'unit_costs_price': 5,
  43. 'unit_goods_value_share': 0,
  44. 'total_goods_value_share': 0,
  45. })
  46. def test_get_prices_w_goods(self):
  47. s = Service.objects.create(
  48. name='Foo', document=self.doc, subscriptions_count=0)
  49. GoodUse.objects.create(
  50. service=s, resource=self.server, share=0.4)
  51. self.assertEqual(s.get_prices(), {
  52. 'total_costs_price': 10/(365*3)*365.25/12,
  53. 'unit_costs_price': 0,
  54. 'unit_goods_value_share': 0,
  55. 'total_goods_value_share': 10.0,
  56. })
  57. s.subscriptions_count = 2
  58. s.save()
  59. self.assertEqual(s.get_prices(), {
  60. 'total_costs_price': 10/(365*3)*365.25/12,
  61. 'unit_costs_price': 10/(365*3)*365.25/12/2,
  62. 'unit_goods_value_share': 5,
  63. 'total_goods_value_share': 10,
  64. })
  65. class AbstractUseTests(TestCase):
  66. """ Testing AbstractUseTests through CostUse
  67. """
  68. def setUp(self):
  69. self.doc = Document.objects.create(name='budget')
  70. self.hosting_service = Service.objects.create(
  71. name='Physical hosting', document=self.doc)
  72. self.mailbox_service = Service.objects.create(
  73. name='Mailbox', document=self.doc)
  74. self.electricity_cost = Cost.objects.create(
  75. name='electricity',
  76. price=10,
  77. document=self.doc,
  78. capacity_unit='A',
  79. total_capacity=4,
  80. )
  81. self.carrier_connection = Cost.objects.create(
  82. name='carrier connection',
  83. price=100,
  84. document=self.doc,
  85. )
  86. def test_can_add_service_share(self):
  87. use = CostUse(
  88. service=self.hosting_service,
  89. resource=self.electricity_cost,
  90. share=0.4)
  91. use.full_clean()
  92. use.save()
  93. def test_can_add_service_share_with_custom_unity(self):
  94. use = CostUse(
  95. service=self.hosting_service,
  96. resource=self.electricity_cost,
  97. share=2) # means 2 Amps
  98. self.hosting_service.subscriptions_count = 2
  99. self.hosting_service.save()
  100. use.full_clean()
  101. use.save()
  102. self.assertEqual(use.share, 2.0)
  103. self.assertEqual(use.real_share(), 4.0)
  104. self.assertEqual(use.cost_share(), 10)
  105. self.assertEqual(use.unit_cost_share(), 5)
  106. def test_cannot_add_excess_share_one(self):
  107. use = CostUse(
  108. service=self.hosting_service,
  109. resource=self.electricity_cost,
  110. share=40.1)
  111. with self.assertRaises(ValidationError):
  112. use.full_clean()
  113. use.save()
  114. def test_add_several_service_share(self):
  115. u1 = CostUse(
  116. service=self.hosting_service,
  117. resource=self.electricity_cost,
  118. share=0.4)
  119. u1.full_clean()
  120. u1.save()
  121. u2 = CostUse(
  122. service=self.mailbox_service,
  123. resource=self.electricity_cost,
  124. share=0.6)
  125. u2.full_clean()
  126. u2.save()
  127. def test_add_several_service_share_excessive_sum(self):
  128. u1 = CostUse(
  129. service=self.hosting_service,
  130. resource=self.electricity_cost,
  131. share=3)
  132. u1.full_clean()
  133. u1.save()
  134. u2 = CostUse(
  135. service=self.mailbox_service,
  136. resource=self.electricity_cost,
  137. share=1.1)
  138. # Would be 4.1 out of 4 amp...
  139. with self.assertRaises(ValidationError):
  140. u2.full_clean()
  141. u2.save()
  142. def test_modify_service_share_no_error(self):
  143. u1 = CostUse(
  144. service=self.hosting_service,
  145. resource=self.electricity_cost,
  146. share=1)
  147. u1.full_clean()
  148. u1.save()
  149. u1.full_clean()
  150. u1.save()
  151. def test_real_shares(self):
  152. u1 = CostUse.objects.create(
  153. service=self.hosting_service,
  154. resource=self.electricity_cost,
  155. share=1.6)
  156. u2 = CostUse.objects.create(
  157. service=self.hosting_service,
  158. resource=self.electricity_cost,
  159. share=0.6)
  160. self.assertEqual(u1.real_share() + u2.real_share(), 4)
  161. self.assertEqual(u1.share/u2.share, u1.real_share()/u2.real_share())
  162. def test_unit_value_share(self):
  163. self.mailbox_service.subscriptions_count = 2
  164. self.mailbox_service.share = 0.5
  165. self.hosting_service.save()
  166. self.hosting_service.subscriptions_count = 1
  167. self.hosting_service.share = 0.5
  168. self.hosting_service.save()
  169. mailbox_use = CostUse.objects.create(
  170. service=self.mailbox_service,
  171. resource=self.electricity_cost,
  172. share=2)
  173. hosting_use = CostUse.objects.create(
  174. service=self.hosting_service,
  175. resource=self.electricity_cost,
  176. share=2)
  177. self.assertEqual(mailbox_use.value_share(), 5)
  178. self.assertEqual(mailbox_use.unit_value_share(), 2.5)
  179. self.assertEqual(hosting_use.value_share(), 5)
  180. self.assertEqual(hosting_use.unit_value_share(), 5)
  181. def test_used(self):
  182. CostUse.objects.create(
  183. service=self.mailbox_service,
  184. resource=self.electricity_cost,
  185. share=0.5)
  186. self.assertEqual(self.electricity_cost.used(), 0.5)
  187. self.assertEqual(self.electricity_cost.unused(), 3.5)
  188. def test_service_using_service(self):
  189. """
  190. Wifi+VPN is a service, but using VPN access
  191. So there is a service using another service
  192. """
  193. vpn_service = Service.objects.create(
  194. name="VPN",
  195. document=self.doc,
  196. subscriptions_count=20, # includes wifi+vpn subscribers
  197. reusable=True,
  198. )
  199. # both should be auto-set
  200. self.assertEqual(vpn_service.capacity_unit, 'services')
  201. self.assertEqual(vpn_service.total_capacity, 20)
  202. wifi_service = Service.objects.create(
  203. name="Wifi, via VPN",
  204. document=self.doc,
  205. subscriptions_count=2,
  206. reusable=True,
  207. )
  208. self.assertEqual(vpn_service.capacity_unit, 'services')
  209. # To simplify, VPN is only using electricity
  210. CostUse.objects.create(
  211. service=vpn_service,
  212. resource=self.electricity_cost,
  213. share=0.5, # Amp
  214. )
  215. # Wifi is using VPN + a carrier connection
  216. wifi_vpn_use = ServiceUse.objects.create(
  217. service=wifi_service,
  218. resource=vpn_service,
  219. share=2,
  220. )
  221. CostUse.objects.create(
  222. service=wifi_service,
  223. resource=self.carrier_connection,
  224. share=1, # 100%
  225. )
  226. self.assertEqual(wifi_vpn_use.share, 0.5*4*10/20*2)
  227. self.assertEqual(wifi_vpn_use.unit_share(), 0.5*4*10/20)
  228. # VPN this is the only service using electricity
  229. self.assertEqual(wifi_vpn_use.unit_real_share(), 10)
  230. def test_service_using_service_edgecases(self):
  231. serva = Service.objects.create(
  232. name='A', document=self.doc,
  233. subscriptions_count=4,
  234. reusable=False,
  235. )
  236. # A default service is not reusable
  237. with self.assertRaises(ValidationError):
  238. su = ServiceUse(
  239. service=self.mailbox_service,
  240. resource=serva,
  241. share=1,
  242. )
  243. su.full_clean()
  244. su.save()