|
| 1 | +import pytest |
| 2 | +from map2loop.data_checks import validate_config_dictionary |
| 3 | + |
| 4 | + |
| 5 | +@pytest.fixture |
| 6 | +def valid_config(): |
| 7 | + return { |
| 8 | + "structure": { |
| 9 | + "orientation_type": "dip direction", |
| 10 | + "dipdir_column": "azimuth", |
| 11 | + "dip_column": "inclinatn", |
| 12 | + "description_column": "DESCRIPTION", |
| 13 | + "bedding_text": "bed", |
| 14 | + "overturned_column": "no_col", |
| 15 | + "overturned_text": "blah", |
| 16 | + "objectid_column": "geographic", |
| 17 | + "desciption_column": "sub_type" |
| 18 | + }, |
| 19 | + "geology": { |
| 20 | + "unitname_column": "formatted_", |
| 21 | + "alt_unitname_column": "abbreviate", |
| 22 | + "group_column": "no_col", |
| 23 | + "supergroup_column": "interpreta", |
| 24 | + "description_column": "text_descr", |
| 25 | + "minage_column": "no_col", |
| 26 | + "maxage_column": "no_col", |
| 27 | + "rocktype_column": "rank", |
| 28 | + "alt_rocktype_column": "type", |
| 29 | + "sill_text": "sill", |
| 30 | + "intrusive_text": "intrusion", |
| 31 | + "volcanic_text": "volc", |
| 32 | + "objectid_column": "ID", |
| 33 | + "ignore_lithology_codes": ["cover"] |
| 34 | + }, |
| 35 | + "fault": { |
| 36 | + "structtype_column": "featuretyp", |
| 37 | + "fault_text": "s", |
| 38 | + "dip_null_value": "0", |
| 39 | + "dipdir_flag": "num", |
| 40 | + "dipdir_column": "no_col", |
| 41 | + "dip_column": "no_col", |
| 42 | + "orientation_type": "dip direction", |
| 43 | + "dipestimate_column": "no_col", |
| 44 | + "dipestimate_text": "no_col", |
| 45 | + "name_column": "no_col", |
| 46 | + "objectid_column": "geographic", |
| 47 | + "minimum_fault_length": 100.0, |
| 48 | + "ignore_fault_codes": [] |
| 49 | + }, |
| 50 | + "fold": { |
| 51 | + "structtype_column": "featuretyp", |
| 52 | + "fold_text": "fold", |
| 53 | + "description_column": "no_col", |
| 54 | + "synform_text": "syn", |
| 55 | + "foldname_column": "NAME", |
| 56 | + "objectid_column": "geographic" |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | +def test_valid_config_no_errors(valid_config): |
| 62 | + # Should not raise any error |
| 63 | + validate_config_dictionary(valid_config) |
| 64 | + |
| 65 | + |
| 66 | +def test_missing_required_section(valid_config): |
| 67 | + |
| 68 | + config_missing_structure = dict(valid_config) |
| 69 | + del config_missing_structure["structure"] # remove required section |
| 70 | + |
| 71 | + with pytest.raises(ValueError) as exc_info: |
| 72 | + validate_config_dictionary(config_missing_structure) |
| 73 | + assert "Missing required section 'structure'" in str(exc_info.value) |
| 74 | + |
| 75 | + |
| 76 | +def test_missing_required_key(valid_config): |
| 77 | + |
| 78 | + config_missing_dip = dict(valid_config) |
| 79 | + |
| 80 | + del config_missing_dip["structure"]["dip_column"] # remove required key |
| 81 | + |
| 82 | + with pytest.raises(ValueError) as exc_info: |
| 83 | + validate_config_dictionary(config_missing_dip) |
| 84 | + assert "Missing required key 'dip_column' for 'structure'" in str(exc_info.value) |
| 85 | + |
| 86 | + |
| 87 | +def test_unrecognized_section(valid_config): |
| 88 | + |
| 89 | + config_extra_section = dict(valid_config) |
| 90 | + config_extra_section["random_section"] = {"random_key": "random_value"} |
| 91 | + |
| 92 | + with pytest.raises(ValueError) as exc_info: |
| 93 | + validate_config_dictionary(config_extra_section) |
| 94 | + assert "Unrecognized section 'random_section'" in str(exc_info.value) |
| 95 | + |
| 96 | + |
| 97 | +def test_unrecognized_key_in_section(valid_config): |
| 98 | + |
| 99 | + config_extra_key = dict(valid_config) |
| 100 | + config_extra_key["structure"]["random_key"] = "random_value" |
| 101 | + |
| 102 | + with pytest.raises(ValueError) as exc_info: |
| 103 | + validate_config_dictionary(config_extra_key) |
| 104 | + assert "Key 'random_key' is not an allowed key in the 'structure' section." in str(exc_info.value) |
| 105 | + |
| 106 | + |
| 107 | +def test_legacy_key_detected(valid_config): |
| 108 | + |
| 109 | + config_with_legacy = dict(valid_config) |
| 110 | + config_with_legacy["structure"]["otype"] = "legacy_value" # 'otype' --> legacy key |
| 111 | + with pytest.raises(ValueError) as exc_info: |
| 112 | + validate_config_dictionary(config_with_legacy) |
| 113 | + assert "Legacy key found in config - 'otype'" in str(exc_info.value) |
| 114 | + |
| 115 | + |
| 116 | +def test_minimum_fault_length_wrong_type(valid_config): |
| 117 | + |
| 118 | + config_wrong_mfl = dict(valid_config) |
| 119 | + config_wrong_mfl["fault"]["minimum_fault_length"] = "one_hundred" # invalid type |
| 120 | + |
| 121 | + with pytest.raises(ValueError) as exc_info: |
| 122 | + validate_config_dictionary(config_wrong_mfl) |
| 123 | + assert "minimum_fault_length must be a number" in str(exc_info.value) |
| 124 | + |
| 125 | + |
| 126 | +def test_minimum_fault_length_missing(valid_config): |
| 127 | + """ |
| 128 | + Remove minimum_fault_length entirely. That should be fine (None -> no check). |
| 129 | + """ |
| 130 | + config_no_mfl = dict(valid_config) |
| 131 | + del config_no_mfl["fault"]["minimum_fault_length"] |
| 132 | + |
| 133 | + # Should not raise any error, as it's optional |
| 134 | + validate_config_dictionary(config_no_mfl) |
| 135 | + |
0 commit comments