forked from NCSU-SE-2024/PackReview_v3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/TeamBenign/PackReview_v4
- Loading branch information
Showing
5 changed files
with
300 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |