Skip to content

Commit

Permalink
web stat done
Browse files Browse the repository at this point in the history
  • Loading branch information
nazmul-me committed Nov 27, 2024
1 parent a0f07fb commit 95eaf2d
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
2 changes: 0 additions & 2 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}}))
Expand Down
41 changes: 41 additions & 0 deletions tests/test_forum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
36 changes: 36 additions & 0 deletions tests/test_top_reviews.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()

0 comments on commit 95eaf2d

Please sign in to comment.