public async Task GetContinentAsync_ContinentExistInCache_ReturnContinentFromCache() { #region Arrange var expectedContinent = new Continent { code = "AF", name = "Africa", countries = new List <Country> { new Country { code = "test", } } }; Mock <ICacheContinentRepository> cacheRepositoryMock = GenerateCacheRepositoryMock(continent: expectedContinent, isContinentInCache: true); Mock <IGraphQLRepository> graphQLMock = GenerateGraphQLMock(continent: expectedContinent); var continentRepository = new ContinentRepository(_logger, cacheRepositoryMock.Object, graphQLMock.Object); #endregion #region Act var result = await continentRepository.GetContinentByCodeAsync(expectedContinent.code); #endregion #region Assert cacheRepositoryMock.Verify(c => c.GetContinentByCodeAsync(It.Is <string>(s => s == expectedContinent.code))); cacheRepositoryMock.Verify(c => c.SaveContinentByCodeAsync(It.Is <string>(s => s == expectedContinent.code), It.IsAny <Continent>())); graphQLMock.VerifyNoOtherCalls(); result.Should().NotBeNull(); result.Should().BeEquivalentTo(expectedContinent); #endregion }
public async Task GetContinentAsync_ContinentNotExistInCache_GraphQLThrowException_LogErrorAndThrowException() { #region Arrange var expectedContinent = new Continent { code = "AF", name = "Africa", countries = new List <Country> { new Country { code = "test", } } }; Mock <ICacheContinentRepository> cacheRepositoryMock = GenerateCacheRepositoryMock(continent: expectedContinent, isContinentInCache: false); Mock <IGraphQLRepository> graphQLMock = new Mock <IGraphQLRepository>(); graphQLMock.Setup(g => g.GetContinentByCodeAsync(It.Is <string>(s => s == expectedContinent.code))) .ThrowsAsync(new Exception("test exception")); var continentRepository = new ContinentRepository(_logger, cacheRepositoryMock.Object, graphQLMock.Object); #endregion #region Act bool isExceptionThrow = false; Continent result = null; try { result = await continentRepository.GetContinentByCodeAsync(expectedContinent.code); } catch (Exception e) { isExceptionThrow = e.Message == ContinentRepository.GetDataFromGraphQLError; } #endregion #region Assert result.Should().BeNull(); isExceptionThrow.Should().BeTrue(); InMemorySink.Instance.Should().HaveMessage(ContinentRepository.GetDataFromGraphQLError) .WithLevel(Serilog.Events.LogEventLevel.Error); #endregion }
public async Task GetContinentAsync_CacheThrowException_LogErrorAndReturnContinentFromGraphQL() { #region Arrange var expectedContinent = new Continent { code = "AF", name = "Africa", countries = new List <Country> { new Country { code = "test", } } }; Mock <ICacheContinentRepository> cacheRepositoryMock = new Mock <ICacheContinentRepository>(); cacheRepositoryMock.Setup(c => c.GetContinentByCodeAsync(It.IsAny <string>())) .ThrowsAsync(new Exception("some exceptio")); cacheRepositoryMock.Setup(c => c.SaveContinentByCodeAsync(It.IsAny <string>(), It.IsAny <Continent>())) .ThrowsAsync(new Exception("some exceptio")); Mock <IGraphQLRepository> graphQLMock = GenerateGraphQLMock(continent: expectedContinent); var continentRepository = new ContinentRepository(_logger, cacheRepositoryMock.Object, graphQLMock.Object); #endregion #region Act var result = await continentRepository.GetContinentByCodeAsync(expectedContinent.code); #endregion #region Assert graphQLMock.Verify(g => g.GetContinentByCodeAsync(It.Is <string>(s => s == expectedContinent.code))); result.Should().NotBeNull(); result.Should().BeEquivalentTo(expectedContinent); InMemorySink.Instance.Should().HaveMessage(ContinentRepository.GetDataFromCacheError) .WithLevel(Serilog.Events.LogEventLevel.Error); InMemorySink.Instance.Should().HaveMessage(ContinentRepository.SaveDataInCacheError) .WithLevel(Serilog.Events.LogEventLevel.Error); #endregion }