def test_validators_fail_base_max_length(self): field = SimpleListField(forms.CharField(max_length=5)) with pytest.raises(exceptions.ValidationError) as excinfo: field.clean('longer,yes') assert (excinfo.value.messages[0] == 'Item 1 in the list did not validate: ' 'Ensure this value has at most 5 characters (it has 6).')
def test_to_python_no_double_commas(self): field = SimpleListField(forms.IntegerField()) with pytest.raises(exceptions.ValidationError) as excinfo: field.clean('1,,2') assert ( excinfo.value.messages[0] == 'No leading, trailing, or double commas.' )
def test_min_length(self): field = SimpleListField(forms.CharField(), min_length=4) with pytest.raises(exceptions.ValidationError) as excinfo: field.clean('a,b,c') assert ( excinfo.value.messages[0] == 'List contains 3 items, it should contain no fewer than 4.' )
def test_max_length(self): field = SimpleListField(forms.CharField(), max_length=2) with pytest.raises(exceptions.ValidationError) as excinfo: field.clean("a,b,c") assert ( excinfo.value.messages[0] == "List contains 3 items, it should contain no more than 2." )
def test_validators_fail(self): field = SimpleListField(forms.RegexField('[a-e]{2}')) with pytest.raises(exceptions.ValidationError) as excinfo: field.clean('a,bc,de') assert ( excinfo.value.messages[0] == 'Item 1 in the list did not validate: Enter a valid value.' )
def test_to_python_base_field_does_not_validate(self): field = SimpleListField(forms.IntegerField()) with pytest.raises(exceptions.ValidationError) as excinfo: field.clean('a,b,9') assert ( excinfo.value.messages[0] == 'Item 1 in the list did not validate: Enter a whole number.' )
def test_min_length(self): field = SimpleListField(forms.CharField(), min_length=4) with self.assertRaises(exceptions.ValidationError) as cm: field.clean('a,b,c') self.assertEqual( cm.exception.messages[0], 'List contains 3 items, it should contain no fewer than 4.' )
def test_to_python_no_double_commas(self): field = SimpleListField(forms.IntegerField()) with self.assertRaises(exceptions.ValidationError) as cm: field.clean('1,,2') self.assertEqual( cm.exception.messages[0], 'No leading, trailing, or double commas.' )
def test_to_python_base_field_does_not_validate(self): field = SimpleListField(forms.IntegerField()) with self.assertRaises(exceptions.ValidationError) as cm: field.clean('a,b,9') self.assertEqual( cm.exception.messages[0], 'Item 1 in the list did not validate: Enter a whole number.' )
def test_validators_fail(self): field = SimpleListField(forms.RegexField('[a-e]{2}')) with self.assertRaises(exceptions.ValidationError) as cm: field.clean('a,bc,de') self.assertEqual( cm.exception.messages[0], 'Item 1 in the list did not validate: Enter a valid value.' )
def test_validate_fail(self): field = SimpleListField( forms.ChoiceField(choices=(("a", "The letter A"), ("b", "The letter B")))) with pytest.raises(exceptions.ValidationError) as excinfo: field.clean("a,c") assert excinfo.value.messages[0] == ( "Item 2 in the list did not validate: Select a valid choice. " + "c is not one of the available choices.")
def test_validators_fail_base_max_length(self): field = SimpleListField(forms.CharField(max_length=5)) with pytest.raises(exceptions.ValidationError) as excinfo: field.clean('longer,yes') assert ( excinfo.value.messages[0] == 'Item 1 in the list did not validate: ' 'Ensure this value has at most 5 characters (it has 6).' )
def test_prepare_value(self): field = SimpleListField(forms.CharField()) value = field.prepare_value(['a', 'b', 'c']) self.assertEqual( value.split(','), ['a', 'b', 'c'] ) self.assertEqual(field.prepare_value('1,a'), '1,a')
def test_validators_fail_base_max_length(self): field = SimpleListField(forms.CharField(max_length=5)) with self.assertRaises(exceptions.ValidationError) as cm: field.clean('longer,yes') self.assertEqual( cm.exception.messages[0], 'Item 1 in the list did not validate: ' 'Ensure this value has at most 5 characters (it has 6).' )
def test_validate_fail(self): field = SimpleListField( forms.ChoiceField(choices=(('a', 'The letter A'), ('b', 'The letter B')))) with pytest.raises(exceptions.ValidationError) as excinfo: field.clean('a,c') assert ( excinfo.value.messages[0] == 'Item 2 in the list did not validate: ' 'Select a valid choice. c is not one of the available choices.')
def test_validators_fail_base_min_max_length(self): # there's just no satisfying some people... field = SimpleListField(forms.CharField(min_length=10, max_length=8)) with pytest.raises(exceptions.ValidationError) as excinfo: field.clean('undefined') assert (excinfo.value.messages[0] == 'Item 1 in the list did not validate: ' 'Ensure this value has at least 10 characters (it has 9).') assert (excinfo.value.messages[1] == 'Item 1 in the list did not validate: ' 'Ensure this value has at most 8 characters (it has 9).')
def test_validate_fail(self): field = SimpleListField( forms.ChoiceField(choices=(('a', 'The letter A'), ('b', 'The letter B'))) ) with self.assertRaises(exceptions.ValidationError) as cm: field.clean('a,c') self.assertEqual( cm.exception.messages[0], 'Item 2 in the list did not validate: ' 'Select a valid choice. c is not one of the available choices.' )
def test_validate_fail(self): field = SimpleListField( forms.ChoiceField(choices=(('a', 'The letter A'), ('b', 'The letter B'))) ) with pytest.raises(exceptions.ValidationError) as excinfo: field.clean('a,c') assert ( excinfo.value.messages[0] == 'Item 2 in the list did not validate: ' 'Select a valid choice. c is not one of the available choices.' )
def test_validators_fail_base_min_max_length(self): # there's just no satisfying some people... field = SimpleListField(forms.CharField(min_length=10, max_length=8)) with self.assertRaises(exceptions.ValidationError) as cm: field.clean('undefined') self.assertEqual( cm.exception.messages[0], 'Item 1 in the list did not validate: ' 'Ensure this value has at least 10 characters (it has 9).' ) self.assertEqual( cm.exception.messages[1], 'Item 1 in the list did not validate: ' 'Ensure this value has at most 8 characters (it has 9).' )
class UserCategoryForm(ModelForm): category_list = SimpleListField(forms.IntegerField(), required=True) class Meta: model = UserCategory fields = ('category_list', ) def clean(self): cleaned_data = super(UserCategoryForm, self).clean() category_list = cleaned_data.get("category_list") if category_list is None: return None for i in category_list: if not Category.objects.filter(id=i).exists(): raise forms.ValidationError(f"Category id {i} Not Exists") return cleaned_data
def test_prepare_value(self): field = SimpleListField(forms.CharField()) value = field.prepare_value(["a", "b", "c"]) assert value.split(",") == ["a", "b", "c"] assert field.prepare_value("1,a") == "1,a"
def test_valid(self): field = SimpleListField(forms.CharField()) value = field.clean("a,b,c") assert value == ["a", "b", "c"]
def test_valid(self): field = SimpleListField(forms.CharField()) value = field.clean('a,b,c') assert value == ['a', 'b', 'c']
def test_prepare_value(self): field = SimpleListField(forms.CharField()) value = field.prepare_value(['a', 'b', 'c']) assert value.split(',') == ['a', 'b', 'c'] assert field.prepare_value('1,a') == '1,a'
def test_required(self): field = SimpleListField(forms.CharField(), required=True) with pytest.raises(exceptions.ValidationError) as excinfo: field.clean('') assert excinfo.value.messages[0] == 'This field is required.'
def test_required(self): field = SimpleListField(forms.CharField(), required=True) with self.assertRaises(exceptions.ValidationError) as cm: field.clean('') self.assertEqual(cm.exception.messages[0], 'This field is required.')
def test_valid(self): field = SimpleListField(forms.CharField()) value = field.clean('a,b,c') self.assertEqual(value, ['a', 'b', 'c'])