using System; using System.Collections.Generic; using Xunit; using System.Linq; using LCU.Shared.Collections.RepoApiLib.Models; using LCU.Shared.Collections.RepoApiLib.Models.Producer; using LCU.Shared.Collections.RepoApiLib.Repositories; using LCU.Shared.Collections.RepoApiLib.Persistence; using Microsoft.EntityFrameworkCore; namespace LCU.API.Collections.RepoApiTests.Repository { public class ProcessingReceiptRepository : DbContextFromJson { private readonly AppDbContext _dbContext; public ProcessingReceiptRepository() { _dbContext = SqliteDbContext; } [Fact] public async void ProcessingReceipt_Basic_Flow_OK() { var repository = new Shared.Collections.RepoApiLib.Repositories.ProcessingReceiptRepository(_dbContext); // act var result = await repository.FindReceiptsAsync("123456-789"); Assert.NotNull(result); Assert.Empty(result); var receipt = new ProcessingReceipt("123456-789") { Message = "x", Outcome = RepoProcessingResult.Error, Processed = DateTime.Now, AccountId = "", CaseId = "", QueuedBy = "Testing" }; await repository.AddOrUpdateAsync(receipt); await _dbContext.SaveChangesAsync(); result = await repository.FindReceiptsAsync("123456-789"); // Assert Assert.NotNull(result); Assert.NotEmpty(result); Assert.Equal(RepoProcessingResult.Error, result.First().Outcome); } [Fact] public async void ProcessingReceipt_Multi_Flow_OK() { var repository = new Shared.Collections.RepoApiLib.Repositories.ProcessingReceiptRepository(_dbContext); // act var results = await repository.GetRetryListAsync(); Assert.NotNull(results); Assert.Empty(results); var list = new List() { new ProcessingReceipt("123456-777") { Message = "x", Outcome = RepoProcessingResult.Error, Processed = DateTime.Now, AccountId = "", CaseId = "", QueuedBy = "Testing" }, new ProcessingReceipt("123456-888") { Message = "y", Outcome = RepoProcessingResult.Created, Processed = DateTime.Now, AccountId = "", CaseId = "", QueuedBy = "Testing" } }; await repository.SaveAllAsync(list); await _dbContext.SaveChangesAsync(); results = await repository.GetRetryListAsync(); // Assert Assert.NotNull(results); Assert.NotEmpty(results); Assert.Equal(RepoProcessingResult.Error, results.First().Outcome); } [Fact] public async void ProcessingReceipt_Update_OK() { var repository = new Shared.Collections.RepoApiLib.Repositories.ProcessingReceiptRepository(_dbContext); // act var list = new List() { new ProcessingReceipt("123456-777") { Message = "x", Outcome = RepoProcessingResult.Error, Processed = DateTime.Now, AccountId = "321", CaseId = "", QueuedBy = "Testing" }, new ProcessingReceipt("123456-888") { Message = "y", Outcome = RepoProcessingResult.Created, Processed = DateTime.Now, AccountId = "", CaseId = "", QueuedBy = "Testing" } }; await repository.SaveAllAsync(list); await _dbContext.SaveChangesAsync(); // Fake the end of the transaction/etc with this... _dbContext.Entry(list.First()).State = EntityState.Detached; var changed = new ProcessingReceipt("123456-777") { Message = "x", Outcome = RepoProcessingResult.MissingRequirement, Processed = DateTime.Now, AccountId = "321", CaseId = "", QueuedBy = "Testing" }; await repository.AddOrUpdateAsync(changed); await _dbContext.SaveChangesAsync(); var results = await repository.GetRetryListAsync(); // Assert Assert.NotNull(results); Assert.NotEmpty(results); Assert.Equal(1, results.Count); Assert.Equal(RepoProcessingResult.MissingRequirement, results.First().Outcome); Assert.Equal("321", results.First().AccountId); } [Fact] public async void ProcessingReceipt_MultiRecord_OK() { var repository = new Shared.Collections.RepoApiLib.Repositories.ProcessingReceiptRepository(_dbContext); // act var list = new List() { new ProcessingReceipt("123456-777") { Message = "x1", Outcome = RepoProcessingResult.Error, Processed = DateTime.Now, AccountId = "321", CaseId = "", QueuedBy = "Testing" }, new ProcessingReceipt("123456-888") { Message = "y", Outcome = RepoProcessingResult.Created, Processed = DateTime.Now, AccountId = "", CaseId = "", QueuedBy = "Testing" } }; await repository.SaveAllAsync(list); await _dbContext.SaveChangesAsync(); // Fake the end of the transaction/etc with this... _dbContext.Entry(list.First()).State = EntityState.Detached; var changed = new ProcessingReceipt("123456-777") { Message = "x2", Outcome = RepoProcessingResult.MissingRequirement, Processed = DateTime.Now, AccountId = "321", CaseId = "123", QueuedBy = "Testing" }; await repository.AddOrUpdateAsync(changed); await _dbContext.SaveChangesAsync(); var results = await repository.GetRetryListAsync(); // Assert Assert.NotNull(results); Assert.NotEmpty(results); Assert.Equal(2, results.Count); Assert.Equal(RepoProcessingResult.Error, results.First().Outcome); Assert.Equal("321", results.First().AccountId); Assert.Equal("x1", results.First().Message); } [Fact] public async void GetReceiptByAccountNoAsync_OK() { var repository = new Shared.Collections.RepoApiLib.Repositories.ProcessingReceiptRepository(_dbContext); // act var list = new List() { new ProcessingReceipt("123456-777") { Message = "x1", Outcome = RepoProcessingResult.Error, Processed = DateTime.Now, AccountId = "321", CaseId = "", QueuedBy = "Testing" }, new ProcessingReceipt("123456-888") { Message = "y", Outcome = RepoProcessingResult.Created, Processed = DateTime.Now, AccountId = "", CaseId = "", QueuedBy = "Testing" }, new ProcessingReceipt("123456-888") { Message = "y", Outcome = RepoProcessingResult.Created, Processed = DateTime.Now, AccountId = "2", CaseId = "22", QueuedBy = "Testing" } }; await repository.SaveAllAsync(list); await _dbContext.SaveChangesAsync(); // Fake the end of the transaction/etc with this... _dbContext.Entry(list.First()).State = EntityState.Detached; //assert finds multiple var results = await repository.GetReceiptsByAccountNoAsync("123456-888"); Assert.NotNull(results); Assert.NotEmpty(results); Assert.Equal(2 , results.Count()); ; Assert.All(results, (item) => { Assert.Contains(item, results); }); //assert finds one results = await repository.GetReceiptsByAccountNoAsync("123456-777"); Assert.NotNull(results); Assert.NotEmpty(results); Assert.Single(results) ; Assert.All(results, (item) => { Assert.Contains(item, results); }); //assert find empy results = await repository.GetReceiptsByAccountNoAsync("000000-000"); Assert.NotNull(results); Assert.Empty(results); } [Fact] public async void GetReceiptByAccountNoAsync_EmptyContext() { var repository = new Shared.Collections.RepoApiLib.Repositories.ProcessingReceiptRepository(_dbContext); // act var list = new List(); await repository.SaveAllAsync(list); await _dbContext.SaveChangesAsync(); var results = await repository.GetReceiptsByAccountNoAsync("123456-888"); Assert.NotNull(results); Assert.Empty(results); } } }