public async Task TestAddCosmosBookHowToUpdateOk() { //SETUP var options = this.GetCosmosDbToEmulatorOptions <CosmosDbContext>(); using (var context = new CosmosDbContext(options)) { await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); var cBook = new CosmosBook { CosmosBookId = 1, Title = "Book1" }; context.Add(cBook); await context.SaveChangesAsync(); } using (var context = new CosmosDbContext(options)) { //ATTEMPT var cBook = await context.Books.FindAsync(1); //You must read to get the "id" cBook.Title = "Book2"; await context.SaveChangesAsync(); } using (var context = new CosmosDbContext(options)) { //VERIFY context.Books.Find(1).Title.ShouldEqual("Book2"); } }
public async Task TestAccessCosmosEmulatorUniqueToMethod() { //SETUP var options = this.CreateUniqueMethodCosmosDbEmulator <CosmosDbContext>(); using var context = new CosmosDbContext(options); await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); //ATTEMPT var book = new CosmosBook { BookId = 123, Title = "Test", Tags = new List <CosmosTag> { new CosmosTag("Tag1"), new CosmosTag("Tag2") } }; context.Add(book); await context.SaveChangesAsync(); //VERIFY context.ChangeTracker.Clear(); var readBook = await context.Books.SingleAsync(); readBook.BookId.ShouldEqual(123); readBook.Tags.Select(x => x.TagId).ShouldEqual(new[] { "Tag1", "Tag2" }); }
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 TestAddCosmosBookWithReviewsOk() { //SETUP var options = this.GetCosmosDbToEmulatorOptions <CosmosDbContext>(); using var context = new CosmosDbContext(options); await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); //ATTEMPT var cBook = new CosmosBook { CosmosBookId = 1, //NOTE: You have to provide a key value! Title = "Book Title", PublishedDate = new DateTime(2019, 1, 1), Reviews = new List <CosmosReview> { new CosmosReview { Comment = "Great!", NumStars = 5, VoterName = "Reviewer1" }, new CosmosReview { Comment = "Hated it", NumStars = 1, VoterName = "Reviewer2" } } }; context.Add(cBook); await context.SaveChangesAsync(); //VERIFY (await context.Books.FindAsync(1)).Reviews.Count.ShouldEqual(2); }
public async Task TestAddCosmosBookAddSameKeyOk() { //SETUP var options = this.GetCosmosDbToEmulatorOptions <CosmosDbContext>(); using (var context = new CosmosDbContext(options)) { await context.Database.EnsureDeletedAsync(); await context.Database.EnsureCreatedAsync(); var cBook = new CosmosBook { CosmosBookId = 1, Title = "Book1" }; context.Add(cBook); await context.SaveChangesAsync(); } using (var context = new CosmosDbContext(options)) { //ATTEMPT var cBook = new CosmosBook { CosmosBookId = 1, Title = "Book2" }; context.Add(cBook); context.Entry(cBook).State.ShouldEqual(EntityState.Added); var ex = await Assert.ThrowsAsync <CosmosException>(async() => await context.SaveChangesAsync()); //VERIFY ex.Message.ShouldContain("Resource with specified id or name already exists."); } using (var context = new CosmosDbContext(options)) { context.Books.Find(1).Title.ShouldEqual("Book1"); } }
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 Task AddDummyCosmosBook(int bookId) { var dummyCosmosBook = new CosmosBook { BookId = bookId, Title = "dummy" }; _cosmosContext.Add(dummyCosmosBook); await _cosmosContext.SaveChangesAsync(); _cosmosContext.ChangeTracker.Clear(); }
/*************************************************************** #A This method is called by the BookUpdated event handler, with the BookId of the SQL book #B The Book App can be run without access to Cosmos DB, in which case it exits immediately #C This method uses a Select method similar to the one used in chapter 2, to a CosmosBook entity class #D If the CosmosBook is successfully filled, then it executes the Cosmos update code #E This updates the CosmosBook to the cosmosContext and then calls a method to save it to the database #F If the SQL book wasn't found we ensure the Cosmos database version was removed ***************************************************************/ public async Task DeleteCosmosBookAsync(int bookId) { if (CosmosNotConfigured) { return; } var cosmosBook = new CosmosBook { BookId = (int)bookId }; _cosmosContext.Remove(cosmosBook); await CosmosSaveChangesWithChecksAsync(WhatDoing.Deleting, bookId); }
public async Task TestAddCosmosBookHaveToSetKeyOk() { //SETUP var options = this.GetCosmosDbToEmulatorOptions <CosmosDbContext>(); using (var context = new CosmosDbContext(options)) { await context.Database.EnsureCreatedAsync(); //ATTEMPT var cBook = new CosmosBook { CosmosBookId = 0, Title = "Book" }; var ex = Assert.Throws <NotSupportedException>(() => context.Add(cBook)); //VERIFY ex.Message.ShouldStartWith("The 'CosmosBookId' on entity type 'CosmosBook' does not have a value set and no value generator is available for properties of type 'int'."); } }
[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."); } }