def test_db_fill_movies(): """ Author: John Andree Lidquist, Marten Bolin Date: 2017-10-11 Last Updated: Purpose: Assert that movies are loaded into the database correctly """ session = create_session() result = session.query(Genre).filter_by(name='Action').first() assert result.name == 'Action' result = session.query(Movie).filter_by(id=1).first() assert result.title == "Toy Story" assert result.year == 1995 result = session.query(MovieInGenre).filter_by(movie_id=1).all() for counter, res in enumerate(result): if counter == 0: assert res.genre == "Adventure" if counter == 1: assert res.genre == "Animation" if counter == 2: assert res.genre == "Children" if counter == 3: assert res.genre == "Comedy" if counter == 4: assert res.genre == "Fantasy" session.close()
def __init__(self): """ Author: Marten Bolin / John Lidquist Date: 2017-11-10 Last update: Purpose: The constructor of the Retrieve class, creates a session to be used by subclass """ self.session = create_session()
def __init__(self, small_data_set): """ Author: John Andree Lidquist, Marten Bolin Date: 12-10-2017 Last update: 9-11-2017 Purpose: Initiate the class and call the fill method """ self.session = create_session() self.fill(small_data_set)
def __init__(self): """ Author: Eric Petersson, Vincent Dehaye Date: 21-11-2017 Last update: 21-11-2017 Purpose: Initiates the class and calls the fill method """ self.session = create_session() self.fill()
def test_insert_recommendation(): """ Author: John Andree Lidquist Date: 2017-11-15 Last Updated: 2017-11-16 Purpose: Assert that recommendations are inserted to the database """ # PRE-CONDITIONS movie_id_1 = -1 movie_id_2 = -2 user_id = -1 # We create a session and add a two dummy movies and a dummy user session = create_session() dummy_movie_1 = Movie(id=movie_id_1, title="dummy1", year=1111) dummy_movie_2 = Movie(id=movie_id_2, title="dummy2", year=1111) dummy_user = User(id=user_id, age=10, gender='Male', occupation='Student') session.add(dummy_movie_1) session.add(dummy_movie_2) session.add(dummy_user) session.commit() # We make the recommendation of the two dummy movies to the dummy user movie_list = [{'id': movie_id_1}, {'id': movie_id_2}] InsertRecommendation().insert_recommendation(movie_list=movie_list, user_id=user_id) # EXPECTED OUTPUT # The expected output are the same variables as the Pre-conditions # OBSERVED OUTPUT # We query the recommendations to get an observed output observed_1 = session.query(Recommendation).filter_by( movie_id=movie_id_1, user_id=user_id).first() observed_2 = session.query(Recommendation).filter_by( movie_id=movie_id_2, user_id=user_id).first() # After adding the dummy movies, the dummy user and the dummy recommendation, # we remove them again. We need to commit twice because of foreign key constraints session.delete(observed_1) session.delete(observed_2) session.commit() session.delete(dummy_movie_1) session.delete(dummy_movie_2) session.delete(dummy_user) session.commit() session.close() assert observed_1 assert observed_1.movie_id == movie_id_1 assert observed_1.user_id == user_id assert observed_2 assert observed_2.movie_id == movie_id_2 assert observed_2.user_id == user_id
def test_db_fill_ratings(): """ Author: John Andree Lidquist, Marten Bolin Date: 2017-10-11 Last Updated: Purpose: Assert that ratings are loaded into the database correctly """ session = create_session() result = session.query(Rating).filter_by(user_id=1, movie_id=13).first() session.close() assert result.rating == 5.0
def test_db_fill_users(): """ Author: John Andree Lidquist, Marten Bolin Date: 2017-10-11 Last Updated: Purpose: Assert that users are loaded into the database correctly """ session = create_session() result = session.query(User).filter_by(id=1).first() assert result.id == 1 session.close()
def test_insert_user(): """ Author: John Andrée Lidquist Date: 2017-11-21 Last Updated: 2017-11-21 Purpose: Assert that users are correctly inserted to the database """ # PRE-CONDITIONS user_age_1 = 3333 user_gender_1 = 'Female' user_occupation_1 = 'Student' user_age_2 = 4444 user_gender_2 = 'Male' # We call the function to be tested and let it add feedback. Two users will be added InsertUser().insert_user(user_age_1, user_gender_1, user_occupation_1) InsertUser().insert_user(user_age_2, user_gender_2) # EXPECTED OUTPUT expected_user_age_1 = user_age_1 expected_user_gender_1 = user_gender_1 expected_user_occupation_1 = user_occupation_1 expected_user_age_2 = user_age_2 expected_user_gender_2 = user_gender_2 expected_user_occupation_2 = 'Unknown' # OBSERVED OUTPUT # We query the rating and recommendation to get an observed output session = create_session() observed_1 = session.query(User).filter(User.age == user_age_1).first() observed_user_age_1 = observed_1.age observed_user_gender_1 = observed_1.gender observed_user_occupation_1 = observed_1.occupation observed_2 = session.query(User).filter(User.age == user_age_2).first() observed_user_age_2 = observed_2.age observed_user_gender_2 = observed_2.gender observed_user_occupation_2 = observed_2.occupation # After adding the dummy users, we remove them again. session.delete(observed_1) session.delete(observed_2) session.commit() session.close() assert observed_1 assert observed_user_age_1 == expected_user_age_1 assert observed_user_gender_1 == expected_user_gender_1 assert observed_user_occupation_1 == expected_user_occupation_1 assert observed_2 assert observed_user_age_2 == expected_user_age_2 assert observed_user_gender_2 == expected_user_gender_2 assert observed_user_occupation_2 == expected_user_occupation_2
def test_update_trend_score(): """ Author: John Andrée Lidquist Date: 2017-11-16 Last Updated: Purpose: Assert that trend scores are updated in the database correctly """ # PRE-CONDITIONS movie_id = -1 score_before_update = 10 score_after_update = 20 # We create a session and add a dummy movie and a dummy trending score for that movie session = create_session() dummy_movie = Movie(id=movie_id, title="dummy", year=1111) session.add(dummy_movie) session.commit() dummy_score = TrendingScore(movie_id=movie_id, total_score=score_before_update, youtube_score=score_before_update, twitter_score=score_before_update) session.add(dummy_score) session.commit() # We use the method to be tested and update to the new score UpdateTrending().update_trend_score(movie_id=movie_id, total_score=score_after_update, youtube_score=score_after_update, twitter_score=score_after_update) # EXPECTED OUTPUT expected_score = score_after_update # OBSERVED OUTPUT # We query the the score to get a observed output observed = session.query(TrendingScore).filter_by( movie_id=movie_id).first() # After adding the dummy movie and the dummy trending score for it, we remove them again. # We need to commit twice because of foreign key constraints session.delete(observed) session.commit() session.delete(dummy_movie) session.commit() session.close() assert observed assert observed.movie_id == movie_id assert observed.total_score == expected_score assert observed.youtube_score == expected_score assert observed.twitter_score == expected_score
def test_retrieve_rating(): """ Author: John Andrée Lidquist Date: 2017-11-16 Last Updated: Purpose: Assert that a rating is retrieved correctly """ # PRE-CONDITIONS user_id = -1 movie_id = -1 rating = 10 # We create a session and add a dummy movie, a dummy user and a dummy rating for # that user and movie session = create_session() dummy_movie = Movie(id=movie_id, title="dummy", year=1111) dummy_user = User(id=user_id, age=10, gender='Male', occupation='Student') session.add(dummy_movie) session.add(dummy_user) session.commit() dummy_rating = Rating(user_id=user_id, movie_id=movie_id, rating=rating) session.add(dummy_rating) session.commit() # EXPECTED OUTPUT expected_rating = rating # OBSERVED OUTPUT # We call the method to be tested to get all the ratings observed_all_ratings = RetrieveRating().retrieve_ratings() # We make sure the dummy rating is part of all those ratings (the for-loop will only run once, # because the rating added will be first in the list observed_rating = 0 for rating in observed_all_ratings: if rating.user_id == user_id and rating.movie_id == movie_id: observed_rating = rating.rating break # After adding the dummy movie, the dummy user and the dummy rating, we remove them again. # We need to commit twice because of foreign key constraints session.delete(dummy_rating) session.commit() session.delete(dummy_user) session.delete(dummy_movie) session.commit() session.close() assert observed_all_ratings assert observed_rating == expected_rating
def test_get_success_rates(): """ Author: John Andrée Lidquist Date: 2017-11-21 Last Updated: 2017-11-27 Purpose: Assert that the success rates are retrieved from the database """ # PRE-CONDITIONS watched = 99 not_watched = 88 average_user_success_rate = 0.123456789 # We create a session and add a dummy success rate session = create_session() dummy_success_rate = SuccessRate( watched=watched, not_watched=not_watched, average_user_success_rate=average_user_success_rate) session.add(dummy_success_rate) session.commit() # EXPECTED OUTPUT expected_watched = watched expected_not_watched = not_watched expected_user_average_success_rate = average_user_success_rate # OBSERVED OUTPUT # We call the method to be tested to get all the success rates observed = RetrieveSuccessRate().get_success_rates() observed_watched = None observed_not_watched = None observed_average_user_success_rate = None # We find the success rate we added for rate in observed: if rate.average_user_success_rate == average_user_success_rate: observed_watched = rate.watched observed_not_watched = rate.not_watched observed_average_user_success_rate = rate.average_user_success_rate break # After adding the dummy success rate we remove it. session.delete(dummy_success_rate) session.commit() session.close() assert observed assert observed_watched == expected_watched assert observed_not_watched == expected_not_watched assert observed_average_user_success_rate == expected_user_average_success_rate
def get_top_recommendations(age_range, gender_list, nr_of_movies): # Example call for all users : get_top_recommendations([], [], 10) # Example call for only Male and Unknown users : get_top_recommendations([],["Male", "Unknown"]) # Example call for users only between 15 and 35 : get_top_recommendations([15,35], []) # Example call for Female users between 35 and 50 : get_top_recommendations([35,50], ["Female"]) # Generates the list of users matching the query list_of_matching_users = get_user_group_ids(age_range, gender_list) # Will be the list of recommended movies, and the number of time the were recommended. toplist = {} # Count the number of times each movie has been watched watched_list = {} session = create_session() # Populates top recommended movies list. for user in list_of_matching_users: recommended_to_user = session.query(Recommendation).filter( Recommendation.user_id == user).all() for recommendation in recommended_to_user: if recommendation.movie_id not in toplist: toplist[recommendation.movie_id] = 1 if recommendation.watched == 1: watched_list[recommendation.movie_id] = 1 else: watched_list[recommendation.movie_id] = 0 else: toplist[recommendation.movie_id] += 1 if recommendation.watched == 1: watched_list[recommendation.movie_id] += 1 output_list = [] # Sort the keys of the movies from the most recommended to the least. keys = sorted(toplist.items(), key=lambda x: x[1], reverse=True) # Fill the list with dictionaries containing movie id, name and count of recommendations # in the order of the most recommended to the least. for idx in range(min(len(keys), nr_of_movies)): tmp_dict = {} movie_id, nr_of_recs = keys[idx] tmp_dict["id"] = movie_id tmp_dict["title"] = gets_from_database.get_movie_title(movie_id) tmp_dict["timesRecommended"] = nr_of_recs tmp_dict["successRate"] = (watched_list[movie_id] / nr_of_recs) * 100 output_list.append(tmp_dict) return output_list
def get_restricted_ids(table, feature, min, max): """ :param table: table name in database :param feature: column name in database :param min: minimum value :param max: maximum value :return: list of the ids of records of the desired table between the min and max boundaries """ session = create_session() sqltable = getattr(DBConn, table) objects_list = session.query(sqltable).\ filter((getattr(sqltable, feature) >= min) & (getattr(sqltable, feature) <= max)).all() output_list = [] session.close() for object in objects_list: output_list.append(object.id) return output_list
def test_add_trend_score(): """ Author: John Andrée Lidquist Date: 2017-11-16 Purpose: Assert that trend scores are inserted to the database correctly """ # PRE-CONDITIONS movie_id = -1 total_score = 10 youtube_score = 20 twitter_score = 30 # We create a session and add a dummy movie to add a score to session = create_session() dummy_movie = Movie(id=movie_id, title="dummy", year=1111) session.add(dummy_movie) session.commit() # We use the method to be tested and add a score to the dummy movie InsertTrending().add_trend_score(movie_id=movie_id, total_score=total_score, youtube_score=youtube_score, twitter_score=twitter_score) # EXPECTED OUTPUT # The expected output are the same variables as the Pre-conditions # OBSERVED OUTPUT # We query the the score to get a observed output observed = session.query(TrendingScore).filter_by( movie_id=movie_id).first() # After adding the dummy movie and the dummy trending score for it, we remove them again. # We need to commit twice because of foreign key constraints session.delete(observed) session.commit() session.delete(dummy_movie) session.commit() session.close() assert observed assert observed.movie_id == movie_id assert observed.total_score == total_score assert observed.youtube_score == youtube_score assert observed.twitter_score == twitter_score
def test_get_simple_success_rate(): """ Author: John Andrée Lidquist Date: 2017-11-27 Last Updated: 2017-11-27 Purpose: Assert that the success rates are retrieved from the DataManager correctly """ # PRE-CONDITIONS watched = 9999 not_watched = 8888 average_user_success_rate = 0.123456789 # We create a session and add a dummy success rate session = create_session() dummy_success_rate = SuccessRate( watched=watched, not_watched=not_watched, average_user_success_rate=average_user_success_rate) session.add(dummy_success_rate) session.commit() # EXPECTED OUTPUT expected_watched = watched expected_not_watched = not_watched # OBSERVED OUTPUT # We call the method to be tested to get all the success rates observed_dict = GetSuccessRate().get_simple_success_rate() observed_watched = None observed_not_watched = None for entry in observed_dict: if entry.get('watched') == expected_watched and \ entry.get('not_watched') == expected_not_watched: observed_watched = entry.get('watched') observed_not_watched = entry.get('not_watched') break # After adding the dummy success rate, we remove it again session.delete(dummy_success_rate) session.commit() session.close() assert len(observed_dict) >= 1 assert observed_watched == expected_watched assert observed_not_watched == expected_not_watched
def test_retrieve_movie(): """ Author: John Andree Lidquist Date: 2017-11-16 Last Updated: Purpose: Assert that a movie, or all movies, are retrieved correctly """ # PRE-CONDITIONS movie_id = -1 movie_title = "dummy" movie_year = 1111 # We create a session and add a dummy movie that we can later retrieve session = create_session() dummy_movie = Movie(id=movie_id, title=movie_title, year=movie_year) session.add(dummy_movie) session.commit( ) # We need to close the session, else we get an error when trying to delete it session.close() # EXPECTED OUTPUT expected_id = movie_id expected_title = movie_title expected_year = movie_year # OBSERVED OUTPUT # We call the method to be tested to get 1) The movie we added above, and 2) All the movies # which is done by not setting the parameter "movie_id" retrieve_movie = RetrieveMovie() observed_one_movie = retrieve_movie.retrieve_movie(movie_id=movie_id) observed_all_movies = retrieve_movie.retrieve_movie() # After adding the dummy movie we remove them again. session.delete(observed_one_movie) session.commit() session.close() assert observed_one_movie assert observed_one_movie.id == expected_id assert observed_one_movie.title == expected_title assert observed_one_movie.year == expected_year assert observed_all_movies
def test_TrendingToDB(): ''' Author: John Andree Lidquist, Marten Bolin Date: 2017-10-30 Purpose: Assert that the database gets filled/updated with trending scores. ''' # The test will first start to run the class TrendingToDB # and then wait (sleep) for 3 seconds before moving on # to make sure that there has been a value stored for # the trending score "total_score". session = create_session() # Pre-conditions trend_to_db = TrendingToDB(daily=False) time.sleep(3) trend_to_db.terminate() # Expected output 1 expected_low = 0 # Observed output 1 result = session.query(TrendingScore).filter_by(movie_id=1).first() observed = result.total_score session.close() assert expected_low <= observed
def test_insert_success_rate(): """ Author: John Andrée Lidquist Date: 2017-11-28 Purpose: Assert that success rates are correctly added to the database """ # PRE-CONDITIONS # Save the size (the number of rates) before insertion, then make the insertion session = create_session() rates_before = session.query(SuccessRate).filter_by().all() InsertSuccessRate().insert_success_rate() # EXPECTED OUTPUT expected_size_after = len(rates_before) + 1 # OBSERVED OUTPUT # We query the rates and find the added rate by comparing it to today's date. # We also get the size rates_after = session.query(SuccessRate).filter_by().all() observed_size_after = len(rates_after) observed_rate = None for rate in rates_after: if rate.timestamp.day == datetime.datetime.now().day: observed_rate = rate session.query(SuccessRate).filter_by(id=observed_rate.id).delete() session.commit() session.close() assert observed_size_after == expected_size_after assert observed_rate.watched >= 0 assert observed_rate.not_watched >= 0 assert observed_rate.average_user_success_rate >= 0 assert observed_rate.average_user_success_rate <= 1
def get_restricted_match(table, feature_list, list_of_matching_strings_list): """ :param table: table name in database :param feature_list: list of strings, features one want to restrict on :param list_of_matching_strings_list: list of lists of values to match for each feature :return: list of the ids of records of the desired table matching the given string """ session = create_session() if len(feature_list) != len(list_of_matching_strings_list): return "Length of features list different of length of list of matching strings list" if not all(isinstance(n, str) for n in feature_list): return "Feature list must only contain strings" if not all(isinstance(n, list) for n in list_of_matching_strings_list): return "List of matching strings list must only contain lists" for sublist in list_of_matching_strings_list: if not all(isinstance(n, str) for n in sublist): return "List of matching strings must only contain strings" if table == "User": basedir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) if "gender" in feature_list: index = feature_list.index("gender") if len(list_of_matching_strings_list[index]) == 0: return "Gender given as feature to restrict on but no value to match provided" with open(basedir + '/Database/MockData/gender_list.csv', 'rt') as f: reader = csv.reader(f, delimiter=',') valid_genders_list = list(reader) for gender_value in list_of_matching_strings_list[index]: if [gender_value] not in valid_genders_list: return "Invalid gender value to match" if "occupation" in feature_list: index = feature_list.index("occupation") if len(list_of_matching_strings_list[index]) == 0: return "Occupation given as feature to restrict on but no value to match provided" with open(basedir + '/Database/MockData/occupation_list.csv', 'rt') as f: reader = csv.reader(f, delimiter=',') valid_occupations_list = list(reader) for occupation_value in list_of_matching_strings_list[index]: if [occupation_value] not in valid_occupations_list: return "Invalid occupation value to match" output_list = [] feature_list_current_position = 0 query = "session.query(" + str(table) + ").filter(" filters = "(" for feature in feature_list: filters += "(" for string in list_of_matching_strings_list[feature_list_current_position]: filters += str(table) + "." + str(feature) + " == '" + string + "') | (" filters = filters[:-4] feature_list_current_position += 1 filters += ") & (" filters = filters[:-4] query += filters + ").all()" result = eval(query) session.close() for object in result: output_list.append(object.id) return output_list
def test_get_trending_twitter(): """ Author: John Andrée Lidquist Date: 2017-11-16 Purpose: Assert that trend twitter_score is retrieved from the database correctly """ # PRE-CONDITIONS movie_id = -1 total_score = 10 youtube_score = 20 twitter_score = 999999999999 # We create a session and add a dummy movie and a dummy total score # Two commits are necessary because of foreign constraints session = create_session() dummy_movie = Movie(id=movie_id, title='Dummy', year=1111) dummy_score = TrendingScore(movie_id=movie_id, total_score=total_score, youtube_score=youtube_score, twitter_score=twitter_score) session.add(dummy_movie) session.commit() session.add(dummy_score) session.commit() # EXPECTED OUTPUT expected_movie_id = movie_id expected_total_score = total_score expected_youtube_score = youtube_score expected_twitter_score = twitter_score # OBSERVED OUTPUT # We call the method to be tested that retrieves all the users observed_scores = RetrieveTrending().get_trending_twitter(num_of_titles=5) observed_movie_id = None observed_total_score = None observed_youtube_score = None observed_twitter_score = None for rating in observed_scores: if rating.movie_id == movie_id: observed_movie_id = rating.movie_id observed_total_score = rating.total_score observed_youtube_score = rating.youtube_score observed_twitter_score = rating.twitter_score break # After adding the dummy user we remove them again. # Two commits are necessary because of foreign constraints session.delete(dummy_score) session.commit() session.delete(dummy_movie) session.commit() session.close() assert observed_scores assert observed_movie_id == expected_movie_id assert observed_total_score == expected_total_score assert observed_youtube_score == expected_youtube_score assert observed_twitter_score == expected_twitter_score
def test_insert_feedback(): """ Author: John Andrée Lidquist Date: 2017-11-21 Last Updated: 2017-11-21 Purpose: Assert that feedback is inserted to the database """ # PRE-CONDITIONS user_id_1 = -1 user_id_2 = -2 user_id_3 = -3 movie_id = -1 rating_new = 1.5 rating_update = 2.5 watched = False # We create a session and add a dummy movie and a dummy user # We need to commit twice because of foreign key constraints session = create_session() dummy_movie = Movie(id=movie_id, title="dummy", year=1111) dummy_user_1 = User(id=user_id_1, age=10, gender='Male', occupation='Student') dummy_user_2 = User(id=user_id_2, age=10, gender='Male', occupation='Student') dummy_user_3 = User(id=user_id_3, age=10, gender='Male', occupation='Student') session.add(dummy_movie) session.add(dummy_user_1) session.add(dummy_user_2) session.add(dummy_user_3) session.commit() dummy_recommendation = Recommendation(user_id=user_id_2, movie_id=movie_id, watched=watched) dummy_rating = Rating(user_id=user_id_3, movie_id=movie_id, rating=rating_update) session.add(dummy_recommendation) session.add(dummy_rating) session.commit() # We call the function to be tested and let it add feedback. Three cases will be tested InsertFeedback().insert_feedback(user_id_1, movie_id, True, rating_new) InsertFeedback().insert_feedback(user_id_2, movie_id, True) InsertFeedback().insert_feedback(user_id_3, movie_id, True, 5.0) # EXPECTED OUTPUT expected_user_id = user_id_1 expected_movie_id = movie_id expected_rating_new = rating_new expected_watched = True expected_rating_update = 5.0 # OBSERVED OUTPUT # We query the rating and recommendation to get an observed output observed_1 = session.query(Rating).filter_by(movie_id=movie_id, user_id=user_id_1).first() observed_rating_new = observed_1.rating observed_movie_id = observed_1.movie_id observed_user_id = observed_1.user_id observed_2 = session.query(Recommendation).filter_by( movie_id=movie_id, user_id=user_id_2).first() observed_watched = observed_2.watched observed_3 = session.query(Rating).filter_by(movie_id=movie_id, user_id=user_id_3).first() observed_rating_update = observed_3.rating # After adding the dummy movie and the dummy users, we remove them again. # We need to commit twice because of foreign key constraints session.delete(observed_1) session.delete(dummy_recommendation) session.delete(dummy_rating) session.commit() session.delete(dummy_movie) session.delete(dummy_user_1) session.delete(dummy_user_2) session.delete(dummy_user_3) session.commit() session.close() assert observed_1 assert observed_rating_new == expected_rating_new assert observed_movie_id == expected_movie_id assert observed_user_id == expected_user_id assert observed_watched == expected_watched assert observed_rating_update == expected_rating_update
from Product.Database.DBConn import create_session, Recommendation from Product.DataManager.restrict import get_restricted_match from Product.RecommendationManager import gets_from_database as gets_from_database """ Author: Eric Petersson Date: 2017-11-15 Last update: Purpose: Illustrate how restrict.py can be used to generate output for visualization """ session = create_session() # Defines what columns in the User table to restrict on. Will be a parameter feature_list = ['gender', 'occupation'] # Defines what is considered a match for gender matching_strings_list_gender = ['Male', 'Female'] # Defines what is considered a match for occupation matching_strings_list_occupation = ['engineer', 'student'] # Defines the list, of lists that will be a parameter. list_of_matching_strings_list = [ matching_strings_list_gender, matching_strings_list_occupation ] # Generates the list of users matching the query list_of_matching_users = get_restricted_match('User', feature_list, list_of_matching_strings_list) # Will be the list of recommended movies, and the number of time the were recommended. toplist = {}