diff --git a/test_pytest.py b/test_pytest.py index a331462..8b4a042 100644 --- a/test_pytest.py +++ b/test_pytest.py @@ -148,81 +148,75 @@ def test_correct_spelling(text, expected): assert response.json['after'] == expected -@pytest.fixture -def unique_resume_data_fixture(): - ''' - Setup temporary file to test load_data - ''' - # Create a temporary directory and file for testing - with tempfile.TemporaryDirectory() as temp_dir: - test_file_path = os.path.join(temp_dir, 'test_resume.json') - test_data = { - "user": [ - { - "name": "Jackie Stewart", - "phone_number": "+4478322678", - "email_address": "jack@resume.com" - } - ], - "experience": [ - { - "title": "Software Developer", - "company": "A Cool Company", - "start_date": "October 2022", - "end_date": "Present", - "description": "Writing Python Code", - "logo": "example-logo.png" - } - ], - "education": [ - { - "degree": "Computer Science", - "institution": "University of Tech", - "start_date": "September 2019", - "end_date": "July 2022", - "grade": "80%", - "logo": "example-logo.png" - } - ], - "skill": [ - { - "name": "Python", - "experience": "1-2 Years", - "logo": "example-logo.png" - } - ] - } - with open(test_file_path, 'w', encoding="utf-8") as file: - json.dump(test_data, file) - - yield test_file_path, test_data - - -def test_load_data(unique_resume_data_fixture): - ''' - Test the load_data util function - ''' - test_file_path, test_data = unique_resume_data_fixture - - # Test if the load_data function successfully loads the data - data = load_data(test_file_path) - assert data == test_data - - # Test if the load_data function handles file not found - non_existent_file = os.path.join(tempfile.gettempdir(), 'non_existent_file.json') - data = load_data(non_existent_file) - assert data is None - - # Test if the load_data function handles invalid JSON - invalid_json_file = os.path.join(tempfile.gettempdir(), 'invalid_resume.json') - with open(invalid_json_file, 'w', encoding="utf-8") as file: - file.write('{invalid_json}') - - data = load_data(invalid_json_file) - assert data is None - - if os.path.exists(invalid_json_file): - os.remove(invalid_json_file) +def test_load_data(): + ''' + Test the load_data function with various scenarios + ''' + # Setup test data + test_data = { + "user": [ + { + "name": "Jackie Stewart", + "phone_number": "+4478322678", + "email_address": "jack@resume.com" + } + ], + "experience": [ + { + "title": "Software Developer", + "company": "A Cool Company", + "start_date": "October 2022", + "end_date": "Present", + "description": "Writing Python Code", + "logo": "example-logo.png" + } + ], + "education": [ + { + "degree": "Computer Science", + "institution": "University of Tech", + "start_date": "September 2019", + "end_date": "July 2022", + "grade": "80%", + "logo": "example-logo.png" + } + ], + "skill": [ + { + "name": "Python", + "experience": "1-2 Years", + "logo": "example-logo.png" + } + ] + } + # Create a temporary file with test data + with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json') as temp_file: + json.dump(test_data, temp_file) + test_file_path = temp_file.name + + try: + # Test 1: Check if load_data function successfully loads the data + data = load_data(test_file_path) + assert data == test_data, "Loaded data does not match test data" + + # Test 2: Check if load_data function handles file not found + non_existent_file = os.path.join(tempfile.gettempdir(), 'non_existent_file.json') + data = load_data(non_existent_file) + assert data is None, "load_data should return None for non-existent file" + + # Test 3: Check if load_data function handles invalid JSON + with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json') as invalid_file: + invalid_file.write('{invalid_json}') + invalid_json_file = invalid_file.name + + data = load_data(invalid_json_file) + assert data is None, "load_data should return None for invalid JSON" + + finally: + # Cleanup + os.unlink(test_file_path) + if 'invalid_json_file' in locals(): + os.unlink(invalid_json_file) # Test cases for AI-suggested improved descriptions