-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dfb6cb6
commit 939eede
Showing
21 changed files
with
437 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Codehard/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Codehard/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Mediat/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ard.Functional/Codehard.Functional.MediatR.Tests/Codehard.Functional.MediatR.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
|
||
<AssemblyName>Codehard.Functional.MediatR.Tests</AssemblyName> | ||
|
||
<RootNamespace>Codehard.Functional.MediatR.Tests</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Lamar" Version="12.0.2" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0"/> | ||
<PackageReference Include="Shouldly" Version="4.2.1" /> | ||
<PackageReference Include="xunit" Version="2.4.1"/> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Codehard.Functional\Codehard.Functional.Mediatr\Codehard.Functional.Mediatr.csproj" /> | ||
<ProjectReference Include="..\..\Codehard.Functional\Codehard.Functional\Codehard.Functional.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
81 changes: 81 additions & 0 deletions
81
src/Codehard.Functional/Codehard.Functional.MediatR.Tests/PublishTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System.Text; | ||
using Lamar; | ||
using MediatR; | ||
using Shouldly; | ||
|
||
namespace Codehard.Functional.MediatR.Tests; | ||
|
||
public class PublishTests | ||
{ | ||
public class Ping : INotification | ||
{ | ||
public string Message { get; init; } | ||
} | ||
|
||
[Fact] | ||
public async Task WhenPublishMessage_ShouldNotifidEachHandlersCorrectly() | ||
{ | ||
// Arrange | ||
var builder = new StringBuilder(); | ||
var writer = new StringWriter(builder); | ||
var container = BuildMediatr(); | ||
|
||
// Act | ||
var mediator = container.GetInstance<IMediator>(); | ||
|
||
await mediator.Publish(new Ping { Message = "Ping" }); | ||
|
||
// Assert | ||
var result = builder.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None); | ||
result.ShouldContain("Ping Pong"); | ||
result.ShouldContain("Ping Pung"); | ||
|
||
Container BuildMediatr() | ||
{ | ||
var container = new Container(cfg => | ||
{ | ||
cfg.Scan(scanner => | ||
{ | ||
scanner.AssemblyContainingType(typeof(PublishTests)); | ||
scanner.IncludeNamespaceContainingType<Ping>(); | ||
scanner.WithDefaultConventions(); | ||
scanner.AddAllTypesOf(typeof (INotificationHandler<>)); | ||
}); | ||
cfg.For<TextWriter>().Use(writer); | ||
cfg.For<IMediator>().Use<Mediator>(); | ||
}); | ||
|
||
return container; | ||
} | ||
} | ||
|
||
public class PongHandler : INotificationHandler<Ping> | ||
{ | ||
private readonly TextWriter _writer; | ||
|
||
public PongHandler(TextWriter writer) | ||
{ | ||
_writer = writer; | ||
} | ||
|
||
public Task Handle(Ping notification, CancellationToken cancellationToken) | ||
{ | ||
return _writer.WriteLineAsync(notification.Message + " Pong"); | ||
} | ||
} | ||
|
||
public class PungHandler : INotificationHandler<Ping> | ||
{ | ||
private readonly TextWriter _writer; | ||
|
||
public PungHandler(TextWriter writer) | ||
{ | ||
_writer = writer; | ||
} | ||
|
||
public Task Handle(Ping notification, CancellationToken cancellationToken) | ||
{ | ||
return _writer.WriteLineAsync(notification.Message + " Pung"); | ||
} | ||
} | ||
} |
145 changes: 145 additions & 0 deletions
145
src/Codehard.Functional/Codehard.Functional.MediatR.Tests/QueryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
using System.Text; | ||
using Lamar; | ||
using LanguageExt; | ||
using MediatR; | ||
using Shouldly; | ||
|
||
namespace Codehard.Functional.MediatR.Tests; | ||
|
||
public class QueryTests | ||
{ | ||
public class Ping : IQuery<PongQueryResult> | ||
{ | ||
public string? Message { get; set; } | ||
} | ||
|
||
public class PingNotFound : IQuery<PongQueryResult> | ||
{ | ||
public string? Message { get; set; } | ||
} | ||
|
||
public class Pong | ||
{ | ||
public string? Message { get; set; } | ||
} | ||
|
||
public class PingHandler | ||
: IQueryHandler<Ping, PongQueryResult>, | ||
IQueryHandler<PingNotFound, PongQueryResult> | ||
{ | ||
public Task<Fin<PongQueryResult>> Handle(Ping request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult( | ||
Fin<PongQueryResult>.Succ( | ||
new PongQueryResult.Success(new Pong { Message = request.Message + " Pong" }))); | ||
} | ||
|
||
public Task<Fin<PongQueryResult>> Handle(PingNotFound request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult( | ||
Fin<PongQueryResult>.Fail( | ||
new ExpectedResultError(new PongQueryResult.NotFound()))); | ||
} | ||
} | ||
|
||
public abstract record PongQueryResult | ||
{ | ||
private PongQueryResult() | ||
{ | ||
} | ||
|
||
public sealed record Success(Pong Pong) : PongQueryResult; | ||
|
||
public sealed record NotFound : PongQueryResult; | ||
} | ||
|
||
[Fact] | ||
public async Task WhenSendQuery_ShouldResponseCorrectly() | ||
{ | ||
// Arrange | ||
var builder = new StringBuilder(); | ||
var writer = new StringWriter(builder); | ||
var container = BuildMediatr(); | ||
|
||
// Act | ||
var mediator = container.GetInstance<IMediator>(); | ||
|
||
var response = | ||
await mediator | ||
.SendQueryAff<Ping, PongQueryResult>(new Ping { Message = "Ping" }) | ||
.MapExpectedResultError() | ||
.Run(); | ||
|
||
// Assert | ||
Assert.True(response.IsSucc); | ||
|
||
var result = response.ThrowIfFail(); | ||
|
||
var successValue = result.ShouldBeOfType<PongQueryResult.Success>(); | ||
|
||
successValue.Pong.Message.ShouldBe("Ping Pong"); | ||
|
||
return; | ||
|
||
Container BuildMediatr() | ||
{ | ||
var container = new Container(cfg => | ||
{ | ||
cfg.Scan(scanner => | ||
{ | ||
scanner.AssemblyContainingType(typeof(QueryTests)); | ||
scanner.IncludeNamespaceContainingType<Ping>(); | ||
scanner.WithDefaultConventions(); | ||
scanner.AddAllTypesOf(typeof(IRequestHandler<,>)); | ||
}); | ||
cfg.For<IMediator>().Use<Mediator>(); | ||
}); | ||
|
||
return container; | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task WhenSendNotFoundQuery_ShouldResponseNotFoundCorrectly() | ||
{ | ||
// Arrange | ||
var builder = new StringBuilder(); | ||
var writer = new StringWriter(builder); | ||
var container = BuildMediatr(); | ||
|
||
// Act | ||
var mediator = container.GetInstance<IMediator>(); | ||
|
||
var response = | ||
await mediator | ||
.SendQueryAff<PingNotFound, PongQueryResult>(new PingNotFound { Message = "Ping" }) | ||
.MapExpectedResultError() | ||
.Run(); | ||
|
||
// Assert | ||
Assert.True(response.IsSucc); | ||
|
||
var result = response.ThrowIfFail(); | ||
|
||
result.ShouldBeOfType<PongQueryResult.NotFound>(); | ||
|
||
return; | ||
|
||
Container BuildMediatr() | ||
{ | ||
var container = new Container(cfg => | ||
{ | ||
cfg.Scan(scanner => | ||
{ | ||
scanner.AssemblyContainingType(typeof(QueryTests)); | ||
scanner.IncludeNamespaceContainingType<Ping>(); | ||
scanner.WithDefaultConventions(); | ||
scanner.AddAllTypesOf(typeof(IRequestHandler<,>)); | ||
}); | ||
cfg.For<IMediator>().Use<Mediator>(); | ||
}); | ||
|
||
return container; | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/Codehard.Functional/Codehard.Functional.MediatR.Tests/Usings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |
16 changes: 16 additions & 0 deletions
16
src/Codehard.Functional/Codehard.Functional.Mediatr/Codehard.Functional.Mediatr.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<AssemblyName>Codehard.Functional.MediatR</AssemblyName> | ||
<RootNamespace>Codehard.Functional.MediatR</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="LanguageExt.Core" Version="4.4.7" /> | ||
<PackageReference Include="MediatR" Version="12.1.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.