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."); }
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."); } }
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."); } }
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."); } }
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."); } }
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."); }
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."); }
/// <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)); }
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); } }
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."); }
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."); }
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."); }
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); } }
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."); }
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); } }
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."); }
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."); }
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(); } }
/// <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)); }
/// <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()); }
/// <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); }
/// <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()); }
/// <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()); }