tests.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from datetime import datetime, timedelta, date
  2. from django.test import TestCase
  3. from django.utils import timezone
  4. from coin.members.models import Member
  5. from .models import Item, ItemType, Loan
  6. def localize(naive_dt):
  7. if not timezone.is_naive(naive_dt):
  8. raise ValueError('Expecting a naive datetime')
  9. else:
  10. return timezone.make_aware(naive_dt, timezone.get_current_timezone())
  11. class HardwareModelsFactoryMixin:
  12. def get_item_type(self, **kwargs):
  13. params = {'name': 'Foos'}
  14. params.update(**kwargs)
  15. item_type, _ = ItemType.objects.get_or_create(**kwargs)
  16. return item_type
  17. def get_item(self, **kwargs):
  18. params = {
  19. 'type': self.get_item_type(),
  20. 'designation': 'Test item',
  21. }
  22. params.update(**kwargs)
  23. item, _ = Item.objects.get_or_create(**params)
  24. return item
  25. class HardwareLoaningTestCase(HardwareModelsFactoryMixin, TestCase):
  26. def setUp(self):
  27. self.member = Member.objects.create(username='jdoe')
  28. self.item = self.get_item()
  29. def test_running_(self):
  30. loan_start_date = localize(datetime(2011, 1, 14, 12, 0, 0))
  31. loan = Loan.objects.create(
  32. item=self.item, user=self.member,
  33. loan_date=loan_start_date)
  34. self.assertEqual(Loan.objects.running().count(), 1)
  35. self.assertEqual(Loan.objects.finished().count(), 0)
  36. loan.item.give_back()
  37. self.assertEqual(Loan.objects.running().count(), 0)
  38. self.assertEqual(Loan.objects.finished().count(), 1)
  39. class ItemTestCase(HardwareModelsFactoryMixin, TestCase):
  40. def setUp(self):
  41. self.member = Member.objects.create(username='jdoe')
  42. self.free_item = self.get_item(designation='free')
  43. self.deployed_item = self.get_item(
  44. designation='deployed', deployed=True)
  45. self.borrowed_item = self.get_item(designation='borrowed')
  46. def test_queryset_methods(self):
  47. self.assertEqual(Item.objects.borrowed().count(), 0)
  48. self.assertEqual(Item.objects.deployed().count(), 1)
  49. self.assertEqual(Item.objects.available().count(), 2)
  50. self.assertEqual(Item.objects.unavailable().count(), 1)
  51. Loan.objects.create(
  52. item=self.borrowed_item, user=self.member,
  53. loan_date=localize(datetime(2011, 1, 14, 12, 0, 0)))
  54. self.assertEqual(Item.objects.borrowed().count(), 1)
  55. self.assertEqual(Item.objects.deployed().count(), 1)
  56. self.assertEqual(Item.objects.available().count(), 1)
  57. self.assertEqual(Item.objects.unavailable().count(), 2)