Пример #1
0
        public void Should_throw_exception_when_movie_is_not_found(
            IWebApiMovieRestContext context,
            DeleteMovieRequestHandler deleteMovieRequestHandler,
            DeleteMovieRequest deleteMovieRequest,
            Exception exception
            )
        {
            "Given a WebApiMovieRestContext"
            .Given(() => context = new WebApiMovieRestContext().AutoRollback());

            "And a DeleteMovieRequestHandler constructed with the context"
            .And(() => deleteMovieRequestHandler = new DeleteMovieRequestHandler(context));

            "And a DeleteMovieRequest containing the id of a Movie that does not exist".
            And(() => deleteMovieRequest = new DeleteMovieRequest()
            {
                MovieId = Guid.Empty
            });

            "When handling the DeleteMovieRequest"
            .When(() => exception = Record.Exception(() => deleteMovieRequestHandler.Handle(deleteMovieRequest)));

            "A ResourceNotFoundException should be thrown"
            .Then(() => exception.Should().BeOfType <ResourceNotFoundException>());

            "With a message indicating the id of the Movie that was not found"
            .Then(() => exception.Message.Should().Be("Movie[Id=00000000-0000-0000-0000-000000000000] could not be found"));
        }
Пример #2
0
        public void Should_return_all_genres_translated_to_a_genres_response(
            IWebApiMovieRestContext context,
            GetGenresRequestHandler getGenresRequestHandler,
            GetGenresRequest getGenreRequest,
            GenresResponse genresResponse
            )
        {
            "Given a WebApiMovieRestContext"
            .Given(() => context = new WebApiMovieRestContext().AutoRollback());

            "And a GetGenresRequestHandler constructed with the context"
            .And(() => getGenresRequestHandler = new GetGenresRequestHandler(context));

            "And a GetGenresRequest".
            And(() => getGenreRequest = new GetGenresRequest());

            "After handling the GetGenresRequest"
            .When(() => genresResponse = getGenresRequestHandler.Handle(getGenreRequest));

            "The GenresResponse should be all existing Genres in the database translated".Then(() =>
            {
                var existingGenresTranslated = new WebApiMovieRestInitializer()
                                               .SeedMovies()
                                               .SelectMany(movie => movie.Genres)
                                               .Distinct()
                                               .ToResponse();

                genresResponse.ShouldBeEquivalentTo(
                    existingGenresTranslated,
                    // do not compare ids
                    o => o.Excluding(x => x.PropertyInfo.Name == "Id")
                    );
            });
        }
Пример #3
0
        public void Should_delete_movie_when_movie_is_found(
            IWebApiMovieRestContext context,
            DeleteMovieRequestHandler deleteMovieRequestHandler,
            Movie newMovie,
            DeleteMovieRequest deleteMovieRequest
            )
        {
            "Given a WebApiMovieRestContext"
            .Given(() => context = new WebApiMovieRestContext().AutoRollback());

            "And a DeleteMovieRequestHandler constructed with the context"
            .And(() => deleteMovieRequestHandler = new DeleteMovieRequestHandler(context));

            "And a new Movie that has been inserted into the database"
            .And(() => newMovie = Db.InsertMovie(new MovieBuilder()));

            "And a DeleteMovieRequest containing the id of the newly inserted Movie".
            And(() => deleteMovieRequest = new DeleteMovieRequest()
            {
                MovieId = newMovie.Id
            });

            "After handling the DeleteMovieRequest"
            .When(() => deleteMovieRequestHandler.Handle(deleteMovieRequest));

            "And commiting the WebApiMovieRestContext"
            .When(() => context.Commit());

            "Should have deleted the movie from the database"
            .Then(() => Db.GetMovieById(newMovie.Id).Should().BeNull());
        }
        public void Should_return_genre_translated_to_a_genres_response_when_genre_is_found(
            IWebApiMovieRestContext context,
            GetGenreRequestHandler getGenreRequestHandler,
            Genre newGenre,
            GetGenreRequest getGenreRequest,
            GenresResponse genresResponse
            )
        {
            "Given a WebApiMovieRestContext"
            .Given(() => context = new WebApiMovieRestContext().AutoRollback());

            "And a GetGenreRequestHandler constructed with the context"
            .And(() => getGenreRequestHandler = new GetGenreRequestHandler(context));

            "And a new Genre that has been inserted into the database"
            .And(() => newGenre = Db.InsertGenre(new Genre("a new genre")));

            "And a GetGenreRequest containing the id of the newly inserted Genre".
            And(() => getGenreRequest = new GetGenreRequest()
            {
                GenreId = newGenre.Id
            });

            "After handling the GetGenreRequest"
            .When(() => genresResponse = getGenreRequestHandler.Handle(getGenreRequest));

            "The GenresResponse should be the newly inserted Genre translated"
            .Then(() => genresResponse.ShouldBeEquivalentTo(newGenre.Yield().ToResponse()));
        }
Пример #5
0
        public void Should_create_movie_and_use_existing_genre_if_genre_exists(
            IWebApiMovieRestContext context,
            CreateMovieRequestHandler createMovieRequestHandler,
            Genre alreadyExistingGenre,
            Movie newMovie,
            CreateMovieRequest createMovieRequest,
            MoviesResponse moviesResponse,
            Movie createdMovie
            )
        {
            "Given a WebApiMovieRestContext"
            .Given(() => context = new WebApiMovieRestContext().AutoRollback());

            "And a CreateMovieRequestHandler constructed with the context"
            .And(() => createMovieRequestHandler = new CreateMovieRequestHandler(context));

            "And a Genre that already exists in the database".And(() =>
            {
                var alreadyExistingGenreName = new WebApiMovieRestInitializer()
                                               .SeedMovies().First()
                                               .Genres.First()
                                               .Name;

                alreadyExistingGenre = Db.GetGenreByName(alreadyExistingGenreName);
            });

            "And a CreateMovieRequest containing a genre that exists in the database".
            And(() => createMovieRequest = new CreateMovieRequestBuilder().WithGenres(new[] { alreadyExistingGenre.Name }));

            "After handling the CreateMovieRequest"
            .When(() => moviesResponse = createMovieRequestHandler.Handle(createMovieRequest));

            "And commiting the WebApiMovieRestContext"
            .When(() => context.Commit());

            "Then a Movie should have be created in the database".Then(() =>
            {
                var createdMovieId = moviesResponse.Movies.First().Id;
                createdMovie       = Db.GetMovieById(createdMovieId);

                createdMovie.Should().NotBeNull();
            });

            "And the already existing Genre should have be used".Then(() =>
                                                                      createdMovie.Genres.Single().ShouldBeEquivalentTo(alreadyExistingGenre));

            "And the new movie should have the same values as the request"
            .Then(() => createdMovie.ShouldBeEquivalentTo(createMovieRequest, o => o.ExcludingMissingProperties()));

            "And the new movie should have the same genre as the request"
            .Then(() => createdMovie.Genres.Single().Name.Should().Be(createMovieRequest.Genres[0]));

            "And the MovieResponse should be the newly created Movie translated"
            .Then(() => moviesResponse.ShouldBeEquivalentTo(createdMovie.Yield().ToResponse()));
        }
 public UpdateMovieRequestHandler(IWebApiMovieRestContext context)
 {
     _context = context;
 }
Пример #7
0
 public GetGenreRequestHandler(IWebApiMovieRestContext context)
 {
     _context = context;
 }
Пример #8
0
        public void Should_update_movie_and_use_existing_genre_if_genre_exists(
            IWebApiMovieRestContext context,
            UpdateMovieRequestHandler updateMovieRequestHandler,
            Genre alreadyExistingGenre,
            Movie newMovie,
            UpdateMovieRequest updateMovieRequest,
            MoviesResponse moviesResponse,
            Movie updatedMovie
            )
        {
            "Given a WebApiMovieRestContext"
            .Given(() => context = new WebApiMovieRestContext().AutoRollback());

            "And a UpdateMovieRequestHandler constructed with the context"
            .And(() => updateMovieRequestHandler = new UpdateMovieRequestHandler(context));

            "And a new Movie that has been inserted into the database".And(() =>
            {
                newMovie = new MovieBuilder().WithGenres(new[] { new Genre("new genre") });

                using (var dbContext = new WebApiMovieRestContext())
                {
                    dbContext.Movies.Add(newMovie);
                    dbContext.SaveChanges();
                }
            });

            "And a Genre that already exists in the database".And(() =>
            {
                var alreadyExistingGenreName = new WebApiMovieRestInitializer()
                                               .SeedMovies().First()
                                               .Genres.First()
                                               .Name;

                using (var dbContext = new WebApiMovieRestContext())
                {
                    alreadyExistingGenre = dbContext.Genres.Single(genre => genre.Name == alreadyExistingGenreName);
                }
            });

            "And a UpdateMovieRequest containing the id of the newly inserted Movie and genre that exists in the database".
            And(() => updateMovieRequest = new UpdateMovieRequestBuilder()
                                           .WithMovieId(newMovie.Id)
                                           .WithDirector("new director")
                                           .WithGenres(new[] { alreadyExistingGenre.Name })
                                           .WithImdbId("newimdbid")
                                           .WithPlot("new plot")
                                           .WithRating(0.1m)
                                           .WithReleaseDate(new DateTime(2000, 01, 01))
                                           .WithTitle("new title"));

            "After handling the UpdateMovieRequest"
            .When(() => moviesResponse = updateMovieRequestHandler.Handle(updateMovieRequest));

            "And commiting the WebApiMovieRestContext"
            .When(() => context.Commit());

            "And retrieving the updated Movie".When(() =>
            {
                using (var dbContext = new WebApiMovieRestContext())
                {
                    updatedMovie = dbContext.Movies
                                   .Include(movie => movie.Genres)
                                   .SingleOrDefault(movie => movie.Id == newMovie.Id);
                }
            });

            "Then the already existing Genre should have be used".Then(() =>
                                                                       updatedMovie.Genres.Single().ShouldBeEquivalentTo(alreadyExistingGenre));

            "And the new movie should have the same values as the request"
            .Then(() => updatedMovie.ShouldBeEquivalentTo(updateMovieRequest, o => o.ExcludingMissingProperties()));

            "And the new movie should have the same genre as the request"
            .Then(() => updatedMovie.Genres.Single().Name.Should().Be(updateMovieRequest.Genres[0]));

            "And the MovieResponse should be the newly updated Movie translated"
            .Then(() => moviesResponse.ShouldBeEquivalentTo(updatedMovie.Yield().ToResponse()));
        }