136 lines
4.3 KiB
Python
136 lines
4.3 KiB
Python
"""Tests for CLI commands."""
|
|
|
|
import json
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from click.testing import CliRunner
|
|
|
|
from testdatagen.cli import main
|
|
|
|
|
|
class TestCLI:
|
|
"""Tests for CLI commands."""
|
|
|
|
def test_cli_version(self):
|
|
"""Test that CLI shows version."""
|
|
runner = CliRunner()
|
|
result = runner.invoke(main, ["--version"])
|
|
|
|
assert result.exit_code == 0
|
|
assert "0.1.0" in result.output
|
|
|
|
def test_generate_help(self):
|
|
"""Test generate command help."""
|
|
runner = CliRunner()
|
|
result = runner.invoke(main, ["generate", "--help"])
|
|
|
|
assert result.exit_code == 0
|
|
assert "--schema" in result.output
|
|
assert "--count" in result.output
|
|
assert "--format" in result.output
|
|
|
|
def test_from_ts_help(self):
|
|
"""Test from-ts command help."""
|
|
runner = CliRunner()
|
|
result = runner.invoke(main, ["from-ts", "--help"])
|
|
|
|
assert result.exit_code == 0
|
|
assert "--input" in result.output
|
|
|
|
def test_from_sample_help(self):
|
|
"""Test from-sample command help."""
|
|
runner = CliRunner()
|
|
result = runner.invoke(main, ["from-sample", "--help"])
|
|
|
|
assert result.exit_code == 0
|
|
assert "--input" in result.output
|
|
|
|
def test_generate_requires_schema(self):
|
|
"""Test that generate requires schema option."""
|
|
runner = CliRunner()
|
|
result = runner.invoke(main, ["generate"])
|
|
|
|
assert result.exit_code != 0
|
|
|
|
def test_generate_with_schema(self):
|
|
"""Test generate with valid schema."""
|
|
runner = CliRunner()
|
|
|
|
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
|
|
json.dump({
|
|
"type": "object",
|
|
"properties": {
|
|
"name": {"type": "string"}
|
|
}
|
|
}, f)
|
|
schema_path = f.name
|
|
|
|
try:
|
|
result = runner.invoke(main, ["generate", "--schema", schema_path, "--count", "1"])
|
|
assert result.exit_code == 0
|
|
finally:
|
|
Path(schema_path).unlink()
|
|
|
|
def test_generate_csv_format(self):
|
|
"""Test generate with CSV format."""
|
|
runner = CliRunner()
|
|
|
|
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
|
|
json.dump({
|
|
"type": "object",
|
|
"properties": {
|
|
"name": {"type": "string"}
|
|
}
|
|
}, f)
|
|
schema_path = f.name
|
|
|
|
try:
|
|
result = runner.invoke(main, ["generate", "--schema", schema_path, "--count", "1", "--format", "csv"])
|
|
assert result.exit_code == 0
|
|
assert "name" in result.output
|
|
finally:
|
|
Path(schema_path).unlink()
|
|
|
|
def test_generate_sql_format(self):
|
|
"""Test generate with SQL format."""
|
|
runner = CliRunner()
|
|
|
|
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
|
|
json.dump({
|
|
"type": "object",
|
|
"properties": {
|
|
"name": {"type": "string"}
|
|
}
|
|
}, f)
|
|
schema_path = f.name
|
|
|
|
try:
|
|
result = runner.invoke(main, ["generate", "--schema", schema_path, "--count", "1", "--format", "sql"])
|
|
assert result.exit_code == 0
|
|
assert "INSERT" in result.output
|
|
finally:
|
|
Path(schema_path).unlink()
|
|
|
|
def test_generate_invalid_json(self):
|
|
"""Test generate with invalid JSON schema."""
|
|
runner = CliRunner()
|
|
|
|
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
|
|
f.write("{ invalid json }")
|
|
schema_path = f.name
|
|
|
|
try:
|
|
result = runner.invoke(main, ["generate", "--schema", schema_path, "--count", "1"])
|
|
assert result.exit_code != 0
|
|
finally:
|
|
Path(schema_path).unlink()
|
|
|
|
def test_generate_nonexistent_file(self):
|
|
"""Test generate with nonexistent schema file."""
|
|
runner = CliRunner()
|
|
result = runner.invoke(main, ["generate", "--schema", "/nonexistent/path.json"])
|
|
|
|
assert result.exit_code != 0
|