def test_is_requred(self): f = MultipleFormField(FooForm) with self.assertRaises(ValidationError) as cm: f.clean([]) self.assertEqual( 'Input is required. Expected not empty list but got [].', cm.exception.message)
def test_max_count(self): f = MultipleFormField(FooForm, max_count=2) with self.assertRaises(ValidationError) as cm: f.clean([{'name': 'abcde'}, {'name': 'fghij'}, {'name': 'klmno'}]) self.assertEqual('There needs to be at most 2 items.', cm.exception.message)
def test_min_count(self): f = MultipleFormField(FooForm, min_count=2) with self.assertRaises(ValidationError) as cm: f.clean(['1']) self.assertEqual('There needs to be at least 2 items.', cm.exception.message)
def test_sub_form_validation(self): f = MultipleFormField(FooForm) with self.assertRaises(ValidationError) as cm: f.clean([{'name': ''}]) self.assertIn('[0]', cm.exception.message) self.assertIn('name', cm.exception.message) self.assertIn('This field is required.', cm.exception.message)
def test_sanitation(self): f = MultipleFormField(FooForm) cleaned_data = f.clean([{'name': 'abcde'}, {'name': 'fghij'}]) self.assertEqual(FooForm, type(cleaned_data[0])) self.assertEqual('abcde', cleaned_data[0].cleaned_data['name']) self.assertEqual(FooForm, type(cleaned_data[1])) self.assertEqual('fghij', cleaned_data[1].cleaned_data['name'])
def test_is_not_requred(self): f = MultipleFormField(FooForm, required=False) # should not raise any exception f.clean(None) f.clean([])