|
| 1 | +import pytest |
| 2 | +from pathlib import Path |
| 3 | +from env_check import validate_env |
| 4 | + |
| 5 | +VALID_ENV_CONTENT = ( |
| 6 | + "DB_HOST=localhost\n" |
| 7 | + "DB_PORT=5432\n" |
| 8 | + "DB_NAME=analytics\n" |
| 9 | + "DB_USER=admin\n" |
| 10 | + "DB_PASSWORD=s3cret\n" |
| 11 | + "BATCH_SIZE=500\n" |
| 12 | + "MAX_RETRIES=3\n" |
| 13 | + "DRY_RUN=false\n" |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +# AC-H1: All vars valid → no errors, exit 0 |
| 18 | +def test_h1_all_valid(clean_env, tmp_path): |
| 19 | + (tmp_path / ".env").write_text(VALID_ENV_CONTENT, encoding="utf-8") |
| 20 | + errors = validate_env(dotenv_path=tmp_path / ".env") |
| 21 | + assert errors == [] |
| 22 | + |
| 23 | + |
| 24 | +# AC-H2: DRY_RUN accepts true/True/TRUE/1/false/False/0 |
| 25 | +@pytest.mark.parametrize("value", ["true", "True", "TRUE", "1", "false", "False", "0"]) |
| 26 | +def test_h2_dry_run_valid_values(clean_env, tmp_path, value): |
| 27 | + content = VALID_ENV_CONTENT.replace("DRY_RUN=false", f"DRY_RUN={value}") |
| 28 | + (tmp_path / ".env").write_text(content, encoding="utf-8") |
| 29 | + errors = validate_env(dotenv_path=tmp_path / ".env") |
| 30 | + assert errors == [] |
| 31 | + |
| 32 | + |
| 33 | +# AC-H3: DB_PORT boundary values 1 and 65535 |
| 34 | +@pytest.mark.parametrize("port", [1, 65535]) |
| 35 | +def test_h3_db_port_boundary(clean_env, tmp_path, port): |
| 36 | + content = VALID_ENV_CONTENT.replace("DB_PORT=5432", f"DB_PORT={port}") |
| 37 | + (tmp_path / ".env").write_text(content, encoding="utf-8") |
| 38 | + errors = validate_env(dotenv_path=tmp_path / ".env") |
| 39 | + assert errors == [] |
| 40 | + |
| 41 | + |
| 42 | +# AC-H4: MAX_RETRIES=0 → OK |
| 43 | +def test_h4_max_retries_zero(clean_env, tmp_path): |
| 44 | + content = VALID_ENV_CONTENT.replace("MAX_RETRIES=3", "MAX_RETRIES=0") |
| 45 | + (tmp_path / ".env").write_text(content, encoding="utf-8") |
| 46 | + errors = validate_env(dotenv_path=tmp_path / ".env") |
| 47 | + assert errors == [] |
| 48 | + |
| 49 | + |
| 50 | +# AC-S1: .env file doesn't exist → 8 FAIL lines, exit 1 |
| 51 | +def test_s1_no_env_file(clean_env, tmp_path): |
| 52 | + errors = validate_env(dotenv_path=tmp_path / ".env") |
| 53 | + assert len(errors) == 8 |
| 54 | + assert all(e.startswith("[FAIL]") for e in errors) |
| 55 | + |
| 56 | + |
| 57 | +# AC-S2: DB_HOST missing |
| 58 | +def test_s2_db_host_missing(clean_env, tmp_path): |
| 59 | + content = "\n".join( |
| 60 | + line for line in VALID_ENV_CONTENT.splitlines() |
| 61 | + if not line.startswith("DB_HOST=") |
| 62 | + ) + "\n" |
| 63 | + (tmp_path / ".env").write_text(content, encoding="utf-8") |
| 64 | + errors = validate_env(dotenv_path=tmp_path / ".env") |
| 65 | + assert "[FAIL] DB_HOST: 未设置" in errors |
| 66 | + |
| 67 | + |
| 68 | +# AC-S3: DB_PASSWORD empty string |
| 69 | +def test_s3_db_password_empty(clean_env, tmp_path): |
| 70 | + content = VALID_ENV_CONTENT.replace("DB_PASSWORD=s3cret", "DB_PASSWORD=") |
| 71 | + (tmp_path / ".env").write_text(content, encoding="utf-8") |
| 72 | + errors = validate_env(dotenv_path=tmp_path / ".env") |
| 73 | + assert "[FAIL] DB_PASSWORD: 值为空字符串" in errors |
| 74 | + |
| 75 | + |
| 76 | +# AC-S4: DB_PORT non-integer |
| 77 | +def test_s4_db_port_not_int(clean_env, tmp_path): |
| 78 | + content = VALID_ENV_CONTENT.replace("DB_PORT=5432", "DB_PORT=abc") |
| 79 | + (tmp_path / ".env").write_text(content, encoding="utf-8") |
| 80 | + errors = validate_env(dotenv_path=tmp_path / ".env") |
| 81 | + assert '[FAIL] DB_PORT: 无法转换为整数(当前值:"abc")' in errors |
| 82 | + |
| 83 | + |
| 84 | +# AC-S5: DB_PORT out of range (0 and 65536) |
| 85 | +@pytest.mark.parametrize("port,expected_val", [(0, 0), (65536, 65536)]) |
| 86 | +def test_s5_db_port_out_of_range(clean_env, tmp_path, port, expected_val): |
| 87 | + content = VALID_ENV_CONTENT.replace("DB_PORT=5432", f"DB_PORT={port}") |
| 88 | + (tmp_path / ".env").write_text(content, encoding="utf-8") |
| 89 | + errors = validate_env(dotenv_path=tmp_path / ".env") |
| 90 | + assert f"[FAIL] DB_PORT: 值超出范围(需满足 1 <= DB_PORT <= 65535,当前值:{expected_val})" in errors |
| 91 | + |
| 92 | + |
| 93 | +# AC-S6: BATCH_SIZE=0 and BATCH_SIZE=-1 |
| 94 | +@pytest.mark.parametrize("size", [0, -1]) |
| 95 | +def test_s6_batch_size_not_positive(clean_env, tmp_path, size): |
| 96 | + content = VALID_ENV_CONTENT.replace("BATCH_SIZE=500", f"BATCH_SIZE={size}") |
| 97 | + (tmp_path / ".env").write_text(content, encoding="utf-8") |
| 98 | + errors = validate_env(dotenv_path=tmp_path / ".env") |
| 99 | + assert "[FAIL] BATCH_SIZE: 值超出范围(需满足 BATCH_SIZE > 0)" in errors |
| 100 | + |
| 101 | + |
| 102 | +# AC-S7: DRY_RUN=yes → invalid bool |
| 103 | +def test_s7_dry_run_invalid(clean_env, tmp_path): |
| 104 | + content = VALID_ENV_CONTENT.replace("DRY_RUN=false", "DRY_RUN=yes") |
| 105 | + (tmp_path / ".env").write_text(content, encoding="utf-8") |
| 106 | + errors = validate_env(dotenv_path=tmp_path / ".env") |
| 107 | + assert '[FAIL] DRY_RUN: 非法布尔值(当前值:"yes",合法值为 true/false/1/0)' in errors |
| 108 | + |
| 109 | + |
| 110 | +# AC-S8: Multiple errors in order |
| 111 | +def test_s8_multiple_errors_in_order(clean_env, tmp_path): |
| 112 | + content = ( |
| 113 | + "DB_HOST=localhost\n" |
| 114 | + "DB_PORT=abc\n" |
| 115 | + "DB_NAME=analytics\n" |
| 116 | + "DB_USER=admin\n" |
| 117 | + "DB_PASSWORD=\n" |
| 118 | + "BATCH_SIZE=-1\n" |
| 119 | + "MAX_RETRIES=3\n" |
| 120 | + "DRY_RUN=yes\n" |
| 121 | + ) |
| 122 | + (tmp_path / ".env").write_text(content, encoding="utf-8") |
| 123 | + errors = validate_env(dotenv_path=tmp_path / ".env") |
| 124 | + assert len(errors) == 4 |
| 125 | + assert '[FAIL] DB_PORT: 无法转换为整数(当前值:"abc")' == errors[0] |
| 126 | + assert "[FAIL] DB_PASSWORD: 值为空字符串" == errors[1] |
| 127 | + assert "[FAIL] BATCH_SIZE: 值超出范围(需满足 BATCH_SIZE > 0)" == errors[2] |
| 128 | + assert '[FAIL] DRY_RUN: 非法布尔值(当前值:"yes",合法值为 true/false/1/0)' == errors[3] |
| 129 | + |
0 commit comments