public void TryUpdate_Fails_WhenDuplicateKeyData() { // Arrange --------------------------- var data = new BudgetClassData("Update-Error-Duplicate - Inserted first", TransactionType.Income); var update = new BudgetClassData("Update-Error-Duplicate - Inserted second", TransactionType.Income); UsingManagerHelper((scope, helper) => { helper.EnsureEntitiesExist(data, update); }); // Act ------------------------------- IEnumerable <ValidationResult> errors = null; UsingManager((scope, manager) => { var mapper = scope.Resolve <BudgetClassDataMapper>(); BudgetClass entity = manager.SingleOrDefault(bc => bc.Name == data.Name); entity = mapper.UpdateEntity(entity, update); errors = manager.TryUpdate(entity).ToList(); }); // Assert ---------------------------- errors.Should().ContainErrorMessage(BudgetClassManager.duplicateByNameError); }
public void TryDelete_DeletesRecord_WhenValidData() { // Arrange --------------------------- var data = new BudgetClassData("Delete-Success-Valid - Inserted", TransactionType.Income); UsingManagerHelper((scope, helper) => { helper.EnsureEntitiesExist(data); }); // Act ------------------------------- IEnumerable <ValidationResult> errors = null; UsingManager((scope, manager) => { BudgetClass entity = manager.SingleOrDefault(bc => bc.Name == data.Name); errors = manager.TryDelete(entity).ToList(); manager.SaveChanges(); }); // Assert ---------------------------- errors.Should().BeEmpty(); UsingManagerHelper((scope, helper) => { helper.AssertEntitiesDoNotExist(data); }); }
public void TryInsert_InsertsRecord_WhenValidData() { IEnumerable <ValidationResult> errors = null; // Arrange --------------------------- var data = new BudgetClassData("Insert-Success-Valid - Inserted", TransactionType.Income); UsingManagerHelper((scope, helper) => { helper.EnsureEntitiesDoNotExist(data); }); // Act ------------------------------- UsingManager((scope, manager) => { var mapper = scope.Resolve <BudgetClassDataMapper>(); BudgetClass entity = mapper.CreateEntity(data); errors = manager.TryInsert(entity).ToList(); manager.SaveChanges(); }); // Assert ---------------------------- errors.Should().BeEmpty(); UsingManagerHelper((scope, helper) => { helper.AssertEntitiesExist(data); }); }
public BudgetClass UpdateEntity(BudgetClass entity, BudgetClassData data) { entity.Name = data.Name; entity.TransactionType = data.TransactionType; return(entity); }
public void TryInsert_Fails_WhenDuplicateKeyData() { // Arrange --------------------------- var data = new BudgetClassData("Insert-Error-Duplicate - Inserted", TransactionType.Income); UsingManagerHelper((scope, helper) => { helper.EnsureEntitiesExist(data); }); // Act ------------------------------- IEnumerable <ValidationResult> errors = null; UsingManager((scope, manager) => { var mapper = scope.Resolve <BudgetClassDataMapper>(); BudgetClass entity = mapper.CreateEntity(data); errors = manager.TryInsert(entity).ToList(); }); // Assert ---------------------------- errors.Should().ContainErrorMessage(BudgetClassManager.duplicateByNameError); }
public async Task ThenIGetTheFollowingBudgetItemsForClass(string name, Table table) { var features = Resolve <BudgetClassFeatures>(); BudgetClass entity = await features.FindBudgetClassByNameAsync(name); table.CompareToSet(entity.BudgetItems); }
public async Task <List <ValidationResult> > RemoveBudgetClassAsync(BudgetClass entity) { DbContext.Remove(entity); await DbContext.SaveChangesAsync(); return(NoError); }
public void TryInsert_InsertsRecord_WhenValidData() { IEnumerable <ValidationResult> errors; // Arrange --------------------------- var data = new BudgetClassData("Insert-Success-Valid - Inserted", TransactionType.Income); // Ensure entitiy does not exist using (var dbContext = dbSetupHelper.GetDbContext()) { var manager = new BudgetClassManager(dbContext); var entity = manager.SingleOrDefault(bc => bc.Name == data.Name); if (entity != null) { errors = manager.TryDelete(entity); errors.Should().BeEmpty(); manager.SaveChanges(); } } // Act ------------------------------- // Insert entity using (var dbContext = dbSetupHelper.GetDbContext()) { var manager = new BudgetClassManager(dbContext); BudgetClass entity = new BudgetClass { Name = data.Name, TransactionType = data.TransactionType }; errors = manager.TryInsert(entity); manager.SaveChanges(); } // Assert ---------------------------- errors.Should().BeEmpty(); // Verify entity exists using (var dbContext = dbSetupHelper.GetDbContext()) { var manager = new BudgetClassManager(dbContext); var entity = manager.SingleOrDefault(bc => bc.Name == data.Name); entity.Should().NotBeNull(); } }
private async Task <BudgetClass> FindDuplicateByNameAsync(BudgetClass entity) { IQueryable <BudgetClass> query = QueryBudgetClasses(bc => bc.Name == entity.Name); if (entity.Id != 0) { query = query.Where(bc => bc.Id != entity.Id); } return(await query.FirstOrDefaultAsync()); }
public async Task ThenICanTAddAnotherClass(string name) { var services = Resolve <BudgetClassServices>(); var budgetClass = new BudgetClass { Name = name, }; var errors = await services.AddBudgetClassAsync(budgetClass); errors.Should().ContainErrorMessage(BudgetClassRepository.DuplicateByNameError); }
public async Task WhenIAddBudgetClass(string name) { var services = Resolve <BudgetClassServices>(); var budgetClass = new BudgetClass { Name = name, }; var errors = await services.AddBudgetClassAsync(budgetClass); errors.Should().BeEmpty(); }
// 12-1. Implement BudgetClass remove //----------------------------------- public async Task <List <ValidationResult> > RemoveBudgetClassAsync(BudgetClass entity) { List <ValidationResult> errors = await BudgetClassRepo.TryDeleteAsync(entity); if (errors.Any()) { return(errors); } await BudgetClassRepo.SaveChangesAsync(); return(Errors.NoError); }
/// <summary> /// Asserts that entities with the supplied key data values do not exist /// </summary> /// <param name="dataSet">Data for the entities to be searched for</param> public void AssertEntitiesDoNotExist(params BudgetClassData[] dataSet) { using (BudgetDbContext dbContext = BudgetDbSetupHelper.GetDbContext()) { var manager = new BudgetClassManager(dbContext); foreach (BudgetClassData data in dataSet) { BudgetClass entity = manager.SingleOrDefault(e => e.Name == data.Name); entity.Should().BeNull(@"because BudgetClass ""{0}"" MUST NOT EXIST!", data.Name); } } }
/// <summary> /// Asserts that entities with the supplied key data values do not exist /// </summary> /// <param name="dataSet">Data for the entities to be searched for</param> public void AssertEntitiesDoNotExist(params BudgetClassData[] dataSet) { using (ILifetimeScope scope = Scope.BeginLifetimeScope()) { var manager = scope.Resolve <BudgetClassManager>(); foreach (BudgetClassData data in dataSet) { BudgetClass entity = manager.SingleOrDefault(e => e.Name == data.Name); entity.Should().BeNull(@"because BudgetClass ""{0}"" MUST NOT EXIST!", data.Name); } } }
public async Task <List <ValidationResult> > AddBudgetClassAsync(BudgetClass entity) { // 3-4. Save to repo //------------------ List <ValidationResult> errors = await BudgetClassRepo.TryInsertAsync(entity); if (errors.Any()) { return(errors); } await BudgetClassRepo.SaveChangesAsync(); return(Errors.NoError); }
public async Task <List <ValidationResult> > AddBudgetItemsRangeAsync(BudgetClass budgetClass, IEnumerable <BudgetItem> items) { foreach (BudgetItem item in items) { budgetClass.BudgetItems.Add(item); } budgetClass.Calculate(); DbContext.Update(budgetClass); await DbContext.SaveChangesAsync(); return(NoError); }
/// <summary> /// Ensures that the entities exist in the database or are successfully added /// </summary> /// <param name="dataSet"></param> /// <param name="dataSet">Data for the entities to be searched for and added or updated if necessary</param> public void EnsureEntitiesExist(params BudgetClassData[] dataSet) { foreach (BudgetClassData data in dataSet) { BudgetClass entity = BudgetClassManager.SingleOrDefault(e => e.Name == data.Name); entity = entity == null?BudgetClassMapper.CreateEntity(data) : BudgetClassMapper.UpdateEntity(entity, data); var errors = BudgetClassManager.TryUpsert(entity); errors.Should().BeEmpty(@"because BudgetClass ""{0}"" has to be added!", data.Name); } BudgetClassManager.SaveChanges(); AssertEntitiesExist(dataSet); }
public async Task <List <ValidationResult> > AddBudgetClassAsync(BudgetClass entity) { entity.Tenant_Id = SessionContext.CurrentTenant.Id; var errors = await ValidateSaveAsync(entity); if (errors.Any()) { return(errors); } DbContext.Add(entity); await DbContext.SaveChangesAsync(); return(NoError); }
public async Task <List <ValidationResult> > ModifyBudgetClassAsync(BudgetClass entity) { entity.Calculate(); var errors = await ValidateSaveAsync(entity); if (errors.Any()) { return(errors); } DbContext.Entry(entity).State = EntityState.Modified; await DbContext.SaveChangesAsync(); return(NoError); }
/// <summary> /// Asserts that entities equivalent to the supplied input data classes exist /// </summary> /// <param name="dataSet">Data for the entities to be searched for</param> public void AssertEntitiesExist(params BudgetClassData[] dataSet) { using (ILifetimeScope scope = Scope.BeginLifetimeScope()) { var manager = scope.Resolve <BudgetClassManager>(); var mapper = scope.Resolve <BudgetClassDataMapper>(); foreach (BudgetClassData data in dataSet) { BudgetClass entity = manager.SingleOrDefault(e => e.Name == data.Name); entity.Should().NotBeNull(@"because BudgetClass ""{0}"" MUST EXIST!", data.Name); BudgetClassData entityData = mapper.CreateData(entity); entityData.ShouldBeEquivalentTo(data); } } }
/// <summary> /// Asserts that entities equivalent to the supplied input data classes exist /// </summary> /// <param name="dataSet">Data for the entities to be searched for</param> public void AssertEntitiesExist(params BudgetClassData[] dataSet) { using (BudgetDbContext dbContext = BudgetDbSetupHelper.GetDbContext()) { var manager = new BudgetClassManager(dbContext); var mapper = new BudgetClassDataMapper(); foreach (BudgetClassData data in dataSet) { BudgetClass entity = manager.SingleOrDefault(e => e.Name == data.Name); entity.Should().NotBeNull(@"because BudgetClass ""{0}"" MUST EXIST!", data.Name); BudgetClassData entityData = mapper.CreateData(entity); entityData.ShouldBeEquivalentTo(data); } } }
/// <summary> /// Ensures that the entities do not exist in the database or are successfully removed /// </summary> /// <param name="dataSet">Data for the entities to be searched for and removed if necessary</param> public void EnsureEntitiesDoNotExist(params BudgetClassData[] dataSet) { foreach (BudgetClassData data in dataSet) { BudgetClass entity = BudgetClassManager.SingleOrDefault(e => e.Name == data.Name); if (entity == null) { continue; } var errors = BudgetClassManager.TryDelete(entity); errors.Should().BeEmpty(@"because BudgetClass ""{0}"" has to be removed!", data.Name); } BudgetClassManager.SaveChanges(); AssertEntitiesDoNotExist(dataSet); }
private async Task <List <ValidationResult> > ValidateSaveAsync(BudgetClass entity) { BudgetClass duplicateByName = await FindDuplicateByNameAsync(entity); if (duplicateByName != null) { return(ErrorList(BudgetClassDuplicateByNameError, duplicateByName.Name, duplicateByName.Id)); } List <string> duplicateNames = entity.BudgetItems .ToLookup(bi => bi.Name) .Where(li => li.Count() > 1) .Select(li => li.Key) .ToList(); if (duplicateNames.Any()) { return(duplicateNames.Select(n => Error(BudgetItemDuplicateByNameError, n)).ToList()); } return(NoError); }
public BudgetClassData CreateData(BudgetClass entity) { var data = new BudgetClassData(entity.Name, entity.TransactionType); return(data); }
public void TryUpdate_UpdatesRecord_WhenValidData() { IEnumerable <ValidationResult> errors; // Arrange --------------------------- var data = new BudgetClassData("Update-Success-Valid - Inserted", TransactionType.Income); var update = new BudgetClassData("Update-Success-Valid - Updated", TransactionType.Income); // Ensure entitiy "data" exists and "update" does not exist using (var dbContext = dbSetupHelper.GetDbContext()) { var manager = new BudgetClassManager(dbContext); var existing = manager.SingleOrDefault(bc => bc.Name == data.Name); if (existing == null) { existing = new BudgetClass { Name = data.Name, TransactionType = data.TransactionType }; errors = manager.TryInsert(existing); errors.Should().BeEmpty(); } var updated = manager.SingleOrDefault(bc => bc.Name == data.Name); if (updated != null) { errors = manager.TryDelete(updated); errors.Should().BeEmpty(); } manager.SaveChanges(); } // Act ------------------------------- using (var dbContext = dbSetupHelper.GetDbContext()) { var manager = new BudgetClassManager(dbContext); var entity = manager.SingleOrDefault(bc => bc.Name == data.Name); entity.Name = update.Name; errors = manager.TryUpdate(entity).ToList(); manager.SaveChanges(); } // Assert ---------------------------- errors.Should().BeEmpty(); using (var dbContext = dbSetupHelper.GetDbContext()) { var manager = new BudgetClassManager(dbContext); var existing = manager.SingleOrDefault(bc => bc.Name == update.Name); existing.Should().NotBeNull(); } }
public BudgetClass CreateEntity(BudgetClassData data) { var entity = new BudgetClass(); return(UpdateEntity(entity, data)); }
public BudgetClassData(BudgetClass entity) { Name = entity.Name; TransactionType = entity.TransactionType; }
public static async Task <BudgetClass> FindDuplicateByNameAsync(this IEntityQuery <BudgetClass> repository, BudgetClass entity) { IQueryable <BudgetClass> query = repository.Query(bc => bc.Name == entity.Name.Trim()); if (entity.Id != 0) { query = query.Where(bc => bc.Id != entity.Id); } return(await query.FirstOrDefaultAsync()); }