예제 #1
0
def test_should_return_the_first_error_if_many_validation_fails(
    mock_validate: MagicMock,
    sut: ValidationComposite,
):
    mock_validate.side_effect = [
        Exception("first error"),
        Exception("second error"),
        Exception("third error"),
    ]
    input = dict(field="field")

    with pytest.raises(Exception) as excinfo:
        sut.validate(input)

    assert excinfo.value.args[0] == "first error"
예제 #2
0
def make_signup_validation():
    signup_schema = Schema(
        dict(
            name=And(str, len, error="Invalid key: 'name'"),
            email=And(str, len, error="Invalid key: 'email'"),
            password=And(
                And(str, len, error="Invalid key: 'password'"),
                And(
                    lambda s: len(s) > 5,
                    error="Invalid key: 'password minimum 6 characters'",
                ),
            ),
            password_confirmation=And(
                And(str, len, error="Invalid key: 'password_confirmation'"),
            ),
        )
    )
    return ValidationComposite(
        [
            RequiredFieldsValidation(signup_schema, "signup"),
            CompareFieldsValidation("password", "password_confirmation"),
            EmailValidation(
                "email", EmailValidatorAdapter(check_mx=False, skip_smtp=True)
            ),
            UniqueFieldValidation("email", MongoDbAdapter(get_collection("accounts"))),
        ]
    )
예제 #3
0
def test_should_return_an_error_if_any_validation_fails(
    mock_validate: MagicMock,
    sut: ValidationComposite,
):
    mock_validate.return_value = Exception("field error")
    input = dict(field="field")
    is_error = sut.validate(input)
    assert is_error.args[0] == "field error"
예제 #4
0
def make_login_validation():
    login_schema = Schema(
        dict(
            email=And(str, len, error="Invalid key: 'email'"),
            password=And(str, len, error="Invalid key: 'password'"),
        ))
    return ValidationComposite([
        RequiredFieldsValidation(login_schema, "login"),
        EmailValidation(
            "email",
            EmailValidatorAdapter(check_mx=False, skip_smtp=True),
        ),
    ])
def make_add_survey_validation():
    add_survey_schema = Schema(
        dict(
            question=And(str, len, error="Invalid key: 'question'"),
            answers=And(
                And(list, len, error="Invalid key: 'answers'"),
                Schema(
                    [
                        {
                            Optional("image"):
                            And(str, len, error="Invalid key: 'image'"),
                            "answer":
                            And(str, len, error="Invalid key: 'answer'"),
                        },
                    ],
                    error="Missing key: 'answer'",
                ),
            ),
        ))
    return ValidationComposite([
        RequiredFieldsValidation(add_survey_schema, "add_survey"),
    ])
예제 #6
0
def test_should_not_return_if_all_validation_succeeds(
    sut: ValidationComposite, ):
    input = dict(field="field")
    is_error = sut.validate(input)
    assert not is_error
예제 #7
0
def sut():
    validation_stub = ValidationStub()
    sut = ValidationComposite(
        [validation_stub, validation_stub, validation_stub])
    yield sut