Skip to content

Commit

Permalink
Changes to test files
Browse files Browse the repository at this point in the history
  • Loading branch information
Shrimadh committed Nov 1, 2024
1 parent 221640e commit 23f768b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
28 changes: 17 additions & 11 deletions test/test_db.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
"""
Test cases for MongoDB connection.
"""

# pylint: skip-file
import os
import unittest
import logging
from dotenv import load_dotenv
from src.recommenderapp.client import client

logging.basicConfig(level=logging.DEBUG)

load_dotenv()


class TestDB(unittest.TestCase):
"""Test cases for MongoDB connection."""

@classmethod
def setUpClass(cls):
"""Set up MongoDB connection URI."""
cls.original_mongo_uri = os.getenv("MONGO_URI")

def test_negative_mongo_connection(self):
WRONG_MONGO_URI = "mongodb://invalid_user:invalid_pass@invalid_host:27017/test"
os.environ["MONGO_URI"] = WRONG_MONGO_URI
"""Test negative case for MongoDB connection."""
invalid_mongo_uri = (
"mongodb://invalid_user:invalid_pass@invalid_host:27017/test"
)
os.environ["MONGO_URI"] = invalid_mongo_uri

try:
with self.assertRaises(Exception) as context:
from src.recommenderapp.client import client
except Exception as mongo_e:
self.assertIn("bad database name", str(mongo_e).lower())

os.environ["MONGO_URI"] = self.original_mongo_uri
from src.recommenderapp.client import client
self.assertIn("bad database name", str(context.exception).lower())

@classmethod
def tearDownClass(cls):
pass
os.environ["MONGO_URI"] = self.original_mongo_uri


if __name__ == "__main__":
Expand Down
12 changes: 9 additions & 3 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
get_genre_count,
)


class BaseTestCase(unittest.TestCase):
"""Base test class with common setup and teardown"""

@classmethod
def setUpClass(cls):
"""Common setup for all test classes"""
Expand Down Expand Up @@ -85,6 +87,7 @@ def tearDownClass(cls):
cls.db.ratings.delete_many({})
client.drop_database("testDB")


class TestUtilityFunctions(BaseTestCase):
"""Test class for utility functions like tags, data formatting, etc."""

Expand Down Expand Up @@ -125,6 +128,7 @@ def test_create_movie_genres(self):
}
self.assertEqual(result, expected_result)


class TestEmailFunctionality(BaseTestCase):
"""Test class for email-related functionality"""

Expand All @@ -138,9 +142,7 @@ def test_send_email_to_user(self, mock_smtp):
}
mock_server_instance = MagicMock()
mock_smtp.return_value.__enter__.return_value = mock_server_instance
send_email_to_user(
"test@example.com", beautify_feedback_data(categorized_data)
)
send_email_to_user("test@example.com", beautify_feedback_data(categorized_data))
mock_server_instance.sendmail.assert_called_once()

@patch("pandas.read_csv")
Expand Down Expand Up @@ -168,6 +170,7 @@ def test_send_email_smtp_error(self, mock_smtp):
"test@example.com", beautify_feedback_data(categorized_data)
)


class TestUserManagement(BaseTestCase):
"""Test class for user management functionality"""

Expand Down Expand Up @@ -220,6 +223,7 @@ def test_add_friend_and_get_friends(self):
friends_usernames = [friend["username"] for friend in friends_list]
self.assertIn("Friend1", friends_usernames)


class TestReviewAndRating(BaseTestCase):
"""Test class for review and rating functionality"""

Expand Down Expand Up @@ -278,6 +282,7 @@ def test_get_user_history_invalid_id(self):
with self.assertRaises(InvalidId):
get_user_history(self.db, "invalid_id_format")


class TestMovieFeatures(BaseTestCase):
"""Test class for movie-related features"""

Expand Down Expand Up @@ -343,5 +348,6 @@ def test_get_genre_count(self, mock_read_csv):
mock_db.ratings.find.assert_called_once()
mock_read_csv.assert_called_once()


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

0 comments on commit 23f768b

Please sign in to comment.