|
1 | 1 | """Tests for the MDIO Environment API.""" |
2 | 2 |
|
3 | 3 | import os |
| 4 | +from collections.abc import Callable |
4 | 5 | from unittest.mock import patch |
5 | 6 |
|
6 | 7 | import pytest |
|
12 | 13 | class TestEnvironment: |
13 | 14 | """Test the Environment API class methods.""" |
14 | 15 |
|
15 | | - @pytest.mark.parametrize("method,expected_type", [ |
16 | | - (Environment.export_cpus, int), |
17 | | - (Environment.import_cpus, int), |
18 | | - (Environment.grid_sparsity_ratio_warn, float), |
19 | | - (Environment.grid_sparsity_ratio_limit, float), |
20 | | - (Environment.save_segy_file_header, bool), |
21 | | - (Environment.raw_headers, bool), |
22 | | - (Environment.ignore_checks, bool), |
23 | | - (Environment.cloud_native, bool), |
24 | | - ]) |
25 | | - def test_default_values(self, method, expected_type): |
| 16 | + @pytest.mark.parametrize( |
| 17 | + ("method", "expected_type"), |
| 18 | + [ |
| 19 | + (Environment.export_cpus, int), |
| 20 | + (Environment.import_cpus, int), |
| 21 | + (Environment.grid_sparsity_ratio_warn, float), |
| 22 | + (Environment.grid_sparsity_ratio_limit, float), |
| 23 | + (Environment.save_segy_file_header, bool), |
| 24 | + (Environment.raw_headers, bool), |
| 25 | + (Environment.ignore_checks, bool), |
| 26 | + (Environment.cloud_native, bool), |
| 27 | + ], |
| 28 | + ) |
| 29 | + def test_default_values(self, method: Callable[[], object], expected_type: type) -> None: |
26 | 30 | """Test all methods return correct types with defaults.""" |
27 | 31 | with patch.dict(os.environ, {}, clear=True): |
28 | 32 | result = method() |
29 | 33 | assert isinstance(result, expected_type) |
30 | 34 |
|
31 | | - def test_mdio_segy_spec_defaults_to_none(self): |
| 35 | + def test_mdio_segy_spec_defaults_to_none(self) -> None: |
32 | 36 | """Test mdio_segy_spec returns None by default.""" |
33 | 37 | with patch.dict(os.environ, {}, clear=True): |
34 | 38 | result = Environment.mdio_segy_spec() |
35 | 39 | assert result is None |
36 | 40 |
|
37 | | - @pytest.mark.parametrize("env_var,value,method,expected", [ |
38 | | - ("MDIO__EXPORT__CPU_COUNT", "8", Environment.export_cpus, 8), |
39 | | - ("MDIO__IMPORT__CPU_COUNT", "4", Environment.import_cpus, 4), |
40 | | - ("MDIO__GRID__SPARSITY_RATIO_WARN", "3.5", Environment.grid_sparsity_ratio_warn, 3.5), |
41 | | - ("MDIO__GRID__SPARSITY_RATIO_LIMIT", "15.0", Environment.grid_sparsity_ratio_limit, 15.0), |
42 | | - ("MDIO__SEGY__SPEC", "/path/to/spec.json", Environment.mdio_segy_spec, "/path/to/spec.json"), |
43 | | - ]) |
44 | | - def test_env_var_overrides(self, env_var, value, method, expected): |
| 41 | + @pytest.mark.parametrize( |
| 42 | + ("env_var", "value", "method", "expected"), |
| 43 | + [ |
| 44 | + ("MDIO__EXPORT__CPU_COUNT", "8", Environment.export_cpus, 8), |
| 45 | + ("MDIO__IMPORT__CPU_COUNT", "4", Environment.import_cpus, 4), |
| 46 | + ("MDIO__GRID__SPARSITY_RATIO_WARN", "3.5", Environment.grid_sparsity_ratio_warn, 3.5), |
| 47 | + ("MDIO__GRID__SPARSITY_RATIO_LIMIT", "15.0", Environment.grid_sparsity_ratio_limit, 15.0), |
| 48 | + ("MDIO__SEGY__SPEC", "/path/to/spec.json", Environment.mdio_segy_spec, "/path/to/spec.json"), |
| 49 | + ], |
| 50 | + ) |
| 51 | + def test_env_var_overrides(self, env_var: str, value: str, method: Callable[[], object], expected: object) -> None: |
45 | 52 | """Test environment variables override defaults.""" |
46 | 53 | with patch.dict(os.environ, {env_var: value}): |
47 | 54 | result = method() |
48 | 55 | assert result == expected |
49 | 56 |
|
50 | | - @pytest.mark.parametrize("method,env_var", [ |
51 | | - (Environment.save_segy_file_header, "MDIO__IMPORT__SAVE_SEGY_FILE_HEADER"), |
52 | | - (Environment.raw_headers, "MDIO__IMPORT__RAW_HEADERS"), |
53 | | - (Environment.ignore_checks, "MDIO_IGNORE_CHECKS"), |
54 | | - (Environment.cloud_native, "MDIO__IMPORT__CLOUD_NATIVE"), |
55 | | - ]) |
56 | | - @pytest.mark.parametrize("value,expected", [ |
57 | | - ("1", True), ("true", True), ("yes", True), ("on", True), |
58 | | - ("0", False), ("false", False), ("no", False), ("off", False), |
59 | | - ("anything_else", False), ("", False), |
60 | | - ]) |
61 | | - def test_boolean_parsing(self, method, env_var, value, expected): |
| 57 | + @pytest.mark.parametrize( |
| 58 | + ("method", "env_var"), |
| 59 | + [ |
| 60 | + (Environment.save_segy_file_header, "MDIO__IMPORT__SAVE_SEGY_FILE_HEADER"), |
| 61 | + (Environment.raw_headers, "MDIO__IMPORT__RAW_HEADERS"), |
| 62 | + (Environment.ignore_checks, "MDIO_IGNORE_CHECKS"), |
| 63 | + (Environment.cloud_native, "MDIO__IMPORT__CLOUD_NATIVE"), |
| 64 | + ], |
| 65 | + ) |
| 66 | + @pytest.mark.parametrize( |
| 67 | + ("value", "expected"), |
| 68 | + [ |
| 69 | + ("1", True), |
| 70 | + ("true", True), |
| 71 | + ("yes", True), |
| 72 | + ("on", True), |
| 73 | + ("0", False), |
| 74 | + ("false", False), |
| 75 | + ("no", False), |
| 76 | + ("off", False), |
| 77 | + ("anything_else", False), |
| 78 | + ("", False), |
| 79 | + ], |
| 80 | + ) |
| 81 | + def test_boolean_parsing(self, method: Callable[[], bool], env_var: str, value: str, expected: bool) -> None: |
62 | 82 | """Test boolean parsing for all boolean methods.""" |
63 | 83 | with patch.dict(os.environ, {env_var: value}): |
64 | 84 | result = method() |
65 | 85 | assert result is expected |
66 | 86 |
|
67 | | - @pytest.mark.parametrize("method,env_var", [ |
68 | | - (Environment.export_cpus, "MDIO__EXPORT__CPU_COUNT"), |
69 | | - (Environment.import_cpus, "MDIO__IMPORT__CPU_COUNT"), |
70 | | - ]) |
| 87 | + @pytest.mark.parametrize( |
| 88 | + ("method", "env_var"), |
| 89 | + [ |
| 90 | + (Environment.export_cpus, "MDIO__EXPORT__CPU_COUNT"), |
| 91 | + (Environment.import_cpus, "MDIO__IMPORT__CPU_COUNT"), |
| 92 | + ], |
| 93 | + ) |
71 | 94 | @pytest.mark.parametrize("invalid_value", ["invalid", "not_a_number", ""]) |
72 | | - def test_int_validation_errors(self, method, env_var, invalid_value): |
| 95 | + def test_int_validation_errors(self, method: Callable[[], int], env_var: str, invalid_value: str) -> None: |
73 | 96 | """Test integer methods raise errors for invalid values.""" |
74 | 97 | with patch.dict(os.environ, {env_var: invalid_value}): |
75 | 98 | with pytest.raises(EnvironmentFormatError) as exc_info: |
76 | 99 | method() |
77 | 100 | assert env_var in str(exc_info.value) |
78 | 101 | assert "int" in str(exc_info.value) |
79 | 102 |
|
80 | | - @pytest.mark.parametrize("method,env_var", [ |
81 | | - (Environment.grid_sparsity_ratio_warn, "MDIO__GRID__SPARSITY_RATIO_WARN"), |
82 | | - (Environment.grid_sparsity_ratio_limit, "MDIO__GRID__SPARSITY_RATIO_LIMIT"), |
83 | | - ]) |
| 103 | + @pytest.mark.parametrize( |
| 104 | + ("method", "env_var"), |
| 105 | + [ |
| 106 | + (Environment.grid_sparsity_ratio_warn, "MDIO__GRID__SPARSITY_RATIO_WARN"), |
| 107 | + (Environment.grid_sparsity_ratio_limit, "MDIO__GRID__SPARSITY_RATIO_LIMIT"), |
| 108 | + ], |
| 109 | + ) |
84 | 110 | @pytest.mark.parametrize("invalid_value", ["invalid", "not_a_number", ""]) |
85 | | - def test_float_validation_errors(self, method, env_var, invalid_value): |
| 111 | + def test_float_validation_errors(self, method: Callable[[], float], env_var: str, invalid_value: str) -> None: |
86 | 112 | """Test float methods raise errors for invalid values.""" |
87 | 113 | with patch.dict(os.environ, {env_var: invalid_value}): |
88 | 114 | with pytest.raises(EnvironmentFormatError) as exc_info: |
89 | 115 | method() |
90 | 116 | assert env_var in str(exc_info.value) |
91 | 117 | assert "float" in str(exc_info.value) |
92 | 118 |
|
93 | | - def test_environment_isolation(self): |
| 119 | + def test_environment_isolation(self) -> None: |
94 | 120 | """Test that environment changes don't affect other tests.""" |
95 | 121 | original_values = { |
96 | 122 | "cpus": Environment.export_cpus(), |
97 | 123 | "ratio": Environment.grid_sparsity_ratio_warn(), |
98 | 124 | "bool": Environment.save_segy_file_header(), |
99 | 125 | } |
100 | 126 |
|
101 | | - with patch.dict(os.environ, { |
102 | | - "MDIO__EXPORT__CPU_COUNT": "99", |
103 | | - "MDIO__GRID__SPARSITY_RATIO_WARN": "99.9", |
104 | | - "MDIO__IMPORT__SAVE_SEGY_FILE_HEADER": "true" |
105 | | - }): |
| 127 | + with patch.dict( |
| 128 | + os.environ, |
| 129 | + { |
| 130 | + "MDIO__EXPORT__CPU_COUNT": "99", |
| 131 | + "MDIO__GRID__SPARSITY_RATIO_WARN": "99.9", |
| 132 | + "MDIO__IMPORT__SAVE_SEGY_FILE_HEADER": "true", |
| 133 | + }, |
| 134 | + ): |
106 | 135 | assert Environment.export_cpus() == 99 |
107 | 136 | assert Environment.grid_sparsity_ratio_warn() == 99.9 |
108 | 137 | assert Environment.save_segy_file_header() is True |
|
0 commit comments