def test_fieldlist_init(self): # TEST: initialize FieldList with an instance of Field FieldList(field=fields.IntegerField()) # TEST: initialize FieldList with non-Fields - throws an error expected_error = FieldList.default_error_messages['not_field'] for non_field in [1, 'blah'] + list(EMPTY_VALUES): with self.assertRaisesMessage(RuntimeError, str(expected_error)): log_input(non_field) FieldList(field=non_field)
def test_fieldlist_required_false(self): field_list = FieldList(field=fields.IntegerField(), required=False) # TEST: list of values matching the FieldList's field test_val = [1, 2, 3, 4] self.assertEqual(test_val, field_list.clean(test_val)) # TEST: required=False - empty values outside of a list return [] for empty_val in EMPTY_VALUES: self.assertEqual([], field_list.clean(empty_val))
def test_fieldlist_required(self): field_list = FieldList(field=fields.IntegerField()) # TEST: valid input valid_val = [1, 2, 3] self.assertEqual(valid_val, field_list.clean(valid_val)) # TEST: invalid input (individual non-list truthy values) invalid_vals = [True, 1, datetime.datetime.now(), 'blah', {'blah'}] expected_error = FieldList.default_error_messages['not_list'] for invalid_val in invalid_vals: with self.assertRaisesMessage(ValidationError, str(expected_error)): field_list.clean(invalid_val) # TEST: invalid input (list of values the FieldList's IntegerField considers invalid) invalid_vals = [False, datetime.datetime.now(), 'blah', {'blah'}] expected_error = str(['Enter a whole number.'] * len(invalid_vals)) with self.assertRaisesMessage(ValidationError, expected_error): field_list.clean(invalid_vals) # TEST: invalid input (list of values that the FieldList's IntegerField considers empty) invalid_vals = list(EMPTY_VALUES) expected_error = str(['This field is required.'] * len(invalid_vals)) with self.assertRaisesMessage(ValidationError, expected_error): field_list.clean(invalid_vals) # TEST: required=True - empty values outside of a list throw error for empty_value in EMPTY_VALUES: with self.assertRaisesMessage(ValidationError, "['This field is required.']"): field_list.clean(empty_value)
class ArtistForm(Form): name = fields.CharField(required=True, max_length=100) genres = FieldList(field=fields.CharField(max_length=30)) members = fields.IntegerField()