protected HttpClient GetClient() { var startupAssembly = typeof(Startup).GetTypeInfo().Assembly; contentRoot = GetProjectPath("src", startupAssembly); var builder = new WebHostBuilder() .UseContentRoot(contentRoot) .UseEnvironment("Testing") .UseStartup <StartupTesting>(); var server = new TestServer(builder); // Seed. using (var scope = server.Host.Services.CreateScope()) { var services = scope.ServiceProvider; var currencyRateContext = services.GetRequiredService <CurrencyRateContext>(); timeService = services.GetRequiredService <ITimeService>(); if (!currencyRateContext.CurrencyRates.Any()) { currencyRateContext.AddRange(RatesOfCurrency.GetTestRates(setIds: false, now: timeService.Now)); currencyRateContext.SaveChanges(); } //var loggerFactory = services.GetRequiredService<ILoggerFactory>(); //var catalogContext = services.GetRequiredService<CatalogContext>(); //CatalogContextSeed.SeedAsync(catalogContext, loggerFactory) // .Wait(); //var userManager = services.GetRequiredService<UserManager<ApplicationUser>>(); //AppIdentityDbContextSeed.SeedAsync(userManager).Wait(); } return(server.CreateClient()); }
public async void RepoEthTooOldAsync() { // Arrange var all = RatesOfCurrency.GetTestRates(setIds: false, now: timeService.Now); var ethRate = all[3]; ethRate.DateTime = timeService.Now.AddDays(-1); currencyRateContext.CurrencyRates.AddRange(all); await currencyRateContext.SaveChangesAsync(); // Act var averageRate = await currencyRateRepository.GetAverageRateBy24hAsync("eth"); // Assert Assert.Null(averageRate); }
public void RepoThreeBtcAverage() { // Arrange var all = RatesOfCurrency.GetTestRates(setIds: false, now: timeService.Now); var expectedAvg = (all[0].Rate + all[1].Rate + all[2].Rate) / 3m; currencyRateContext.CurrencyRates.AddRange(all); currencyRateContext.SaveChanges(); // Act var averageRage = currencyRateRepository.GetAverageRateBy24h("btc"); // Assert Assert.NotNull(averageRage); Assert.Equal(expectedAvg, averageRage.Value); }
public void RepoEthNotTooOld() { // Arrange var all = RatesOfCurrency.GetTestRates(setIds: false, now: timeService.Now); var ethRate = all[3]; ethRate.DateTime = timeService.Now.AddDays(-1).AddMilliseconds(1); currencyRateContext.CurrencyRates.AddRange(all); currencyRateContext.SaveChanges(); // Act var averageRage = currencyRateRepository.GetAverageRateBy24h("eth"); // Assert Assert.NotNull(averageRage); Assert.Equal(ethRate.Rate, averageRage.Value); }
public void RepoThreeBTCsNonCanonicalCase() { // Arrange var spec = new RateOfCurrencySpecification("btc"); currencyRateContext.CurrencyRates.AddRange(RatesOfCurrency.GetTestRates(setIds: false)); currencyRateContext.SaveChanges(); // Act var result = currencyRateRepository.List(spec); Assert.Collection(result, item => Assert.Equal(Currency.BTC, item.Currency), item => Assert.Equal(Currency.BTC, item.Currency), item => Assert.Equal(Currency.BTC, item.Currency) ); }
public async Task ReturnsAverageBtc() { // Arrange var expectedBtcs = RatesOfCurrency .GetTestRates(now: timeService.Now) .Where(c => c.Currency == Currency.BTC) .ToList(); var expectedAverage = (expectedBtcs[0].Rate + expectedBtcs[1].Rate + expectedBtcs[2].Rate) / 3m; var url = c_baseUri + "/btc/avg24h"; // Act var response = await httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); var stringResponse = await response.Content.ReadAsStringAsync(); var average = JsonConvert.DeserializeObject <decimal>(stringResponse); // Assert Assert.Equal(expectedAverage, average); }