def test_activity_instance_retrieval(self): scheduling_manager = ActivityInstanceManager() vendor1 = create_vendor('vendor1') activity1 = create_activity('activity1', vendor1) activity2 = create_activity('activity2', vendor1) activity1_instance = ActivityInstance(activity=activity1, start_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Australia/Sydney')).replace(hour=3, minute=30, month=1).isoformat('T')), end_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Australia/Sydney')).replace(hour=4, minute=0, month=1).isoformat('T')), price=12.12, capacity=10, bookings=0) activity1_instance.save() activity2_instance = ActivityInstance(activity=activity2, start_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('US/Eastern')).replace(hour=8, month=6).isoformat('T')), end_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('US/Eastern')).replace(hour=15, month=6).isoformat('T')), price=12.12, capacity=10, bookings=4) activity2_instance.save() retrieved_activity1_instance = scheduling_manager.retrieve_activity_instance( activity1_instance.activity_instance_id) self.assertIsNotNone(retrieved_activity1_instance) retrieved_activity2_instance = scheduling_manager.retrieve_activity_instance( activity2_instance.activity_instance_id) self.assertIsNotNone(retrieved_activity2_instance) self.assertIsNone(scheduling_manager.retrieve_activity_instance("0")) self.assertIsNone(scheduling_manager.retrieve_activity_instance("")) self.assertIsNone(scheduling_manager.retrieve_activity_instance(None))
def test_get_activity_instance(self): vendor1 = create_vendor('vendor1') activity1 = create_activity('activity1', vendor1) activity2 = create_activity('activity2', vendor1) start_time = datetime.now(tz=pytz.timezone('Australia/Sydney')).replace(hour=3, minute=30, month=1) end_time = datetime.now(tz=pytz.timezone('Australia/Sydney')).replace(hour=4, minute=0, month=1) activity1_instance = ActivityInstance(activity=activity1, start_time=util.iso8601_to_utc_timestamp(start_time.isoformat('T')), end_time=util.iso8601_to_utc_timestamp(end_time.isoformat('T')), price=12.12, capacity=10, bookings=0) activity1_instance.save() activity2_instance = ActivityInstance(activity=activity2, start_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('US/Eastern')).replace(hour=8, month=6).isoformat('T')), end_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('US/Eastern')).replace(hour=15, month=6).isoformat('T')), price=12.12, capacity=10, bookings=4) activity2_instance.save() retrieved_activity1_instance = json.loads(self.client.get(reverse('activity_instance_view', args=[activity1_instance.activity_instance_id])).content.decode('utf-8')) self.assertIsNotNone(retrieved_activity1_instance['activity_instance_id']) self.assertEqual(util.iso8601_to_utc_timestamp(retrieved_activity1_instance['start_time']), util.iso8601_to_utc_timestamp(start_time.isoformat('T'))) self.assertEqual(util.iso8601_to_utc_timestamp(retrieved_activity1_instance['end_time']), util.iso8601_to_utc_timestamp(end_time.isoformat('T'))) retrieved_activity2_instance = json.loads(self.client.get(reverse('activity_instance_view', args=[activity2_instance.activity_instance_id])).content.decode('utf-8')) self.assertIsNotNone(retrieved_activity2_instance['activity_instance_id']) self.assertEqual(self.client.get(reverse('activity_instance_view', args=["0"])).status_code, 404) self.assertEqual(self.client.get(reverse('activity_instance_view', args=[""])).status_code, 404) self.assertEqual(self.client.get(reverse('activity_instance_view', args=[None])).status_code, 404)
def create_activity_instance(self, activity_id, start_time, end_time, price, capacity, bookings=0): activity = Activity.objects.filter(activity_id=activity_id) if activity.count() is not 1: raise InvalidActivityException(activity_id) if capacity < 1: raise InvalidCapacityException(capacity) if bookings > capacity: raise BookingsCountInvalidException(capacity, bookings) if end_time < start_time or end_time-start_time < 1800: raise InvalidActivityInstanceDuration() activity_instance = ActivityInstance(activity=activity[0], start_time=start_time, end_time=end_time, price=price, capacity=capacity, bookings=bookings) activity_instance.save() return activity_instance
def test_put_activity_instance(self): vendor1 = create_vendor('vendor1') activity1 = create_activity('activity1', vendor1) activity_instance1 = ActivityInstance(activity=activity1, start_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Hongkong')).replace(hour=14, month=7).isoformat('T')), end_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Hongkong')).replace(hour=15, month=7).isoformat('T')), price=12.12, capacity=10, bookings=0) activity_instance1.save() activity_instance1 = json.loads(self.client.put(reverse('activity_instance_view', args=[activity_instance1.activity_instance_id]), {'start_time': datetime.now(tz=pytz.timezone('Asia/Kolkata')).replace(hour=1).isoformat('T')}).content.decode('utf-8')) retrieved_instance1 = ActivityInstance.objects.get(activity_instance_id=activity_instance1['activity_instance_id']) self.assertEqual(util.iso8601_to_utc_timestamp(activity_instance1['start_time']), retrieved_instance1.start_time) activity_instance1 = json.loads(self.client.put(reverse('activity_instance_view', args=[activity_instance1['activity_instance_id']]), {'end_time': datetime.now(tz=pytz.timezone('Greenwich')).replace(hour=14).isoformat('T')}).content.decode('utf-8')) retrieved_instance1 = ActivityInstance.objects.get(activity_instance_id=activity_instance1['activity_instance_id']) self.assertEqual(util.iso8601_to_utc_timestamp(activity_instance1['end_time']), retrieved_instance1.end_time) activity_instance1 = json.loads(self.client.put(reverse('activity_instance_view', args=[activity_instance1['activity_instance_id']]), {'price': 15.23}).content.decode('utf-8')) retrieved_instance1 = ActivityInstance.objects.get(activity_instance_id=activity_instance1['activity_instance_id']) self.assertEqual(float(activity_instance1['price']), float(retrieved_instance1.price)) activity_instance1 = json.loads(self.client.put(reverse('activity_instance_view', args=[activity_instance1['activity_instance_id']]), {'capacity': 20}).content.decode('utf-8')) retrieved_instance1 = ActivityInstance.objects.get(activity_instance_id=activity_instance1['activity_instance_id']) self.assertEqual(int(activity_instance1['capacity']), retrieved_instance1.capacity) activity_instance1 = json.loads(self.client.put(reverse('activity_instance_view', args=[activity_instance1['activity_instance_id']]), {'bookings': 10}).content.decode('utf-8')) retrieved_instance1 = ActivityInstance.objects.get(activity_instance_id=activity_instance1['activity_instance_id']) self.assertEqual(int(activity_instance1['bookings']), retrieved_instance1.bookings) activity_instance1 = json.loads(self.client.put(reverse('activity_instance_view', args=[activity_instance1['activity_instance_id']]), {'bookings': 15, 'capacity': 25, 'start_time': datetime.now(tz=pytz.timezone('Iran')).replace(hour=2, month=9).isoformat('T'), 'end_time': datetime.now(tz=pytz.timezone('Turkey')).replace(hour=4, month=9).isoformat('T'), 'price': 300}).content.decode('utf-8')) retrieved_instance1 = ActivityInstance.objects.get(activity_instance_id=activity_instance1['activity_instance_id']) self.assertEqual(int(activity_instance1['bookings']), retrieved_instance1.bookings) self.assertEqual(int(activity_instance1['capacity']), retrieved_instance1.capacity) self.assertEqual(util.iso8601_to_utc_timestamp(activity_instance1['start_time']), retrieved_instance1.start_time) self.assertEqual(util.iso8601_to_utc_timestamp(activity_instance1['end_time']), retrieved_instance1.end_time) self.assertEqual(int(activity_instance1['price']), float(retrieved_instance1.price)) with self.assertRaises(BookingsCountInvalidException): activity_instance1 = json.loads(self.client.put(reverse('activity_instance_view', args=[activity_instance1['activity_instance_id']]), {'bookings': 30}).content.decode('utf-8')) with self.assertRaises(BookingsCountInvalidException): activity_instance1 = json.loads(self.client.put(reverse('activity_instance_view', args=[activity_instance1['activity_instance_id']]), {'capacity': 100, 'bookings': 500}).content.decode('utf-8')) with self.assertRaises(BookingsCountInvalidException): activity_instance1 = json.loads(self.client.put(reverse('activity_instance_view', args=[activity_instance1['activity_instance_id']]), {'bookings': 1100, 'capacity': 200}).content.decode('utf-8')) try: activity_instance1 = json.loads(self.client.put(reverse('activity_instance_view', args=[activity_instance1['activity_instance_id']]), {'bookings': 1, 'capacity': 1}).content.decode('utf-8')) except BookingsCountInvalidException: self.fail("Equal capacity and bookings value raised exception.") with self.assertRaises(InvalidCapacityException): activity_instance1 = json.loads(self.client.put(reverse('activity_instance_view', args=[activity_instance1['activity_instance_id']]), {'capacity': 0}).content.decode('utf-8')) with self.assertRaises(InvalidCapacityException): activity_instance1 = json.loads(self.client.put(reverse('activity_instance_view', args=[activity_instance1['activity_instance_id']]), {'capacity': 0, 'bookings': 12}).content.decode('utf-8')) with self.assertRaises(InvalidCapacityException): activity_instance1 = json.loads(self.client.put(reverse('activity_instance_view', args=[activity_instance1['activity_instance_id']]), {'capacity': -1}).content.decode('utf-8'))
def test_activity_instance_update(self): scheduling_manager = ActivityInstanceManager() vendor1 = create_vendor('vendor1') activity1 = create_activity('activity1', vendor1) activity_instance1 = ActivityInstance(activity=activity1, start_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Hongkong')).replace(hour=14, month=7).isoformat('T')), end_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Hongkong')).replace(hour=15, month=7).isoformat('T')), price=12.12, capacity=10, bookings=0) activity_instance1.save() activity_instance1 = scheduling_manager.update_activity_instance(activity_instance1.activity_instance_id, start_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Asia/Kolkata')).replace(hour=1).isoformat('T'))) retrieved_instance1 = ActivityInstance.objects.get(activity_instance_id=activity_instance1.activity_instance_id) self.assertEqual(activity_instance1.start_time, retrieved_instance1.start_time) activity_instance1 = scheduling_manager.update_activity_instance(activity_instance1.activity_instance_id, end_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Greenwich')).replace(hour=14).isoformat('T'))) retrieved_instance1 = ActivityInstance.objects.get(activity_instance_id=activity_instance1.activity_instance_id) self.assertEqual(activity_instance1.end_time, retrieved_instance1.end_time) activity_instance1 = scheduling_manager.update_activity_instance(activity_instance1.activity_instance_id, price=15.23) retrieved_instance1 = ActivityInstance.objects.get(activity_instance_id=activity_instance1.activity_instance_id) self.assertEqual(activity_instance1.price, float(retrieved_instance1.price)) activity_instance1 = scheduling_manager.update_activity_instance(activity_instance1.activity_instance_id, capacity=20) retrieved_instance1 = ActivityInstance.objects.get(activity_instance_id=activity_instance1.activity_instance_id) self.assertEqual(activity_instance1.capacity, retrieved_instance1.capacity) activity_instance1 = scheduling_manager.update_activity_instance(activity_instance1.activity_instance_id, bookings=10) retrieved_instance1 = ActivityInstance.objects.get(activity_instance_id=activity_instance1.activity_instance_id) self.assertEqual(activity_instance1.bookings, retrieved_instance1.bookings) activity_instance1 = scheduling_manager.update_activity_instance(activity_instance1.activity_instance_id, bookings=15, capacity=25, start_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Iran')).replace(hour=2, month=9).isoformat('T')), end_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Turkey')).replace(hour=4, month=9).isoformat('T')), price=300) retrieved_instance1 = ActivityInstance.objects.get(activity_instance_id=activity_instance1.activity_instance_id) self.assertEqual(activity_instance1.bookings, retrieved_instance1.bookings) self.assertEqual(activity_instance1.capacity, retrieved_instance1.capacity) self.assertEqual(activity_instance1.start_time, retrieved_instance1.start_time) self.assertEqual(activity_instance1.end_time, retrieved_instance1.end_time) self.assertEqual(activity_instance1.price, float(retrieved_instance1.price)) with self.assertRaises(BookingsCountInvalidException): activity_instance1 = scheduling_manager.update_activity_instance(activity_instance1.activity_instance_id, bookings=30) with self.assertRaises(BookingsCountInvalidException): activity_instance1 = scheduling_manager.update_activity_instance(activity_instance1.activity_instance_id, capacity=100, bookings=500) with self.assertRaises(BookingsCountInvalidException): activity_instance1 = scheduling_manager.update_activity_instance(activity_instance1.activity_instance_id, bookings=1100, capacity=200) try: activity_instance1 = scheduling_manager.update_activity_instance(activity_instance1.activity_instance_id, bookings=1, capacity=1) except BookingsCountInvalidException: self.fail("Equal capacity and bookings value raised exception.") with self.assertRaises(InvalidCapacityException): activity_instance1 = scheduling_manager.update_activity_instance(activity_instance1.activity_instance_id, capacity=0) with self.assertRaises(InvalidCapacityException): activity_instance1 = scheduling_manager.update_activity_instance(activity_instance1.activity_instance_id, capacity=0, bookings=12) with self.assertRaises(InvalidCapacityException): activity_instance1 = scheduling_manager.update_activity_instance(activity_instance1.activity_instance_id, capacity=-1)
def test_activity_instance_deletion(self): scheduling_manager = ActivityInstanceManager() vendor1 = create_vendor('vendor1') activity1 = create_activity('activity1', vendor1) activity2 = create_activity('activity2', vendor1) activity3 = create_activity('activity3', vendor1) activity1_instance1 = ActivityInstance(activity=activity1, start_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Australia/Sydney')).replace(hour=12).isoformat('T')), end_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Australia/Sydney')).replace(hour=14).isoformat('T')), price=12.12, capacity=10, bookings=0) activity1_instance1.save() activity1_instance2 = ActivityInstance(activity=activity1, start_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('US/Eastern')).replace(hour=13, month=11).isoformat('T')), end_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('US/Eastern')).replace(hour=17, month=11).isoformat('T')), price=12.12, capacity=10, bookings=0) activity1_instance2.save() activity2_instance1 = ActivityInstance(activity=activity2, start_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Europe/Warsaw')).replace(hour=11).isoformat('T')), end_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Europe/Warsaw')).replace(hour=14).isoformat('T')), price=12.12, capacity=10, bookings=0) activity2_instance1.save() activity2_instance2 = ActivityInstance(activity=activity2, start_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Canada/Mountain')).replace(hour=1, month=4).isoformat('T')), end_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Canada/Mountain')).replace(hour=2, month=4).isoformat('T')), price=12.12, capacity=10, bookings=0) activity2_instance2.save() activity3_instance1 = ActivityInstance(activity=activity3, start_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Europe/Berlin')).replace(hour=1, minute=0).isoformat('T')), end_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Europe/Berlin')).replace(hour=1, minute=30).isoformat('T')), price=12.12, capacity=10, bookings=0) activity3_instance1.save() activity3_instance2 = ActivityInstance(activity=activity3, start_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Brazil/West')).replace(hour=15, minute=0, month=2).isoformat('T')), end_time=util.iso8601_to_utc_timestamp(datetime.now(tz=pytz.timezone('Brazil/West')).replace(hour=15, minute=45, month=2).isoformat('T')), price=12.12, capacity=10, bookings=0) activity3_instance2.save() self.assertTrue(scheduling_manager.delete_activity_instance(activity1_instance1.activity_instance_id)) self.assertEqual(ActivityInstance.objects.filter( activity_instance_id=activity1_instance1.activity_instance_id).count(), 0) self.assertTrue(scheduling_manager.delete_activity_instance(activity1_instance2.activity_instance_id)) self.assertEqual(ActivityInstance.objects.filter( activity_instance_id=activity1_instance2.activity_instance_id).count(), 0) delete_activity(activity2) self.assertEqual(ActivityInstance.objects.filter( activity_instance_id=activity2_instance1.activity_instance_id).count(), 0) self.assertEqual(ActivityInstance.objects.filter( activity_instance_id=activity2_instance2.activity_instance_id).count(), 0) delete_vendor(vendor1) self.assertEqual(Activity.objects.filter(activity_id=activity1.activity_id).count(), 0) self.assertEqual(Activity.objects.filter(activity_id=activity2.activity_id).count(), 0) self.assertEqual(Activity.objects.filter(activity_id=activity3.activity_id).count(), 0) self.assertEqual(ActivityInstance.objects.filter( activity_instance_id=activity3_instance1.activity_instance_id).count(), 0) self.assertEqual(ActivityInstance.objects.filter( activity_instance_id=activity3_instance2.activity_instance_id).count(), 0) self.assertFalse(scheduling_manager.delete_activity_instance(activity1_instance1.activity_instance_id)) self.assertFalse(scheduling_manager.delete_activity_instance(activity1_instance2.activity_instance_id))