Ejemplo n.º 1
0
def test_properties():
    movie_entity = MovieEntity()

    movie_entity.load_from_data(mock_data)

    assert movie_entity.separated_directors == [
        'Andrew Adamson', 'Vicky Jenson'
    ]
    assert movie_entity.separated_countries == ["USA", 'Foo']
    assert movie_entity.separated_genres == [
        "Animation", "Adventure", "Comedy", "Family", "Fantasy"
    ]
    assert movie_entity.separated_cast == [
        "Mike Myers", "Eddie Murphy", "Cameron Diaz", "John Lithgow"
    ]
    assert movie_entity.separated_languages == ["English", "Foo"]
    assert movie_entity.separated_writers == [
        "William Steig", "Ted Elliott", "Terry Rossio", "Joe Stillman",
        "Roger S.H. Schulman", "Cody Cameron", "Chris Miller", "Conrad Vernon"
    ]
    assert movie_entity.separated_awards == {
        'won_oscars': 1,
        'oscars_nomination': 0,
        'another_wins': 36,
        'another_nominations': 60
    }
 def __get_data_and_insert_movie(self, movie_title, lock):
     result = Utils.fetch_movie_data(movie_title)
     if result.get('Response') == 'False':
         return "Movie with title {} not found in API omdbapi. Movie won't be added.".format(
             movie_title)
     movie = MovieEntity()
     movie.load_from_data(result)
     self.__add_movie(movie, lock)
 def get_titles_of_unfilled_movies(self):
     connection = self.create_connection()
     how_many = len(MovieEntity.get_movie_parameters()[:6]) - 2
     insert = Utils.repeat('{}', how_many, insert_between=' AND ')
     result = connection.execute(
         "SELECT TITLE FROM {} WHERE ({}) IS NULL".format(
             self.Movies_table,
             insert).format(*MovieEntity.get_movie_parameters()[2:7]))
     result = result.fetchall()
     self.close_connection(connection)
     return result
Ejemplo n.º 4
0
def test_load_from_data():
    movie_entity = MovieEntity()

    movie_entity.load_from_data(mock_data)

    assert movie_entity.Title == 'Shrek'
    assert movie_entity.Year == 2001
    assert movie_entity.Runtime == 90
    assert movie_entity.Genre == "Animation, Adventure, Comedy, Family, Fantasy"
    assert movie_entity.Director == "Andrew Adamson, Vicky Jenson"
    assert movie_entity.Cast == "Mike Myers, Eddie Murphy, Cameron Diaz, John Lithgow"
    assert movie_entity.Writer == "William Steig (based upon the book by), Ted Elliott, Terry Rossio, Joe Stillman, Roger S.H. Schulman, Cody Cameron (additional dialogue), Chris Miller (additional dialogue), Conrad Vernon (additional dialogue)"
    assert movie_entity.Language == "English, Foo"
    assert movie_entity.Country == "USA, Foo"
    assert movie_entity.Awards == "Won 1 Oscar. Another 36 wins & 60 nominations."
    assert movie_entity.IMDb_rating == 7.8
    assert movie_entity.IMDb_votes == 573686
    assert movie_entity.Box_office == 266982666
 def __replace_movie(self, movie: MovieEntity, lock):
     connection = self.create_connection()
     with lock:
         connection.execute(
             'UPDATE {} Set Year= :year, Runtime= :runtime, Genre= :genre, Director= :director, Cast= :cast, Writer= :writer, Language= :language, \
                                Country= :country, Awards= :awards, IMDb_rating= :imdb_rating, IMDb_votes= :imdb_votes, Box_office= :box_office \
                                WHERE Title= :title'.format(
                 self.Movies_table), movie.get_dict())
     self.commit(connection)
     self.close_connection(connection)
 def __add_movie(self, movie: MovieEntity, lock):
     connection = self.create_connection()
     with lock:
         result = connection.execute(
             'SELECT Title FROM Movie_attributes WHERE Title = :title',
             movie.get_dict()).fetchone()
     if result is not None:
         return "Movie {} already in database.".format(movie.Title)
     with lock:
         connection.execute(
             'Insert INTO {} (Title, Year, Runtime, Genre, Director, Cast, Writer, Language, Country,\
                             Awards, IMDb_rating, IMDB_votes, Box_office) VALUES (:title, :year, :runtime, :genre,\
                             :director, :cast, :writer, :language, :country, :awards, :imdb_rating, :imdb_votes,\
                             :box_office)'.format(self.Movies_table),
             movie.get_dict())
         movie.ID = connection.execute(
             "SELECT ID FROM MOVIES WHERE TITLE=:title",
             movie.get_dict()).fetchone()[0]
         self.commit(connection)
     self.close_connection(connection)
     self.__fill_rest_of_the_tables(movie, lock)
     return 'Movie {} added.'.format(movie.Title)
    def __fill_rest_of_the_tables(self, movie: MovieEntity, lock):
        connection = self.create_connection()
        movie_properties = movie.get_dict()
        movie_properties.update(movie.separated_awards)
        with lock:
            connection.execute(
                'Insert INTO Movie_attributes VALUES (:id, :title, :year, :runtime, :won_oscars, :oscars_nomination, :another_wins, :another_nominations, :imdb_rating, :imdb_votes, :box_office)',
                movie_properties)
            connection.commit()

        records = Utils.create_unique_records(movie.separated_countries,
                                              movie.ID)
        with lock:
            connection.executemany('INSERT INTO Country VALUES (?, ?)',
                                   records)
            connection.commit()

        records = Utils.create_unique_records(movie.separated_genres, movie.ID)
        with lock:
            connection.executemany('INSERT INTO Genre VALUES (?, ?)', records)
            connection.commit()

        records = Utils.create_unique_records(movie.separated_languages,
                                              movie.ID)
        with lock:
            connection.executemany('INSERT INTO Language VALUES (?, ?)',
                                   records)
            connection.commit()

        records = Utils.create_unique_records(movie.separated_writers,
                                              movie.ID)
        with lock:
            connection.executemany('INSERT INTO Writer VALUES (?, ?)', records)
            connection.commit()

        records = Utils.create_unique_records(movie.separated_directors,
                                              movie.ID)
        with lock:
            connection.executemany('INSERT INTO Director VALUES (?, ?)',
                                   records)
            connection.commit()

        records = Utils.create_unique_records(movie.separated_cast, movie.ID)
        with lock:
            connection.executemany('INSERT INTO Actors VALUES (?, ?)', records)
            connection.commit()

        self.close_connection(connection)
 def __update_movie(self, movie_title, lock):
     result = Utils.fetch_movie_data(movie_title)
     movie = MovieEntity()
     movie.load_from_data(result)
     self.__replace_movie(movie, lock)
     connection = self.create_connection()
     with lock:
         movie.ID = connection.execute(
             "SELECT ID FROM MOVIES WHERE TITLE=:title",
             movie.get_dict()).fetchone()[0]
     self.close_connection(connection)
     self.__fill_rest_of_the_tables(movie, lock)