Skip to content

Commit d7d6467

Browse files
committed
Fix failed tests
1 parent bbe4c32 commit d7d6467

File tree

14 files changed

+111
-56
lines changed

14 files changed

+111
-56
lines changed

src/Codehard.Common/Codehard.Common/Exceptions/ConfigurationMissingException.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Runtime.Serialization;
2-
31
namespace Codehard.Common.Exceptions;
42

53
/// <summary>
@@ -16,9 +14,4 @@ public ConfigurationMissingException(string configurationName)
1614
: base($"Configuration '{configurationName}' is not set")
1715
{
1816
}
19-
20-
private ConfigurationMissingException(SerializationInfo info, StreamingContext context)
21-
: base(info, context)
22-
{
23-
}
2417
}

src/Codehard.Functional/Codehard.Functional.AspNetCore.Tests/AsyncEffectExtensionTests.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class AsyncEffectExtensionTests
1111
public async Task WhenMapFailToInternalServerError_ShouldHaveHttpResultErrorWithCorrespondingHttpStatusCode()
1212
{
1313
// Arrange
14-
var eff = liftEff(() => ValueTask.FromException<int>(new Exception("Something went wrong")));
14+
var eff = liftEff(async () => await ValueTask.FromException<int>(new Exception("Something went wrong")));
1515

1616
// Act
1717
var res =
@@ -23,6 +23,7 @@ await eff.MapFailToInternalServerError("Err001")
2323
var error = res.Match(
2424
Succ: _ => Error.New("?"),
2525
Fail: err => err);
26+
2627
Assert.IsType<HttpResultError>(error);
2728

2829
var httpResultErr = error as HttpResultError;
@@ -37,7 +38,7 @@ await eff.MapFailToInternalServerError("Err001")
3738
public async Task WhenMapFailToInternalServerError_ShouldHaveHttpResultErrorWithCorrespondingErrorMessage()
3839
{
3940
// Arrange
40-
var eff = liftEff(() => ValueTask.FromException<int>(new Exception("Something not right")));
41+
var eff = FailEff<int>(Error.New("Something went wrong"));
4142

4243
// Act
4344
var res =
@@ -49,6 +50,7 @@ await eff.MapFailToInternalServerError(message: "Error Message")
4950
var error = res.Match(
5051
Succ: _ => Error.New("?"),
5152
Fail: err => err);
53+
5254
Assert.IsType<HttpResultError>(error);
5355

5456
var httpResultErr = error as HttpResultError;
@@ -61,7 +63,7 @@ record SomeData(int Number, string Text);
6163
public async Task WhenMapFailToInternalServerError_ShouldHaveHttpResultErrorWithCorrespondingObject()
6264
{
6365
// Arrange
64-
var aff = liftEff(() => ValueTask.FromException<int>(new Exception("Something went crazy")));
66+
var aff = liftEff(async () => await ValueTask.FromException<int>(new Exception("Something went crazy")));
6567

6668
// Act
6769
var res =
@@ -73,6 +75,7 @@ public async Task WhenMapFailToInternalServerError_ShouldHaveHttpResultErrorWith
7375
var error = res.Match(
7476
Succ: _ => Error.New("?"),
7577
Fail: err => err);
78+
7679
Assert.IsType<HttpResultError>(error);
7780

7881
var httpResultErr = error as HttpResultError;
@@ -85,7 +88,10 @@ public async Task WhenMapFailToInternalServerError_ShouldHaveHttpResultErrorWith
8588
public async Task WhenMapFailToInternalServerErrorWithMessageFunc_ShouldHaveHttpResultErrorWithCorrespondingMessage()
8689
{
8790
// Arrange
88-
var aff = liftEff(() => ValueTask.FromException<int>(new Exception("Something went crazy")));
91+
var aff =
92+
liftEff(
93+
async () =>
94+
await ValueTask.FromException<int>(new Exception("Something went crazy")));
8995

9096
// Act
9197
var res =
@@ -97,6 +103,7 @@ await aff.MapFailToInternalServerError(err => $"The error message is {err.Messag
97103
var error = res.Match(
98104
Succ: _ => Error.New("?"),
99105
Fail: err => err);
106+
100107
Assert.IsType<HttpResultError>(error);
101108

102109
var httpResultErr = error as HttpResultError;
@@ -108,7 +115,9 @@ public async Task WhenMapFailToOkWithAlreadyMapFailInternalServerError_ShouldOve
108115
{
109116
// Arrange
110117
var aff =
111-
liftEff(() => ValueTask.FromException<int>(new Exception("Something not right")));
118+
liftEff(
119+
async () =>
120+
await ValueTask.FromException<int>(new Exception("Something not right")));
112121

113122
// Act
114123
var res =
@@ -121,6 +130,7 @@ await aff.MapFailToInternalServerError(message: "Error Message")
121130
var error = res.Match(
122131
Succ: _ => Error.New("?"),
123132
Fail: err => err);
133+
124134
Assert.IsType<HttpResultError>(error);
125135

126136
var httpResultErr = error as HttpResultError;
@@ -131,7 +141,10 @@ await aff.MapFailToInternalServerError(message: "Error Message")
131141
public async Task WhenMapFailToOkWithNotOverrideOptionOnAlreadyMapFailInternalServerError_ShouldNotOverrideInternalServerError()
132142
{
133143
// Arrange
134-
var aff = liftEff(() => ValueTask.FromException<int>(new Exception("Something not right")));
144+
var aff =
145+
liftEff(
146+
async () =>
147+
await ValueTask.FromException<int>(new Exception("Something not right")));
135148

136149
// Act
137150
var res =
@@ -144,6 +157,7 @@ await aff.MapFailToInternalServerError(message: "Error Message")
144157
var error = res.Match(
145158
Succ: _ => Error.New("?"),
146159
Fail: err => err);
160+
147161
Assert.IsType<HttpResultError>(error);
148162

149163
var httpResultErr = error as HttpResultError;

src/Codehard.Functional/Codehard.Functional.AspNetCore.Tests/ControllerExtensionTests.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Net;
22
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Http.Features;
34
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.AspNetCore.Mvc.Infrastructure;
46
using Moq;
57
using static LanguageExt.Prelude;
68

@@ -18,12 +20,20 @@ public async Task WhenRunToResultWithFail_ShouldLogAndReturnCorrespondingHttpHea
1820
{
1921
// Arrange
2022
var eff =
21-
liftEff(() => ValueTask.FromException<int>(new Exception("Something went wrong")));
23+
liftEff(async () => await ValueTask.FromException<int>(new Exception("Something went wrong")));
2224

2325
var actionContextMock = new Mock<ActionContext>();
2426
var httpContextMock = new Mock<HttpContext>();
2527
var httpResponseMock = new Mock<HttpResponse>();
26-
var headerDictionary = new HeaderDictionary();
28+
var httpRequestMock = new Mock<HttpRequest>();
29+
var serviceProviderMock = new Mock<IServiceProvider>();
30+
var actionResultExecutorMock = new Mock<IActionResultExecutor<ObjectResult>>();
31+
32+
var headerDictionary = new HeaderDictionary();
33+
34+
httpContextMock
35+
.SetupGet(hc => hc.RequestServices)
36+
.Returns(() => serviceProviderMock.Object);
2737

2838
httpResponseMock
2939
.SetupGet(hr => hr.Headers)
@@ -32,18 +42,27 @@ public async Task WhenRunToResultWithFail_ShouldLogAndReturnCorrespondingHttpHea
3242
httpContextMock
3343
.SetupGet(hc => hc.Response)
3444
.Returns(() => httpResponseMock.Object);
35-
45+
46+
httpContextMock
47+
.SetupGet(hc => hc.Request)
48+
.Returns(() => httpRequestMock.Object);
49+
3650
actionContextMock.Object.HttpContext = httpContextMock.Object;
3751

52+
serviceProviderMock
53+
.Setup(sp => sp.GetService(typeof(IActionResultExecutor<ObjectResult>)))
54+
.Returns(actionResultExecutorMock.Object);
55+
3856
// Act
3957
var actionResult =
40-
await eff.MapFailToHttpResultError(expectedStatusCode, expectedData)
58+
await eff
59+
.MapFailToHttpResultError(expectedStatusCode, expectedData)
4160
.RunToResultAsync();
4261

4362
await actionResult.ExecuteResultAsync(actionContextMock.Object);
44-
45-
// Assert
63+
4664
httpResponseMock.VerifySet(hr => hr.StatusCode = (int)expectedStatusCode, Times.Once());
65+
4766
Assert.IsType<ErrorWrapperActionResult>(actionResult);
4867
Assert.Equal(expectedData, headerDictionary["x-error-code"]);
4968
}

src/Codehard.Functional/Codehard.Functional.AspNetCore/Commons.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ internal static Eff<A> MapFailToHttpResultError<A>(
1717
bool @override = true)
1818
=> ma.MapFail(err =>
1919
{
20-
var message = messageOpt.IfNone(string.Empty);
20+
var message = messageOpt.IfNone(code.ToString);
2121

2222
return
2323
err switch
2424
{
2525
HttpResultError hre when !@override => hre,
26+
//_ => new Expected(message, (int)code, HttpResultError.New(code, message, errorCode, data, err))
2627
_ =>
2728
HttpResultError.New(
2829
code, message, errorCode, data, err),

src/Codehard.Functional/Codehard.Functional.AspNetCore/HttpResultError.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -78,41 +78,34 @@ public void GetObjectData(SerializationInfo info, StreamingContext context)
7878
info.AddValue(nameof(this.Inner), this.Inner);
7979
}
8080

81-
/// <inheritdoc/>
82-
public override bool IsType<E>()
83-
{
84-
return this.Exception
85-
.Match(
86-
Some: ex => ex is E,
87-
None: () => false);
88-
}
89-
9081
/// <inheritdoc/>
9182
public override ErrorException ToErrorException() =>
92-
new ExceptionalException(this.Message, this.Code);
83+
IsExceptional
84+
? new WrappedErrorExceptionalException(this)
85+
: new WrappedErrorExpectedException(this);
9386

9487
/// <summary>
95-
/// Create an 'HttpResultError' error.
88+
/// Creates a new instance of <see cref="HttpResultError"/>.
9689
/// </summary>
97-
/// <param name="statusCode"></param>
98-
/// <param name="message"></param>
99-
/// <param name="errorCode"></param>
100-
/// <param name="data"></param>
101-
/// <param name="error"></param>
102-
/// <returns></returns>
90+
/// <param name="statusCode">The HTTP status code associated with the error.</param>
91+
/// <param name="message">The error message.</param>
92+
/// <param name="errorCode">An optional error code.</param>
93+
/// <param name="data">Optional additional data related to the error.</param>
94+
/// <param name="inner">An optional inner error.</param>
95+
/// <returns>A new instance of <see cref="HttpResultError"/>.</returns>
10396
public static HttpResultError New(
10497
HttpStatusCode statusCode,
10598
string message,
10699
Option<string> errorCode = default,
107100
Option<object> data = default,
108-
Option<Error> error = default)
101+
Option<Error> inner = default)
109102
{
110103
return
111104
new HttpResultError(
112105
statusCode,
113106
message,
114107
errorCode,
115108
data,
116-
error);
109+
inner);
117110
}
118-
}
111+
}

src/Codehard.Functional/Codehard.Functional.EntityFramework.Tests/Codehard.Functional.EntityFramework.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.8.0"/>
1414
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
1515
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
16-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.14" />
16+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" />
1717
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
1818
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4">
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

src/Codehard.Functional/Codehard.Functional.FSharp.Tests/ResultTypeTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void WhenConvertErrorResultToFin_ShouldContainFailValue()
3131
var fin = fSharpResult.ToFin(ResultType.mapError);
3232

3333
// Assert
34-
Assert.Throws<ExpectedException>(
34+
Assert.Throws<WrappedErrorExpectedException>(
3535
() => fin.ThrowIfFail());
3636
}
3737

@@ -60,7 +60,7 @@ public void WhenConvertErrorResultToEff_ShouldRunToFail()
6060
var result = eff.Run();
6161

6262
// Assert
63-
Assert.Throws<ExpectedException>(
63+
Assert.Throws<WrappedErrorExpectedException>(
6464
() => result.ThrowIfFail());
6565
}
6666

@@ -89,7 +89,7 @@ public async Task WhenConvertTaskOfErrorResultToAff_ShouldRunToFail()
8989
var result = await aff.RunAsync();
9090

9191
// Assert
92-
Assert.Throws<ExpectedException>(
92+
Assert.Throws<WrappedErrorExpectedException>(
9393
() => result.ThrowIfFail());
9494
}
9595

@@ -118,7 +118,7 @@ public void WhenWrapErrorResultInEff_ShouldRunToFail()
118118
var result = eff.Run();
119119

120120
// Assert
121-
Assert.Throws<ExpectedException>(
121+
Assert.Throws<WrappedErrorExpectedException>(
122122
() => result.ThrowIfFail());
123123
}
124124

src/Codehard.Functional/Codehard.Functional.Logger.Tests/LoggerExtensionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void WhenLogWithHttpResultErrorWithInnerError_ShouldLogAllError()
107107
var error = HttpResultError.New(
108108
HttpStatusCode.InternalServerError,
109109
"Outer error",
110-
error: innerError);
110+
inner: innerError);
111111

112112
var mockedLogger = new Mock<ILogger>();
113113

src/Codehard.Functional/Codehard.Functional.MediatR.Tests/PublishTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Ping : INotification
1414
}
1515

1616
[Fact]
17-
public async Task WhenPublishMessage_ShouldNotifidEachHandlersCorrectly()
17+
public async Task WhenPublishMessage_ShouldNotifiedEachHandlersCorrectly()
1818
{
1919
// Arrange
2020
var builder = new StringBuilder();
@@ -24,8 +24,8 @@ public async Task WhenPublishMessage_ShouldNotifidEachHandlersCorrectly()
2424
// Act
2525
var mediator = container.GetInstance<IMediator>();
2626

27-
await mediator.PublishAff(new Ping { Message = "Ping" })
28-
.RunAsync();
27+
_ = await mediator.PublishEff(new Ping { Message = "Ping" })
28+
.RunAsync();
2929

3030
// Assert
3131
var result = builder.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);

src/Codehard.Functional/Codehard.Functional.MediatR/PublisherExtensions.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55
// ReSharper disable once CheckNamespace
66
namespace MediatR;
77

8+
/// <summary>
9+
/// Provides extension methods for the <see cref="IPublisher"/> interface.
10+
/// </summary>
811
public static class PublisherExtensions
912
{
1013
/// <summary>
11-
/// Publishes a notification
14+
/// Publishes a notification asynchronously within an Eff monad.
1215
/// </summary>
13-
public static Eff<LanguageExt.Unit> PublishAff<TNotification>(
16+
/// <typeparam name="TNotification">The type of the notification.</typeparam>
17+
/// <param name="publisher">The publisher to use for publishing the notification.</param>
18+
/// <param name="notification">The notification to publish.</param>
19+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
20+
/// <returns>An <see cref="Eff{Unit}"/> representing the asynchronous operation.</returns>
21+
public static Eff<LanguageExt.Unit> PublishEff<TNotification>(
1422
this IPublisher publisher,
1523
TNotification notification,
1624
CancellationToken cancellationToken = default)

0 commit comments

Comments
 (0)