Exemple #1
0
        public void DoNotMatch(params string[] errorCodes)
        {
            var predicate = ErrorPredicate.ByCode(errorCodes);

            var isMatch = predicate.IsMatch(new Error("invalid", "test"));

            Assert.False(isMatch);
        }
        public void DoNotMatch(params Type[] errorTypes)
        {
            var predicate = ErrorPredicate.ByType(errorTypes);

            var isMatch = predicate.IsMatch(TestError.Default);

            Assert.False(isMatch);
        }
        public void DoNotMatch()
        {
            var predicate = ErrorPredicate.ByType <AnotherTestError>();

            var isMatch = predicate.IsMatch(TestError.Default);

            Assert.False(isMatch);
        }
        public void ByGenericType_1Args_Match()
        {
            var predicate = ErrorPredicate.ByType <AnotherTestError>();

            var isMatch = predicate.IsMatch(AnotherTestError.Default);

            Assert.True(isMatch);
        }
Exemple #5
0
        public void Match(params string[] errorCodes)
        {
            var predicate = ErrorPredicate.ByCode(errorCodes);

            var isMatch = predicate.IsMatch(new Error(errorCodes[0], "test"));

            Assert.True(isMatch);
        }
        public void Match(params Type[] errorTypes)
        {
            var predicate = ErrorPredicate.ByType(errorTypes);

            var isMatch = predicate.IsMatch((Error)Activator.CreateInstance(errorTypes[0]));

            Assert.True(isMatch);
        }
Exemple #7
0
        public void DoNotMatch_WithCondition(params string[] errorCodes)
        {
            var predicate = ErrorPredicate
                            .ByCode(errorCodes)
                            .WithCondition(_ => false);

            var isMatch = predicate.IsMatch(new Error(errorCodes[0], "test"));

            Assert.False(isMatch);
        }
        public void DoNotMatch_WithCondition(params Type[] errorTypes)
        {
            var predicate = ErrorPredicate
                            .ByType(errorTypes)
                            .WithCondition(_ => false);

            var isMatch = predicate.IsMatch((Error)Activator.CreateInstance(errorTypes[0]));

            Assert.False(isMatch);
        }
        public void DoNotMatch_WithCondition()
        {
            var predicate = ErrorPredicate
                            .ByType <AnotherTestError>()
                            .WithCondition(_ => false);

            var isMatch = predicate.IsMatch(AnotherTestError.Default);

            Assert.False(isMatch);
        }
        public static async Task <ApiResponse <T> > Fetch <T>(
            this ISqlHattemSessionFactory sessionFactory,
            Func <ISqlHattemSession, Task <ApiResponse <T> > > handler
            )
        {
            using var session = sessionFactory.Create();

            return(await handler(session)
                   .OnError(ErrorPredicate.Any(), _ => session.Rollback())
                   .OnSuccess(_ => session.Commit()));
        }
        public void ByType_Match()
        {
            var predicate = ErrorPredicate
                            .ByType(
                typeof(AnotherTestError),
                typeof(AnotherTestError2)
                );

            var isMatch = predicate.IsMatch(AnotherTestError.Default);

            Assert.True(isMatch);
        }
        public static async Task <ApiResponse <T> > Execute <T>(
            this ISqlHattemSessionFactory sessionFactory,
            Func <ISqlHattemSession, Task <ApiResponse <T> > > handler,
            IsolationLevel isolationLevel = IsolationLevel.ReadCommitted
            )
        {
            using var session = sessionFactory.Create(isolationLevel);

            return(await handler(session)
                   .OnError(ErrorPredicate.Any(), _ => session.Rollback())
                   .OnSuccess(_ => session.Commit()));
        }
        public async Task Sync_AsyncValueTask_CodeErrorPredicate()
        {
            const int ifErrorData = 3;

            var response = await ApiResponse
                           .Error <int>(AnotherTestError.Default)
                           .IfError(
                ErrorPredicate.ByCode(AnotherTestError.Default.Code),
                e => ApiResponse.Ok(ifErrorData).AsValueTask());

            Assert.True(response.IsOk);
            Assert.Equal(ifErrorData, response.Data);
        }
Exemple #14
0
        public async Task Sync_Async_TypeErrorPredicate()
        {
            const int ifErrorData = 3;

            var response = await ApiResponse
                           .Error <int>(AnotherTestError.Default)
                           .IfError(
                ErrorPredicate.ByType <AnotherTestError, AnotherTestError2>(),
                e => ApiResponse.Ok(ifErrorData).AsTask());

            Assert.True(response.IsOk);
            Assert.Equal(ifErrorData, response.Data);
        }
        public void Sync_Sync_ExactTypeErrorPredicate()
        {
            const int ifErrorData = 3;

            var response = ApiResponse
                           .Error <int>(AnotherTestError.Default)
                           .IfError(
                ErrorPredicate.ByType <AnotherTestError>(),
                e => ApiResponse.Ok(ifErrorData));

            Assert.True(response.IsOk);
            Assert.Equal(ifErrorData, response.Data);
        }
Exemple #16
0
        public void Sync_TypeErrorPredicate_HasErrors_ValidError_ReturnsOk()
        {
            const int expectedStatusCode = 203;

            var response = ApiResponse
                           .Error <int>(TestError.Default)
                           .WithStatusCode(expectedStatusCode)
                           .SuppressErrors(
                ErrorPredicate.ByType <TestError, AnotherTestError>());

            Assert.True(response.IsOk);
            Assert.Null(response.StatusCode);
        }
Exemple #17
0
        public async Task Async_ExactTypeErrorPredicate_HasErrors_ValidError_ReturnsOk()
        {
            const int expectedStatusCode = 203;

            var response = await ApiResponse
                           .Error <int>(TestError.Default)
                           .WithStatusCode(expectedStatusCode)
                           .AsTask()
                           .SuppressErrors(
                ErrorPredicate.ByType <TestError>());

            Assert.True(response.IsOk);
            Assert.Null(response.StatusCode);
        }
        public void Sync_Sync_CodeErrorPredicate_HasErrors_ValidError_ExecuteOnError()
        {
            var onErrorMock = new Mock <ISyncExecutionProvider <Error> >();

            var response = ApiResponse
                           .Error(TestError.Default)
                           .OnError(
                ErrorPredicate.ByCode(TestError.Default.Code),
                onErrorMock.Object.Execute);

            Assert.True(response.HasErrors);
            Assert.Equal(TestError.Default, response.Error, ErrorComparer.Default);

            onErrorMock.Verify(v => v.Execute(TestError.Default), Times.Once());
        }
        public async Task Sync_Async_ExactTypeErrorPredicate_HasErrors_ValidError_ExecuteOnError()
        {
            var onErrorMock = new Mock <IAsyncExecutionProvider <TestError> >();

            var response = await ApiResponse
                           .Error(TestError.Default)
                           .OnError(
                ErrorPredicate.ByType <TestError>(),
                onErrorMock.Object.Execute);

            Assert.True(response.HasErrors);
            Assert.Equal(TestError.Default, response.Error, ErrorComparer.Default);

            onErrorMock.Verify(v => v.Execute(TestError.Default), Times.Once());
        }
Exemple #20
0
        public async Task AsyncValueTask_AsyncValueTask_AnyErrorPredicate_HasErrors_ValidError_ExecuteOnError()
        {
            var onErrorMock = new Mock <IAsyncValueTaskExecutionProvider <Error> >();

            var response = await ApiResponse
                           .Error(TestError.Default)
                           .AsValueTask()
                           .OnError(
                ErrorPredicate.Any(),
                e => onErrorMock.Object.Execute(e));

            Assert.True(response.HasErrors);
            Assert.Equal(TestError.Default, response.Error, ErrorComparer.Default);

            onErrorMock.Verify(v => v.Execute(TestError.Default), Times.Once());
        }