def test_eq(self): actor1 = Actor("Tom Cruise") actor2 = Actor("Dwayne Johnson") actor3 = Actor("Tom Cruise") assert (actor1 == actor3) == True assert (actor1 == actor2) == False # Yes, I know I could do !=
def test_init_and_repr(self): actor1 = Actor("Tom Cruise") assert repr(actor1) == "<Actor Tom Cruise>" actor2 = Actor("") assert actor2.actor_full_name is None actor3 = Actor(42) assert actor3.actor_full_name is None
def test_setters(self): # inherently tests getters too a_movie = Movie("Back To The Future", 1965) director1 = Director("Brad Bird") a_movie.title = " Mission Impossible: Ghost Protocol " assert a_movie.title == "Mission Impossible: Ghost Protocol" a_movie.title = 15 #assert a_movie.title == "Mission Impossible: Ghost Protocol" a_movie.description = " An action movie " assert a_movie.description == "An action movie" a_movie.director = director1 assert a_movie.director == director1 actor1 = Actor("Tom Cruise") actor2 = Actor("Simon Pegg") a_movie.actors = [actor1, actor2] assert a_movie.actors == [actor1, actor2] genre1 = Genre("Action") genre2 = Genre("Comedy") a_movie.genres = [genre1, genre2] assert a_movie.genres == [genre1, genre2] a_movie.runtime_minutes = 100 assert a_movie.runtime_minutes == 100 assert type(a_movie.runtime_minutes) == int with pytest.raises(ValueError): a_movie.runtime_minutes = 0 a_movie.runtime_minutes = -10
def test_hash(self): actor1 = Actor("Tom Cruise") actor2 = Actor("Dwayne Johnson") actor3 = Actor("Tom Cruise") assert (hash(actor1) == hash(actor2)) == False # two hashes ?shouldn't? be the same assert ( hash(actor1) == hash(actor3) ) == True # same name, so should have same hash (that's how this hash is decided)
def a_movie(): # these values are half made up a_movie = Movie("Guardians", 2014) a_movie.runtime_minutes = 100 a_movie.description = "descriptiongoeshere" a_movie.director = Director("James Gunn") a_movie.actors = [ Actor("Chris Pratt"), Actor("Vin Diesel"), Actor("Bradley Cooper") ] a_movie.genres = [Genre("Action"), Genre("Adventure"), Genre("Sci-Fi")] a_movie.external_rating = 8.1 a_movie.external_rating_votes = 31204 a_movie.revenue = 301 a_movie.metascore = 74 return a_movie
def test_more_methods(self): a_movie = Movie("Back To The Future", 1965) actor1 = Actor("Michael J. Fox") actor2 = Actor("Christopher Lloyd") genre1 = Genre("Horror") genre2 = Genre("Comedy") a_movie.add_actor(actor1) assert a_movie.actors == [actor1] a_movie.add_actor(actor1) assert a_movie.actors == [actor1 ] # test it doesn't add duplicate actors a_movie.add_actor(actor2) assert a_movie.actors == [ actor1, actor2 ] # tests actors are added (and in order, not that it matters) a_movie.remove_actor(actor1) assert a_movie.actors == [actor2] a_movie.remove_actor( 5) # test it doesn't throw an error if not an actor assert a_movie.actors == [ actor2 ] # test it doesn't randomly remove an actor (I don't see how but yeah) a_movie.add_genre(genre1) assert a_movie.genres == [genre1] a_movie.add_genre(genre1) assert a_movie.genres == [genre1 ] # test it doesn't add duplicate actors a_movie.add_genre(genre2) assert a_movie.genres == [ genre1, genre2 ] # tests actors are added (and in order, not that it matters) a_movie.remove_genre(genre1) assert a_movie.genres == [genre2] a_movie.remove_genre( 5) # test it doesn't throw an error if not a genre assert a_movie.genres == [ genre2 ] # test it doesn't randomly remove an genre (I don't see how but yeah)
def read_csv_file(self): # this assumes there aren't repeated actors, genres, or movies (i.e. not a entry with same: release year and movie title) with open(self.__file_name, mode='r', encoding='utf-8-sig') as csvfile: movie_file_reader = csv.DictReader(csvfile) self.__dataset_of_movies = list() self.__dataset_of_actors = list() self.__dataset_of_directors = list() self.__dataset_of_genres = list() index = 0 for row in movie_file_reader: title = row['Title'].strip() # stripping to safe release_year = int(row['Year']) actor_full_name_list = get_list_from_comma_string(row['Actors']) director_full_name = row['Director'].strip() # stripping to safe genre_name_list = get_list_from_comma_string(row['Genre']) #- other things rank = int(row['Rank']) description = row['Description'] runtime_minutes = int(row['Runtime (Minutes)']) external_rating = float(row['Rating']) external_rating_votes = int(row['Votes']) # These require try/except because some values are N/A's (preferably all would but it's SUPER ugly code) try: revenue = float(row['Revenue (Millions)']) except: revenue = None try: metascore = int(row['Metascore']) except: metascore = None #-------- Make Actor, Director and Genre Objects actor_object_list = [Actor(actor_full_name) for actor_full_name in actor_full_name_list] # add actor colleagues, who are the actors who are in the movie with them (i.e. add all other actors to colleagues for all actors) for actor in actor_object_list: for potential_actor_colleague in actor_object_list: # potential because one of them is them if actor != potential_actor_colleague: actor.add_actor_colleague(potential_actor_colleague) director_object = Director(director_full_name) genre_object_list = [Genre(genre_name) for genre_name in genre_name_list] #-------- Create and set up Movie Object movie_object = Movie(title, release_year) movie_object.actors = actor_object_list # thanks to my setter implementation movie_object.director = director_object movie_object.genres = genre_object_list # ^ ditto movie_object.release_year = release_year #- other things movie_object.description = description movie_object.runtime_minutes = runtime_minutes movie_object.external_rating = external_rating movie_object.external_rating_votes = external_rating_votes if revenue != None: movie_object.revenue = revenue if metascore != None: movie_object.metascore = metascore #-------- Take all the above information, and add to datasets self.__dataset_of_movies.append(movie_object) # the assumption is that each row in the csv is a unique movie # I could create a function that adds unique things to lists only, nvm for actor_object in actor_object_list: if actor_object not in self.__dataset_of_actors: self.__dataset_of_actors.append(actor_object) if director_object not in self.__dataset_of_directors: self.__dataset_of_directors.append(director_object) for genre_object in genre_object_list: if genre_object not in self.__dataset_of_genres: self.__dataset_of_genres.append(genre_object) index += 1 # To test my assigmnent, run the following lines of code (I have copy and pasted these in my test_all.py file too) #movie_file_csv_reader_object = MovieFileCSVReader(r"C:\Users\Nathan Longhurst\OneDrive - The University of Auckland\b Comp235\Assignment\GitHub Clone (Current)\CS235FlixSkeleton\datafiles\Data1000Movies.csv") #unclean_dict = movie_file_csv_reader_object.check_csv_file() #movie_file_csv_reader_object.clean_csv_file(unclean_dict)
def test_lt(self): actor1 = Actor("Tom Cruise") actor2 = Actor("Dwayne Johnson") assert (actor2 < actor1) == True # expect Dwayne to be less than Taika
def test_add_actor_colleague(self): actor1 = Actor("Tom Cruise") colleague1 = Actor("Dwayne Johnson") actor1.add_actor_colleague(colleague1) assert actor1.check_if_this_actor_worked_with(colleague1) == True