diff --git a/app/routes.py b/app/routes.py index cd09b1f..d58fd2f 100644 --- a/app/routes.py +++ b/app/routes.py @@ -87,8 +87,6 @@ def get_my_jobs(username): """A method to get all jobs for required user""" intialize_db() user = USERS_DB.find_one({"username": username}) - if user is None: - pass reviews = user['reviews'] return process_jobs(JOBS_DB.find({"_id": {'$in': reviews}})) diff --git a/tests/test_forum.py b/tests/test_forum.py index 7ab1cdf..24eefa7 100644 --- a/tests/test_forum.py +++ b/tests/test_forum.py @@ -2,6 +2,7 @@ from unittest.mock import patch, MagicMock from app import app from app.routes import intialize_db, set_test +import app.routes as routes from bson import ObjectId import json @@ -324,6 +325,46 @@ def test_downvote_post(self): response = self.client.post(f'/forum/{topic_id}/downvote_post') self.assertEqual(response.status_code, 302) self.assertEqual(response.location, '/forum') + def test_get_web_statistics(self): + """Test the get_web_statistics function.""" + jobs = [ + {'title': 'Software Engineer', 'company': 'Company A', 'locations': 'Location 1', 'rating': 4.5}, + {'title': 'Software Engineer', 'company': 'Company A', 'locations': 'Location 1', 'rating': 4.0}, + {'title': 'Data Scientist', 'company': 'Company B', 'locations': 'Location 2', 'rating': 5.0}, + {'title': 'Data Scientist', 'company': 'Company B', 'locations': 'Location 2', 'rating': 4.5}, + {'title': 'Software Engineer', 'company': 'Company A', 'locations': 'Location 2', 'rating': 3.5} + ] + users = [ + {'username': 'user1'}, + {'username': 'user2'}, + {'username': 'user3'} + ] + + expected_output = { + "total_jobs": 5, + "total_companies": 2, + "total_titles": 2, + "total_locations": 2, + 'avg_ratings': 4.3, + 'total_users': 3 + } + result = routes.get_web_statistics(jobs, users) + self.assertEqual(result, expected_output) + + def test_get_web_statistics_empty(self): + """Test the get_web_statistics function with empty lists.""" + jobs = [] + users = [] + expected_output = { + "total_jobs": 0, + "total_companies": 0, + "total_titles": 0, + "total_locations": 0, + 'avg_ratings': 0, + 'total_users': 0 + } + result = routes.get_web_statistics(jobs, users) + self.assertEqual(result, expected_output) def __main__(): unittest.main() \ No newline at end of file diff --git a/tests/test_top_reviews.py b/tests/test_top_reviews.py index 7f507dc..db358a4 100644 --- a/tests/test_top_reviews.py +++ b/tests/test_top_reviews.py @@ -1,6 +1,7 @@ import unittest from unittest.mock import patch from app import app +from app.routes import process_jobs, get_all_users class TestTopJobsRoute(unittest.TestCase): @@ -40,6 +41,41 @@ def test_top_jobs_functions_called(self, mock_initialize_db, mock_get_all_jobs): # Check if the mock functions were called mock_initialize_db.assert_called_once() # Ensure the database initialization function was called once mock_get_all_jobs.assert_called_once() +class TestProcessJobs(unittest.TestCase): + def test_process_jobs(self): + """Test the process_jobs function.""" + job_list = [ + {"_id": "123", "title": "Software Engineer"}, + {"_id": "456", "title": "Data Scientist"} + ] + + expected_output = [ + {"id": "123", "title": "Software Engineer"}, + {"id": "456", "title": "Data Scientist"} + ] + + result = process_jobs(job_list) + self.assertEqual(result, expected_output) + +class TestGetAllUsers(unittest.TestCase): + @patch('app.routes.USERS_DB.find') + @patch('app.routes.process_jobs') + def test_get_all_users(self, mock_process_jobs, mock_find): + """Test the get_all_users function.""" + mock_find.return_value = [ + {"_id": "123", "username": "user1"}, + {"_id": "456", "username": "user2"} + ] + + mock_process_jobs.return_value = [ + {"id": "123", "username": "user1"}, + {"id": "456", "username": "user2"} + ] + + result = get_all_users() + self.assertEqual(result, mock_process_jobs.return_value) + mock_find.assert_called_once() + mock_process_jobs.assert_called_once_with(list(mock_find.return_value)) if __name__ == '__main__': unittest.main()