private SaleDataDto GetSaleData(int additionalSum = 0) { SaleDataDto saleData = new SaleDataDto() { SourceFileName = "AlNaz_18121980.json", FileDate = DateTime.UtcNow }; List <SaleDto> saleDtos = new List <SaleDto>(); for (int i = 0; i < 10; i++) { SaleDto saleDto = new SaleDto() { SaleDate = new DateTime(2018, i + 1, i + 1), CustomerName = $"Customer #{i + 1}", ProductName = $"Product #{i + 1}", TotalSum = (i + 10) * 10 }; SaleDto saleDto2 = new SaleDto() { SaleDate = new DateTime(2018, i + 1, i + 1), CustomerName = $"Customer #{i + 1}", ProductName = $"Product #{i + 1}", TotalSum = (i + 10) * 10 + additionalSum }; saleDtos.Add(saleDto); saleDtos.Add(saleDto2); } saleData.Sales = saleDtos; return(saleData); }
public void GetSalesFromGithub_ReturnsCorrectData_WithCorrectToken() { string url = "https://api.github.com/repos/NAlex2004/SalesData/contents/Manager_2/AlNaz_04032019.json"; SaleDataDto saleData = FileHandler.GetSalesFromGithubSync(url); Assert.AreEqual(5, saleData.Sales.Count); Assert.AreEqual(1348.7m, saleData.Sales.Sum(s => s.TotalSum)); }
public void SaleDbDataManager_AddSaleData_AddsWithNoDuplicates() { SaleDataDto saleDataDto = GetSaleData(); var groupedSales = saleDataDto.Sales .GroupBy(sale => new { sale.CustomerName, sale.ProductName, sale.SaleDate }, (key, sales) => new SaleDto() { CustomerName = key.CustomerName, ProductName = key.ProductName, SaleDate = key.SaleDate, TotalSum = sales.Sum(s => s.TotalSum) }); int customersCount = saleDataDto.Sales.Select(s => s.CustomerName).Distinct().Count(); int productsCount = saleDataDto.Sales.Select(s => s.ProductName).Distinct().Count(); int salesCount = groupedSales.Count(); using (ISalesUnitOfWork unitOfWork = DependencyContainer.Container.Resolve <ISalesUnitOfWork>()) using (ISalesDataManager manager = DependencyContainer.Container.Resolve <ISalesDataManager>()) { int initialCustomersCount = unitOfWork.Customers.Get().Count(); int initialProductsCount = unitOfWork.Products.Get().Count(); int initialSalesCount = unitOfWork.Sales.Get().Count(); var res = manager.AddOrUpdateSaleDataAsync(saleDataDto).GetAwaiter().GetResult(); int savedSalesCount = unitOfWork.Sales.Get().Count(); int savedCustomersCount = unitOfWork.Customers.Get().Count(); int savedProductsCount = unitOfWork.Products.Get().Count(); Assert.IsTrue(res.Succeeded); Assert.AreEqual(customersCount + initialCustomersCount, savedCustomersCount); Assert.AreEqual(productsCount + initialProductsCount, savedProductsCount); Assert.AreEqual(salesCount + initialSalesCount, savedSalesCount); // Second time same file saleDataDto = GetSaleData(1); groupedSales = saleDataDto.Sales .GroupBy(sale => new { sale.CustomerName, sale.ProductName, sale.SaleDate }, (key, sales) => new SaleDto() { CustomerName = key.CustomerName, ProductName = key.ProductName, SaleDate = key.SaleDate, TotalSum = sales.Sum(s => s.TotalSum) }); res = manager.AddOrUpdateSaleDataAsync(saleDataDto).GetAwaiter().GetResult(); savedSalesCount = unitOfWork.Sales.Get().Count(); savedCustomersCount = unitOfWork.Customers.Get().Count(); savedProductsCount = unitOfWork.Products.Get().Count(); Assert.IsTrue(res.Succeeded); Assert.AreEqual(customersCount + initialCustomersCount, savedCustomersCount); Assert.AreEqual(productsCount + initialProductsCount, savedProductsCount); Assert.AreEqual(salesCount + initialSalesCount, savedSalesCount); } }
public async Task <SaleDataObtainmentResult> GetSaleDataAsync() { ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; Uri uri = new Uri(fileEntry.Url); string fileName = Path.GetFileName(uri.LocalPath); SaleDataDto saleData = new SaleDataDto() { SourceFileName = fileName, FileDate = fileEntry.CommitDate }; SaleDataObtainmentResult result = new SaleDataObtainmentResult() { SaleData = saleData, Success = false }; Debug.WriteLine($"[GetSaleDataAsync]: {fileName}"); using (HttpClient httpClient = new HttpClient()) { try { HttpWebRequestElement webRequestElement = new HttpWebRequestElement(); webRequestElement.UseUnsafeHeaderParsing = true; httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "NAlex2004"); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", token); string responseBody = await httpClient.GetStringAsync(fileEntry.Url).ConfigureAwait(false); IList <SaleDto> salesFromFile = await GetSalesFromResponseJson(responseBody).ConfigureAwait(false); // If we'd like to take any correct data from file, skipping incorrect //salesFromFile = salesFromFile.Where(s => s != null && s.HasValidData()).Select(s => s).ToList(); // Let's assume, all data must be correct if (salesFromFile == null || salesFromFile.Count == 0 || salesFromFile.Any(s => s == null || !s.HasValidData())) { result.ErrorMessage = $"File '{fileName}' has no suitable data."; return(result); } Debug.WriteLine($"[GetSaleDataAsync]: CORRECT {fileName}"); saleData.Sales = salesFromFile; result.Success = true; return(result); } catch (Exception e) { Debug.WriteLine($"[GetSaleDataAsync]: ERROR: {e.Message}"); result.ErrorMessage = e.Message; } return(result); } }
public void GetSalesFromGithub_ResultIsEmpty_OnBadToken() { string url = "https://api.github.com/repos/NAlex2004/SalesData/contents/Manager_2/AlNaz_04032019.json"; var dataManager = DependencyContainer.Container.Resolve <ISalesDataManager>(); FileHandlerTestClass fileHandler = new FileHandlerTestClass(dataManager, "123"); SaleDataDto saleData = fileHandler.GetSalesFromGithubSync(url); Assert.AreEqual(0, saleData.Sales.Count); }
protected virtual async Task <SaleManagementResult> AddSaleDataAsync(SaleDataDto saleData) { SourceFile sourceFile = new SourceFile() { FileName = saleData.SourceFileName, FileDate = saleData.FileDate }; SaleManagementResult result = await AddSaleDetailsDataAsync(sourceFile, saleData.Sales).ConfigureAwait(false); result.FileName = saleData.SourceFileName; return(result); }
//public SaleDbDataManager() // : this(new SalesDbUnitOfWork(new SalesDbContext())) //{ //} public async Task <SaleManagementResult> AddOrUpdateSaleDataAsync(SaleDataDto saleData) { if (saleData == null || string.IsNullOrEmpty(saleData.SourceFileName)) { string message = saleData == null ? "saleData is null" : "SourceFileName is null or empty"; return(new SaleManagementResult() { Succeeded = false, ErrorMessage = message }); } // Group data saleData.Sales = saleData.Sales .GroupBy(sale => new { sale.CustomerName, sale.ProductName, sale.SaleDate }, (key, sales) => new SaleDto() { CustomerName = key.CustomerName, ProductName = key.ProductName, SaleDate = key.SaleDate, TotalSum = sales.Sum(s => s.TotalSum) }).ToList(); SourceFile sourceFile = await unitOfWork.SourceFiles.Get(file => file.FileName.Equals(saleData.SourceFileName)).FirstOrDefaultAsync().ConfigureAwait(false); SaleManagementResult addOrUpdateResult; if (sourceFile != null) { var deleted = unitOfWork.Sales.Delete(sale => sale.SourceFileId == sourceFile.Id); unitOfWork.SourceFiles.Delete(file => file.Id == sourceFile.Id); } addOrUpdateResult = await AddSaleDataAsync(saleData); return(addOrUpdateResult); }