protected async Task <AnalyzerResult> LoadAsync(string ticker, string outputSize) { ticker = ticker.ToUpperInvariant(); var stock = await _dbContext.Stocks.FirstOrDefaultAsync(s => s.Id == ticker); var initialStockLoad = false; if (stock == null) { stock = _dbContext.Stocks.Add(new Stock() { Id = ticker, MinDate = DateTime.MaxValue, MaxDate = DateTime.MinValue }); initialStockLoad = true; } if (stock.MaxDate == DateTime.Today.AddDays(-1) && !IsMarketClosed) { return(AnalyzerResult.Sucess()); } var test = 0; try { var quotes = await _loader.LoadAsync(ticker, outputSize); foreach (var quote in quotes.OrderBy(s => s.Date).Where(q => initialStockLoad || q.Date <stock.MinDate || q.Date.Date> stock.MaxDate.Date)) { if (quote.Date == DateTime.Today && !IsMarketClosed) { continue; } _dbContext.StockQuotes.Add(quote); test++; stock.MinDate = stock.MinDate > quote.Date ? quote.Date : stock.MinDate; stock.MaxDate = stock.MaxDate < quote.Date ? quote.Date : stock.MaxDate; } } catch (Exception e) { return(AnalyzerResult.Fail(e.Message)); } stock.LastLoadDate = DateTime.UtcNow; await _dbContext.SaveChangesAsync(); return(AnalyzerResult.Sucess()); }
public async Task StockRepository_GetStockWithChartVm_ByTake_Failed_FailedLoader() { var dataFixture = new StockDataFixture(); var processor = new StockProcessorTestFixture(); var repository = StockRepositoryFactory.Create(processor); repository.MockDbContext.Setup(s => s.StockQuotes).Returns(dataFixture.QuoteDataFixture.MockDataSet.Object); repository.MockDbContext.Setup(s => s.Stocks).Returns(dataFixture.MockDataSet.Object); processor.LoadCompactAsyncResult = AnalyzerResult.Fail("Failed to load."); var result = await repository.GetChartAsync("ticker", 30); Assert.IsNull(result); }