private async Task <int> WriteToCosmosThreeWays(int i) { int numToAdd = 100; var directBooks = BookTestData.CreateDummyBooks(numToAdd).ToList() .Select(x => new DirectCosmosBook(i++, x)).ToList(); //ATTEMPT using (new TimeThings(_output, "Direct", numToAdd)) { foreach (var book in directBooks) { var response = await _cosmosContainer.CreateItemAsync(book); } } using (new TimeThings(_output, "SQL", numToAdd)) { _sqlContext.AddRange(BookTestData.CreateDummyBooks(numToAdd)); await _sqlContext.SaveChangesAsync(); } var cBooks = BookTestData.CreateDummyBooks(numToAdd).AsQueryable() .MapBookToCosmosBook().ToList(); cBooks.ForEach(x => x.BookId = i++); using (new TimeThings(_output, "Via EfCore", numToAdd)) { _cosmosContext.AddRange(cBooks); await _cosmosContext.SaveChangesAsync(); } return(i); }
public async Task TestFromSqlRaw() { //SETUP var options = this.GetCosmosDbOptions <CosmosDbContext>(); using var context = new CosmosDbContext(options); await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); var book1 = new CosmosBook { BookId = 123, Title = "Year2000", PublishedOn = new DateTime(2000, 1, 1), YearPublished = 2000 }; var book2 = new CosmosBook { BookId = 567, Title = "Year3000", PublishedOn = new DateTime(3000, 1, 1), YearPublished = 3000 }; context.AddRange(book1, book2); await context.SaveChangesAsync(); //ATTEMPT var list = await context.Books.FromSqlRaw("SELECT * FROM c ORDER BY c.BookId").ToListAsync(); //VERIFY list.Select(x => x.BookId).ShouldEqual(new[] { 123, 567 }); //WRONG IN EF CORE 5 }
public async Task TestNullableIntWhereOk() { //SETUP var options = this.GetCosmosDbToEmulatorOptions <CosmosDbContext>(); using (var context = new CosmosDbContext(options)) { await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); var cBook1 = new CosmosBook { CosmosBookId = 1, NullableInt = null }; var cBook2 = new CosmosBook { CosmosBookId = 2, NullableInt = 1 }; context.AddRange(cBook1, cBook2); await context.SaveChangesAsync(); } using (var context = new CosmosDbContext(options)) { //ATTEMPT var book = await context.Books.SingleOrDefaultAsync(x => x.NullableInt > 0); //VERIFY book.CosmosBookId.ShouldEqual(2); } }
public async Task TestStringWithNullOrderByOk() { //SETUP var options = this.GetCosmosDbToEmulatorOptions <CosmosDbContext>(); using (var context = new CosmosDbContext(options)) { await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); var cBook1 = new CosmosBook { CosmosBookId = 1, }; var cBook2 = new CosmosBook { CosmosBookId = 2, Title = "Book2" }; context.AddRange(cBook1, cBook2); await context.SaveChangesAsync(); } using (var context = new CosmosDbContext(options)) { //ATTEMPT var ex = await Assert.ThrowsAsync <NotSupportedException>(async() => await context.Books.OrderBy(x => x.Title).ToListAsync()); //VERIFY ex.Message.ShouldStartWith("Cannot execute cross partition order-by queries on mix types. " + "Consider using IS_STRING/IS_NUMBER to get around this exception."); } }
public async Task TestNullableIntOk() { //SETUP var options = this.GetCosmosDbToEmulatorOptions <CosmosDbContext>(); using (var context = new CosmosDbContext(options)) { await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); var cBook1 = new CosmosBook { CosmosBookId = 1, NullableInt = null }; var cBook2 = new CosmosBook { CosmosBookId = 2, NullableInt = 1 }; context.AddRange(cBook1, cBook2); await context.SaveChangesAsync(); } using (var context = new CosmosDbContext(options)) { //ATTEMPT var cBook1 = await context.Books.FindAsync(1); var cBook2 = await context.Books.FindAsync(2); //VERIFY cBook1.NullableInt.ShouldBeNull(); cBook2.NullableInt.ShouldEqual(1); } }
private async ValueTask OptionalCosmosWriteAsync(List <Book> books, bool wipeDatabase = false) { if (!IncludeCosmosDb) { return; } using var cosmosContext = new CosmosDbContext(_cosmosOptions); if (wipeDatabase) { await cosmosContext.Database.EnsureDeletedAsync(); await cosmosContext.Database.EnsureCreatedAsync(); } var cosmosBooks = books.Select(b => new CosmosBook { BookId = b.BookId, Title = b.Title, PublishedOn = b.PublishedOn, EstimatedDate = b.EstimatedDate, YearPublished = b.PublishedOn.Year, OrgPrice = b.OrgPrice, ActualPrice = b.ActualPrice, PromotionalText = b.PromotionalText, ManningBookUrl = b.ManningBookUrl, AuthorsOrdered = string.Join(", ", b.AuthorsLink .OrderBy(q => q.Order) .Select(q => q.Author.Name)), ReviewsCount = b.Reviews?.Count ?? 0, ReviewsAverageVotes = b.Reviews != null && b.Reviews.Any() ? b.Reviews.Average(y => y.NumStars) : 0.0, Tags = b.Tags .Select(x => new CosmosTag(x.TagId)).ToList(), TagsString = $"| {string.Join(" | ", b.Tags.Select(x => x.TagId))} |" }); cosmosContext.AddRange(cosmosBooks); await cosmosContext.SaveChangesAsync(); }
[Fact] public async Task TestFilterCosmosBookByTagsEfCoreViaTagsStringOk() { //SETUP var options = this.GetCosmosDbOptions <CosmosDbContext>(); using var context = new CosmosDbContext(options); await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); var book1 = new CosmosBook { BookId = 123, Title = "Test1", Tags = new List <CosmosTag> { new CosmosTag("Tag1"), new CosmosTag("Tag2") }, TagsString = "| Tag1 | Tag2 |" }; var book2 = new CosmosBook { BookId = 567, Title = "Test2", Tags = new List <CosmosTag> { new CosmosTag("Tag3"), new CosmosTag("Tag2") }, TagsString = "| Tag3 | Tag2 |" }; context.AddRange(book1, book2); await context.SaveChangesAsync(); //ATTEMPT context.ChangeTracker.Clear(); var books = await context.Books.Where(x => x.TagsString.Contains("| Tag3 |")).ToListAsync(); //VERIFY books.Count.ShouldEqual(1); books.Single().Title.ShouldEqual("Test2"); }
public async Task TestFilterCosmosBookByTagsEfCoreBad() { //SETUP var options = this.GetCosmosDbOptions <CosmosDbContext>(); using var context = new CosmosDbContext(options); await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); var book1 = new CosmosBook { BookId = 123, Title = "Test", Tags = new List <CosmosTag> { new CosmosTag("Tag1"), new CosmosTag("Tag2") } }; var book2 = new CosmosBook { BookId = 567, Title = "Test", Tags = new List <CosmosTag> { new CosmosTag("Tag3"), new CosmosTag("Tag2") } }; context.AddRange(book1, book2); await context.SaveChangesAsync(); //ATTEMPT context.ChangeTracker.Clear(); var ex = await Assert.ThrowsAsync <InvalidOperationException>(async() => await context.Books.Where(x => x.Tags .Select(y => y.TagId).Contains("Tag3")).ToListAsync()); //VERIFY ex.Message.ShouldContain("could not be translated."); }
public async Task TestFilterDropdownYears() { //SETUP var options = this.GetCosmosDbOptions <CosmosDbContext>(); using var context = new CosmosDbContext(options); await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); var book1 = new CosmosBook { BookId = 123, Title = "Year2000", PublishedOn = new DateTime(2000, 1, 1), YearPublished = 2000 }; var book2 = new CosmosBook { BookId = 567, Title = "Year3000", PublishedOn = new DateTime(3000, 1, 1), YearPublished = 3000 }; context.AddRange(book1, book2); await context.SaveChangesAsync(); var service = new CosmosEfBookFilterDropdownService(context, new BookAppSettings { CosmosDatabaseName = GetType().Name }, null); //ATTEMPT var dropdown = await service.GetFilterDropDownValuesAsync(BooksFilterBy.ByPublicationYear); //VERIFY dropdown.Select(x => x.Text).ToArray().ShouldEqual(new [] { "Coming Soon", "2000" }); }
public async Task TestAnyOk() { //SETUP var options = this.GetCosmosDbToEmulatorOptions <CosmosDbContext>(); using (var context = new CosmosDbContext(options)) { await context.Database.EnsureCreatedAsync(); var cBook1 = new CosmosBook { CosmosBookId = 1, Title = "Book1" }; var cBook2 = new CosmosBook { CosmosBookId = 2, Title = "Book2" }; context.AddRange(cBook1, cBook2); //ATTEMPT var ex = await Assert.ThrowsAsync <CosmosException>(async() => await context.SaveChangesAsync()); //VERIFY ex.Message.ShouldContain("Resource with specified id or name already exists."); } }