Esempio n. 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();
            }
        }
        private void UsingManager(Action <BudgetClassManager> action)
        {
            using (BudgetDbContext dbContext = DbSetupHelper.GetDbContext())
            {
                var manager = new BudgetClassManager(dbContext);

                action.Invoke(manager);
            }
        }
Esempio n. 3
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();
            }
        }