|
@@ -14,20 +14,30 @@ def localize(naive_dt):
|
|
|
return timezone.make_aware(naive_dt, timezone.get_current_timezone())
|
|
|
|
|
|
|
|
|
-class HardwareLoaningTestCase(TestCase):
|
|
|
+class HardwareModelsFactoryMixin:
|
|
|
+ def get_item_type(self, **kwargs):
|
|
|
+ params = {'name': 'Foos'}
|
|
|
+ params.update(**kwargs)
|
|
|
+ item_type, _ = ItemType.objects.get_or_create(**kwargs)
|
|
|
+ return item_type
|
|
|
+
|
|
|
+ def get_item(self, **kwargs):
|
|
|
+ params = {
|
|
|
+ 'type': self.get_item_type(),
|
|
|
+ 'designation': 'Test item',
|
|
|
+ }
|
|
|
+ params.update(**kwargs)
|
|
|
+ item, _ = Item.objects.get_or_create(**params)
|
|
|
+ return item
|
|
|
+
|
|
|
+
|
|
|
+class HardwareLoaningTestCase(HardwareModelsFactoryMixin, TestCase):
|
|
|
def setUp(self):
|
|
|
- self.member = Member.objects.create(
|
|
|
- first_name='John',
|
|
|
- last_name='Doe',
|
|
|
- username='jdoe')
|
|
|
- self.item_type = ItemType.objects.create(name='Foos')
|
|
|
- self.item = Item.objects.create(
|
|
|
- type=self.item_type,
|
|
|
- designation='Bar Wheel',
|
|
|
- buy_date=date(2012,12,5))
|
|
|
+ self.member = Member.objects.create(username='jdoe')
|
|
|
+ self.item = self.get_item()
|
|
|
|
|
|
def test_running_(self):
|
|
|
- loan_start_date = localize(datetime(2011,1,14,12,0,0))
|
|
|
+ loan_start_date = localize(datetime(2011, 1, 14, 12, 0, 0))
|
|
|
loan = Loan.objects.create(
|
|
|
item=self.item, user=self.member,
|
|
|
loan_date=loan_start_date)
|
|
@@ -37,3 +47,28 @@ class HardwareLoaningTestCase(TestCase):
|
|
|
loan.item.give_back()
|
|
|
self.assertEqual(Loan.objects.running().count(), 0)
|
|
|
self.assertEqual(Loan.objects.finished().count(), 1)
|
|
|
+
|
|
|
+
|
|
|
+class ItemTestCase(HardwareModelsFactoryMixin, TestCase):
|
|
|
+ def setUp(self):
|
|
|
+ self.member = Member.objects.create(username='jdoe')
|
|
|
+
|
|
|
+ self.free_item = self.get_item(designation='free')
|
|
|
+ self.deployed_item = self.get_item(
|
|
|
+ designation='deployed', deployed=True)
|
|
|
+ self.borrowed_item = self.get_item(designation='borrowed')
|
|
|
+
|
|
|
+ def test_queryset_methods(self):
|
|
|
+ self.assertEqual(Item.objects.borrowed().count(), 0)
|
|
|
+ self.assertEqual(Item.objects.deployed().count(), 1)
|
|
|
+ self.assertEqual(Item.objects.available().count(), 2)
|
|
|
+ self.assertEqual(Item.objects.unavailable().count(), 1)
|
|
|
+
|
|
|
+ Loan.objects.create(
|
|
|
+ item=self.borrowed_item, user=self.member,
|
|
|
+ loan_date=localize(datetime(2011, 1, 14, 12, 0, 0)))
|
|
|
+
|
|
|
+ self.assertEqual(Item.objects.borrowed().count(), 1)
|
|
|
+ self.assertEqual(Item.objects.deployed().count(), 1)
|
|
|
+ self.assertEqual(Item.objects.available().count(), 1)
|
|
|
+ self.assertEqual(Item.objects.unavailable().count(), 2)
|