Open wesslen opened 6 months ago
Here is a basic outline for unit tests you might consider:
import pytest
import json
import os
from your_script_name import create_qmd_file # Make sure to import your functions correctly
from pathlib import Path
from datetime import datetime
# Create a fixture for sample data
@pytest.fixture
def example_data():
return {
"meta": {
"title": "Sample Article",
"subtitle": "An overview of a sample article",
"model": "Test Model",
"publish_date": "2023-04-01",
"url": "https://example.com/sample-article",
"image": None,
"categories": ["Research", "Sample"],
"is_truncated": False,
"word_count": 500
},
"text": "This is a summary of the sample article."
}
def test_template_rendering(tmp_path, example_data):
create_qmd_file(example_data, tmp_path)
folder_name = example_data["meta"]["title"].replace(" ", "_")
expected_file_name = "2023-04-01-Sample_Article.qmd"
expected_file_path = tmp_path / folder_name / expected_file_name
assert expected_file_path.is_file()
with open(expected_file_path, 'r') as file:
content = file.read()
assert "title: \"Sample Article\"" in content
assert "author: \"Test Model\"" in content
assert "This is a summary of the sample article." in content
def test_output_folder_creation(tmp_path, example_data):
create_qmd_file(example_data, tmp_path)
folder_name = example_data["meta"]["title"].replace(" ", "_")
folder_path = tmp_path / folder_name
assert folder_path.is_dir()
def test_file_creation_without_image(tmp_path, example_data):
example_data_no_image = json.loads(json.dumps(example_data)) # deep copy
example_data_no_image["meta"]["image"] = None
create_qmd_file(example_data_no_image, tmp_path)
folder_name = example_data_no_image["meta"]["title"].replace(" ", "_")
expected_file_name = "2023-04-01-Sample_Article.qmd"
expected_file_path = tmp_path / folder_name / expected_file_name
assert expected_file_path.is_file()
with open(expected_file_path, 'r') as file:
content = file.read()
assert "../../../bayesian-beagle.png" in content
def test_file_already_exists(tmp_path, example_data):
create_qmd_file(example_data, tmp_path)
folder_name = example_data["meta"]["title"].replace(" ", "_")
expected_file_name = "2023-04-01-Sample_Article.qmd"
expected_file_path = tmp_path / folder_name / expected_file_name
assert expected_file_path.is_file()
# Check if the logging message is correct when the file already exists
with pytest.raises(FileExistsError):
create_qmd_file(example_data, tmp_path)
# Additional tests can be added here...
# Execute the tests with pytest from the terminal:
# pytest your_test_script_name.py
Here's what changed transitioning to pytest
:
tmp_path
fixture, which pytest
provides out of the box for handling temporary directory creation that automatically gets cleaned up after tests.pytest.raises()
to assert that an error is raised when needed.assert
statement, which is one of the features of pytest
that simplifies the syntax.example_data
fixture that initial setup code writes, which can be reused across different tests.Remember to replace your_test_script_name.py
with the actual file name of your tests, and replace your_script_name
with the actual name of the module (Python script) that contains the create_qmd_file
function.
Please note that pytest
doesn't require a class-based structure for tests. This can lead to much simpler code, especially for more straightforward test cases.
is_valid_arxiv_id()
.extract_text_from_html()
.remove_double_quotes()
.count_words()
.truncate_string()
.extract_first_png_image()
.OpenAIAssistant.process_text()
.summarize
command function.However, in this answer, I'm going to focus on writing mock unit tests for these functionalities because we typically can't make network requests to external services, like arXiv or OpenAI, in our testing environments.
Here is the outline of the tests using pytest and unittest.mock:
Remember, these examples are just templates—you will have to adapt them to fit the actual implementation details and to verify the expected outputs. Additionally, you should mock any network interactions to avoid performing actual HTTP requests during testing.
Please also ensure that you have installed
pytest
(pip install pytest
) and have the necessary imports at the beginning of your test script. The...
dots in the imports comment mean you should import any other functions or classes you create tests for from your actual script.When preparing your test environment, take care to set up any necessary mock responses and ensure that the function calls within your mock tests match the actual calls the functions make. This will mean examining the code and understanding how each unit interacts with other services.