Beispiel #1
0
        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();
            }
        }
Beispiel #2
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();
            }
        }
Beispiel #3
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);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        ///     Ensures that the entities exist in the database or are succesfully 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);
        }
Beispiel #5
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);
                }
            }
        }
Beispiel #6
0
        /// <summary>
        ///     Ensures that the entities do not exist in the database or are succesfully 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);
        }
Beispiel #7
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();
            }
        }