public async Task CreateMovieWithValidInputData() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "MoviesDbTest").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfRepository <Movie>(dbContext); var service = new MovieService(repository); var inputModel = new MovieInputModel { Title = "Titanic", Actors = "Test actor, test actor, test actor", Country = MegaCinema.Data.Models.Enums.Country.USA, Description = "test description test description", Director = "John West", Duration = new System.TimeSpan(1, 35, 33), Genre = GenreType.Adventure, Language = MegaCinema.Data.Models.Enums.Language.English, Poster = "http://testposter.com", Rating = MPAARating.G, ReleaseDate = new System.DateTime(2020, 2, 15), Score = 4.5, Trailer = "sometext", }; var id = await service.CreateMovie(inputModel); var movie = repository.All().FirstOrDefault(x => x.Id == id); Assert.True(movie.Title == "Titanic"); Assert.True(movie.Director == "John West"); Assert.True(movie.Score == 4.5); }
public void CreateMovie_ShouldCallUnitOfWorkCommitMethod_WhenValidParametersPassed() { var title = "Star Wars"; var year = 2001; var length = 136; var movieRepositoryMock = new Mock <IRepository <Movie> >(); var unitOfWorkMock = new Mock <IUnitOfWork>(); var movieFactoryMock = new Mock <IMovieFactory>(); var producerServiceMock = new Mock <IProducerService>(); var studioServiceMock = new Mock <IStudioService>(); var genreServiceMock = new Mock <IGenreService>(); var bookServiceMock = new Mock <IBookService>(); var starServiceMock = new Mock <IStarService>(); var movieService = new MovieService(movieRepositoryMock.Object, unitOfWorkMock.Object, movieFactoryMock.Object, producerServiceMock.Object, studioServiceMock.Object, genreServiceMock.Object, bookServiceMock.Object, starServiceMock.Object); var genres = new List <string>() { "Adventure" }; var stars = new List <string>() { "Harrison Ford" }; movieService.CreateMovie(title, year, null, length, null, null, null, genres, stars); unitOfWorkMock.Verify(n => n.Commit(), Times.Once); }
public void CreateMovie_ShouldCallCreateBookMethod_WhenBookIsNotFound() { var title = "Star Wars"; var bookTitle = "some title"; var year = 2001; var length = 136; var movieRepositoryMock = new Mock <IRepository <Movie> >(); var unitOfWorkMock = new Mock <IUnitOfWork>(); var movieFactoryMock = new Mock <IMovieFactory>(); var producerServiceMock = new Mock <IProducerService>(); var studioServiceMock = new Mock <IStudioService>(); var genreServiceMock = new Mock <IGenreService>(); var bookServiceMock = new Mock <IBookService>(); var starServiceMock = new Mock <IStarService>(); bookServiceMock.Setup(s => s.GetBookByTitle(bookTitle)).Returns((Book)null); var movieService = new MovieService(movieRepositoryMock.Object, unitOfWorkMock.Object, movieFactoryMock.Object, producerServiceMock.Object, studioServiceMock.Object, genreServiceMock.Object, bookServiceMock.Object, starServiceMock.Object); var genres = new List <string>() { "Adventure" }; var stars = new List <string>() { "Harrison Ford" }; movieService.CreateMovie(title, year, null, length, null, null, bookTitle, genres, stars); bookServiceMock.Verify(s => s.CreateBook(bookTitle, null, null), Times.Once); }
public void CreateMovie_ShouldCallCreateStarMethod_WhenStarIsNotFound() { var title = "Star Wars"; var year = 2001; var length = 136; var movieRepositoryMock = new Mock <IRepository <Movie> >(); var unitOfWorkMock = new Mock <IUnitOfWork>(); var movieFactoryMock = new Mock <IMovieFactory>(); var producerServiceMock = new Mock <IProducerService>(); var studioServiceMock = new Mock <IStudioService>(); var genreServiceMock = new Mock <IGenreService>(); var bookServiceMock = new Mock <IBookService>(); var starServiceMock = new Mock <IStarService>(); starServiceMock.Setup(s => s.GetStarByName("Harrison", "Ford")).Returns((Star)null); var movieService = new MovieService(movieRepositoryMock.Object, unitOfWorkMock.Object, movieFactoryMock.Object, producerServiceMock.Object, studioServiceMock.Object, genreServiceMock.Object, bookServiceMock.Object, starServiceMock.Object); var genres = new List <string>() { "Adventure" }; var stars = new List <string>() { "Harrison Ford" }; movieService.CreateMovie(title, year, null, length, null, null, null, genres, stars); starServiceMock.Verify(s => s.CreateStar(It.IsAny <string>(), It.IsAny <string>(), null, null), Times.Exactly(1)); }
public void CreateMovie_ShouldCallGetGenreByMethod_WhenValidParametersPassed() { var title = "Star Wars"; var year = 2001; var length = 136; var movieRepositoryMock = new Mock <IRepository <Movie> >(); var unitOfWorkMock = new Mock <IUnitOfWork>(); var movieFactoryMock = new Mock <IMovieFactory>(); var producerServiceMock = new Mock <IProducerService>(); var studioServiceMock = new Mock <IStudioService>(); var genreServiceMock = new Mock <IGenreService>(); var bookServiceMock = new Mock <IBookService>(); var starServiceMock = new Mock <IStarService>(); var genre = new Genre("Adventure"); genreServiceMock.Setup(s => s.GetGenreBy(It.IsAny <string>())).Returns(genre); var movieService = new MovieService(movieRepositoryMock.Object, unitOfWorkMock.Object, movieFactoryMock.Object, producerServiceMock.Object, studioServiceMock.Object, genreServiceMock.Object, bookServiceMock.Object, starServiceMock.Object); var genres = new List <string>() { "Adventure" }; var stars = new List <string>() { "Harrison Ford" }; movieService.CreateMovie(title, year, null, length, null, null, null, genres, stars); genreServiceMock.Verify(s => s.GetGenreBy(It.IsAny <string>()), Times.Exactly(genres.Count)); }
public async Task MovieCountShouldReturnCorrectumber() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "MoviesDbTest12").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfRepository <Movie>(dbContext); var service = new MovieService(repository); await service.CreateMovie(new MovieInputModel { Title = "TestTitle" }); await service.CreateMovie(new MovieInputModel { Title = "Test123" }); await service.CreateMovie(new MovieInputModel { Id = 12 }); Assert.True(service.MoviesCount() == 3); }
public async Task MoviesCountShouldReturnMovieInDataBaseUsingDbContext() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "MoviesDbTest").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfRepository <Movie>(dbContext); var service = new MovieService(repository); await service.CreateMovie(new MovieInputModel { Id = 1 }); await service.CreateMovie(new MovieInputModel { Id = 2 }); await service.CreateMovie(new MovieInputModel { Id = 3 }); await service.CreateMovie(new MovieInputModel { Id = 4 }); Assert.Equal(4, service.MoviesCount()); }
public void CreateMovie_ShouldReturnCorrectMovie_WhenValidParametersPassed() { var title = "Star Wars"; var year = 2001; var length = 136; var genres = new List <string>() { "Adventure" }; var stars = new List <string>() { "Harrison Ford" }; var movieRepositoryMock = new Mock <IRepository <Movie> >(); var unitOfWorkMock = new Mock <IUnitOfWork>(); var movieFactoryMock = new Mock <IMovieFactory>(); var producerServiceMock = new Mock <IProducerService>(); var studioServiceMock = new Mock <IStudioService>(); var genreServiceMock = new Mock <IGenreService>(); var bookServiceMock = new Mock <IBookService>(); var starServiceMock = new Mock <IStarService>(); var genresObjects = new List <Genre>() { new Genre("Adventure") }; var starsObjects = new List <Star>() { new Star("Harrison", "Ford", null, null) }; var producer = new Producer("George Lucas"); producerServiceMock.Setup(s => s.CreateProducer("George Lucas")).Returns(producer); var book = new Book("Star Wars", null, null); bookServiceMock.Setup(s => s.CreateBook("Star Wars", null, null)).Returns(book); var studio = new Studio("Twentieth Century", null); studioServiceMock.Setup(s => s.CreateStudio("Twentieth Century", null)).Returns(studio); starServiceMock.Setup(s => s.GetStarByName("Harrison", "Ford")).Returns(starsObjects[0]); genreServiceMock.Setup(s => s.GetGenreBy("Adventure")).Returns(genresObjects[0]); var movie = new Movie(title, year, null, length, producer, studio, book, genresObjects, starsObjects); movieFactoryMock.Setup(f => f.CreateMovie(title, year, null, length, producer, studio, book, genresObjects, starsObjects)).Returns(movie); var movieService = new MovieService(movieRepositoryMock.Object, unitOfWorkMock.Object, movieFactoryMock.Object, producerServiceMock.Object, studioServiceMock.Object, genreServiceMock.Object, bookServiceMock.Object, starServiceMock.Object); var createdMovie = movieService.CreateMovie(title, year, null, length, "George Lucas", "Twentieth Century", "Star Wars", genres, stars); Assert.AreSame(movie, createdMovie); }
public IHttpActionResult PostMovie([FromBody] Movie movie) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _movieService.CreateMovie(movie); return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Create(Movie movie) { try { MovieService movieService = new MovieService(); movieService.CreateMovie(movie); return(RedirectToAction(nameof(Index))); } catch { return(View()); } }
public async Task MovieDeleteByIdShouldReturnCorrectData() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "MoviesDbTest132").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfRepository <Movie>(dbContext); var service = new MovieService(repository); await service.CreateMovie(new MovieInputModel { Title = "TestTitle", Id = 1, }); await service.CreateMovie(new MovieInputModel { Title = "Test123", Id = 2, }); await service.CreateMovie(new MovieInputModel { Title = "Some Movie", Id = 3 }); await service.DeleteById(3); await service.DeleteById(1); await service.DeleteById(2); Assert.True(service.MoviesCount() == 0); }
public async Task DeleteMovieShouldRemoveMovieFromRepository() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "MoviesDbTest12").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfRepository <Movie>(dbContext); var service = new MovieService(repository); await service.CreateMovie(new MovieInputModel { Title = "TestTitle", Id = 1, }); await service.CreateMovie(new MovieInputModel { Title = "Test123", Id = 2, }); await service.CreateMovie(new MovieInputModel { Title = "AnotherTitle", Id = 12 }); await service.DeleteById(2); await service.DeleteById(1); Assert.False(service.MovieTitleExists("Test123")); Assert.False(service.MovieTitleExists("estTitle")); Assert.True(service.MoviesCount() == 1); }
public void CreateMovie_calls_dbset_add_and_saves_changes_to_context() { //Arrange Movie movieToBeAdded = _fixture.CreateAutoFilledObject <Movie>(); Mock <DbSet <Movie> > mockSet = _fixture.CreateManyMoqDbSet <Movie>(5); _mockContext.Setup(c => c.Movies) .Returns(mockSet.Object); var service = new MovieService(_mockContext.Object); //Act service.CreateMovie(movieToBeAdded); //Assert mockSet.Verify(m => m.Add(It.IsAny <Movie>()), Times.Once()); _mockContext.Verify(m => m.SaveChanges(), Times.Once()); }
public ActionResult Create(MovieCreate model) { if (!ModelState.IsValid) { ModelState.AddModelError("", "Could not add movie, please make sure all fields are filled"); return(View(model)); } MovieService service = new MovieService(); if (service.CreateMovie(model)) { return(RedirectToAction("AllMovies")); } return(View(model)); }
public ActionResult Create(MovieCreate model) { if (!ModelState.IsValid) { return(View(model)); } //var userId = Guid.Parse(User.Identity.GetUserId()); var service = new MovieService(); if (service.CreateMovie(model)) { TempData["SaveResult"] = "Your Movie was Created"; return(RedirectToAction("Index")); } return(View(model)); }
public async Task TestCreateMovieShouldAddMovieToRepository() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "MoviesDbTest135").Options; var dbContext = new ApplicationDbContext(options); var repository = new EfRepository <Movie>(dbContext); var service = new MovieService(repository); var result = await service.CreateMovie(this.MovieInputModelTest()); var movie = await repository.All().FirstOrDefaultAsync(x => x.Id == result); Assert.Equal("Title test", movie.Title); Assert.Equal(7.0, movie.Score); Assert.Equal("actors test model", movie.Actors); Assert.Equal(1, result); Assert.True(service.MovieExist(1)); Assert.Equal(1, service.MoviesCount()); }
public void SucessfullyCreateMovie_WhenInvokedWithValidParameters() { // Arrange var movieDtoArgument = new MovieDto(); this.movieRepoMock.Setup(x => x.All) .Returns(new List <Movie> { new Movie() }.AsQueryable()); var sut = new MovieService(mapperMock.Object, movieRepoMock.Object, dataSaverMock.Object, configurationMock.Object, jsonExporterProviderMock.Object); // Act var result = sut.CreateMovie(movieDtoArgument); // Assert Assert.IsTrue(result); }