Skip to content

Commit

Permalink
added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nazmul-me committed Nov 27, 2024
1 parent 00cffca commit 14f9f2d
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
4 changes: 4 additions & 0 deletions test_output/test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name,age,city
John,30,New York
Anna,22,London
Mike,32,San Francisco
52 changes: 52 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from bson import ObjectId
from tests.test_forum import TestForum
from tests.test_dashboard import TestDashboard
import json, os, csv

class FlaskAppTests(unittest.TestCase):
"""
Expand Down Expand Up @@ -115,6 +116,57 @@ def test_home_page(self):
response = self.client.get('/home')
self.assertEqual(response.status_code, 200)
self.assertIn(b"Home", response.data)
@patch('app.routes.query_gemini_model')
def test_get_gemini_response(self, mock_query_gemini_model):
"""Test get_gemini_response route."""
mock_query_gemini_model.return_value = ("AI response", "12345")
data = {'message': 'Tell me about job reviews'}
response = self.client.post('/get_gemini_response', data=json.dumps(data), content_type='application/json')
self.assertEqual(response.status_code, 200)
response_data = json.loads(response.data)
self.assertIn('AI response', response_data['ai_message'])
self.assertIn('Click <a href=', response_data['ai_message'])

def test_dict_to_csv(self):
"""Test dict_to_csv function."""
data = [
{'name': 'John', 'age': 30, 'city': 'New York'},
{'name': 'Anna', 'age': 22, 'city': 'London'},
{'name': 'Mike', 'age': 32, 'city': 'San Francisco'}
]
csv_path = 'test_output/test.csv'
from app.routes import dict_to_csv
dict_to_csv(data, csv_path)
self.assertTrue(os.path.exists(csv_path))
with open(csv_path, mode='r') as file:
reader = csv.DictReader(file)
rows = list(reader)
self.assertEqual(len(rows), 3)
self.assertEqual(rows[0]['name'], 'John')
self.assertEqual(rows[1]['age'], '22')
self.assertEqual(rows[2]['city'], 'San Francisco')
@patch('app.routes.get_all_jobs')
@patch('app.routes.recommend_jobs')
@patch('app.routes.transform_jobs')
def test_job_recommendations_logged_in(self, mock_transform_jobs, mock_recommend_jobs, mock_get_all_jobs):
"""Test job recommendations route when user is logged in."""
with self.client.session_transaction() as session:
session['username'] = 'testuser'

mock_get_all_jobs.return_value = [{'title': 'Job1', 'company': 'Company A', 'locations': 'Location 1', 'department': 'Engineering'}]
mock_recommend_jobs.return_value = [{'title': 'Job1', 'company': 'Company A', 'locations': 'Location 1', 'department': 'Engineering'}]
mock_transform_jobs.return_value = [{'title': 'Job1', 'company': 'Company A', 'locations': 'Location 1', 'department': 'Engineering', 'other_attributes': []}]

response = self.client.get('/job_recommendations')
self.assertEqual(response.status_code, 200)
self.assertIn(b'Job1', response.data)
self.assertIn(b'Company A', response.data)

def test_job_recommendations_not_logged_in(self):
"""Test job recommendations route when user is not logged in."""
response = self.client.get('/job_recommendations')
self.assertEqual(response.status_code, 302)
self.assertIn('/login', response.location)

def test_forum(self):
"""Test the forum class."""
Expand Down
96 changes: 96 additions & 0 deletions tests/test_transform_jobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import unittest
from collections import defaultdict
from app.routes import transform_jobs # Adjust the import based on your project structure

class TestTransformJobs(unittest.TestCase):
def test_transform_jobs(self):
"""Test the transform_jobs function."""
jobs = [
{
'title': 'Software Engineer',
'company': 'Company A',
'locations': 'Location 1',
'department': 'Engineering',
'salary': 100000,
'experience': 5
},
{
'title': 'Software Engineer',
'company': 'Company A',
'locations': 'Location 1',
'department': 'Engineering',
'salary': 110000,
'experience': 6
},
{
'title': 'Data Scientist',
'company': 'Company B',
'locations': 'Location 2',
'department': 'Data',
'salary': 120000,
'experience': 4
}
]

expected_output = [
{
'title': 'Software Engineer',
'company': 'Company A',
'locations': 'Location 1',
'department': 'Engineering',
'other_attributes': [
{'salary': 100000, 'experience': 5},
{'salary': 110000, 'experience': 6}
]
},
{
'title': 'Data Scientist',
'company': 'Company B',
'locations': 'Location 2',
'department': 'Data',
'other_attributes': [
{'salary': 120000, 'experience': 4}
]
}
]

result = transform_jobs(jobs)
self.assertEqual(result, expected_output)

def test_transform_jobs_empty(self):
"""Test the transform_jobs function with an empty list."""
jobs = []
expected_output = []
result = transform_jobs(jobs)
self.assertEqual(result, expected_output)

def test_transform_jobs_single_entry(self):
"""Test the transform_jobs function with a single job entry."""
jobs = [
{
'title': 'Software Engineer',
'company': 'Company A',
'locations': 'Location 1',
'department': 'Engineering',
'salary': 100000,
'experience': 5
}
]

expected_output = [
{
'title': 'Software Engineer',
'company': 'Company A',
'locations': 'Location 1',
'department': 'Engineering',
'other_attributes': [
{'salary': 100000, 'experience': 5}
]
}
]

result = transform_jobs(jobs)
self.assertEqual(result, expected_output)

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

0 comments on commit 14f9f2d

Please sign in to comment.