public async Task DeleteAsync_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); } this.request.Headers.IfMatch.Add(EntityTagHeaderValue.Parse("\"QUJDREVG\"")); foreach (Movie movie in movies) { this.context = new MovieContext(); EntityDomainManagerMock deleteDomainManager = new EntityDomainManagerMock(this); HttpResponseException ex = await AssertEx.ThrowsAsync <HttpResponseException>(async() => await deleteDomainManager.DeleteAsync(movie.Id)); Movie conflict; ex.Response.TryGetContentValue <Movie>(out conflict); Assert.Equal(HttpStatusCode.PreconditionFailed, ex.Response.StatusCode); Assert.Equal(movie.Version, conflict.Version); } }
public async Task UpdateAsync_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); } foreach (Movie movie in movies) { this.context = new MovieContext(); EntityDomainManagerMock updateDomainManager = new EntityDomainManagerMock(this); Delta <Movie> patch = new Delta <Movie>(); patch.TrySetPropertyValue("Category", UpdatedCategory); patch.TrySetPropertyValue("Version", Encoding.UTF8.GetBytes("Unknown")); HttpResponseException ex = await AssertEx.ThrowsAsync <HttpResponseException>(async() => await updateDomainManager.UpdateAsync(movie.Id, patch)); Movie conflict; ex.Response.TryGetContentValue <Movie>(out conflict); Assert.Equal(HttpStatusCode.Conflict, ex.Response.StatusCode); Assert.Equal(movie.Category, conflict.Category); Assert.Equal(movie.Version, conflict.Version); } }
public EntityDomainManagerTests() { this.config = new HttpConfiguration(); new MobileAppConfiguration().AddTables(new MobileAppTableConfiguration().AddEntityFramework()); var provider = new Mock <IMobileAppSettingsProvider>(); provider.Setup(p => p.GetMobileAppSettings()).Returns(new MobileAppSettingsDictionary()); this.config.SetMobileAppSettingsProvider(provider.Object); this.request = new HttpRequestMessage(HttpMethod.Get, new Uri("http://localhost")); this.request.SetConfiguration(this.config); this.context = new MovieContext(); this.manager = new EntityDomainManagerMock(this); this.manager.Reset(); }
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 MovieContext(); EntityDomainManagerMock replaceDomainManager = new EntityDomainManagerMock(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); } }