From ac979ad67a86214de142870aa905ff5ca3d35742 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 22 Mar 2026 15:14:03 +0000 Subject: [PATCH] Initial upload: EnvSchema v0.1.0 with CI/CD workflow --- tests/unit/test_schema.py | 174 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 tests/unit/test_schema.py diff --git a/tests/unit/test_schema.py b/tests/unit/test_schema.py new file mode 100644 index 0000000..ffbe935 --- /dev/null +++ b/tests/unit/test_schema.py @@ -0,0 +1,174 @@ +"""Unit tests for schema parsing.""" + +import json +import tempfile +from pathlib import Path + +import pytest + +from envschema.schema import ( + Schema, + EnvVar, + EnvVarType, + load_schema_from_file, + load_json_schema, + load_yaml_schema, +) + + +class TestEnvVar: + """Tests for EnvVar model.""" + + def test_env_var_creation(self): + var = EnvVar(name="TEST_VAR", type=EnvVarType.STRING) + assert var.name == "TEST_VAR" + assert var.type == EnvVarType.STRING + assert var.required is False + assert var.default is None + + def test_env_var_with_all_fields(self): + var = EnvVar( + name="DATABASE_URL", + type=EnvVarType.STRING, + required=True, + default="postgres://localhost", + description="Database connection string", + pattern=r"^postgres://.*", + ) + assert var.required is True + assert var.default == "postgres://localhost" + assert var.description == "Database connection string" + assert var.pattern == r"^postgres://.*" + + def test_env_var_name_uppercase(self): + var = EnvVar(name="test_var") + assert var.name == "TEST_VAR" + + def test_env_var_invalid_name(self): + with pytest.raises(ValueError): + EnvVar(name="invalid name with spaces") + + +class TestSchema: + """Tests for Schema model.""" + + def test_schema_creation(self): + schema = Schema() + assert schema.version == "1.0" + assert schema.envvars == [] + + def test_schema_with_vars(self): + schema = Schema( + envvars=[ + EnvVar(name="VAR1", type=EnvVarType.STRING), + EnvVar(name="VAR2", type=EnvVarType.INTEGER, required=True), + ] + ) + assert len(schema.envvars) == 2 + + def test_get_var(self): + schema = Schema( + envvars=[ + EnvVar(name="DATABASE_URL", type=EnvVarType.STRING), + ] + ) + var = schema.get_var("DATABASE_URL") + assert var is not None + assert var.name == "DATABASE_URL" + + def test_get_var_case_insensitive(self): + schema = Schema( + envvars=[ + EnvVar(name="DATABASE_URL", type=EnvVarType.STRING), + ] + ) + var = schema.get_var("database_url") + assert var is not None + + def test_get_var_not_found(self): + schema = Schema() + var = schema.get_var("NONEXISTENT") + assert var is None + + def test_get_required_vars(self): + schema = Schema( + envvars=[ + EnvVar(name="VAR1", required=True), + EnvVar(name="VAR2", required=False), + EnvVar(name="VAR3", required=True), + ] + ) + required = schema.get_required_vars() + assert len(required) == 2 + assert {v.name for v in required} == {"VAR1", "VAR3"} + + +class TestLoadJsonSchema: + """Tests for JSON schema loading.""" + + def test_load_valid_json_schema(self): + json_content = json.dumps({ + "version": "1.0", + "envVars": [ + {"name": "TEST_VAR", "type": "str"} + ] + }) + schema = load_json_schema(json_content) + assert schema.version == "1.0" + assert len(schema.envvars) == 1 + + def test_load_invalid_json(self): + with pytest.raises(ValueError, match="Invalid JSON"): + load_json_schema("not valid json") + + def test_load_invalid_schema_structure(self): + with pytest.raises((ValueError, Exception), match="Invalid schema"): + load_json_schema('{"version": "1.0", "envVars": [{"name": "VAR", "type": "invalid_type"}]}') + + +class TestLoadYamlSchema: + """Tests for YAML schema loading.""" + + def test_load_valid_yaml_schema(self): + yaml_content = """ +version: "1.0" +envVars: + - name: TEST_VAR + type: str +""" + schema = load_yaml_schema(yaml_content) + assert schema.version == "1.0" + assert len(schema.envvars) == 1 + + def test_load_invalid_yaml(self): + with pytest.raises(ValueError, match="Invalid YAML"): + load_yaml_schema("invalid: yaml: content:") + + +class TestLoadSchemaFromFile: + """Tests for file-based schema loading.""" + + def test_load_json_file(self, tmp_path): + schema_file = tmp_path / "schema.json" + schema_file.write_text(json.dumps({ + "version": "1.0", + "envVars": [{"name": "TEST", "type": "str"}] + })) + schema = load_schema_from_file(str(schema_file)) + assert schema.version == "1.0" + + def test_load_yaml_file(self, tmp_path): + schema_file = tmp_path / "schema.yaml" + schema_file.write_text('version: "1.0"\nenvVars: []') + schema = load_schema_from_file(str(schema_file)) + assert schema.version == "1.0" + + def test_file_not_found(self): + with pytest.raises(FileNotFoundError): + load_schema_from_file("/nonexistent/path/schema.json") + + def test_unsupported_format(self, tmp_path): + schema_file = tmp_path / "schema.txt" + schema_file.write_text("some content") + with pytest.raises(ValueError, match="Unsupported schema format"): + load_schema_from_file(str(schema_file)) \ No newline at end of file