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)
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_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_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_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(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 create_closed_calendars(self, activity, closed_calendars, schedules): calendars = closed_calendars.replace(' ', '').split(';') for calendar_data in calendars: epoch = UnixEpochDateField() date, capacity, price = calendar_data.split('-') data = { 'activity': activity.id, 'initial_date': epoch.to_representation( datetime.datetime.strptime(date, '%d/%m/%Y').date()), 'session_price': price, 'available_capacity': capacity, 'schedules': schedules, } serializer = CalendarSerializer(data=data) serializer.is_valid(raise_exception=True) serializer.save()
def create_open_calendars(self, activity, open_calendars, schedules): calendars = open_calendars.replace(' ', '').split(';') epoch = UnixEpochDateField() data = { 'activity': activity.id, 'available_capacity': 100, 'initial_date': epoch.to_representation(datetime.datetime.max.date()), 'schedules': schedules, 'packages': [], } for calendar_data in calendars: quantity, price, type_ = calendar_data.split('-') data['packages'].append({ 'quantity': quantity, 'price': price, 'type': 1 if type_ == 'mes' else 2 }) serializer = CalendarSerializer(data=data) serializer.is_valid(raise_exception=True) serializer.save()