def test_fields_required_empty(self): """Test fields with the required prop and ensure they raise an error when given empty data """ testfield = CharField('Test', required=True) with self.assertRaises(InvalidField): testfield.validate(None) with self.assertRaises(InvalidField): testfield.validate('') testfield = TextField('Test', required=True) with self.assertRaises(InvalidField): testfield.validate(None) with self.assertRaises(InvalidField): testfield.validate('') testfield = DateTimeField('Test', required=True) with self.assertRaises(InvalidField): testfield.validate(None) with self.assertRaises(InvalidField): testfield.validate('') testfield = IntegerField('Test', required=True) with self.assertRaises(InvalidField): testfield.validate(None) with self.assertRaises(InvalidField): testfield.validate('')
def test_intfield_bounds(self): """IntegerField should throw InvalidField when given a string that parses into a value outside the bounds defined by min_value and max_value""" # test the bounds sanity check with self.assertRaises(InvalidField): testfield = IntegerField('Test', min_value=1, max_value=0) testfield = IntegerField('Test', min_value=0, max_value=100) with self.assertRaises(InvalidField): testfield.validate('101') with self.assertRaises(InvalidField): testfield.validate('-1')
def test_intfield_invalid_data(self): """IntegerField should throw InvalidField when given a string that can not be parsed into an int""" testfield = IntegerField('Test') def validate_test(input): with self.assertRaises(InvalidField): testfield.validate(input) validate_test('1234.56789') validate_test('text')
def test_intfield(self): """IntegerField should correctly parse integer strings""" testfield = IntegerField('Test') def value_test(value): value_sz = str(value) testfield.validate(value_sz) self.assertEqual(testfield.prepare_data(value_sz), value) value_test(-7418529) value_test(0) value_test(4573495)
def test_fields_notrequired_empty(self): """Not required fields should accept empty values""" testfield = CharField('Test') testfield.validate('') self.assertEqual(testfield.prepare_data(''), '') testfield = TextField('Test') testfield.validate('') self.assertEqual(testfield.prepare_data(''), '') testfield = DateTimeField('Test') testfield.validate('') self.assertEqual(testfield.prepare_data(''), None) testfield = IntegerField('Test') testfield.validate('') self.assertEqual(testfield.prepare_data(''), None)
def test_fields_required(self): """Test fields with the required prop and ensure they validate""" testfield = CharField('Test', required=True) testfield.validate('test') self.assertEqual(testfield.prepare_data('test'), 'test') testfield = TextField('Test', required=True) testfield.validate('test') self.assertEqual(testfield.prepare_data('test'), 'test') testfield = DateTimeField('Test', required=True) date = datetime.today() date_sz = date.isoformat() testfield.validate(date_sz) self.assertEqual(testfield.prepare_data(date_sz), date) testfield = IntegerField('Test', required=True) testfield.validate('5') self.assertEqual(testfield.prepare_data('5'), 5)
class UpcomingEventsWidget(Widget): id = 'upcoming-events' name = 'Upcoming Events' template = 'widgets/upcoming-events.html' zones = (HomePageSidebarBottom, ) featured_events = EventField('Featured Event(s)', many=True) featured_event_until = DateTimeField('Featured Event Time Limit') number_of_events = IntegerField('Number of Upcoming Events', min_value=0) def context(self, result): """Override context to add the next N events occuring to the context""" num_events = result['number_of_events'] if num_events is None: num_events = 5 if result['featured_event_until']: today = datetime.today() if today > result['featured_event_until'].replace(tzinfo=None): result['featured_events'] = None if result['featured_events']: exclusions = map(lambda e: e.pk, result['featured_events']) else: exclusions = [] events = Event.objects \ .filter(is_submission=False) \ .filter(is_published=True) \ .filter(start_time__gt=datetime.today()) \ .exclude(pk__in=exclusions) \ .order_by('start_time')[:num_events] result['upcoming'] = events return result