test: graphQL caching

This commit is contained in:
Mikhail Shabanov committed 2021-06-07 14:29:16 +02:00
1 parent 638489a842
commit 5d66c3641f
3 files changed
+79 -33

No files matched your search

@@ -1,8 +1,11 @@
using System.IO; using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using FluentAssertions; using FluentAssertions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Moq; using Moq;
using Moq.Protected;
using Newtonsoft.Json; using Newtonsoft.Json;
using NUnit.Framework; using NUnit.Framework;
using WotBlitzStatisticsPro.Common.Model; using WotBlitzStatisticsPro.Common.Model;
@@ -56,20 +59,32 @@ namespace WotBlitzStatisticsPro.Tests.OperationStepsTests
await File.ReadAllTextAsync(GetFixturePath("MappedAccountHistory.json")); await File.ReadAllTextAsync(GetFixturePath("MappedAccountHistory.json"));
accountInfoHistorySerialized.Should().Be(expectedAccountInfoHistory); accountInfoHistorySerialized.Should().Be(expectedAccountInfoHistory);
// ToDo: Check cache var accountCache = _cache.GetAccountData(AccountId);
Assert.Fail("Implement test"); accountCache.Should().NotBeNull();
} accountCache?.AccountInfo.Should().NotBeNull();
accountCache?.AccountInfoHistory.Should().NotBeNull();
[Test]
public async Task ShouldGetAndSaveAccountInfoInformationIfCacheExpired()
{
Assert.Fail("Implement test");
} }
[Test] [Test]
public async Task ShouldReadDataFromCacheIfItIsNotEmpty() public async Task ShouldReadDataFromCacheIfItIsNotEmpty()
{ {
Assert.Fail("Implement test"); var accountInfo = GetAccountInfoFromFixture();
var accountInfoHistory = GetAccountInfoHistoryFromFixture();
_cache.SetAccountData(AccountId, new AccountDataCache(accountInfo, accountInfoHistory));
var context = new OperationContext(new AccountRequest(AccountId, Realm, Language));
context.AddOrReplace(_contextData);
await _operation.Invoke(context, null);
_contextData.AccountInfo.Should().NotBeNull();
_contextData.AccountInfoHistory.Should().NotBeNull();
HttpHandlerMock
.Protected()
.Verify<Task<HttpResponseMessage>>("SendAsync", Times.Never(),
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>());
} }
} }
} }
@@ -1,8 +1,11 @@
using System.IO; using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using FluentAssertions; using FluentAssertions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Moq; using Moq;
using Moq.Protected;
using Newtonsoft.Json; using Newtonsoft.Json;
using NUnit.Framework; using NUnit.Framework;
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline; using WotBlitzStatisticsPro.Logic.AccountInformationPipeline;
@@ -57,20 +60,32 @@ namespace WotBlitzStatisticsPro.Tests.OperationStepsTests
await File.ReadAllTextAsync(GetFixturePath("MappedTanksHistory.json")); await File.ReadAllTextAsync(GetFixturePath("MappedTanksHistory.json"));
tanksHistorySerialized.Should().Be(expectedTanksHistory); tanksHistorySerialized.Should().Be(expectedTanksHistory);
// ToDo: Check cache var tanksCache = _cache.GetTanksData(AccountId);
Assert.Fail("Implement test"); tanksCache.Should().NotBeNull();
} tanksCache?.TankInfo.Should().NotBeNull();
tanksCache?.TankInfoHistory.Should().NotBeNull();
[Test]
public async Task ShouldGetAndSaveTankInfoInformationIfCacheExpired()
{
Assert.Fail("Implement test");
} }
[Test] [Test]
public async Task ShouldReadDataFromCacheIfItIsNotEmpty() public async Task ShouldReadDataFromCacheIfItIsNotEmpty()
{ {
Assert.Fail("Implement test"); var tanksInfo = GetTanksInfoRomFixture();
var tanksInfoHistory = GetTanksInfoHistoryFromFixture();
_cache.SetTanksData(AccountId, new TanksDataCache(tanksInfo, tanksInfoHistory));
var context = new OperationContext(new AccountRequest(AccountId, Realm, Language));
context.AddOrReplace(_contextData);
await _operation.Invoke(context, null);
_contextData.Tanks.Should().NotBeNull();
_contextData.TanksHistory.Should().NotBeNull();
HttpHandlerMock
.Protected()
.Verify<Task<HttpResponseMessage>>("SendAsync", Times.Never(),
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>());
} }
} }
@@ -38,6 +38,8 @@ namespace WotBlitzStatisticsPro.Tests.OperationStepsTests
protected Mock<IDictionariesDataAccessor> DictionariesDataAccessorMock; protected Mock<IDictionariesDataAccessor> DictionariesDataAccessorMock;
protected Mock<IWargamingAccountDataAccessor> WargamingDataAccessorMock; protected Mock<IWargamingAccountDataAccessor> WargamingDataAccessorMock;
protected Mock<HttpMessageHandler> HttpHandlerMock;
protected void InitAutoMapper() protected void InitAutoMapper()
{ {
var config = new MapperConfiguration(cfg => { var config = new MapperConfiguration(cfg => {
@@ -82,24 +84,26 @@ namespace WotBlitzStatisticsPro.Tests.OperationStepsTests
Content = new StringContent(File.ReadAllText(tanksInfoFilePath)), Content = new StringContent(File.ReadAllText(tanksInfoFilePath)),
}; };
var handlerMock = new Mock<HttpMessageHandler>(); HttpHandlerMock = new Mock<HttpMessageHandler>();
handlerMock HttpHandlerMock
.Protected() .Protected()
.Setup<Task<HttpResponseMessage>>( .Setup<Task<HttpResponseMessage>>(
"SendAsync", "SendAsync",
ItExpr.Is<HttpRequestMessage>(m => m.RequestUri.ToString() == playerInfoRequestUrl), ItExpr.Is<HttpRequestMessage>(m => m.RequestUri.ToString() == playerInfoRequestUrl),
ItExpr.IsAny<CancellationToken>()) ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(playerInfoResponse); .ReturnsAsync(playerInfoResponse)
handlerMock .Verifiable();
HttpHandlerMock
.Protected() .Protected()
.Setup<Task<HttpResponseMessage>>( .Setup<Task<HttpResponseMessage>>(
"SendAsync", "SendAsync",
ItExpr.Is<HttpRequestMessage>(m => m.RequestUri.ToString() == tanksInfoRequestUrl), ItExpr.Is<HttpRequestMessage>(m => m.RequestUri.ToString() == tanksInfoRequestUrl),
ItExpr.IsAny<CancellationToken>()) ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(tanksInfoResponse); .ReturnsAsync(tanksInfoResponse)
.Verifiable();
var client = new WargamingApiClient( var client = new WargamingApiClient(
new HttpClient(handlerMock.Object), new HttpClient(HttpHandlerMock.Object),
settingsMock.Object, settingsMock.Object,
(new Mock<ILogger<WagramingApiClientBase>>()).Object); (new Mock<ILogger<WagramingApiClientBase>>()).Object);
WargamingApiClient = client; WargamingApiClient = client;
@@ -125,17 +129,11 @@ namespace WotBlitzStatisticsPro.Tests.OperationStepsTests
protected void FillAccountAndTanks(AccountInformationPipelineContextData contextData) protected void FillAccountAndTanks(AccountInformationPipelineContextData contextData)
{ {
var mappedAccountInfoHistory = File.ReadAllText(GetFixturePath("MappedAccountHistory.json"));
var mappedTanks = File.ReadAllText(GetFixturePath("MappedTanks.json"));
var mappedTanksHistory = File.ReadAllText(GetFixturePath("MappedTanksHistory.json"));
contextData.AccountInfo = GetAccountInfoFromFixture(); contextData.AccountInfo = GetAccountInfoFromFixture();
contextData.AccountInfoHistory = contextData.AccountInfoHistory = GetAccountInfoHistoryFromFixture();
JsonConvert.DeserializeObject<AccountInfoHistory>(mappedAccountInfoHistory); contextData.Tanks = GetTanksInfoRomFixture();
contextData.Tanks = JsonConvert.DeserializeObject<List<TankInfo>>(mappedTanks); contextData.TanksHistory = GetTanksInfoHistoryFromFixture();
contextData.TanksHistory =
JsonConvert.DeserializeObject<Dictionary<long, TankInfoHistory>>(mappedTanksHistory);
} }
protected AccountInfo GetAccountInfoFromFixture() protected AccountInfo GetAccountInfoFromFixture()
@@ -144,6 +142,24 @@ namespace WotBlitzStatisticsPro.Tests.OperationStepsTests
return JsonConvert.DeserializeObject<AccountInfo>(mappedAccountInfo); return JsonConvert.DeserializeObject<AccountInfo>(mappedAccountInfo);
} }
protected AccountInfoHistory GetAccountInfoHistoryFromFixture()
{
var mappedAccountInfoHistory = File.ReadAllText(GetFixturePath("MappedAccountHistory.json"));
return JsonConvert.DeserializeObject<AccountInfoHistory>(mappedAccountInfoHistory);
}
protected List<TankInfo> GetTanksInfoRomFixture()
{
var mappedTanks = File.ReadAllText(GetFixturePath("MappedTanks.json"));
return JsonConvert.DeserializeObject<List<TankInfo>>(mappedTanks);
}
protected Dictionary<long, TankInfoHistory> GetTanksInfoHistoryFromFixture()
{
var mappedTanksHistory = File.ReadAllText(GetFixturePath("MappedTanksHistory.json"));
return JsonConvert.DeserializeObject<Dictionary<long, TankInfoHistory>>(mappedTanksHistory);
}
protected string GetFixturePath(string fixtureFileName) protected string GetFixturePath(string fixtureFileName)
{ {
return Path.Combine(TestContext.CurrentContext.TestDirectory, "Fixtures", fixtureFileName); return Path.Combine(TestContext.CurrentContext.TestDirectory, "Fixtures", fixtureFileName);