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 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 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"); } }
private async Task AddDummyCosmosBook(int bookId) { var dummyCosmosBook = new CosmosBook { BookId = bookId, Title = "dummy" }; _cosmosContext.Add(dummyCosmosBook); await _cosmosContext.SaveChangesAsync(); _cosmosContext.ChangeTracker.Clear(); }
public virtual async Task <T> AddAsync <T>(T item, CancellationToken token = default) where T : class { using (var context = new CosmosDbContext(_settings)) { context.Add(item); if (await context.SaveChangesAsync(token) == 1) { return(item); } ; throw new DbAddFailedException(); } }
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'."); } }
public async Task AddCosmosBookAsync(int bookId) { if (CosmosNotConfigured) { return; } var cosmosBook = await MapBookToCosmosBookAsync(bookId); if (cosmosBook != null) { _cosmosContext.Add(cosmosBook); await CosmosSaveChangesWithChecksAsync (WhatDoing.Adding, bookId); } else { await DeleteCosmosBookAsync(bookId); } }