From 2d615f6cea17e7650e9cda89b8368a3eaaab8634 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 22 Mar 2026 15:14:07 +0000 Subject: [PATCH] Initial upload: EnvSchema v0.1.0 with CI/CD workflow --- tests/unit/test_generator.py | 105 +++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 tests/unit/test_generator.py diff --git a/tests/unit/test_generator.py b/tests/unit/test_generator.py new file mode 100644 index 0000000..6c6c17f --- /dev/null +++ b/tests/unit/test_generator.py @@ -0,0 +1,105 @@ +"""Unit tests for the .env.example generator.""" + +import pytest + +from envschema.schema import Schema, EnvVar, EnvVarType +from envschema.generator import generate_env_example, generate_env_example_to_file + + +class TestGenerateEnvExample: + """Tests for generate_env_example function.""" + + def test_empty_schema(self): + schema = Schema() + result = generate_env_example(schema) + assert "# Environment Variables Schema" in result + + def test_basic_variable(self): + schema = Schema( + envvars=[ + EnvVar(name="TEST_VAR", type=EnvVarType.STRING), + ] + ) + result = generate_env_example(schema) + assert "TEST_VAR=" in result + + def test_required_variable(self): + schema = Schema( + envvars=[ + EnvVar(name="REQUIRED_VAR", required=True), + ] + ) + result = generate_env_example(schema) + assert "# REQUIRED" in result + assert "REQUIRED_VAR=" in result + + def test_variable_with_default(self): + schema = Schema( + envvars=[ + EnvVar(name="VAR_WITH_DEFAULT", default="default_value"), + ] + ) + result = generate_env_example(schema) + assert "VAR_WITH_DEFAULT=default_value" in result + + def test_variable_with_description(self): + schema = Schema( + envvars=[ + EnvVar( + name="DESCRIBED_VAR", + description="This is a description", + ), + ] + ) + result = generate_env_example(schema) + assert "# This is a description" in result + + def test_variable_with_type(self): + schema = Schema( + envvars=[ + EnvVar(name="INT_VAR", type=EnvVarType.INTEGER), + ] + ) + result = generate_env_example(schema) + assert "INT_VAR=" in result + + def test_no_descriptions(self): + schema = Schema( + envvars=[ + EnvVar( + name="VAR", + description="Some description", + ), + ] + ) + result = generate_env_example(schema, include_descriptions=False) + assert "Some description" not in result + + def test_multiple_variables(self): + schema = Schema( + envvars=[ + EnvVar(name="VAR1", required=True, description="First var"), + EnvVar(name="VAR2", default="value"), + EnvVar(name="VAR3", type=EnvVarType.INTEGER), + ] + ) + result = generate_env_example(schema) + assert "VAR1=" in result + assert "VAR2=value" in result + assert "VAR3=" in result + + +class TestGenerateEnvExampleToFile: + """Tests for generate_env_example_to_file function.""" + + def test_write_to_file(self, tmp_path): + schema = Schema( + envvars=[ + EnvVar(name="TEST_VAR"), + ] + ) + output_path = tmp_path / ".env.example" + generate_env_example_to_file(schema, str(output_path)) + + content = output_path.read_text() + assert "TEST_VAR=" in content \ No newline at end of file