def test_validate_required_missing():
    """
    When given a None value the ListOrItemField validate method should raise a ValidationError.
    """
    field = ListOrItemField()
    with pytest.raises(ValidationError):
        field.validate(None)
def test_invalid_item():
    """
    When given an invalid value the ListOrItemField validate method should raise a ValidationError.
    """
    field = ListOrItemField(CharField(max_length=5))
    with pytest.raises(ValidationError):
        field.validate('123456')
def test_list_value_invalid_items():
    """
    When given a list with an invalid value the ListOrItemField validate method should raise a
    ValidationError.
    """
    field = ListOrItemField(CharField(max_length=5))
    with pytest.raises(ValidationError) as e:
        field.validate(['12345', '123456'])
        assert [1] == e.messages[0].keys()
def test_validate_not_required_missing():
    """
    When given a null value and is not required, do not raise a ValidationError
    """
    field = ListOrItemField(required=False)

    try:
        field.validate(None)
    except ValidationError:
        assert False, "ValidationError was raised"