-
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.
Merge pull request #57 from romankr/romankr/upcoming-events-function
- Loading branch information
Showing
10 changed files
with
155 additions
and
48 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
59 changes: 30 additions & 29 deletions
59
OddsCollector.Functions.Tests/Tests/Functions/UpcomingEventsFunction.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 |
---|---|---|
@@ -1,82 +1,83 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Testing; | ||
using NSubstitute.ExceptionExtensions; | ||
using OddsCollector.Functions.Models; | ||
using OddsCollector.Functions.OddsApi; | ||
using LoggerFactory = OddsCollector.Functions.Tests.Infrastructure.Logger.LoggerFactory; | ||
using OddsCollector.Functions.Processors; | ||
|
||
namespace OddsCollector.Functions.Tests.Tests.Functions; | ||
|
||
internal class UpcomingEventsFunction | ||
{ | ||
[Test] | ||
[SuppressMessage("Usage", "CA2254:Template should be a static expression")] | ||
public async Task Run_WithValidParameters_ReturnsEventResults() | ||
public async Task Run_WithValidMessages_ReturnsEventResultList() | ||
{ | ||
// Arrange | ||
IEnumerable<UpcomingEvent> expectedEventResults = new List<UpcomingEvent> { new() }; | ||
IEnumerable<UpcomingEvent> expectedEventResults = [new()]; | ||
|
||
var loggerMock = LoggerFactory.GetLoggerMock<OddsCollector.Functions.Functions.UpcomingEventsFunction>(); | ||
var loggerMock = new FakeLogger<OddsCollector.Functions.Functions.UpcomingEventsFunction>(); | ||
|
||
var clientStub = Substitute.For<IOddsApiClient>(); | ||
clientStub.GetUpcomingEventsAsync(Arg.Any<Guid>(), Arg.Any<DateTime>(), Arg.Any<CancellationToken>()) | ||
.Returns(Task.FromResult(expectedEventResults)); | ||
var processorStub = Substitute.For<IUpcomingEventsProcessor>(); | ||
|
||
var function = new OddsCollector.Functions.Functions.UpcomingEventsFunction(loggerMock, clientStub); | ||
processorStub.GetUpcomingEventsAsync(Arg.Any<CancellationToken>()).Returns(Task.FromResult(expectedEventResults)); | ||
|
||
var function = new OddsCollector.Functions.Functions.UpcomingEventsFunction(loggerMock, processorStub); | ||
|
||
// Act | ||
var eventResults = await function.Run(new CancellationToken()); | ||
|
||
// Assert | ||
eventResults.Should().NotBeNull().And.BeEquivalentTo(expectedEventResults); | ||
|
||
loggerMock.ReceivedWithAnyArgs().LogInformation(string.Empty, 1); | ||
loggerMock.LatestRecord.Level.Should().Be(LogLevel.Information); | ||
loggerMock.LatestRecord.Message.Should().Be("1 event(s) received"); | ||
} | ||
|
||
[Test] | ||
public async Task Run_WithException_ReturnsEmptyEventResults() | ||
public async Task Run_WithException_ReturnsEmptyEventResultList() | ||
{ | ||
// Arrange | ||
var exception = new Exception(); | ||
|
||
var loggerMock = LoggerFactory.GetLoggerMock<OddsCollector.Functions.Functions.UpcomingEventsFunction>(); | ||
var loggerMock = new FakeLogger<OddsCollector.Functions.Functions.UpcomingEventsFunction>(); | ||
|
||
var processorStub = Substitute.For<IUpcomingEventsProcessor>(); | ||
|
||
var clientStub = Substitute.For<IOddsApiClient>(); | ||
clientStub.GetUpcomingEventsAsync(Arg.Any<Guid>(), Arg.Any<DateTime>(), Arg.Any<CancellationToken>()) | ||
.Throws(exception); | ||
processorStub.GetUpcomingEventsAsync(Arg.Any<CancellationToken>()).Throws(exception); | ||
|
||
var function = new OddsCollector.Functions.Functions.UpcomingEventsFunction(loggerMock, clientStub); | ||
var function = new OddsCollector.Functions.Functions.UpcomingEventsFunction(loggerMock, processorStub); | ||
|
||
// Act | ||
var eventResults = await function.Run(new CancellationToken()); | ||
|
||
// Assert | ||
eventResults.Should().NotBeNull().And.BeEmpty(); | ||
|
||
loggerMock.Received().LogError(exception, "Failed to get upcoming events"); | ||
loggerMock.LatestRecord.Level.Should().Be(LogLevel.Error); | ||
loggerMock.LatestRecord.Message.Should().Be("Failed to get events"); | ||
loggerMock.LatestRecord.Exception.Should().Be(exception); | ||
} | ||
|
||
[Test] | ||
[SuppressMessage("Usage", "CA2254:Template should be a static expression")] | ||
public async Task Run_WithValidParameters_ReturnsNoEventResults() | ||
public async Task Run_WithEmptyMessages_ReturnsEmptyEventResultList() | ||
{ | ||
// Arrange | ||
IEnumerable<UpcomingEvent> expectedEventResults = new List<UpcomingEvent>(); | ||
IEnumerable<UpcomingEvent> expectedEventResults = []; | ||
|
||
var loggerMock = new FakeLogger<OddsCollector.Functions.Functions.UpcomingEventsFunction>(); | ||
|
||
var loggerMock = LoggerFactory.GetLoggerMock<OddsCollector.Functions.Functions.UpcomingEventsFunction>(); | ||
var processorStub = Substitute.For<IUpcomingEventsProcessor>(); | ||
|
||
var clientStub = Substitute.For<IOddsApiClient>(); | ||
clientStub.GetUpcomingEventsAsync(Arg.Any<Guid>(), Arg.Any<DateTime>(), Arg.Any<CancellationToken>()) | ||
.Returns(Task.FromResult(expectedEventResults)); | ||
processorStub.GetUpcomingEventsAsync(Arg.Any<CancellationToken>()).Returns(Task.FromResult(expectedEventResults)); | ||
|
||
var function = new OddsCollector.Functions.Functions.UpcomingEventsFunction(loggerMock, clientStub); | ||
var function = new OddsCollector.Functions.Functions.UpcomingEventsFunction(loggerMock, processorStub); | ||
|
||
// Act | ||
var eventResults = await function.Run(new CancellationToken()); | ||
|
||
// Assert | ||
eventResults.Should().NotBeNull().And.BeEquivalentTo(expectedEventResults); | ||
|
||
loggerMock.ReceivedWithAnyArgs().LogWarning(string.Empty, 1); | ||
loggerMock.LatestRecord.Level.Should().Be(LogLevel.Warning); | ||
loggerMock.LatestRecord.Message.Should().Be("No events received"); | ||
} | ||
} |
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
71 changes: 71 additions & 0 deletions
71
OddsCollector.Functions.Tests/Tests/Processors/UpcomingEventProcessor.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,71 @@ | ||
using OddsCollector.Functions.OddsApi; | ||
|
||
namespace OddsCollector.Functions.Tests.Tests.Processors; | ||
|
||
internal class UpcomingEventProcessor | ||
{ | ||
[Test] | ||
public async Task GetUpcomingEventsAsync_PassesTraceId() | ||
{ | ||
// Arrange | ||
var clientMock = Substitute.For<IOddsApiClient>(); | ||
|
||
var processor = new OddsCollector.Functions.Processors.UpcomingEventsProcessor(clientMock); | ||
|
||
// Act | ||
await processor.GetUpcomingEventsAsync(new CancellationToken()); | ||
|
||
// Assert | ||
var calls = clientMock.ReceivedCalls().ToList(); | ||
calls.Should().NotBeNullOrEmpty().And.HaveCount(1); | ||
|
||
var arguments = calls[0].GetArguments(); | ||
arguments.Should().NotBeNullOrEmpty().And.HaveCount(3); | ||
|
||
arguments[0].Should().BeOfType<Guid>().And.NotBe(Guid.Empty); | ||
} | ||
|
||
[Test] | ||
public async Task GetUpcomingEventsAsync_PassesDateTime() | ||
{ | ||
// Arrange | ||
var clientMock = Substitute.For<IOddsApiClient>(); | ||
|
||
var processor = new OddsCollector.Functions.Processors.UpcomingEventsProcessor(clientMock); | ||
|
||
// Act | ||
await processor.GetUpcomingEventsAsync(new CancellationToken()); | ||
|
||
// Assert | ||
var calls = clientMock.ReceivedCalls().ToList(); | ||
calls.Should().NotBeNullOrEmpty().And.HaveCount(1); | ||
|
||
var arguments = calls[0].GetArguments(); | ||
arguments.Should().NotBeNullOrEmpty().And.HaveCount(3); | ||
|
||
arguments[1].Should().BeOfType<DateTime>().And.NotBe(DateTime.MinValue).And.NotBe(DateTime.MaxValue); | ||
} | ||
|
||
[Test] | ||
public async Task GetUpcomingEventsAsync_PassesCancellationToken() | ||
{ | ||
// Arrange | ||
var cancellationToken = new CancellationToken(); | ||
|
||
var clientMock = Substitute.For<IOddsApiClient>(); | ||
|
||
var processor = new OddsCollector.Functions.Processors.UpcomingEventsProcessor(clientMock); | ||
|
||
// Act | ||
await processor.GetUpcomingEventsAsync(cancellationToken); | ||
|
||
// Assert | ||
var calls = clientMock.ReceivedCalls().ToList(); | ||
calls.Should().NotBeNullOrEmpty().And.HaveCount(1); | ||
|
||
var arguments = calls[0].GetArguments(); | ||
arguments.Should().NotBeNullOrEmpty().And.HaveCount(3); | ||
|
||
arguments[2].Should().BeOfType<CancellationToken>().And.Be(cancellationToken); | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
OddsCollector.Functions/Processors/IUpcomingEventsProcessor.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,8 @@ | ||
using OddsCollector.Functions.Models; | ||
|
||
namespace OddsCollector.Functions.Processors; | ||
|
||
internal interface IUpcomingEventsProcessor | ||
{ | ||
Task<IEnumerable<UpcomingEvent>> GetUpcomingEventsAsync(CancellationToken cancellationToken); | ||
} |
12 changes: 12 additions & 0 deletions
12
OddsCollector.Functions/Processors/UpcomingEventsProcessor.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,12 @@ | ||
using OddsCollector.Functions.Models; | ||
using OddsCollector.Functions.OddsApi; | ||
|
||
namespace OddsCollector.Functions.Processors; | ||
|
||
internal class UpcomingEventsProcessor(IOddsApiClient client) : IUpcomingEventsProcessor | ||
{ | ||
public async Task<IEnumerable<UpcomingEvent>> GetUpcomingEventsAsync(CancellationToken cancellationToken) | ||
{ | ||
return await client.GetUpcomingEventsAsync(Guid.NewGuid(), DateTime.UtcNow, cancellationToken); | ||
} | ||
} |