Add constants.py and its test

Task: MIL-001#3
This commit is contained in:
2026-10-13 15:26:20 +08:00
parent 3db6634b15
commit a44339927c
4 changed files with 114 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
"""Shared pytest configuration for the weather data tests."""
from pathlib import Path
import pytest
@pytest.fixture(autouse=True)
def isolated_working_directory(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
"""Run every test in an empty temporary working directory.
The export has a default file name relative to the working directory. Without
this fixture, a test that uses the default would write into the folder pytest
was started from.
"""
monkeypatch.chdir(tmp_path)
+36
View File
@@ -0,0 +1,36 @@
"""Tests of the constants that the day-25 lectures fix."""
from weather import constants
def test_data_file_is_the_one_of_the_lecture() -> None:
assert constants.DATA_FILE_NAME == "weather_data.csv"
def test_data_file_lies_next_to_the_modules_not_in_the_working_folder() -> None:
assert constants.DATA_FILE.is_absolute()
assert constants.DATA_FILE.parent == constants.PACKAGE_DIR
assert constants.DATA_FILE.name == constants.DATA_FILE_NAME
def test_column_names_match_the_header_of_the_lecture() -> None:
assert constants.COLUMN_DAY == "day"
assert constants.COLUMN_TEMP == "temp"
assert constants.COLUMN_CONDITION == "condition"
def test_temperature_is_the_second_value_of_a_row() -> None:
assert constants.TEMP_COLUMN_INDEX == 1
def test_fahrenheit_factors_give_the_lecture_formula() -> None:
celsius = 100
fahrenheit = (
celsius * constants.FAHRENHEIT_NUMERATOR / constants.FAHRENHEIT_DENOMINATOR
+ constants.FAHRENHEIT_OFFSET
)
assert fahrenheit == 212
def test_exported_file_has_the_name_of_the_lecture() -> None:
assert constants.EXPORT_FILE_NAME == "new_data.csv"