Skip to content

Commit

Permalink
Added test cases for utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
adipai committed Oct 17, 2023
1 parent 575b8c7 commit 6433703
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 34 deletions.
31 changes: 0 additions & 31 deletions src/recommenderapp/constants.py

This file was deleted.

34 changes: 31 additions & 3 deletions src/recommenderapp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from email.mime.multipart import MIMEMultipart

import pandas as pd
import constants as c

def create_colored_tags(genres):
"""
Expand Down Expand Up @@ -83,19 +82,48 @@ def create_movie_genres(movie_genre_df):
genres = row[1]['genres'].split('|')
movie_to_genres[movie] = genres
return movie_to_genres

def send_email_to_user(recipient_email, categorized_data):
"""
Utility function to send movie recommendations to user over email
"""

email_html_content = """
<html>
<head></head>
<body>
<h1 style="color: #333333;">Movie Recommendations from PopcornPicks</h1>
<p style="color: #555555;">Dear Movie Enthusiast,</p>
<p style="color: #555555;">We hope you're having a fantastic day!</p>
<div style="padding: 10px; border: 1px solid #cccccc; border-radius: 5px; background-color: #f9f9f9;">
<h2>Your Movie Recommendations:</h2>
<h3>Movies Liked:</h3>
<ul style="color: #555555;">
{}
</ul>
<h3>Movies Disliked:</h3>
<ul style="color: #555555;">
{}
</ul>
<h3>Movies Yet to Watch:</h3>
<ul style="color: #555555;">
{}
</ul>
</div>
<p style="color: #555555;">Enjoy your movie time with PopcornPicks!</p>
<p style="color: #555555;">Best regards,<br>PopcornPicks Team 🍿</p>
</body>
</html>
"""

# Email configuration
smtp_server = 'smtp.gmail.com'
# Port for TLS
smtp_port = 587
sender_email = '[email protected]'

# Use an app password since 2-factor authentication is enabled
sender_password = ''
sender_password = 'uxnd shis sazo mstj'
subject = 'Your movie recommendation from PopcornPicks'

# Create the email message
Expand All @@ -108,7 +136,7 @@ def send_email_to_user(recipient_email, categorized_data):
# Creating movie-genres map
movie_to_genres = create_movie_genres(movie_genre_df)
# Create the email message with HTML content
html_content = c.EMAIL_HTML_CONTENT.format(
html_content = email_html_content.format(
'\n'.join(f'<li>{movie} \
{create_colored_tags(movie_to_genres.get(movie, ["Unknown Genre"]))}</li><br>' \
for movie in categorized_data['Liked']),
Expand Down
63 changes: 63 additions & 0 deletions test/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
Test suit for search feature
"""

import sys
import unittest
import warnings
import pandas as pd
sys.path.append("../")
#pylint: disable=wrong-import-position
from src.recommenderapp.utils import create_colored_tags, beautify_feedback_data, create_movie_genres, send_email_to_user
#pylint: enable=wrong-import-position

warnings.filterwarnings("ignore")


class Tests(unittest.TestCase):
"""
Test cases for utility functions
"""

def test_beautify_feedback_data(self):
"""
Test case 1
"""
data = {'Movie 1': 'Yet to watch',
'Movie 2': 'Like', 'Movie 3': 'Dislike'}
result = beautify_feedback_data(data)
expected_result = {"Liked": ['Movie 2'], "Disliked": [
'Movie 3'], "Yet to Watch": ['Movie 1']}

self.assertTrue(result == expected_result)

def test_create_colored_tags(self):
"""
Test case 2
"""
expected_result = '<span style="background-color: #FF1493; color: #FFFFFF; padding: 5px; border-radius: 5px;">Musical</span>'
result = create_colored_tags(['Musical'])
self.assertTrue(result == expected_result)

def test_create_movie_genres(self):
"""
Test case 3
"""
expected_result = {'Toy Story (1995)': ['Animation', 'Comedy', 'Family'], 'Jumanji (1995)': [
'Adventure', 'Fantasy', 'Family']}
movie_genre_df = pd.read_csv('../data/movies.csv').head(n=2)
result = create_movie_genres(movie_genre_df)
self.assertTrue(result == expected_result)

def test_send_email_to_user(self):
"""
Test case 4
"""
data = {"Liked": ['Toy Story (1995)'], "Disliked": [
'Cutthroat Island (1995)'], "Yet to Watch": ['Assassins (1995)']}
with self.assertRaises(Exception):
send_email_to_user("wrong_email", beautify_feedback_data(data))


if __name__ == "__main__":
unittest.main()

0 comments on commit 6433703

Please sign in to comment.