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);
            });
        }
Esempio n. 4
0
        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);
        }
Esempio n. 6
0
        public async Task ThenIGetTheFollowingBudgetItemsForClass(string name, Table table)
        {
            var features = Resolve <BudgetClassFeatures>();

            BudgetClass entity = await features.FindBudgetClassByNameAsync(name);

            table.CompareToSet(entity.BudgetItems);
        }
Esempio n. 7
0
        public async Task <List <ValidationResult> > RemoveBudgetClassAsync(BudgetClass entity)
        {
            DbContext.Remove(entity);

            await DbContext.SaveChangesAsync();

            return(NoError);
        }
Esempio n. 8
0
        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();
            }
        }
Esempio n. 9
0
        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());
        }
Esempio n. 10
0
        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);
        }
Esempio n. 11
0
        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);
        }
Esempio n. 13
0
        /// <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);
                }
            }
        }
Esempio n. 14
0
        /// <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);
        }
Esempio n. 16
0
        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);
        }
Esempio n. 17
0
        /// <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);
        }
Esempio n. 18
0
        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);
        }
Esempio n. 19
0
        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);
        }
Esempio n. 20
0
        /// <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);
                }
            }
        }
Esempio n. 21
0
        /// <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);
                }
            }
        }
Esempio n. 22
0
        /// <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);
        }
Esempio n. 23
0
        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);
        }
Esempio n. 24
0
        public BudgetClassData CreateData(BudgetClass entity)
        {
            var data = new BudgetClassData(entity.Name, entity.TransactionType);

            return(data);
        }
Esempio n. 25
0
        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();
            }
        }
Esempio n. 26
0
        public BudgetClass CreateEntity(BudgetClassData data)
        {
            var entity = new BudgetClass();

            return(UpdateEntity(entity, data));
        }
Esempio n. 27
0
        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());
        }