def reserve(cls, *args, **kwargs): try: group = cls.objects.create() AtomicReserver.non_transactional_reserve(*args, group=group, **kwargs) transaction.commit() return group except: transaction.rollback() raise
def test_improper_atomic_reservation(self): initial_count = RoomReservation.objects.count() with self.assertRaises(ReservationError): AtomicReserver.reserve( (self.room5, {'start': '2012-04-01', 'end': '2012-05-12', 'size': 3}), (self.room1, {'start': '2012-04-01', 'end': '2012-05-12', 'size': 1}), (self.room3, {'start': '2012-04-01', 'end': '2012-05-12', 'size': 5}) ) self.assertEqual(RoomReservation.objects.count(), initial_count, 'There should be no reservation objects added to the database')
def test_proper_atomic_reservation(self): initial_count = RoomReservation.objects.count() reservations = AtomicReserver.reserve( (self.room5, {'start': '2012-04-01', 'end': '2012-05-12', 'size': 3}), (self.room1, {'start': '2012-04-01', 'end': '2012-05-12', 'size': 1}), (self.room3, {'start': '2012-04-01', 'end': '2012-05-12', 'size': 2}) ) self.assertEqual(len(reservations), 3, 'There should be 3 reservation objects returned') self.assertEqual(reservations[0].size, 3, 'First reservation should have size equal to 3') self.assertEqual(RoomReservation.objects.count(), initial_count + 3, 'There should be 3 reservation objects added to the database')