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