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 MovieModelContext();
                MappedEntityDomainManagerMock deleteDomainManager = new MappedEntityDomainManagerMock(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 MovieModelContext();
                MappedEntityDomainManagerMock updateDomainManager = new MappedEntityDomainManagerMock(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 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 MappedEntityDomainManagerTests()
        {
            new MobileAppConfiguration()
            .AddTables(
                new MobileAppTableConfiguration()
                .AddEntityFramework());

            this.config = new HttpConfiguration();

            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 MovieModelContext();
            this.manager = new MappedEntityDomainManagerMock(this);
            this.manager.Reset();

            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <MovieModel, Movie>();

                cfg.CreateMap <Movie, MovieModel>();
            });
        }
        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);
            }
        }