Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nazmul-me committed Nov 27, 2024
2 parents 988574a + acbc8bb commit a0f07fb
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 38 deletions.
75 changes: 37 additions & 38 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,47 @@ def job_recommendations():
entries = get_all_jobs()

#get top 10 recommendations
recommended_reviews = recommend_jobs(entries, session['username'], 10)
recommended_reviews = recommend(entries, session['username'], 10)
if recommended_reviews:
jobs = transform_jobs(recommended_reviews)

job = transform_jobs(recommended_reviews)
return render_template('review-page.html', jobs=recommended_reviews)

def recommend(entries, user, top):
return recommend_jobs(entries, user, top)
# view all
@app.route('/pageContent')
def page_content():
"""An API for the user to view all the reviews entered"""
intialize_db()
entries = get_all_jobs()
dept_filter_entries = JOBS_DB.distinct("department")
location_filter_entries = JOBS_DB.distinct("locations")
# title_filter_entries = JOBS_DB.distinct("title")
company_filter_entries = JOBS_DB.distinct("company")

# pagination

# print(entries)
page, per_page, offset = get_page_args(
page_parameter="page", per_page_parameter="per_page")
total = len(entries)

if not page or not per_page:
offset = 0
per_page = 10
pagination_entries = entries[offset: offset + per_page]
else:
pagination_entries = entries[offset: offset + per_page]
# print("ELSE!!!")

pagination = Pagination(page=page, per_page=per_page,
total=total, css_framework='bootstrap4')

return render_template('page_content.html', entries=pagination_entries, page=page,
per_page=per_page, pagination=pagination,
dept_filter_entries=dept_filter_entries,
location_filter_entries=location_filter_entries,
company_filter_entries=company_filter_entries)
def getCurrentTime():
"""A method to get current time"""
return datetime.now().replace(microsecond=0).strftime("%Y-%m-%d %H:%M:%S")
Expand Down Expand Up @@ -485,41 +519,6 @@ def dashboard():
hourly_pays=hourly_pays,
ratings=ratings,
rating_counts=rating_counts)

# view all
@app.route('/pageContent')
def page_content():
"""An API for the user to view all the reviews entered"""
intialize_db()
entries = get_all_jobs()
dept_filter_entries = JOBS_DB.distinct("department")
location_filter_entries = JOBS_DB.distinct("locations")
# title_filter_entries = JOBS_DB.distinct("title")
company_filter_entries = JOBS_DB.distinct("company")

# pagination

# print(entries)
page, per_page, offset = get_page_args(
page_parameter="page", per_page_parameter="per_page")
total = len(entries)

if not page or not per_page:
offset = 0
per_page = 10
pagination_entries = entries[offset: offset + per_page]
else:
pagination_entries = entries[offset: offset + per_page]
# print("ELSE!!!")

pagination = Pagination(page=page, per_page=per_page,
total=total, css_framework='bootstrap4')

return render_template('page_content.html', entries=pagination_entries, page=page,
per_page=per_page, pagination=pagination,
dept_filter_entries=dept_filter_entries,
location_filter_entries=location_filter_entries,
company_filter_entries=company_filter_entries)

@app.route('/pageContentPost', methods=['POST'])
def page_content_post():
Expand Down
51 changes: 51 additions & 0 deletions tests/test_gemini_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import unittest
from unittest.mock import patch
from flask import json
from app import app

class TestGeminiResponse(unittest.TestCase):
def setUp(self):
# Configure the Flask test client
self.app = app.test_client()
self.app.testing = True

@patch('app.routes.query_gemini_model') # Mock query_gemini_model function
def test_get_gemini_response(self, mock_query_gemini_model):
"""
Test the /get_gemini_response route when a valid message is sent.
"""
# Sample input data
user_message = "What is the review of the google?"

# Mock the return value of query_gemini_model
mock_query_gemini_model.return_value = ("This is the AI response.", "12345")

# Simulate a POST request with JSON data
response = self.app.post('/get_gemini_response',
data=json.dumps({"message": user_message}),
content_type='application/json')

# Assertions
self.assertEqual(response.status_code, 200) # Ensure the request was successful
response_json = response.get_json()

# Check that the response JSON contains the expected 'ai_message'
expected_ai_message = "This is the AI response.\n\nClick <a href=\"/review?review_id=12345\">here</a> to see the review details."
self.assertEqual(response_json['ai_message'], expected_ai_message)

# Check that query_gemini_model was called once with the correct argument
mock_query_gemini_model.assert_called_once_with(user_message)

@patch('app.routes.query_gemini_model') # Mock query_gemini_model function
def test_get_gemini_response_invalid_json(self, mock_query_gemini_model):

# Simulate a POST request with invalid JSON data
response = self.app.post('/get_gemini_response',
data="invalid json",
content_type='application/json')

# Assertions
self.assertEqual(response.status_code, 400)

if __name__ == '__main__':
unittest.main()
115 changes: 115 additions & 0 deletions tests/test_recommendation_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import unittest
from unittest.mock import patch, MagicMock
from flask import session, redirect, flash
from app import app
import app.routes as routes
from app.recommendation import recommend_jobs

class TestJobRecommendations(unittest.TestCase):
def setUp(self):
# Configure the Flask test client
self.app = app.test_client()
self.app.testing = True

@patch('app.routes.intialize_db')
@patch('app.routes.get_all_jobs')
@patch('app.routes.recommend') # Correct path for recommend_jobs
@patch('app.routes.transform_jobs')
def test_job_recommendations_logged_in(self, mock_transform_jobs, mock_recommend_jobs, mock_get_all_jobs, mock_initialize_db):
"""
Test the /job_recommendations route when the user is logged in and recommendations are available.
"""
# Mock the database initialization
mock_initialize_db.return_value = None

# Mock the return value of get_all_jobs with the provided data format
mock_get_all_jobs.return_value = [
{'id': 1, 'title': 'Job 1','rating': 4, 'recommendation': 2},
{'id': 2, 'title': 'Job 2','rating': 4, 'recommendation': 2}
]

# Mock the return value of recommend_jobs and transform_jobs
mock_recommend_jobs.return_value = [
{'id': 1, 'title': 'Job 1','rating': 4, 'recommendation': 2},
{'id': 2, 'title': 'Job 2','rating': 4, 'recommendation': 1}
]
mock_transform_jobs.return_value = [
{'id': 1, 'title': 'Job 1','rating': 4, 'recommendation': 2},
{'id': 2, 'title': 'Job 2','rating': 4, 'recommendation': 1}
]

# Simulate a logged-in user
with self.app as client:
with client.session_transaction() as sess:
sess['username'] = 'testuser' # Simulate a logged-in user

# Call the route
response = client.get('/job_recommendations')
mock_initialize_db.assert_called_once() # Check if DB initialization was called
#mock_get_all_jobs.assert_called_once() # Check if get_all_jobs was called
mock_recommend_jobs.assert_called_once_with(mock_get_all_jobs.return_value, 'testuser', 10) # Check if recommend_jobs was called
#mock_transform_jobs.assert_called_once_with(mock_recommend_jobs.return_value) # Check if transform_jobs was called
# Assertions
#self.assertEqual(response.status_code, 200) # Success expected
#mock_initialize_db.assert_called_once() # Check if DB initialization was called
#mock_get_all_jobs.assert_called_once() # Check if get_all_jobs was called
#mock_recommend_jobs.assert_called_once_with(mock_get_all_jobs.return_value, 'testuser', 10) # Check if recommend_jobs was called
#mock_transform_jobs.assert_called_once_with(mock_recommend_jobs.return_value) # Check if transform_jobs was called

@patch('app.routes.intialize_db')
@patch('app.routes.get_all_jobs')
def test_job_recommndations_not_logged_in(self, mock_get_all_jobs, mock_initialize_db):

# Mock the database initialization
mock_initialize_db.return_value = None

# Mock the return value of get_all_jobs with the provided data format
mock_get_all_jobs.return_value = [
{'title': 'Web Developer', 'company': 'Oracle', 'description': 'Good', 'locations': 'VA', 'department': 'Development', 'hourly_pay': '40', 'benefits': 'HI', 'review': 'Good', 'rating': '5', 'recommendation': '5', 'author': 'test', 'upvote': 0, 'id': 'Web Developer_Oracle_VA'}
]

# Simulate no user logged in
with self.app as client:
with client.session_transaction() as sess:
sess.pop('username', None) # Ensure no user is logged in

# Call the route
response = client.get('/job_recommendations')

# Assertions
self.assertEqual(response.status_code, 302) # Redirect expected (to login page)

@patch('app.routes.intialize_db')
@patch('app.routes.get_all_jobs')
@patch('app.routes.recommend') # Correct path for recommend_jobs
@patch('app.routes.transform_jobs')
def test_job_recommendations_no_recommendations(self, mock_transform_jobs, mock_recommend_jobs, mock_get_all_jobs, mock_initialize_db):

# Mock the database initialization
mock_initialize_db.return_value = None

# Mock the return value of get_all_jobs with the provided data format
mock_get_all_jobs.return_value = [
{'title': 'Web Developer', 'company': 'Oracle', 'description': 'Good', 'locations': 'VA', 'department': 'Development', 'hourly_pay': '40', 'benefits': 'HI', 'review': 'Good', 'rating': '5', 'recommendation': '5', 'author': 'test', 'upvote': 0, 'id': 'Web Developer_Oracle_VA'}
]

# Mock the return value of recommend_jobs to be empty
mock_recommend_jobs.return_value = []
mock_transform_jobs.return_value = []

# Simulate a logged-in user
with self.app as client:
with client.session_transaction() as sess:
sess['username'] = 'testuser' # Simulate a logged-in user

# Call the route
response = client.get('/job_recommendations')

# Assertions
self.assertEqual(response.status_code, 200) # Success expected
mock_initialize_db.assert_called_once() # Check if DB initialization was called
mock_get_all_jobs.assert_called_once() # Check if get_all_jobs was called
mock_recommend_jobs.assert_called_once_with(mock_get_all_jobs.return_value, 'testuser', 10) # Check if recommend_jobs was called

if __name__ == '__main__':
unittest.main()
45 changes: 45 additions & 0 deletions tests/test_top_reviews.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import unittest
from unittest.mock import patch
from app import app


class TestTopJobsRoute(unittest.TestCase):
def setUp(self):
# Configure the Flask test client
self.app = app.test_client()
self.app.testing = True

@patch('app.routes.get_all_jobs') # Mock the `get_all_jobs` function
@patch('app.routes.intialize_db') # Mock the database initialization
def test_top_jobs_functions_called(self, mock_initialize_db, mock_get_all_jobs):
# Mock database initialization
mock_initialize_db.return_value = None

# Mock job data to simulate the response from `get_all_jobs()`
mock_get_all_jobs.return_value = [
{'title': 'Job 1', 'recommendation': 5, 'rating': 4},
{'title': 'Job 2', 'recommendation': 3, 'rating': 5},
{'title': 'Job 3', 'recommendation': 4, 'rating': 3},
{'title': 'Job 4', 'recommendation': 6, 'rating': 5},
{'title': 'Job 5', 'recommendation': 2, 'rating': 2},
{'title': 'Job 6', 'recommendation': 7, 'rating': 4},
{'title': 'Job 7', 'recommendation': 8, 'rating': 5},
{'title': 'Job 8', 'recommendation': 9, 'rating': 3},
{'title': 'Job 9', 'recommendation': 5, 'rating': 4},
{'title': 'Job 10', 'recommendation': 3, 'rating': 5},
{'title': 'Job 11', 'recommendation': 4, 'rating': 5},
]

with self.app as client:
# Simulate a request to the /top_jobs route
response = client.get('/top_jobs')

# Check if the status code is 200 (OK)
self.assertEqual(response.status_code, 200)

# 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()

if __name__ == '__main__':
unittest.main()
52 changes: 52 additions & 0 deletions tests/test_transform_review.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import unittest
from collections import defaultdict
from app.routes import transform_jobs # Replace 'yourmodule' with the actual module name

class TestTransformJobs(unittest.TestCase):
def test_transform_jobs(self):
# Input data
jobs = [
{'title': 'Engineer', 'company': 'ABC', 'locations': 'NY', 'department': 'R&D', 'salary': 100000},
{'title': 'Engineer', 'company': 'ABC', 'locations': 'NY', 'department': 'R&D', 'benefits': 'Health'},
{'title': 'Manager', 'company': 'XYZ', 'locations': 'CA', 'department': 'Sales', 'salary': 120000},
{'title': 'Engineer', 'company': 'ABC', 'locations': 'NY', 'department': 'R&D', 'bonus': 5000},
{'title': 'Manager', 'company': 'XYZ', 'locations': 'CA', 'department': 'Sales', 'benefits': '401k'},
]

# Expected output
expected_output = [
{
'title': 'Engineer',
'company': 'ABC',
'locations': 'NY',
'department': 'R&D',
'other_attributes': [
{'salary': 100000},
{'benefits': 'Health'},
{'bonus': 5000},
],
},
{
'title': 'Manager',
'company': 'XYZ',
'locations': 'CA',
'department': 'Sales',
'other_attributes': [
{'salary': 120000},
{'benefits': '401k'},
],
},
]

# Call the function
result = transform_jobs(jobs)

# Sort the results to ensure order doesn't affect comparison
result_sorted = sorted(result, key=lambda x: (x['title'], x['company'], x['locations'], x['department']))
expected_output_sorted = sorted(expected_output, key=lambda x: (x['title'], x['company'], x['locations'], x['department']))

# Assert the results match the expected output
self.assertEqual(result_sorted, expected_output_sorted)

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

0 comments on commit a0f07fb

Please sign in to comment.