class CalendarTestCase(APITestCase): """ Class to test the model Calendar """ def setUp(self): self.calendar = CalendarFactory(available_capacity=10) self.orders = self.create_orders() self.assistants = self.create_assistants() def create_orders(self): statuses = [ Order.ORDER_APPROVED_STATUS, Order.ORDER_PENDING_STATUS, Order.ORDER_DECLINED_STATUS, Order.ORDER_CANCELLED_STATUS ] orders = mommy.make(Order, calendar=self.calendar, status=cycle(statuses), quantity=4, _quantity=4) return orders def create_assistants(self): enrolled = [True, False] return mommy.make(Assistant, order=cycle(self.orders), enrolled=cycle(enrolled), _quantity=16) def test_available_capacity(self): """ Test the available capacity property """ # Capacity = 10 # Num enrolled assistants = 4 # Available capacity = 10 - 4 = 6 self.assertEqual(self.calendar.available_capacity, 6) # Capacity = 10 # Num enrolled assistants = 4 # Available capacity = 10 - 4 = 6 # Cancel assistant # Num enrolled assistants = 4 - 1 = 3 # Available capacity = 10 - 3 = 7 approved_order = self.orders[0] assistants = approved_order.assistants.all() first_assistant = assistants[0] first_assistant.enrolled = False first_assistant.save() self.assertEqual(self.calendar.available_capacity, 7) # Capacity = 10 # Num enrolled assistants = 4 # Available capacity = 10 - 4 = 6 # Cancel assistant # Num enrolled assistants = 4 - 1 = 3 # Available capacity = 10 - 3 = 7 # Cancel order # Num enrolled assistants = 3 - 3 = 0 # Available capacity = 10 - 0 = 10 approved_order.change_status(Order.ORDER_CANCELLED_STATUS) self.assertEqual(self.calendar.available_capacity, 10) # Capacity = 10 # Num enrolled assistants = 4 # Available capacity = 10 - 4 = 6 # Cancel assistant # Num enrolled assistants = 4 - 1 = 3 # Available capacity = 10 - 3 = 7 # Cancel order # Num enrolled assistants = 3 - 3 = 0 # Available capacity = 10 - 0 = 10 # Approve order # Num enrolled assistants = 0 + 3 = 0 # Available capacity = 10 - 3 = 7 approved_order.change_status(Order.ORDER_APPROVED_STATUS) self.assertEqual(self.calendar.available_capacity, 7) def test_permissions(self): """ When an instance is created should set the permissions """ user = self.calendar.activity.organizer.user self.assertTrue( user.has_perm('activities.change_calendar', self.calendar)) self.assertTrue( user.has_perm('activities.delete_calendar', self.calendar)) def test_get_assistants(self): """ Test the method get_assistants """ assistants = Assistant.objects.filter(order__calendar=self.calendar, order__status__in=[ Order.ORDER_APPROVED_STATUS, Order.ORDER_PENDING_STATUS ], enrolled=True) self.assertEqual(self.calendar.get_assistants(), list(assistants))
class CalendarSerializerTest(APITestCase): """ Test cases for CalendarSerializer """ def setUp(self): self.calendar = CalendarFactory(session_price=100000) self.order = mommy.make(Order, status=Order.ORDER_APPROVED_STATUS, calendar=self.calendar, quantity=3) self.assistants = mommy.make(Assistant, order=self.order, _quantity=2) def test_read(self): """ Test the serialize data """ epoch = UnixEpochDateField() mommy.make(Assistant, order=self.order, enrolled=False) package = CalendarPackageFactory(calendar=self.calendar, type=1) serializer = CalendarSerializer(self.calendar) content = { 'id': self.calendar.id, 'activity': self.calendar.activity.id, 'initial_date': epoch.to_representation(self.calendar.initial_date), 'enroll_open': True, 'session_price': self.calendar.session_price, 'schedules': '<p><strong>Lunes - Viernes</strong></p><p>6:00pm - 9:00pm</p>', 'assistants': AssistantsSerializer(self.calendar.get_assistants(), many=True, remove_fields=['student']).data, 'is_weekend': self.calendar.is_weekend, 'is_free': self.calendar.is_free, 'available_capacity': self.calendar.available_capacity, 'packages': [{ 'id': package.id, 'quantity': package.quantity, 'price': package.price, 'type': 1, 'type_name': 'Mes(es)', }], } self.assertTrue(all(item in serializer.data.items() for item in content.items())) def test_create(self): """ The serializer should create the calendar with the data passed """ today = now() epoch = UnixEpochDateField() activity = ActivityFactory() data = { 'activity': activity.id, 'initial_date': epoch.to_representation(today), 'session_price': 300000, 'available_capacity': 10, 'note': 'Note', 'schedules': '<p><strong>Lunes - Viernes</strong></p><p>6:00pm - 9:00pm</p>', 'packages': [{ 'quantity': 16, 'price': 100000, 'type': 1, }] } calendar_counter = Calendar.objects.count() package_counter = CalendarPackage.objects.count() serializer = CalendarSerializer(data=data) serializer.is_valid(raise_exception=True) serializer.save() self.assertEqual(Calendar.objects.count(), calendar_counter + 1) self.assertEqual(CalendarPackage.objects.count(), package_counter + 1) def test_update(self): """ The serializer should update the data even the packages data """ package = CalendarPackageFactory(calendar=self.calendar, price=100000, quantity=4) data = { 'session_price': 500000, 'packages': [{ 'id': package.id, 'quantity': 6, }] } serializer = CalendarSerializer(self.calendar, data=data, partial=True) self.assertTrue(serializer.is_valid(raise_exception=True)) serializer.save() calendar = Calendar.objects.get(id=self.calendar.id) package = CalendarPackage.objects.get(id=package.id) self.assertEqual(calendar.session_price, 500000) self.assertEqual(package.quantity, 6) def test_create_other_package_in_update(self): """ If the serializer receive one more package in the update method the package should be created """ calendar = CalendarFactory(activity__is_open=True) packages = CalendarPackageFactory.create_batch(2, calendar=calendar) data = { 'packages':[{ 'id': p.id, 'quantity': p.quantity, 'price': p.price, } for p in packages] } # The new package data['packages'].append({ 'quantity': 16, 'price': 183740, 'type': 1, }) packages_counter = CalendarPackage.objects.count() serializer = CalendarSerializer(calendar, data=data, partial=True) self.assertTrue(serializer.is_valid(raise_exception=True)) serializer.save() self.assertEqual(CalendarPackage.objects.count(), packages_counter + 1) def test_should_not_update_schedule_if_there_are_orders(self): """ The serializer shouldn't allow to update the schedules field if there are orders associated to the calendar """ data = {'schedules': '<p>No Schedule!</p>'} msg = 'No se puede cambiar el horario debido a que existen ordenes relacionadas.' serializer = CalendarSerializer(self.calendar, data=data, partial=True) with self.assertRaisesMessage(ValidationError, "{'schedules': ['%s']}" % msg): serializer.is_valid(raise_exception=True) def test_if_activity_is_closed_session_price_should_be_required(self): """ If the activity is_open is False the CalendarSerializer should require the session price and not the packages """ today = now() epoch = UnixEpochDateField() activity = ActivityFactory(is_open=False) data = { 'activity': activity.id, 'initial_date': epoch.to_representation(today), 'available_capacity': 10, 'note': 'Note', 'schedules': '<p><strong>Lunes - Viernes</strong></p><p>6:00pm - 9:00pm</p>', } serializer = CalendarSerializer(data=data) with self.assertRaisesMessage(ValidationError, "{'session_price': ['Este campo es" " requerido.']}"): serializer.is_valid(raise_exception=True) def test_if_activity_is_open_packages_should_be_required(self): """ If the activity is_open is False the CalendarSerializer should require the session price and not the packages """ today = now() epoch = UnixEpochDateField() activity = ActivityFactory(is_open=True) data = { 'activity': activity.id, 'initial_date': epoch.to_representation(today), 'available_capacity': 10, 'note': 'Note', 'schedules': '<p><strong>Lunes - Viernes</strong></p><p>6:00pm - 9:00pm</p>', } serializer = CalendarSerializer(data=data) with self.assertRaisesMessage(ValidationError, "{'packages': ['Este campo es" " requerido.']}"): serializer.is_valid(raise_exception=True) def test_only_one_calendar_activity_open(self): """ If the activity is open the serializer shouldn't let create more than 1 calendar """ today = now() epoch = UnixEpochDateField() activity = ActivityFactory(is_open=True) CalendarFactory(activity=activity) data = { 'activity': activity.id, 'initial_date': epoch.to_representation(today), 'available_capacity': 10, 'note': 'Note', 'schedules': '<p><strong>Lunes - Viernes</strong></p><p>6:00pm - 9:00pm</p>', 'packages': [{ 'quantity': 3, 'price': 123843, 'type': 1, }] } serializer = CalendarSerializer(data=data) with self.assertRaisesMessage(ValidationError, "{'non_field_errors': ['No se puede crear" " más de un calendario cuando la actividad" " es de horario abierto']}"): serializer.is_valid(raise_exception=True)