test_models.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. THREE_YEARS = datetime.timedelta(days=365*3)
  7. class ServiceTests(TestCase):
  8. def setUp(self):
  9. self.doc = Document.objects.create(name='budget')
  10. self.electricity_cost = Cost.objects.create(
  11. name='electricity',
  12. price=10,
  13. document=self.doc,
  14. )
  15. self.server = Good.objects.create(
  16. name="Computer",
  17. price=10,
  18. document=self.doc,
  19. provisioning_duration=THREE_YEARS,
  20. )
  21. def test_get_prices_zero(self):
  22. s = Service.objects.create(name='Foo', document=self.doc)
  23. self.assertEqual(s.get_prices(), {
  24. 'total_recurring_price': 0,
  25. 'total_goods_value_share': 0,
  26. 'unit_recurring_price': 0,
  27. 'unit_goods_value_share': 0,
  28. 'unit_recurring_price': 0,
  29. 'unit_consolidated_cost': 0,
  30. 'unit_staggered_goods_share': 0,
  31. })
  32. def test_get_prices_w_costs(self):
  33. s = Service.objects.create(name='Foo', document=self.doc)
  34. CostUse.objects.create(
  35. service=s, resource=self.electricity_cost, share=0.4)
  36. self.assertEqual(s.get_prices(), {
  37. 'total_recurring_price': 10,
  38. 'unit_recurring_price': 0,
  39. 'unit_goods_value_share': 0,
  40. 'total_goods_value_share': 0,
  41. 'unit_staggered_goods_share': 0.0,
  42. 'unit_consolidated_cost': 0,
  43. })
  44. s.subscriptions_count = 2
  45. s.save()
  46. self.assertEqual(s.get_prices(), {
  47. 'total_recurring_price': 10,
  48. 'total_goods_value_share': 0,
  49. 'unit_recurring_price': 5,
  50. 'unit_goods_value_share': 0,
  51. 'unit_staggered_goods_share': 0,
  52. 'unit_consolidated_cost': 5.0,
  53. })
  54. def test_get_prices_w_goods(self):
  55. s = Service.objects.create(
  56. name='Foo', document=self.doc, subscriptions_count=0)
  57. GoodUse.objects.create(
  58. service=s, resource=self.server, share=0.4)
  59. self.assertEqual(s.get_prices(), {
  60. 'total_recurring_price': 10/(365*3)*365.25/12,
  61. 'unit_recurring_price': 0,
  62. 'unit_goods_value_share': 0,
  63. 'total_goods_value_share': 10,
  64. 'unit_consolidated_cost': 0.0,
  65. 'unit_staggered_goods_share': 0.0,
  66. })
  67. s.subscriptions_count = 2
  68. s.save()
  69. total_recurring_price = 10/(365*3)*365.25/12
  70. self.assertEqual(s.get_prices(), {
  71. 'total_recurring_price': total_recurring_price,
  72. 'total_goods_value_share': 10,
  73. 'unit_recurring_price': total_recurring_price/2,
  74. 'unit_goods_value_share': 5,
  75. 'unit_staggered_goods_share': 5/36,
  76. 'unit_consolidated_cost': total_recurring_price/2 + 5/36,
  77. })
  78. def test_compare_service(self):
  79. s_first = Service.objects.create(
  80. name='Foo', document=self.doc, subscriptions_count=3)
  81. # The first service has no cost/goods associated
  82. s_second = Service.objects.create(
  83. name='Bar', document=self.doc, subscriptions_count=2)
  84. GoodUse.objects.create(
  85. service=s_second, resource=self.server, share=0.4)
  86. total_recurring_price = 10/(365*3)*365.25/12
  87. self.assertEqual(s_second.compare(s_first), {
  88. 'total_recurring_price': total_recurring_price,
  89. 'total_goods_value_share': 10,
  90. 'unit_recurring_price': total_recurring_price/2,
  91. 'unit_goods_value_share': 5,
  92. 'unit_staggered_goods_share': 5/36,
  93. 'unit_consolidated_cost': total_recurring_price/2 + 5/36,
  94. 'subscriptions_count': -1,
  95. })
  96. class AbstractUseTests(TestCase):
  97. """ Testing AbstractUseTests through CostUse
  98. """
  99. def setUp(self):
  100. self.doc = Document.objects.create(name='budget')
  101. self.hosting_service = Service.objects.create(
  102. name='Physical hosting', document=self.doc)
  103. self.mailbox_service = Service.objects.create(
  104. name='Mailbox', document=self.doc)
  105. self.electricity_cost = Cost.objects.create(
  106. name='electricity',
  107. price=10,
  108. document=self.doc,
  109. capacity_unit='A',
  110. total_capacity=4,
  111. )
  112. self.carrier_connection = Cost.objects.create(
  113. name='carrier connection',
  114. price=100,
  115. document=self.doc,
  116. )
  117. def test_can_add_service_share(self):
  118. use = CostUse(
  119. service=self.hosting_service,
  120. resource=self.electricity_cost,
  121. share=0.4)
  122. use.full_clean()
  123. use.save()
  124. def test_can_add_service_share_with_custom_unity(self):
  125. use = CostUse(
  126. service=self.hosting_service,
  127. resource=self.electricity_cost,
  128. share=2) # means 2 Amps
  129. self.hosting_service.subscriptions_count = 2
  130. self.hosting_service.save()
  131. use.full_clean()
  132. use.save()
  133. self.assertEqual(use.share, 2.0)
  134. self.assertEqual(use.real_share(), 4.0)
  135. self.assertEqual(use.cost_share(), 10)
  136. self.assertEqual(use.unit_cost_share(), 5)
  137. def test_cannot_add_excess_share_one(self):
  138. use = CostUse(
  139. service=self.hosting_service,
  140. resource=self.electricity_cost,
  141. share=40.1)
  142. with self.assertRaises(ValidationError):
  143. use.full_clean()
  144. use.save()
  145. def test_add_several_service_share(self):
  146. u1 = CostUse(
  147. service=self.hosting_service,
  148. resource=self.electricity_cost,
  149. share=0.4)
  150. u1.full_clean()
  151. u1.save()
  152. u2 = CostUse(
  153. service=self.mailbox_service,
  154. resource=self.electricity_cost,
  155. share=0.6)
  156. u2.full_clean()
  157. u2.save()
  158. def test_add_several_service_share_excessive_sum(self):
  159. u1 = CostUse(
  160. service=self.hosting_service,
  161. resource=self.electricity_cost,
  162. share=3)
  163. u1.full_clean()
  164. u1.save()
  165. u2 = CostUse(
  166. service=self.mailbox_service,
  167. resource=self.electricity_cost,
  168. share=1.1)
  169. # Would be 4.1 out of 4 amp...
  170. with self.assertRaises(ValidationError):
  171. u2.full_clean()
  172. u2.save()
  173. def test_modify_service_share_no_error(self):
  174. u1 = CostUse(
  175. service=self.hosting_service,
  176. resource=self.electricity_cost,
  177. share=1)
  178. u1.full_clean()
  179. u1.save()
  180. u1.full_clean()
  181. u1.save()
  182. def test_real_shares(self):
  183. u1 = CostUse.objects.create(
  184. service=self.hosting_service,
  185. resource=self.electricity_cost,
  186. share=1.6)
  187. u2 = CostUse.objects.create(
  188. service=self.hosting_service,
  189. resource=self.electricity_cost,
  190. share=0.6)
  191. self.assertEqual(u1.real_share() + u2.real_share(), 4)
  192. self.assertEqual(u1.share/u2.share, u1.real_share()/u2.real_share())
  193. def test_unit_value_share(self):
  194. self.mailbox_service.subscriptions_count = 2
  195. self.mailbox_service.share = 0.5
  196. self.hosting_service.save()
  197. self.hosting_service.subscriptions_count = 1
  198. self.hosting_service.share = 0.5
  199. self.hosting_service.save()
  200. mailbox_use = CostUse.objects.create(
  201. service=self.mailbox_service,
  202. resource=self.electricity_cost,
  203. share=2)
  204. hosting_use = CostUse.objects.create(
  205. service=self.hosting_service,
  206. resource=self.electricity_cost,
  207. share=2)
  208. self.assertEqual(mailbox_use.value_share(), 5)
  209. self.assertEqual(mailbox_use.unit_value_share(), 2.5)
  210. self.assertEqual(hosting_use.value_share(), 5)
  211. self.assertEqual(hosting_use.unit_value_share(), 5)
  212. def test_used(self):
  213. CostUse.objects.create(
  214. service=self.mailbox_service,
  215. resource=self.electricity_cost,
  216. share=0.5)
  217. self.assertEqual(self.electricity_cost.used(), 0.5)
  218. self.assertEqual(self.electricity_cost.unused(), 3.5)
  219. def test_service_using_service(self):
  220. """
  221. Wifi+VPN is a service, but using VPN access
  222. So there is a service using another service
  223. """
  224. vpn_service = Service.objects.create(
  225. name="VPN",
  226. document=self.doc,
  227. subscriptions_count=20, # includes wifi+vpn subscribers
  228. reusable=True,
  229. )
  230. # both should be auto-set
  231. self.assertEqual(vpn_service.capacity_unit, 'services')
  232. self.assertEqual(vpn_service.total_capacity, 20)
  233. wifi_service = Service.objects.create(
  234. name="Wifi, via VPN",
  235. document=self.doc,
  236. subscriptions_count=2,
  237. reusable=True,
  238. )
  239. self.assertEqual(vpn_service.capacity_unit, 'services')
  240. # To simplify, VPN is only using electricity
  241. CostUse.objects.create(
  242. service=vpn_service,
  243. resource=self.electricity_cost,
  244. share=0.5, # Amp
  245. )
  246. # Wifi is using VPN + a carrier connection
  247. wifi_vpn_use = ServiceUse.objects.create(
  248. service=wifi_service,
  249. resource=vpn_service,
  250. share=2,
  251. )
  252. CostUse.objects.create(
  253. service=wifi_service,
  254. resource=self.carrier_connection,
  255. share=1, # 100%
  256. )
  257. self.assertEqual(wifi_vpn_use.share, 0.5*4*10/20*2)
  258. self.assertEqual(wifi_vpn_use.unit_share(), 0.5*4*10/20)
  259. # VPN this is the only service using electricity
  260. self.assertEqual(wifi_vpn_use.unit_real_share(), 10)
  261. # VPN is now using some gear, with deprecation provisioning
  262. hosting_access_fee = Good.objects.create(
  263. name='hosting access fee', price=360,
  264. provisioning_duration=THREE_YEARS, document=self.doc)
  265. GoodUse.objects.create(
  266. service=vpn_service, resource=hosting_access_fee, share=2)
  267. self.assertEqual(
  268. wifi_service.get_prices()['total_goods_value_share'], 36)
  269. self.assertEqual(
  270. wifi_service.get_prices()['unit_goods_value_share'], 18)
  271. def test_service_using_non_usable_service(self):
  272. serva = Service.objects.create(
  273. name='A', document=self.doc,
  274. subscriptions_count=4,
  275. reusable=False,
  276. )
  277. with self.assertRaises(ValidationError):
  278. su = ServiceUse(
  279. service=self.mailbox_service,
  280. resource=serva,
  281. share=1,
  282. )
  283. su.full_clean()
  284. su.save()
  285. def test_service_using_cyclic_service(self):
  286. """ We should not save any service dependency building a cycle
  287. """
  288. a = Service.objects.create(
  289. name='a', document=self.doc,
  290. reusable=True,
  291. )
  292. b = Service.objects.create(
  293. name='b', document=self.doc,
  294. reusable=True,
  295. )
  296. c = Service.objects.create(
  297. name='c', document=self.doc,
  298. reusable=True,
  299. )
  300. def create_clean(user, used):
  301. new_use = ServiceUse(service=user, resource=used, share=1)
  302. new_use.full_clean()
  303. new_use.save()
  304. create_clean(a, b)
  305. with self.assertRaises(ValidationError):
  306. create_clean(b, a)
  307. create_clean(b, c)
  308. with self.assertRaises(ValidationError):
  309. create_clean(c, a)
  310. class TestDocuments(TestCase):
  311. fixtures = ['full_example.yaml']
  312. _all_models = [Document, Cost, Good, Service, GoodUse, CostUse, ServiceUse]
  313. def _count(self):
  314. return [i.objects.count() for i in self._all_models]
  315. def test_copy(self):
  316. initial_counts = self._count()
  317. old_doc = Document.objects.first()
  318. new_doc = old_doc.copy()
  319. self.assertNotEqual(new_doc.pk, old_doc.pk)
  320. self.assertEqual(self._count(), [i*2 for i in initial_counts])
  321. old_doc.delete()
  322. self.assertEqual(self._count(), initial_counts)