|
| 1 | +import pytest |
| 2 | +from pydantic import ValidationError |
| 3 | +from tempfile import NamedTemporaryFile |
| 4 | +from project_config import ProjectConfig |
| 5 | + |
| 6 | +@pytest.fixture |
| 7 | +def valid_yaml_config(): |
| 8 | + return """ |
| 9 | + gcs_raw_data_bucket_name: "my-bucket" |
| 10 | + taxi_data_years: [2021, 2022] |
| 11 | + taxi_data_months: [1, 2, 3] |
| 12 | + taxi_type: "yellow" |
| 13 | + """ |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def invalid_yaml_config_missing_field(): |
| 17 | + return """ |
| 18 | + taxi_data_years: [2021, 2022] |
| 19 | + taxi_data_months: [1, 2, 3] |
| 20 | + taxi_type: "yellow" |
| 21 | + """ |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def invalid_yaml_config_wrong_type(): |
| 25 | + return """ |
| 26 | + gcs_raw_data_bucket_name: "my-bucket" |
| 27 | + taxi_data_years: "2021, 2022" # Should be a list[int], but it's a string |
| 28 | + taxi_data_months: [1, 2, 3] |
| 29 | + taxi_type: "yellow" |
| 30 | + """ |
| 31 | + |
| 32 | +def test_project_config_from_valid_yaml(valid_yaml_config): |
| 33 | + with NamedTemporaryFile(mode="w", delete=False) as temp_file: |
| 34 | + temp_file.write(valid_yaml_config) |
| 35 | + temp_file.flush() |
| 36 | + config = ProjectConfig.from_yaml(temp_file.name) |
| 37 | + |
| 38 | + assert config.gcs_raw_data_bucket_name == "my-bucket" |
| 39 | + assert config.taxi_data_years == [2021, 2022] |
| 40 | + assert config.taxi_data_months == [1, 2, 3] |
| 41 | + assert config.taxi_type == "yellow" |
| 42 | + |
| 43 | +def test_project_config_missing_field(invalid_yaml_config_missing_field): |
| 44 | + with NamedTemporaryFile(mode="w", delete=False) as temp_file: |
| 45 | + temp_file.write(invalid_yaml_config_missing_field) |
| 46 | + temp_file.flush() |
| 47 | + |
| 48 | + with pytest.raises(ValidationError): |
| 49 | + ProjectConfig.from_yaml(temp_file.name) |
| 50 | + |
| 51 | +def test_project_config_wrong_type(invalid_yaml_config_wrong_type): |
| 52 | + with NamedTemporaryFile(mode="w", delete=False) as temp_file: |
| 53 | + temp_file.write(invalid_yaml_config_wrong_type) |
| 54 | + temp_file.flush() |
| 55 | + |
| 56 | + with pytest.raises(ValidationError): |
| 57 | + ProjectConfig.from_yaml(temp_file.name) |
0 commit comments