public static Dictionary<string, TransactionType> CreateTransactionTypes(HttpContext context)
        {
            var dbContext = context.RequestServices.GetRequiredService<AccountingContext>();

            CategoryType assets = null;
            CategoryType expenses = null;
            CategoryType income = null;
            if (dbContext.CategoryTypes.Any(ct => ct.Name == CategoryTypeBuilder.AssetsName)
                && dbContext.CategoryTypes.Any(ct => ct.Name == CategoryTypeBuilder.ExpensesName)
                && dbContext.CategoryTypes.Any(ct => ct.Name == CategoryTypeBuilder.IncomeName))
            {
                assets = dbContext.CategoryTypes.First(ct => ct.Name == CategoryTypeBuilder.AssetsName);
                expenses = dbContext.CategoryTypes.First(ct => ct.Name == CategoryTypeBuilder.ExpensesName);
                income = dbContext.CategoryTypes.First(ct => ct.Name == CategoryTypeBuilder.IncomeName);
            }
            else
            {
                var catTypes = CategoryTypeBuilder.CreateCategoryTypes(context);
                assets = catTypes[CategoryTypeBuilder.AssetsName];
                expenses = catTypes[CategoryTypeBuilder.ExpensesName];
                income = catTypes[CategoryTypeBuilder.IncomeName];
            }

            var incomeTransType = new TransactionType { Name = IncomeTransactionType, From = income, To = assets };
            var expenseTransType = new TransactionType { Name = ExpenseTransactionType, From = assets, To = expenses };

            dbContext.TransactionTypes.Add(incomeTransType);
            dbContext.TransactionTypes.Add(expenseTransType);

            var dict = new Dictionary<string, TransactionType>();
            dict.Add(incomeTransType.Name, incomeTransType);
            dict.Add(expenseTransType.Name, expenseTransType);

            return dict;
        }
        public void FindAllReturnsNoRecords()
        {
            var fakeTransactionType = new TransactionType[] { };
            var options = new DbContextOptionsBuilder().Options;
            var context = new Mock<AccountingContext>(options);
            context.Setup(x => x.TransactionTypes).ReturnsDbSet(fakeTransactionType);
            context.Setup(i => i.Set<TransactionType>()).ReturnsDbSet(fakeTransactionType);
            var sut = new MockBaseRepository<TransactionType>(context.Object);

            var result = sut.FindAll();
            var resultList = result.ToList();

            result.Should().NotBeNull("the TransactionTypes should not exist but an emtpy IQueryable should be returned");
            resultList.Count.Should().Be(0, "there are no TransactionTypes");
        }
 public static Transaction BuildTransaction(AccountingUser user, decimal total = 100, Category fromCat = null, Category toCat = null, TransactionType transType = null)
 {
     return new Transaction
     {
         Total = total,
         AccountingUser = user,
         From = fromCat == null ? new Category
         {
             Name = "TestCategoryType"
         } : fromCat,
         To = toCat == null ? new Category
         {
             Name = "OtherTestCategory"
         } : toCat,
         DateTime = DateTime.Now,
         TransactionType = transType == null ? new TransactionType
         {
             Name = "TestTransactionType"
         } : transType
     };
 }
        public void FindAllWhereReturnCorrectRecords()
        {
            var fakeTransactionType = new TransactionType[]
            {
                new TransactionType { Id = 1, Name="FakeTransaction1" },
                new TransactionType { Id = 2, Name="FakeTransaction2" },
                new TransactionType { Id = 3, Name="FakeTransaction3" },
                new TransactionType { Id = 4, Name="FakeTransaction1" }
            };
            var options = new DbContextOptionsBuilder().Options;
            var context = new Mock<AccountingContext>(options);
            context.Setup(x => x.TransactionTypes).ReturnsDbSet(fakeTransactionType);
            context.Setup(i => i.Set<TransactionType>()).ReturnsDbSet(fakeTransactionType);
            var sut = new MockBaseRepository<TransactionType>(context.Object);

            var result = sut.FindAllWhere(t => t.Name.Equals("FakeTransaction1", StringComparison.InvariantCultureIgnoreCase));
            var resultList = result.ToList();

            resultList.Should().NotBeNull("the TransactionType should exist therefore it should be returned  in a set and not be Null");
            resultList.Count.Should().BeGreaterThan(0, "there are TransactionTypes therefore there are more than 0");
            resultList.Count.Should().Be(2, "there are only 2 TransactionTypes with the name \"FakeTransaction1\" therefore there must be only 2");
            resultList.Should().Contain(fakeTransactionType[0], "this is one of the fakeTransactions that should exist with the name therefore it should be in the set returned");
            resultList.Should().Contain(fakeTransactionType[3], "this is one of the fakeTransactions that should exist with the name therefore it should be in the set returned");
        }
        public void FindAllReturnsAllRecords()
        {
            var fakeTransactionType = new TransactionType[]
            {
                new TransactionType { Id = 1, Name="FakeTransaction1" },
                new TransactionType { Id = 2, Name="FakeTransaction2" },
                new TransactionType { Id = 3, Name="FakeTransaction3" }
            };
            var options = new DbContextOptionsBuilder().Options;
            var context = new Mock<AccountingContext>(options);
            context.Setup(x => x.TransactionTypes).ReturnsDbSet(fakeTransactionType);
            context.Setup(i => i.Set<TransactionType>()).ReturnsDbSet(fakeTransactionType);
            var sut = new MockBaseRepository<TransactionType>(context.Object);

            var result = sut.FindAll();
            var resultList = result.ToList();

            result.Should().NotBeNull("the TransactionTypes should exist therefore they should be returned and not be Null");
            resultList.Count.Should().BeGreaterThan(0, "there are TransactionTypes therefore there are more than 0");
            resultList.Count.Should().BeLessThan(4, "there are only 3 TransactionTypes therefore there must be less than 4");
            resultList.Should().Contain(fakeTransactionType[0], "this is one of the fakeTransactions that should exist therefore be in the set returned");
            resultList.Should().Contain(fakeTransactionType[1], "this is one of the fakeTransactions that should exist therefore be in the set returned");
            resultList.Should().Contain(fakeTransactionType[2], "this is one of the fakeTransactions that should exist therefore be in the set returned");
        }
Exemple #6
0
        private async Task SeedDevelopmentData()
        {
            if (await this.userManager.FindByEmailAsync("*****@*****.**") == null)
            {
                var newUser = new AccountingUser
                {
                    UserName = "******",
                    Email = "*****@*****.**"
                };

                var result = await this.userManager.CreateAsync(newUser, "password");
            }
            
            var base64Token = "OMuo2TLAB/iOX4IQs98+ag=="; 
            SignupToken token = null;
            if (!this.context.SignupTokens.Any(t => t.Token == base64Token))
            {
                this.context.SignupTokens.Add(new SignupToken { Token = base64Token });
            }
            else
            {
                token = this.context.SignupTokens.First(t => t.Token == base64Token);
                token.Used = false;
                token.AccountingUser = null;
                token.AccountingUserId = null;
                this.context.SignupTokens.Update(token);
            }
            

            var user = await this.userManager.FindByNameAsync("testuser");

            var expenseType = new CategoryType { Name = "Expenses", DisplayOrder = 2, AddTo = true };
            var assetType = new CategoryType { Name = "Assets", DisplayOrder = 1, AddTo = true, AddFrom = false };
            var incomeType = new CategoryType { Name = "Income", DisplayOrder = 3, AddFrom = true };

            if (!this.context.CategoryTypes.Any(c => c.Name == expenseType.Name))
            {
                this.context.CategoryTypes.Add(expenseType);
            }
            else
            {
                expenseType = this.context.CategoryTypes.First(c => c.Name == expenseType.Name);
            }

            if (!this.context.CategoryTypes.Any(c => c.Name == assetType.Name))
            {
                this.context.CategoryTypes.Add(assetType);
            }
            else
            {
                assetType = this.context.CategoryTypes.First(c => c.Name == assetType.Name);
            }

            if (!this.context.CategoryTypes.Any(c => c.Name == incomeType.Name))
            {
                this.context.CategoryTypes.Add(incomeType);
            }
            else
            {
                incomeType = this.context.CategoryTypes.First(c => c.Name == incomeType.Name);
            }

            var savings = new Category { Name = "Savings", NormalizedName = Category.NormalizeName("Savings"), CategoryType = assetType, AccountingUser = user };
            var grocceries = new Category { Name = "Grocceries", NormalizedName = Category.NormalizeName("Grocceries"), CategoryType = expenseType, AccountingUser = user };
            var salary = new Category { Name = "Salary", NormalizedName = Category.NormalizeName("Salary"), CategoryType = incomeType, AccountingUser = user };
            var work = new Category { Name = "Work", NormalizedName = Category.NormalizeName("Work"), CategoryType = incomeType, AccountingUser = user, ParentCategory = salary };
            var subwork = new Category { Name = "Subwork", NormalizedName = Category.NormalizeName("Subwork"), CategoryType = incomeType, AccountingUser = user, ParentCategory = work };
            var incomeTransType = new TransactionType { Name = "Income", From = incomeType, To = assetType };
            var expenseTransType = new TransactionType { Name = "Expense", From = assetType, To = expenseType };
            var transferTransType = new TransactionType { Name = "Transfer", From = assetType, To = assetType };

            if (!this.context.Categories.Any(c => c.Name == savings.Name))
            {
                this.context.Categories.Add(savings);
                this.context.Categories.Add(grocceries);
                this.context.Categories.Add(salary);
                this.context.Categories.Add(work);
                this.context.Categories.Add(subwork);
            }
            else
            {
                savings = this.context.Categories.SingleOrDefault(c => c.Name == savings.Name);
                grocceries = this.context.Categories.SingleOrDefault(c => c.Name == grocceries.Name);
                salary = this.context.Categories.SingleOrDefault(c => c.Name == salary.Name);
                work = this.context.Categories.SingleOrDefault(c => c.Name == work.Name);
                subwork = this.context.Categories.SingleOrDefault(c => c.Name == subwork.Name);
            }

            if (!this.context.TransactionTypes.Any(t => t.Name == incomeTransType.Name))
            {
                this.context.TransactionTypes.Add(incomeTransType);
            }
            else
            {
                incomeTransType = this.context.TransactionTypes.SingleOrDefault(t => t.Name == incomeTransType.Name);
            }

            if (!this.context.TransactionTypes.Any(t => t.Name == expenseTransType.Name))
            {
                this.context.TransactionTypes.Add(expenseTransType);
            }
            else
            {
                expenseTransType = this.context.TransactionTypes.SingleOrDefault(t => t.Name == expenseTransType.Name);
            }
            
            if (!this.context.TransactionTypes.Any(t => t.Name == transferTransType.Name))
            {
                this.context.TransactionTypes.Add(transferTransType);
            }
            else
            {
                transferTransType = this.context.TransactionTypes.SingleOrDefault(t => t.Name == transferTransType.Name);
            }

            this.context.SaveChanges();

            var trans = new Transaction { From = salary, To = savings, TransactionType = incomeTransType, Total = 100, DateTime = DateTime.UtcNow, AccountingUser = user };

            if (!this.context.Transactions.Any())
            {
                this.context.Transactions.Add(trans);
                trans.To.AddTransaction(trans, from: false);
                trans.From.AddTransaction(trans, from: true);
                this.context.SaveChanges();
            }

            
        }
        public void FindAllWhereReturnNoRecords()
        {
            var fakeTransactionType = new TransactionType[]
            {
                new TransactionType { Id = 1, Name="FakeTransaction1" },
                new TransactionType { Id = 2, Name="FakeTransaction2" },
                new TransactionType { Id = 3, Name="FakeTransaction3" },
                new TransactionType { Id = 4, Name="FakeTransaction1" }
            };
            var options = new DbContextOptionsBuilder().Options;
            var context = new Mock<AccountingContext>(options);
            context.Setup(x => x.TransactionTypes).ReturnsDbSet(fakeTransactionType);
            context.Setup(i => i.Set<TransactionType>()).ReturnsDbSet(fakeTransactionType);
            var sut = new MockBaseRepository<TransactionType>(context.Object);

            var result = sut.FindAllWhere(t => t.Name.Equals("FakeTransaction4", StringComparison.InvariantCultureIgnoreCase));
            var resultList = result.ToList();

            resultList.Should().NotBeNull("the TransactionType even though empty should just return an empty set and not be Null");
            resultList.Count.Should().Be(0, "there are no TransactionTypes therefore there should be 0");
        }
        public void FindByIdReturnsNothing()
        {
            var fakeTransactionType = new TransactionType[]
            {
                new TransactionType { Id = 1, Name="FakeTransaction1" },
                new TransactionType { Id = 2, Name="FakeTransaction2" },
            };
            var options = new DbContextOptionsBuilder().Options;
            var context = new Mock<AccountingContext>(options);
            context.Setup(x => x.TransactionTypes).ReturnsDbSet(fakeTransactionType);
            context.Setup(i => i.Set<TransactionType>()).ReturnsDbSet(fakeTransactionType);
            var sut = new MockBaseRepository<TransactionType>(context.Object);

            var result = sut.FindById(3);

            result.Should().BeNull("the TransactionType should'nt therefore nothing should be returned i.e Be Null");
        }
        public void FindByIdReturnsARecord()
        {
            var fakeTransactionType = new TransactionType[]
            {
                new TransactionType { Id = 1, Name="FakeTransaction1" },
                new TransactionType { Id = 2, Name="FakeTransaction2" },
            };
            var options = new DbContextOptionsBuilder().Options;
            var context = new Mock<AccountingContext>(options);
            context.Setup(x => x.TransactionTypes).ReturnsDbSet(fakeTransactionType);
            context.Setup(i => i.Set<TransactionType>()).ReturnsDbSet(fakeTransactionType);
            var sut = new MockBaseRepository<TransactionType>(context.Object);

            var result = sut.FindById(1);

            result.Should().NotBeNull("the TransactionType should exist therefore should be returned and not be Null");
            result.Id.ShouldBeEquivalentTo(1, "this is the ID we used to retrieve the TransactionType");
        }