/// <summary> /// Functie care insereaza un film in baza de date si verifica daca inserarea a avut loc cu succes. /// </summary> /// <param name="movie"></param> private static void InsertMovieAndCheckIfMovieExists(Movie movie) { // Deschid conexiunea in using. Conexiunea se inchide automat cand se termina blocul using. using (NpgsqlConnection npgsqlConnection = DatabaseHelpers.GetConn()) { // Inserez un film in baza de date InsertMovie(movie, npgsqlConnection); NpgsqlCommand npgsqlSelectCommand = SelectMovieCommand(movie, npgsqlConnection); // Deschid un sql reader si execut comanda de selectare a filmului using (NpgsqlDataReader npgsqlDataReader = npgsqlSelectCommand.ExecuteReader()) { // Daca un film in sql reader, exists = true, altfel exists = false. bool exists = npgsqlDataReader.Read(); // Daca filmul nu a fost selectat inseamna ca nu exista in baza de date if (!exists) { Assert.Fail(); } } } }
public void GetMovieByIdTest() { // Arrange - creez un film... Movie movie = new Movie { MovieId = Guid.NewGuid().ToString(), Title = "movie-name-getbyid-test", Description = "Movie-description-getbyid-test" }; //....si o inserez in baza de date. using (NpgsqlConnection npgsqlConnection = DatabaseHelpers.GetConn()) InsertMovie(movie, npgsqlConnection); // Act - execut functia din repository pe care vreau sa o testez: GetById. IMoviesRepository moviesRepository = new MoviesRepository(DatabaseHelpers.GetContext()); IResult <Movie> result = moviesRepository.GetById(movie.MovieId); // Assert - in functie de rezultat imi dau seama daca testul a trecut sau a picat switch (result) { case OK <Movie> ok: Assert.AreEqual(movie.MovieId, ok.Result.MovieId); Assert.AreEqual(movie.Title, ok.Result.Title); Assert.AreEqual(movie.Description, ok.Result.Description); break; case Error <Movie> error: Console.WriteLine(error.Message); break; default: throw new NotImplementedException(); } }