public async Task ReplaceAsync_ReplacesData() { // Arrange const string Category = "你好世界"; Collection <Movie> movies = new Collection <Movie>(); foreach (Movie movie in TestData.Movies) { Movie insertedMovie = await this.manager.InsertAsync(movie); movies.Add(insertedMovie); } // Create new context to avoid an exception saying: Attaching an entity of type 'Microsoft.Azure.Mobile.Server.TestModels.MovieModel' // failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or // setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because // some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity // state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate. // This condition won't apply when running the service as you always get a new context. this.context = new MovieModelContext(); MappedEntityDomainManagerMock replaceDomainManager = new MappedEntityDomainManagerMock(this); foreach (Movie movie in movies) { movie.Category = Category; Movie result = await replaceDomainManager.ReplaceAsync(movie.Id, movie); Assert.Equal(Category, result.Category); } }
public async Task ReplaceAsync_Throws_Conflict_IfVersionMismatch() { // Arrange Collection <Movie> movies = new Collection <Movie>(); foreach (Movie movie in TestData.Movies) { Movie insertedMovie = await this.manager.InsertAsync(movie); movies.Add(insertedMovie); } // Create new context to avoid an exception saying: Attaching an entity of type 'Microsoft.Azure.Mobile.Server.TestModels.MovieModel' // failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or // setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because // some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity // state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate. // This condition won't apply when running the service as you always get a new context. foreach (Movie movie in movies) { this.context = new MovieModelContext(); MappedEntityDomainManagerMock replaceDomainManager = new MappedEntityDomainManagerMock(this); string originalCategory = movie.Category; byte[] originalVersion = movie.Version; movie.Category = UpdatedCategory; movie.Version = Encoding.UTF8.GetBytes("Unknown"); HttpResponseException ex = await AssertEx.ThrowsAsync <HttpResponseException>(async() => await replaceDomainManager.ReplaceAsync(movie.Id, movie)); Movie conflict; ex.Response.TryGetContentValue <Movie>(out conflict); Assert.Equal(HttpStatusCode.Conflict, ex.Response.StatusCode); Assert.Equal(originalCategory, conflict.Category); Assert.Equal(originalVersion, conflict.Version); } }