public async Task GetValues_CorrectSums() { // Arrange context.AddRange(new List <Payment> { new Payment(DateTime.Today, 60, PaymentType.Income, new Account("Foo1")), new Payment(DateTime.Today, 20, PaymentType.Income, new Account("Foo2")), new Payment(DateTime.Today, 50, PaymentType.Expense, new Account("Foo3")), new Payment(DateTime.Today, 40, PaymentType.Expense, new Account("Foo3")) }); context.SaveChanges(); // Act List <StatisticEntry> result = await new GetCashFlowQueryHandler(contextAdapterMock.Object).Handle(new GetCashFlowQuery { StartDate = DateTime .Today .AddDays(-3), EndDate = DateTime .Today .AddDays(3) }, default); // Assert result[0].Value.ShouldEqual(80); result[1].Value.ShouldEqual(90); result[2].Value.ShouldEqual(-10); }
public async Task CalculateCorrectSums() { // Arrange Account account = new("Foo1"); Category category = new("abcd"); context.AddRange(new List <Payment> { new Payment(DateTime.Today, 60, PaymentType.Income, account, category: category), new Payment(DateTime.Today, 20, PaymentType.Expense, account, category: category), new Payment(DateTime.Today.AddMonths(-1), 50, PaymentType.Expense, account, category: category), new Payment(DateTime.Today.AddMonths(-2), 40, PaymentType.Expense, account, category: category) }); context.Add(account); context.Add(category); context.SaveChanges(); // Act List <StatisticEntry> result = await new GetCategoryProgressionHandler(contextAdapterMock.Object).Handle( new GetCategoryProgressionQuery(category.Id, DateTime.Today.AddYears(-1), DateTime.Today.AddDays(3)), default); // Assert result[0].Value.Should().Be(40); result[1].Value.Should().Be(-50); result[2].Value.Should().Be(-40); }
private CaseSensitivity CheckCaseSensitivity(DbContextOptions <EfCoreContext> options, string databaseType) { using (var context = new EfCoreContext(options)) { if (SetupDatabaseTrueIfNeedsNewData(context)) { //new database created, so seed var book1 = new Book { Title = NormalTitle }; var book2 = new Book { Title = "entity FRAMEWORK in action" }; context.AddRange(book1, book2); context.SaveChanges(); } //ATTEMPT var caseSensitivity = CaseSensitivity.NotSet; caseSensitivity = OutputResult(context.Books.Where(x => x.Title == NormalTitle), "==", databaseType, caseSensitivity); caseSensitivity = OutputResult(context.Books.Where(x => x.Title.Equals(NormalTitle)), "Equals", databaseType, caseSensitivity); caseSensitivity = OutputResult(context.Books.Where(x => x.Title.StartsWith("Entity")), "StartsWith", databaseType, caseSensitivity); caseSensitivity = OutputResult(context.Books.Where(x => x.Title.EndsWith("Action")), "EndsWith", databaseType, caseSensitivity); caseSensitivity = OutputResult(context.Books.Where(x => x.Title.Contains("Framework")), "Contains", databaseType, caseSensitivity); caseSensitivity = OutputResult(context.Books.Where(x => x.Title.IndexOf("Entity") == 0), "IndexOf", databaseType, caseSensitivity); caseSensitivity = OutputResult(context.Books.Where(x => EF.Functions.Like(x.Title, NormalTitle)), "Like", databaseType, caseSensitivity); return(caseSensitivity); } }
public void TestCreateBookWithExistingAuthorsOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using var context = new EfCoreContext(options); context.Database.EnsureCreated(); context.AddRange(new Author { Name = "Author1" }, new Author { Name = "Author2" }); context.SaveChanges(); context.ChangeTracker.Clear(); //ATTEMPT var existingAuthor1 = context.Authors .Single(a => a.Name == "Author1"); var existingAuthor2 = context.Authors .Single(a => a.Name == "Author2"); var newBook = new Book() { Title = "My Book", //... other property settings left out //Set your AuthorsLink property to an empty collection AuthorsLink = new List <BookAuthor>() }; newBook.AuthorsLink.Add(new BookAuthor { Book = newBook, Author = existingAuthor1, Order = 0 }); newBook.AuthorsLink.Add(new BookAuthor { Book = newBook, Author = existingAuthor2, Order = 1 }); context.Add(newBook); context.SaveChanges(); context.ChangeTracker.Clear(); //VERIFY var checkBook = context.Books .Include(book => book.AuthorsLink) .ThenInclude(bookAuthor => bookAuthor.Author) .Single(); checkBook.AuthorsLink.Select(x => x.Author.Name).ShouldEqual(new[] { "Author1", "Author2" }); }
public static void SeedDummyOrder(this EfCoreContext context, DateTime orderDate = new DateTime()) { if (orderDate == new DateTime()) { orderDate = DateTime.Today; } var books = context.Books.ToList(); context.AddRange(BuildDummyOrder(DummyUserId, orderDate, books.First())); context.SaveChanges(); }
public async Task PaymentsClearedCorrectly() { // Arrange var paymentList = new List <Payment> { new Payment(DateTime.Now.AddDays(1), 100, PaymentType.Expense, new Account("Foo")), new Payment(DateTime.Now, 100, PaymentType.Expense, new Account("Foo")), new Payment(DateTime.Now.AddDays(-1), 100, PaymentType.Expense, new Account("Foo")) }; context.AddRange(paymentList); await context.SaveChangesAsync(); // Act await new ClearPaymentsCommand.Handler(contextAdapterMock.Object).Handle(new ClearPaymentsCommand(), default); // Assert paymentList[0].IsCleared.ShouldBeFalse(); paymentList[1].IsCleared.ShouldBeTrue(); paymentList[2].IsCleared.ShouldBeTrue(); }
private static void AddDummyOrders(this EfCoreContext context, List <Book> books = null) { if (books == null) { books = context.Books.ToList(); } var orders = new List <Order>(); var i = 0; foreach (var usersId in DummyUsersIds) { orders.Add(BuildDummyOrder(usersId, DateTime.UtcNow.AddDays(-10), books[i++])); orders.Add(BuildDummyOrder(usersId, DateTime.UtcNow, books[i++])); } context.AddRange(orders); }
public void TestCreateBookWithExistingTagsOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using var context = new EfCoreContext(options); context.Database.EnsureCreated(); context.AddRange(new Tag { TagId = "Tag1" }, new Tag { TagId = "Tag2" }); context.SaveChanges(); context.ChangeTracker.Clear(); //ATTEMPT var existingTag1 = context.Tags .Single(t => t.TagId == "Tag1"); var existingTag2 = context.Tags .Single(t => t.TagId == "Tag2"); var newBook = new Book() { Title = "My Book", //... other property settings left out //Set your Tags property to an empty collection Tags = new List <Tag>() }; newBook.Tags.Add(existingTag1); newBook.Tags.Add(existingTag2); context.Add(newBook); context.SaveChanges(); context.ChangeTracker.Clear(); //VERIFY var checkBook = context.Books .Include(book => book.Tags) .Single(); checkBook.Tags.Select(x => x.TagId).ShouldEqual(new [] { "Tag1", "Tag2" }); }
public async Task PaymentsClearedAndSaved() { // Arrange var payment = new Payment(DateTime.Now.AddDays(-1), 166, PaymentType.Expense, new Account("Foo")); payment.AddRecurringPayment(PaymentRecurrence.Daily); context.AddRange(payment); await context.SaveChangesAsync(); // Act await new CreateRecurringPaymentsCommand.Handler(contextAdapterMock.Object).Handle(new CreateRecurringPaymentsCommand(), default); var loadedPayments = context.Payments.ToList(); // Assert loadedPayments.Count.ShouldEqual(2); loadedPayments.ForEach(x => x.Amount.ShouldEqual(166)); }
private void CheckCaseSensitivity(DbContextOptions <EfCoreContext> options, string databaseType) { using (var context = new EfCoreContext(options)) { if (SetupDatabaseTrueIfNeedsNewData(context)) { //new database created, so seed var book1 = new Book { Title = NormalTitle }; var book2 = new Book { Title = "entity FRAMEWORK in action" }; context.AddRange(book1, book2); context.SaveChanges(); } void OutputResult(IQueryable <Book> query, string type) { var foundCount = query.Count(); var caseText = foundCount == 2 ? "INsensitive" : "sensitive"; _output.WriteLine($"{type} on {databaseType} is\t case-{caseText}. SQL created is:"); foreach (var line in query.ToQueryString().Split('\n').Select(x => x.Trim())) { _output.WriteLine(" " + line); } } //ATTEMPT OutputResult(context.Books.Where(x => x.Title == NormalTitle), "=="); OutputResult(context.Books.Where(x => x.Title.Equals(NormalTitle)), "Equals"); OutputResult(context.Books.Where(x => x.Title.StartsWith("Entity")), "StartsWith"); OutputResult(context.Books.Where(x => x.Title.EndsWith("Action")), "EndsWith"); OutputResult(context.Books.Where(x => x.Title.Contains("Framework")), "Contains"); OutputResult(context.Books.Where(x => x.Title.IndexOf("Entity") == 0), "IndexOf"); OutputResult(context.Books.Where(x => EF.Functions.Like(x.Title, NormalTitle)), "Like"); } }