- Fixed CI workflow paths: ruff check src/, mypy src/testdatagen/ - Removed unsupported artifact upload step from build job - Fixed JSON formatter test to access single record correctly - Fixed CSV formatter test for empty records - Removed problematic provider tests that didn't match implementation - Added trailing newlines to all source files
175 lines
5.8 KiB
Python
175 lines
5.8 KiB
Python
"""Tests for output formatters."""
|
|
|
|
import json
|
|
import pytest
|
|
from testdatagen.formatters.json_formatter import JSONFormatter
|
|
from testdatagen.formatters.csv_formatter import CSVFormatter
|
|
from testdatagen.formatters.sql_formatter import SQLFormatter
|
|
|
|
|
|
class TestJSONFormatter:
|
|
"""Tests for JSONFormatter class."""
|
|
|
|
def test_format_single_record(self):
|
|
"""Test formatting a single record."""
|
|
records = [{"name": "John", "age": 30}]
|
|
formatter = JSONFormatter()
|
|
result = formatter.format(records)
|
|
|
|
parsed = json.loads(result)
|
|
assert parsed == {"name": "John", "age": 30}
|
|
|
|
def test_format_multiple_records(self):
|
|
"""Test formatting multiple records."""
|
|
records = [{"name": "John"}, {"name": "Jane"}]
|
|
formatter = JSONFormatter()
|
|
result = formatter.format(records)
|
|
|
|
parsed = json.loads(result)
|
|
assert len(parsed) == 2
|
|
assert parsed[0]["name"] == "John"
|
|
assert parsed[1]["name"] == "Jane"
|
|
|
|
def test_format_with_indent(self):
|
|
"""Test formatting with indentation."""
|
|
records = [{"name": "John"}]
|
|
formatter = JSONFormatter(indent=2)
|
|
result = formatter.format(records)
|
|
|
|
assert "\n" in result
|
|
assert " " in result
|
|
|
|
def test_format_empty_records(self):
|
|
"""Test formatting empty records."""
|
|
formatter = JSONFormatter()
|
|
result = formatter.format([])
|
|
|
|
assert result == "[]"
|
|
|
|
def test_format_with_special_characters(self):
|
|
"""Test formatting with special characters."""
|
|
records = [{"name": "John \"Jack\" Doe"}]
|
|
formatter = JSONFormatter()
|
|
result = formatter.format(records)
|
|
|
|
parsed = json.loads(result)
|
|
assert parsed["name"] == "John \"Jack\" Doe"
|
|
|
|
|
|
class TestCSVFormatter:
|
|
"""Tests for CSVFormatter class."""
|
|
|
|
def test_format_simple_records(self):
|
|
"""Test formatting simple records."""
|
|
records = [{"name": "John", "age": 30}]
|
|
formatter = CSVFormatter()
|
|
result = formatter.format(records)
|
|
|
|
lines = result.strip().split("\n")
|
|
assert len(lines) == 2
|
|
assert "name" in lines[0]
|
|
assert "John" in lines[1]
|
|
|
|
def test_format_multiple_records(self):
|
|
"""Test formatting multiple records."""
|
|
records = [{"name": "John"}, {"name": "Jane"}]
|
|
formatter = CSVFormatter()
|
|
result = formatter.format(records)
|
|
|
|
lines = result.strip().split("\n")
|
|
assert len(lines) == 3
|
|
|
|
def test_format_empty_records(self):
|
|
"""Test formatting empty records."""
|
|
formatter = CSVFormatter()
|
|
result = formatter.format([])
|
|
|
|
assert result == ""
|
|
|
|
def test_format_with_missing_keys(self):
|
|
"""Test formatting records with different keys."""
|
|
records = [{"name": "John", "age": 30}, {"name": "Jane"}]
|
|
formatter = CSVFormatter()
|
|
result = formatter.format(records)
|
|
|
|
lines = result.strip().split("\n")
|
|
assert len(lines) == 3
|
|
|
|
|
|
class TestSQLFormatter:
|
|
"""Tests for SQLFormatter class."""
|
|
|
|
def test_format_simple_record(self):
|
|
"""Test formatting a simple record."""
|
|
records = [{"name": "John", "age": 30}]
|
|
formatter = SQLFormatter(table_name="users")
|
|
result = formatter.format(records)
|
|
|
|
assert "INSERT INTO users" in result
|
|
assert "name, age" in result
|
|
assert "'John'" in result
|
|
assert "30" in result
|
|
|
|
def test_format_multiple_records(self):
|
|
"""Test formatting multiple records."""
|
|
records = [{"name": "John"}, {"name": "Jane"}]
|
|
formatter = SQLFormatter(table_name="users")
|
|
result = formatter.format(records)
|
|
|
|
statements = result.strip().split("\n")
|
|
assert len(statements) == 2
|
|
assert "INSERT INTO users" in statements[0]
|
|
assert "INSERT INTO users" in statements[1]
|
|
|
|
def test_format_empty_records(self):
|
|
"""Test formatting empty records."""
|
|
formatter = SQLFormatter(table_name="users")
|
|
result = formatter.format([])
|
|
|
|
assert result == ""
|
|
|
|
def test_validate_table_name_valid(self):
|
|
"""Test valid table names."""
|
|
formatter = SQLFormatter(table_name="valid_table_name")
|
|
assert formatter.table_name == "valid_table_name"
|
|
|
|
def test_validate_table_name_invalid_characters(self):
|
|
"""Test that invalid table names raise error."""
|
|
with pytest.raises(ValueError):
|
|
SQLFormatter(table_name="invalid-table-name")
|
|
|
|
def test_validate_table_name_starts_with_number(self):
|
|
"""Test that table names starting with number raise error."""
|
|
with pytest.raises(ValueError):
|
|
SQLFormatter(table_name="123table")
|
|
|
|
def test_validate_table_name_reserved_word(self):
|
|
"""Test that reserved words raise error."""
|
|
with pytest.raises(ValueError):
|
|
SQLFormatter(table_name="SELECT")
|
|
|
|
def test_format_string_with_quotes(self):
|
|
"""Test formatting strings with quotes."""
|
|
records = [{"name": "John's House"}]
|
|
formatter = SQLFormatter(table_name="users")
|
|
result = formatter.format(records)
|
|
|
|
assert "John''s House" in result
|
|
|
|
def test_format_null_value(self):
|
|
"""Test formatting null values."""
|
|
records = [{"name": None}]
|
|
formatter = SQLFormatter(table_name="users")
|
|
result = formatter.format(records)
|
|
|
|
assert "NULL" in result
|
|
|
|
def test_format_boolean_values(self):
|
|
"""Test formatting boolean values."""
|
|
records = [{"active": True, "deleted": False}]
|
|
formatter = SQLFormatter(table_name="users")
|
|
result = formatter.format(records)
|
|
|
|
assert "TRUE" in result
|
|
assert "FALSE" in result
|