127 lines
3.9 KiB
Python
127 lines
3.9 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_single_record(self):
|
|
"""Test formatting a single record."""
|
|
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 returns empty string."""
|
|
formatter = CSVFormatter()
|
|
result = formatter.format([])
|
|
|
|
assert result == ""
|
|
|
|
|
|
class TestSQLFormatter:
|
|
"""Tests for SQLFormatter class."""
|
|
|
|
def test_format_single_record(self):
|
|
"""Test formatting a single record."""
|
|
records = [{"name": "John", "age": 30}]
|
|
formatter = SQLFormatter(table_name="users")
|
|
result = formatter.format(records)
|
|
|
|
assert "INSERT" in result
|
|
assert "users" in result
|
|
assert "John" 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)
|
|
|
|
assert result.count("INSERT") == 2
|
|
|
|
def test_format_empty_records(self):
|
|
"""Test formatting empty records."""
|
|
formatter = SQLFormatter(table_name="users")
|
|
result = formatter.format([])
|
|
|
|
assert "INSERT" not in result
|
|
|
|
def test_custom_table_name(self):
|
|
"""Test with custom table name."""
|
|
records = [{"name": "John"}]
|
|
formatter = SQLFormatter(table_name="custom_table")
|
|
result = formatter.format(records)
|
|
|
|
assert "custom_table" in result
|