private static void LoadSeedData() { Console.WriteLine("Seeding data...\n"); using (var dbContext = _dbHelper.GetDbContext()) { var manager = new BudgetClassManager(dbContext); foreach (var item in _dataSet) { var entity = manager.SingleOrDefault(bc => bc.Name.StartsWith(item.Name)); if (entity == null) { manager.TryInsert(item); } else { var tokens = entity.Name.Split('-'); if (tokens.Length == 1) { entity.Name += " - 1"; } else { entity.Name = tokens[0].Trim() + $" - {int.Parse(tokens[1]) + 1}"; } } } manager.SaveChanges(); } }
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(); } }
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(); } }