public void Should_return_all_movie_translated_to_a_movies_response_when_no_genre_specified_in_request( IWebApiMovieRestContext context, GetMoviesRequestHandler getMoviesRequestHandler, GetMoviesRequest getMovieRequest, MoviesResponse moviesResponse ) { "Given a WebApiMovieRestContext" .Given(() => context = new WebApiMovieRestContext().AutoRollback()); "And a GetMoviesRequestHandler constructed with the context" .And(() => getMoviesRequestHandler = new GetMoviesRequestHandler(context)); "And a GetMoviesRequest with no genre specified". And(() => getMovieRequest = new GetMoviesRequest()); "After handling the GetMoviesRequest" .When(() => moviesResponse = getMoviesRequestHandler.Handle(getMovieRequest)); "The MovieResponse should be all existing Movies in the database translated".Then(() => { var existingMoviesTranslated = new WebApiMovieRestInitializer() .SeedMovies() .ToResponse(); moviesResponse.ShouldBeEquivalentTo( existingMoviesTranslated, // do not compare ids or links (as ids will be different) o => o.Excluding(x => x.PropertyInfo.Name == "Id" || x.PropertyInfo.Name == "Links") ); }); }
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") ); }); }
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 void Should_seed_database_on_initialization( WebApiMovieRestContext context ) { "Given a WebApiMovieRestContext" .Given(() => context = new WebApiMovieRestContext().Using()); "When initializing" .When(() => context.Database.Initialize(false)); "Should seed the database" .Then(() => { var expectedMovies = new WebApiMovieRestInitializer().SeedMovies(); var retrievedMovies = context.Movies .Include(movie => movie.Genres) .ToList(); ShouldBeEquivalent(retrievedMovies, expectedMovies); }); }
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())); }