Skip to content

Commit

Permalink
next batch of unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
romankr committed Nov 26, 2023
1 parent f7ed0e4 commit 53cded9
Show file tree
Hide file tree
Showing 36 changed files with 400 additions and 789 deletions.
Original file line number Diff line number Diff line change
@@ -1,36 +1,75 @@
using FluentAssertions;
using Microsoft.Extensions.Options;
using NSubstitute;
using OddsCollector.Common.KeyVault.Client;
using OddsCollector.Common.Models;
using OddsCollector.Common.OddsApi.Client;
using OddsCollector.Common.OddsApi.Configuration;
using OddsCollector.Common.OddsApi.Converter;
using OddsCollector.Common.OddsApi.WebApi;

namespace OddsCollector.Common.Tests.OddsApi.Client;

internal sealed class OddsClientTests
internal sealed class OddsApiClientTests
{
[Test]
public void Constructor_WithValidDependencies_ReturnsNewInstance()
{
var optionsStub = Substitute.For<IOptions<OddsApiClientOptions>>();
optionsStub.Value.Returns(new OddsApiClientOptions { Leagues = new HashSet<string> { } });
var webApiClientStub = Substitute.For<IClient>();
var keyVaultClientStub = Substitute.For<IKeyVaultClient>();
var converterStub = Substitute.For<IOddsApiObjectConverter>();

var result = new OddsClient(webApiClientStub, keyVaultClientStub, converterStub);
var result = new OddsApiClient(optionsStub, webApiClientStub, keyVaultClientStub, converterStub);

result.Should().NotBeNull();
}

[Test]
public void Constructor_WithNullOptions_ThrowsException()
{
var optionsStub = Substitute.For<IOptions<OddsApiClientOptions>>();
var webApiClientStub = Substitute.For<IClient>();
var keyVaultClientStub = Substitute.For<IKeyVaultClient>();
var converterStub = Substitute.For<IOddsApiObjectConverter>();

var action = () =>
{
_ = new OddsApiClient(null, webApiClientStub, keyVaultClientStub, converterStub);
};

action.Should().Throw<ArgumentNullException>().WithParameterName("options");
}

[Test]
public void Constructor_WithNullLeaguesInOptions_ThrowsException()
{
var optionsStub = Substitute.For<IOptions<OddsApiClientOptions>>();
optionsStub.Value.Returns(null as OddsApiClientOptions);
var webApiClientStub = Substitute.For<IClient>();
var keyVaultClientStub = Substitute.For<IKeyVaultClient>();
var converterStub = Substitute.For<IOddsApiObjectConverter>();

var action = () =>
{
_ = new OddsApiClient(optionsStub, webApiClientStub, keyVaultClientStub, converterStub);
};

action.Should().Throw<ArgumentNullException>().WithParameterName("options");
}

[Test]
public void Constructor_WithNullWebApiClient_ThrowsException()
{
var optionsStub = Substitute.For<IOptions<OddsApiClientOptions>>();
optionsStub.Value.Returns(new OddsApiClientOptions { Leagues = new HashSet<string> { } });
var keyVaultClientStub = Substitute.For<IKeyVaultClient>();
var converterStub = Substitute.For<IOddsApiObjectConverter>();

var action = () =>
{
_ = new OddsClient(null, keyVaultClientStub, converterStub);
_ = new OddsApiClient(optionsStub, null, keyVaultClientStub, converterStub);
};

action.Should().Throw<ArgumentNullException>().WithParameterName("webApiClient");
Expand All @@ -39,12 +78,14 @@ public void Constructor_WithNullWebApiClient_ThrowsException()
[Test]
public void Constructor_WithNullKeyVaultClient_ThrowsException()
{
var optionsStub = Substitute.For<IOptions<OddsApiClientOptions>>();
optionsStub.Value.Returns(new OddsApiClientOptions { Leagues = new HashSet<string> { } });
var webApiClientStub = Substitute.For<IClient>();
var converterStub = Substitute.For<IOddsApiObjectConverter>();

var action = () =>
{
_ = new OddsClient(webApiClientStub, null, converterStub);
_ = new OddsApiClient(optionsStub, webApiClientStub, null, converterStub);
};

action.Should().Throw<ArgumentNullException>().WithParameterName("keyVaultClient");
Expand All @@ -53,12 +94,14 @@ public void Constructor_WithNullKeyVaultClient_ThrowsException()
[Test]
public void Constructor_WithNullConverter_ThrowsException()
{
var optionsStub = Substitute.For<IOptions<OddsApiClientOptions>>();
optionsStub.Value.Returns(new OddsApiClientOptions { Leagues = new HashSet<string> { } });
var webApiClientStub = Substitute.For<IClient>();
var keyVaultClientStub = Substitute.For<IKeyVaultClient>();

var action = () =>
{
_ = new OddsClient(webApiClientStub, keyVaultClientStub, null);
_ = new OddsApiClient(optionsStub, webApiClientStub, keyVaultClientStub, null);
};

action.Should().Throw<ArgumentNullException>().WithParameterName("objectConverter");
Expand All @@ -83,11 +126,16 @@ public async Task GetUpcomingEventsAsync_WithLeagues_ReturnsUpcomingEvents()
var keyVaultClientStub = Substitute.For<IKeyVaultClient>();
keyVaultClientStub.GetOddsApiKey().Returns(Task.FromResult(secretValue));

var oddsClient = new OddsClient(webApiClientMock, keyVaultClientStub, converterMock);

const string league = nameof(league);
var optionsStub = Substitute.For<IOptions<OddsApiClientOptions>>();
optionsStub.Value.Returns(new OddsApiClientOptions { Leagues = new HashSet<string> { league } });

var oddsClient = new OddsApiClient(optionsStub, webApiClientMock, keyVaultClientStub, converterMock);

var results = await oddsClient.GetUpcomingEventsAsync(league);
var traceId = Guid.NewGuid();
var timestamp = DateTime.UtcNow;

var results = await oddsClient.GetUpcomingEventsAsync(traceId, timestamp);

results.Should().NotBeNull().And.Equal(upcomingEvents);

Expand All @@ -108,8 +156,8 @@ await webApiClientMock.Received()

firstReceivedArguments.Should().NotBeNull();
firstReceivedArguments[0].Should().Be(rawUpcomingEvents);
firstReceivedArguments[1].Should().NotBe(default(Guid));
firstReceivedArguments[2].Should().NotBe(default(DateTime));
firstReceivedArguments[1].Should().Be(traceId);
firstReceivedArguments[2].Should().Be(timestamp);
}

[Test]
Expand All @@ -125,15 +173,20 @@ public async Task GetEventResultsAsync_WithLeagues_ReturnsEventResults()
converterMock.ToEventResults(Arg.Any<ICollection<Anonymous3>?>(), Arg.Any<Guid>(), Arg.Any<DateTime>())
.Returns(eventResults);

const string league = nameof(league);
var optionsStub = Substitute.For<IOptions<OddsApiClientOptions>>();
optionsStub.Value.Returns(new OddsApiClientOptions { Leagues = new HashSet<string> { league } });

const string secretValue = nameof(secretValue);
var keyVaultClientStub = Substitute.For<IKeyVaultClient>();
keyVaultClientStub.GetOddsApiKey().Returns(Task.FromResult(secretValue));

var oddsClient = new OddsClient(webApiClientMock, keyVaultClientStub, converterMock);
var oddsClient = new OddsApiClient(optionsStub, webApiClientMock, keyVaultClientStub, converterMock);

const string league = nameof(league);
var traceId = Guid.NewGuid();
var timestamp = DateTime.UtcNow;

var results = await oddsClient.GetEventResultsAsync(league);
var results = await oddsClient.GetEventResultsAsync(traceId, timestamp);

results.Should().NotBeNull().And.Equal(eventResults);

Expand All @@ -151,87 +204,7 @@ public async Task GetEventResultsAsync_WithLeagues_ReturnsEventResults()

firstReceivedArguments.Should().NotBeNull();
firstReceivedArguments[0].Should().Be(rawEventResults);
firstReceivedArguments[1].Should().NotBe(default(Guid));
firstReceivedArguments[2].Should().NotBe(default(DateTime));
}

[Test]
public async Task GetUpcomingEventsAsync_WithMultipleLeagues_ReturnsUpcomingEvents()
{
ICollection<Anonymous2> rawUpcomingEvents = new HashSet<Anonymous2> { new() };
var webApiClientStub = Substitute.For<IClient>();
webApiClientStub
.OddsAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<Regions>(), Arg.Any<Markets>(),
Arg.Any<DateFormat>(), Arg.Any<OddsFormat>(), Arg.Any<string>(), Arg.Any<string>())
.Returns(Task.FromResult(rawUpcomingEvents));

var keyVaultClientStub = Substitute.For<IKeyVaultClient>();

var upcomingEvents = new List<UpcomingEvent> { new() };
var converterStub = Substitute.For<IOddsApiObjectConverter>();
converterStub.ToUpcomingEvents(Arg.Any<ICollection<Anonymous2>?>(), Arg.Any<Guid>(), Arg.Any<DateTime>())
.Returns(upcomingEvents);

var oddsClient = new OddsClient(webApiClientStub, keyVaultClientStub, converterStub);

var result = await oddsClient.GetUpcomingEventsAsync(new HashSet<string> { "league1", "league2" });

result.Should().NotBeNull().And.HaveCount(2);
}

[Test]
public async Task GetEventResultsAsync_WithMultipleLeagues_ReturnsEventResults()
{
ICollection<Anonymous3> rawEventResults = new HashSet<Anonymous3> { new() };
var webApiClientStub = Substitute.For<IClient>();
webApiClientStub.ScoresAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<int?>())
.Returns(Task.FromResult(rawEventResults));

var keyVaultClientStub = Substitute.For<IKeyVaultClient>();

var eventResults = new List<EventResult> { new() };
var converterStub = Substitute.For<IOddsApiObjectConverter>();
converterStub.ToEventResults(Arg.Any<ICollection<Anonymous3>?>(), Arg.Any<Guid>(), Arg.Any<DateTime>())
.Returns(eventResults);

var oddsClient = new OddsClient(webApiClientStub, keyVaultClientStub, converterStub);

var result = await oddsClient.GetEventResultsAsync(new HashSet<string> { "league1", "league2" });

result.Should().NotBeNull().And.HaveCount(2);
}

[Test]
public void GetUpcomingEventsAsync_WithNullLeagues_ThrowsException()
{
var webApiClient = Substitute.For<IClient>();
var keyVaultClient = Substitute.For<IKeyVaultClient>();
var converter = Substitute.For<IOddsApiObjectConverter>();

var oddsClient = new OddsClient(webApiClient, keyVaultClient, converter);

var action = () =>
{
_ = oddsClient.GetUpcomingEventsAsync(leagues: null).GetAwaiter().GetResult();
};

action.Should().Throw<ArgumentNullException>().WithParameterName("leagues");
}

[Test]
public void GetEventResultsAsync_WithNullLeagues_ThrowsException()
{
var webApiClient = Substitute.For<IClient>();
var keyVaultClient = Substitute.For<IKeyVaultClient>();
var converter = Substitute.For<IOddsApiObjectConverter>();

var oddsClient = new OddsClient(webApiClient, keyVaultClient, converter);

var action = () =>
{
_ = oddsClient.GetEventResultsAsync(leagues: null).GetAwaiter().GetResult();
};

action.Should().Throw<ArgumentNullException>().WithParameterName("leagues");
firstReceivedArguments[1].Should().Be(traceId);
firstReceivedArguments[2].Should().Be(timestamp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void SetLeagues_WithValidLeague_ReturnsValidInstance()
{
string league = nameof(league);

var options = new OddsApiOptions();
var options = new OddsApiClientOptions();
options.SetLeagues(league);

options.Leagues.Should().NotBeNull().And.HaveCount(1);
Expand All @@ -22,7 +22,7 @@ public void SetLeagues_WithValidLeagues_ReturnsValidInstance()
{
var leagues = "league1;league2";

var options = new OddsApiOptions();
var options = new OddsApiClientOptions();
options.SetLeagues(leagues);

options.Leagues.Should().NotBeNull().And.HaveCount(2);
Expand All @@ -35,7 +35,7 @@ public void SetLeagues_WithDuplicatedLeagues_ReturnsNewInstance()
{
var leagues = "league1;league1";

var options = new OddsApiOptions();
var options = new OddsApiClientOptions();
options.SetLeagues(leagues);

options.Leagues.Should().NotBeNull().And.HaveCount(1);
Expand All @@ -48,7 +48,7 @@ public void SetLeagues_WithNullOrEmptyLeagues_ThrowsException(string? leagues)
{
var action = () =>
{
var options = new OddsApiOptions();
var options = new OddsApiClientOptions();
options.SetLeagues(leagues);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,42 @@ public void ToUpcomingEvents_WithOddList_ReturnsConvertedEvents()

firstEvent.Should().NotBeNull();
firstEvent.AwayTeam.Should().NotBeNull().And.Be(TestAnonymous2Builder.DefaultAwayTeam);
firstEvent.CommenceTime.Should().NotBeNull().And.Be(TestAnonymous2Builder.DefaultCommenceTime);
firstEvent.CommenceTime.Should().Be(TestAnonymous2Builder.DefaultCommenceTime);
firstEvent.HomeTeam.Should().NotBeNull().And.Be(TestAnonymous2Builder.DefaultHomeTeam);
firstEvent.Id.Should().NotBeNull().And.Be(TestAnonymous2Builder.DefaultId);
firstEvent.Timestamp.Should().NotBeNull().And.Be(timestamp);
firstEvent.TraceId.Should().NotBeNull().And.Be(traceId);
firstEvent.Timestamp.Should().Be(timestamp);
firstEvent.TraceId.Should().Be(traceId);
firstEvent.Odds.Should().NotBeNull().And.HaveCount(2);
firstEvent.Odds!.ElementAt(0).Should().NotBeNull();
firstEvent.Odds!.ElementAt(0)!.Bookmaker.Should().NotBeNull().And.Be("betclic");
firstEvent.Odds!.ElementAt(0)!.Away.Should().NotBeNull().And.Be(4.08);
firstEvent.Odds!.ElementAt(0)!.Draw.Should().NotBeNull().And.Be(3.82);
firstEvent.Odds!.ElementAt(0)!.Home.Should().NotBeNull().And.Be(1.7);
firstEvent.Odds!.ElementAt(0)!.Away.Should().Be(4.08);
firstEvent.Odds!.ElementAt(0)!.Draw.Should().Be(3.82);
firstEvent.Odds!.ElementAt(0)!.Home.Should().Be(1.7);
firstEvent.Odds!.ElementAt(1).Should().NotBeNull();
firstEvent.Odds!.ElementAt(1)!.Bookmaker.Should().NotBeNull().And.Be("sport888");
firstEvent.Odds!.ElementAt(1)!.Away.Should().NotBeNull().And.Be(4.33);
firstEvent.Odds!.ElementAt(1)!.Draw.Should().NotBeNull().And.Be(4.33);
firstEvent.Odds!.ElementAt(1)!.Home.Should().NotBeNull().And.Be(1.7);
firstEvent.Odds!.ElementAt(1)!.Away.Should().Be(4.33);
firstEvent.Odds!.ElementAt(1)!.Draw.Should().Be(4.33);
firstEvent.Odds!.ElementAt(1)!.Home.Should().Be(1.7);

var secondEvent = upcomingEvents.ElementAt(1);

secondEvent.Should().NotBeNull();
secondEvent.AwayTeam.Should().NotBeNull().And.Be(TestAnonymous2Builder.DefaultAwayTeam);
secondEvent.CommenceTime.Should().NotBeNull().And.Be(TestAnonymous2Builder.DefaultCommenceTime);
secondEvent.CommenceTime.Should().Be(TestAnonymous2Builder.DefaultCommenceTime);
secondEvent.HomeTeam.Should().NotBeNull().And.Be(TestAnonymous2Builder.DefaultHomeTeam);
secondEvent.Id.Should().NotBeNull().And.Be("1766194919f1cbfbd846576434f0499b");
secondEvent.Timestamp.Should().NotBeNull().And.Be(timestamp);
secondEvent.TraceId.Should().NotBeNull().And.Be(traceId);
secondEvent.Timestamp.Should().Be(timestamp);
secondEvent.TraceId.Should().Be(traceId);
secondEvent.Odds.Should().NotBeNull().And.HaveCount(2);
secondEvent.Odds!.ElementAt(0).Should().NotBeNull();
secondEvent.Odds!.ElementAt(0)!.Bookmaker.Should().NotBeNull().And.Be("betclic");
secondEvent.Odds!.ElementAt(0)!.Away.Should().NotBeNull().And.Be(4.08);
secondEvent.Odds!.ElementAt(0)!.Draw.Should().NotBeNull().And.Be(3.82);
secondEvent.Odds!.ElementAt(0)!.Home.Should().NotBeNull().And.Be(1.7);
secondEvent.Odds!.ElementAt(0)!.Away.Should().Be(4.08);
secondEvent.Odds!.ElementAt(0)!.Draw.Should().Be(3.82);
secondEvent.Odds!.ElementAt(0)!.Home.Should().Be(1.7);
secondEvent.Odds!.ElementAt(1)!.Bookmaker.Should().NotBeNull().And.Be("sport888");
secondEvent.Odds!.ElementAt(1)!.Away.Should().NotBeNull().And.Be(4.33);
secondEvent.Odds!.ElementAt(1)!.Draw.Should().NotBeNull().And.Be(4.33);
secondEvent.Odds!.ElementAt(1)!.Home.Should().NotBeNull().And.Be(1.7);
secondEvent.Odds!.ElementAt(1)!.Away.Should().Be(4.33);
secondEvent.Odds!.ElementAt(1)!.Draw.Should().Be(4.33);
secondEvent.Odds!.ElementAt(1)!.Home.Should().Be(1.7);
}

[Test]
Expand Down Expand Up @@ -528,28 +528,28 @@ public void ToEventResults_WithCompletedEvents_ReturnsConvertedEvents()
var firstResult = results.ElementAt(0);

firstResult.Should().NotBeNull();
firstResult.CommenceTime.Should().NotBeNull().And.Be(TestAnonymous3Builder.DefaultCommenceTime);
firstResult.CommenceTime.Should().Be(TestAnonymous3Builder.DefaultCommenceTime);
firstResult.Id.Should().NotBeNull().And.Be(TestAnonymous3Builder.DefaultId);
firstResult.Timestamp.Should().NotBeNull().And.Be(timestamp);
firstResult.TraceId.Should().NotBeNull().And.Be(traceId);
firstResult.Timestamp.Should().Be(timestamp);
firstResult.TraceId.Should().Be(traceId);
firstResult.Winner.Should().NotBeNull().And.Be("Manchester City");

var secondResult = results.ElementAt(1);

secondResult.Should().NotBeNull();
secondResult.CommenceTime.Should().NotBeNull().And.Be(TestAnonymous3Builder.DefaultCommenceTime);
secondResult.CommenceTime.Should().Be(TestAnonymous3Builder.DefaultCommenceTime);
secondResult.Id.Should().NotBeNull().And.Be(TestAnonymous3Builder.DefaultId);
secondResult.Timestamp.Should().NotBeNull().And.Be(timestamp);
secondResult.TraceId.Should().NotBeNull().And.Be(traceId);
secondResult.Timestamp.Should().Be(timestamp);
secondResult.TraceId.Should().Be(traceId);
secondResult.Winner.Should().NotBeNull().And.Be("Liverpool");

var thirdResult = results.ElementAt(2);

thirdResult.Should().NotBeNull();
thirdResult.CommenceTime.Should().NotBeNull().And.Be(TestAnonymous3Builder.DefaultCommenceTime);
thirdResult.CommenceTime.Should().Be(TestAnonymous3Builder.DefaultCommenceTime);
thirdResult.Id.Should().NotBeNull().And.Be(TestAnonymous3Builder.DefaultId);
thirdResult.Timestamp.Should().NotBeNull().And.Be(timestamp);
thirdResult.TraceId.Should().NotBeNull().And.Be(traceId);
thirdResult.Timestamp.Should().Be(timestamp);
thirdResult.TraceId.Should().Be(traceId);
thirdResult.Winner.Should().NotBeNull().And.Be(Constants.Draw);
}

Expand Down
Loading

0 comments on commit 53cded9

Please sign in to comment.