Exemplo n.º 1
0
            public void ThrowsExceptionWhenNewZeroUserIdPasedIn()
            {
                // Arrange.
                this.category.UserId = 1;
                this.userId          = 0;

                // Act.
                Exception caughtException = null;

                try
                {
                    using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                    {
                        CategoriesRepository repository = new CategoriesRepository(context);
                        repository.Save(this.category, this.userId);
                    }
                }
                catch (Exception ex)
                {
                    caughtException = ex;
                }

                // Assert.
                Assert.IsNotNull(caughtException, "An exception should be thrown.");
                Assert.IsInstanceOfType(caughtException, typeof(ArgumentException), "This should be an argument exception.");
                string exceptionMessage = "A user ID is expected to be passed in.\r\nParameter name: userId";

                Assert.AreEqual(exceptionMessage, caughtException.Message, "The exception message should be correct.");
            }
Exemplo n.º 2
0
            public void UpdatesBudgetInTheContext()
            {
                // Act.
                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    BudgetsRepository repository = new BudgetsRepository(context);
                    repository.Save(this.budget, this.userId);
                }

                // Assert.
                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    int expectedBudgetCount = 5;
                    Assert.AreEqual(expectedBudgetCount, context.Budgets.Count(), "The number of budgets should not change.");

                    Budget actual = ContextDataService.GetBudgetsSet(context)
                                    .FirstOrDefault(x => x.Id == this.budget.Id);
                    Assert.IsNotNull(actual, "A budget should be found.");

                    Budget expected = this.budget;
                    Assert.AreEqual(expected.Id, actual.Id, "The ID for the entity should match.");
                    Assert.AreEqual(expected.Name, actual.Name, "The name for the entity should match.");
                    Assert.AreEqual(expected.UserId, actual.UserId, "The user ID for the entity should match.");
                }
            }
Exemplo n.º 3
0
            public void ReturnsCategoriesOnlyForTheUser()
            {
                // Act.
                List <Category> actualList;

                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    CategoriesRepository repository = new CategoriesRepository(context);
                    actualList = repository.GetAll(this.userId).ToList();
                }

                // Assert.
                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    int numberOfCategoryUsers = context.Categories
                                                .GroupBy(a => a.UserId)
                                                .Count();
                    Assert.AreNotEqual(numberOfCategoryUsers, 0, "For the test to work there must be more than one user with categories.");
                    Assert.AreNotEqual(numberOfCategoryUsers, 1, "For the test to work there must be more than one user with categories.");
                }

                foreach (Category actual in actualList)
                {
                    Assert.AreEqual(this.userId, actual.UserId, "The user ID for each category should match the input user ID.");
                }
            }
Exemplo n.º 4
0
            public void ReturnsBudgetsOnlyForTheUser()
            {
                // Act.
                List <BudgetSummary> actualList;

                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    BudgetsRepository repository = new BudgetsRepository(context);
                    actualList = repository.GetTotals(this.userId).ToList();
                }

                // Assert.
                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    int numberOfBudgetUsers = context.Budgets
                                              .GroupBy(a => a.UserId)
                                              .Count();
                    Assert.AreNotEqual(numberOfBudgetUsers, 0, "For the test to work there must be more than one user with budgets.");
                    Assert.AreNotEqual(numberOfBudgetUsers, 1, "For the test to work there must be more than one user with budgets.");
                }

                foreach (BudgetSummary actual in actualList)
                {
                    Assert.AreEqual(this.userId, actual.UserId, "The user ID for each budget should match the input user ID.");
                }
            }
Exemplo n.º 5
0
            public void UpdatesCategoryInTheContext()
            {
                // Act.
                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    CategoriesRepository repository = new CategoriesRepository(context);
                    repository.Save(this.category, this.userId);
                }

                // Assert.
                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    int expectedCategoryCount = 4;
                    Assert.AreEqual(expectedCategoryCount, context.Categories.Count(), "The number of categories should not change.");

                    Category actual = ContextDataService.GetCategoriesSet(context)
                                      .FirstOrDefault(x => x.Id == this.category.Id);
                    Assert.IsNotNull(actual, "A category should be found.");

                    Category expected = this.category;
                    Assert.AreEqual(expected.Id, actual.Id, "The ID for the entity should match.");
                    Assert.AreEqual(expected.Name, actual.Name, "The name for the entity should match.");
                    Assert.AreEqual(expected.UserId, actual.UserId, "The user ID for the entity should match.");
                }
            }
Exemplo n.º 6
0
            public void ThrowsExceptionWhenIdPresent()
            {
                // Arrange.
                this.category.Id = 5;

                // Act.
                Exception caughtException = null;

                try
                {
                    using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                    {
                        CategoriesRepository repository = new CategoriesRepository(context);
                        repository.Add(this.category, this.userId);
                    }
                }
                catch (Exception ex)
                {
                    caughtException = ex;
                }

                // Assert.
                Assert.IsNotNull(caughtException, "An exception should be thrown.");
                Assert.IsInstanceOfType(caughtException, typeof(ArgumentException), "This should be an argument exception.");
                string exceptionMessage = "A new category without a specified ID should have been used. To update a category, use the Save method.\r\nParameter name: category.Id";

                Assert.AreEqual(exceptionMessage, caughtException.Message, "The exception message should be correct.");
            }
Exemplo n.º 7
0
            public void ThrowsExceptionWhenRecordFoundDoesNotBelongToTheUser()
            {
                // Arrange.
                this.budgetId = 1;
                this.userId   = 2;

                // Act.
                Exception caughtException = null;

                try
                {
                    using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                    {
                        BudgetsRepository repository = new BudgetsRepository(context);
                        repository.Get(this.budgetId, this.userId);
                    }
                }
                catch (Exception ex)
                {
                    caughtException = ex;
                }

                // Assert.
                Assert.IsNotNull(caughtException, "An exception should be thrown.");
                Assert.IsInstanceOfType(caughtException, typeof(NotFoundException), "This should be an argument exception.");
                string exceptionMessage = "The budget was not found.";

                Assert.AreEqual(exceptionMessage, caughtException.Message, "The exception message should be correct.");
            }
Exemplo n.º 8
0
 /// <summary>
 /// Gets the database set for transactions with the appropriate includes.
 /// </summary>
 /// <param name="context">The database context.</param>
 /// <returns>The transactions database set.</returns>
 public static IQueryable <Transaction> GetTransactionsSet(CheckbookContext context)
 {
     return(context.Transactions
            .Include(t => t.FromAccount)
            .Include(t => t.ToAccount)
            .Include(t => t.Items)
            .ThenInclude(i => i.Budget)
            .ThenInclude(s => s.Category));
 }
Exemplo n.º 9
0
            public override void Initialize()
            {
                base.Initialize();

                // Initialize the database set.
                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    DatabaseSeed.AddEntities(context);
                    this.entities = ContextDataService.GetBudgets(context);
                }
            }
Exemplo n.º 10
0
            public void ReturnsBudgetFromContext()
            {
                // Act.
                Budget actual;

                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    BudgetsRepository repository = new BudgetsRepository(context);
                    actual = repository.Save(this.budget, this.userId);
                }

                // Assert.
                Assert.AreEqual(this.budget.Id, actual.Id, "The ID for the entity should match.");
            }
Exemplo n.º 11
0
            public void ReturnsCategoryFromContext()
            {
                // Act.
                Category actual;

                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    CategoriesRepository repository = new CategoriesRepository(context);
                    actual = repository.Save(this.category, this.userId);
                }

                // Assert.
                Assert.AreEqual(this.category.Id, actual.Id, "The ID for the entity should match.");
            }
Exemplo n.º 12
0
            public void ReturnsEmptyListWhenNoRecordsFound()
            {
                // Arrange.
                this.userId = 123;

                // Act.
                List <BudgetSummary> actualList;

                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    BudgetsRepository repository = new BudgetsRepository(context);
                    actualList = repository.GetTotals(this.userId).ToList();
                }

                // Assert.
                Assert.AreEqual(0, actualList.Count(), "An empty list should be returned.");
            }
Exemplo n.º 13
0
            public override void Initialize()
            {
                base.Initialize();

                // Initialize the inputs.
                this.userId   = 1;
                this.category = new Category
                {
                    Name   = "Third Category",
                    UserId = this.userId,
                };

                // Initialize the database set.
                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    DatabaseSeed.AddEntities(context);
                    this.entities = ContextDataService.GetCategories(context);
                }
            }
Exemplo n.º 14
0
            public void ReturnsBudgetTotalsFromContext()
            {
                // Act.
                List <BudgetSummary> actualList;

                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    BudgetsRepository repository = new BudgetsRepository(context);
                    actualList = repository.GetTotals(this.userId).ToList();
                }

                // Assert.
                Assert.AreEqual(this.entities.Count(), actualList.Count(), "The entity count should match.");

                BudgetSummary expected = new BudgetSummary(this.entities.ElementAt(0));
                BudgetSummary actual   = actualList.ElementAt(0);
                string        index    = "first";

                Assert.AreNotEqual(0, actual.Balance, "The current total should not be zero.");
                Assert.AreEqual(expected.Balance, actual.Balance, $"The current total for the {index} entity should match.");
            }
Exemplo n.º 15
0
            public override void Initialize()
            {
                base.Initialize();

                // Initialize the input.
                this.userId = 1;
                this.budget = new Budget
                {
                    Id         = 2,
                    Name       = "Second Budget x",
                    UserId     = this.userId,
                    CategoryId = 1,
                };

                // Initialize the database set.
                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    DatabaseSeed.AddEntities(context);
                    this.entities = ContextDataService.GetBudgets(context);
                }
            }
Exemplo n.º 16
0
            public void ReturnsBudgetsFromContext()
            {
                // Act.
                List <Budget> actualList;

                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    BudgetsRepository repository = new BudgetsRepository(context);
                    actualList = repository.GetAll(this.userId).ToList();
                }

                // Assert.
                Assert.AreEqual(this.entities.Count(), actualList.Count(), "The entity count should match.");

                Budget expected = this.entities.ElementAt(0);
                Budget actual   = actualList.ElementAt(0);
                string index    = "first";

                Assert.AreEqual(expected.Id, actual.Id, $"The ID for the {index} entity should match.");
                Assert.AreEqual(expected.Name, actual.Name, $"The name for the {index} entity should match.");
                Assert.AreEqual(expected.UserId, actual.UserId, $"The user ID for the {index} entity should match.");
            }
Exemplo n.º 17
0
            public void ReturnsBudgetFromContext()
            {
                // Arrange.
                this.budgetId = 1;
                this.userId   = 1;

                // Act.
                Budget actual;

                using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
                {
                    BudgetsRepository repository = new BudgetsRepository(context);
                    actual = repository.Get(this.budgetId, this.userId);
                }

                // Assert.
                Budget expected = this.entities.ElementAt(0);

                Assert.AreEqual(expected.Id, actual.Id, "The ID for the entity should match.");
                Assert.AreEqual(expected.Name, actual.Name, "The name for the entity should match.");
                Assert.AreEqual(expected.UserId, actual.UserId, "The user ID for the entity should match.");
            }
Exemplo n.º 18
0
        public virtual void Initialize()
        {
            // Open a connection to a SQLite database.
            string connectionString = new SqliteConnectionStringBuilder
            {
                DataSource = ":memory:",
            }.ToString();

            this.dbConnection = new SqliteConnection(connectionString);
            this.dbConnection.Open();

            // Create a new context.
            DbContextOptionsBuilder <CheckbookContext> builder = new DbContextOptionsBuilder <CheckbookContext>();

            builder.UseSqlite(this.dbConnection);
            this.dbContextOptions = builder.Options;

            // Make sure the database has been created.
            using (CheckbookContext context = new CheckbookContext(this.dbContextOptions))
            {
                context.Database.EnsureCreated();
            }
        }
Exemplo n.º 19
0
 /// <summary>
 /// Gets the database set for budgets with the appropriate includes.
 /// </summary>
 /// <param name="context">The database context.</param>
 /// <returns>The budgets database set.</returns>
 public static IQueryable <Budget> GetBudgetsSet(CheckbookContext context)
 {
     return(context.Budgets
            .Include(b => b.Category));
 }
Exemplo n.º 20
0
 /// <summary>
 /// Gets the set of transaction information from the context with child objects.
 /// </summary>
 /// <param name="context">The database context.</param>
 /// <returns>The list of transactions.</returns>
 public static List <Transaction> GetTransactions(CheckbookContext context)
 {
     return(GetTransactionsSet(context).ToList());
 }
Exemplo n.º 21
0
 /// <summary>
 /// Gets the database set for categories with the appropriate includes.
 /// </summary>
 /// <param name="context">The database context.</param>
 /// <returns>The categories database set.</returns>
 public static IQueryable <Category> GetCategoriesSet(CheckbookContext context)
 {
     return(context.Categories);
 }
Exemplo n.º 22
0
 /// <summary>
 /// Gets the set of budget information from the context with child objects.
 /// </summary>
 /// <param name="context">The database context.</param>
 /// <returns>The list of budgets.</returns>
 public static List <Budget> GetBudgets(CheckbookContext context)
 {
     return(GetBudgetsSet(context).ToList());
 }
Exemplo n.º 23
0
 /// <summary>
 /// Gets the set of category information from the context with child objects.
 /// </summary>
 /// <param name="context">The database context.</param>
 /// <returns>The list of categories.</returns>
 public static List <Category> GetCategories(CheckbookContext context)
 {
     return(GetCategoriesSet(context).ToList());
 }