def test_multiple_non_list(self): model_field = MultipleModelField(FooModel) obj = FooModel(one='a') obj.pk = 1 with self.assertRaisesRegexp(ValidationError, "[Ii]terable"): cleaned_data = model_field.clean(obj)
def test_is_required(self): f = MultipleModelField(FooModel) with self.assertRaises(ValidationError) as cm: f.clean(None) self.assertEqual( 'Input is required expected list of models but got None.', cm.exception.message)
def test_multiple_valid(self): model_field = MultipleModelField(FooModel) objects = [FooModel(one='a'), FooModel(one='b')] for obj in objects: obj.pk = 1 cleaned_data = model_field.clean(objects) self.assertEqual(objects, cleaned_data)
def test_multiple_unsaved_false(self): model_field = MultipleModelField(FooModel) objects = [FooModel(one='a'), FooModel(one='b')] for obj in objects: obj.pk = 1 objects.append(FooModel(one='c')) with self.assertRaisesRegexp(ValidationError, "[Uu]nsaved"): cleaned_data = model_field.clean(objects)
def test_multiple_invalid_type(self): model_field = MultipleModelField(FooModel, allow_unsaved=True) objects = [ FooModel(one='a'), FooModel(one='b'), BarModel(one='c') ] with self.assertRaisesRegexp(ValidationError, "FooModel"): cleaned_data = model_field.clean(objects)
def test_is_not_requred(self): f = MultipleModelField(FooModel, required=False) # should not raise any exception f.clean(None)