Files
dataforge-cli/tests/test_dataforge_commands.py
7000pctAUTO 9757d6e358
Some checks failed
CI / test (ubuntu-latest, 3.10) (push) Has been cancelled
CI / test (ubuntu-latest, 3.11) (push) Has been cancelled
CI / test (ubuntu-latest, 3.12) (push) Has been cancelled
CI / test (ubuntu-latest, 3.8) (push) Has been cancelled
CI / test (ubuntu-latest, 3.9) (push) Has been cancelled
CI / test-minimal (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / build (push) Has been cancelled
CI / release (push) Has been cancelled
Fix CI/CD issues: linting errors and test file corruption
2026-02-03 05:13:39 +00:00

248 lines
7.8 KiB
Python

"""Tests for CLI commands."""
import os
import tempfile
from unittest.mock import patch
import pytest
from dataforge.commands import convert, batch_convert, validate, batch_validate, typecheck
from dataforge.parsers import dump_data
FIXTURES_DIR = os.path.join(os.path.dirname(__file__), "dataforge_fixtures")
class TestConvert:
"""Tests for convert command."""
def test_convert_json_to_yaml(self, tmp_path):
input_file = tmp_path / "input.json"
input_file.write_text('{"name": "test", "value": 42}')
output_file = tmp_path / "output.yaml"
runner = pytest.CliRunner()
result = runner.invoke(
convert,
[str(input_file), str(output_file), "--to", "yaml"],
)
assert result.exit_code == 0
assert "Successfully converted" in result.output
def test_convert_yaml_to_toml(self, tmp_path):
input_file = tmp_path / "input.yaml"
input_file.write_text("name: test\nvalue: 42")
output_file = tmp_path / "output.toml"
runner = pytest.CliRunner()
result = runner.invoke(
convert,
[str(input_file), str(output_file), "--to", "toml"],
)
assert result.exit_code == 0
def test_convert_with_from_format(self, tmp_path):
input_file = tmp_path / "input.txt"
input_file.write_text('{"name": "test"}')
output_file = tmp_path / "output.json"
runner = pytest.CliRunner()
result = runner.invoke(
convert,
[str(input_file), str(output_file), "--from", "json", "--to", "json"],
)
assert result.exit_code == 0
def test_convert_invalid_format(self, tmp_path):
input_file = tmp_path / "input.json"
input_file.write_text('{"name": "test"}')
output_file = tmp_path / "output.xyz"
runner = pytest.CliRunner()
result = runner.invoke(
convert,
[str(input_file), str(output_file), "--to", "xyz"],
)
assert result.exit_code != 0
def test_convert_stdin_stdout(self):
runner = pytest.CliRunner()
result = runner.invoke(
convert,
["-", "-", "--from", "json", "--to", "yaml"],
input='{"name": "test"}',
)
assert result.exit_code == 0
assert "name: test" in result.output
def test_convert_quiet_mode(self, tmp_path):
input_file = tmp_path / "input.json"
input_file.write_text('{"name": "test"}')
output_file = tmp_path / "output.yaml"
runner = pytest.CliRunner()
result = runner.invoke(
convert,
[str(input_file), str(output_file), "--to", "yaml", "--quiet"],
)
assert result.exit_code == 0
assert result.output == ""
class TestBatchConvert:
"""Tests for batch-convert command."""
def test_batch_convert_files(self, tmp_path):
input_dir = tmp_path / "input"
input_dir.mkdir()
output_dir = tmp_path / "output"
output_dir.mkdir()
(input_dir / "file1.json").write_text('{"name": "test1"}')
(input_dir / "file2.json").write_text('{"name": "test2"}')
runner = pytest.CliRunner()
result = runner.invoke(
batch_convert,
[
"--to",
"yaml",
"--output-dir",
str(output_dir),
"--pattern",
"*.json",
],
)
assert result.exit_code == 0
assert (output_dir / "file1.yaml").exists()
assert (output_dir / "file2.yaml").exists()
def test_batch_convert_empty_pattern(self, tmp_path):
runner = pytest.CliRunner()
result = runner.invoke(
batch_convert,
["--to", "yaml", "--pattern", "*.nonexistent"],
)
assert result.exit_code == 0
assert "No files found" in result.output
class TestValidate:
"""Tests for validate command."""
def test_validate_valid_file(self, tmp_path):
input_file = tmp_path / "input.json"
input_file.write_text('{"name": "test", "version": "1.0.0"}')
schema_file = tmp_path / "schema.json"
schema_file.write_text(
'{"type": "object", "properties": {"name": {"type": "string"}, "version": {"type": "string"}}, "required": ["name", "version"]}'
)
runner = pytest.CliRunner()
result = runner.invoke(
validate,
[str(input_file), "--schema", str(schema_file)],
)
assert result.exit_code == 0
assert "Validation passed" in result.output
def test_validate_invalid_file(self, tmp_path):
input_file = tmp_path / "input.json"
input_file.write_text('{"name": 123}')
schema_file = tmp_path / "schema.json"
schema_file.write_text(
'{"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}'
)
import jsonschema
runner = pytest.CliRunner()
result = runner.invoke(
validate,
[str(input_file), "--schema", str(schema_file)],
)
assert result.exit_code != 0
assert "Validation failed" in result.output
def test_validate_without_schema(self, tmp_path):
input_file = tmp_path / "input.json"
input_file.write_text('{"name": "test"}')
runner = pytest.CliRunner()
result = runner.invoke(validate, [str(input_file)])
assert result.exit_code == 0
assert "File is valid" in result.output
class TestBatchValidate:
"""Tests for batch-validate command."""
def test_batch_validate_files(self, tmp_path):
input_dir = tmp_path / "input"
input_dir.mkdir()
(input_dir / "file1.json").write_text('{"name": "test1"}')
(input_dir / "file2.json").write_text('{"name": "test2"}')
(input_dir / "file3.json").write_text('{"name": "test3"}')
schema_file = tmp_path / "schema.json"
schema_file.write_text(
'{"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}'
)
runner = pytest.CliRunner()
result = runner.invoke(
batch_validate,
[
"--schema",
str(schema_file),
"--pattern",
"*.json",
],
)
assert result.exit_code == 0
assert "Summary: 3 valid, 0 invalid" in result.output
def test_batch_validate_no_files(self, tmp_path):
runner = pytest.CliRunner()
result = runner.invoke(
batch_validate,
["--schema", "nonexistent.json", "--pattern", "*.nonexistent"],
)
assert result.exit_code == 0
assert "No files found" in result.output
class TestTypecheck:
"""Tests for typecheck command."""
def test_typecheck_file(self, tmp_path):
input_file = tmp_path / "input.json"
input_file.write_text('{"name": "test", "value": 42}')
runner = pytest.CliRunner()
result = runner.invoke(typecheck, [str(input_file)])
assert result.exit_code == 0
assert "Type: object with 2 keys" in result.output
def test_typecheck_infer_schema(self, tmp_path):
input_file = tmp_path / "input.json"
input_file.write_text('{"name": "test", "value": 42}')
runner = pytest.CliRunner()
result = runner.invoke(
typecheck,
[str(input_file), "--infer"],
)
assert result.exit_code == 0
assert '"type": "object"' in result.output
def test_typecheck_quiet_mode(self, tmp_path):
input_file = tmp_path / "input.json"
input_file.write_text('{"name": "test"}')
runner = pytest.CliRunner()
result = runner.invoke(typecheck, [str(input_file), "--quiet"])
assert result.exit_code == 0
assert result.output == ""