import subprocess
import json
import pytest
import csv
import os
from pathlib import Path
def run_sql_cli(csv_file, query):
cmd = [
"./target/release/sql-cli",
csv_file,
"-q", query,
"-o", "json"
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
print(f"Error running command: {' '.join(cmd)}")
print(f"stderr: {result.stderr}")
print(f"stdout: {result.stdout}")
raise Exception(f"Command failed with return code {result.returncode}")
try:
return json.loads(result.stdout)
except json.JSONDecodeError as e:
print(f"Failed to parse JSON output: {result.stdout}")
raise e
def setup_test_data():
test_file = "data/test_strings.csv"
os.makedirs("data", exist_ok=True)
with open(test_file, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['id', 'name', 'description'])
writer.writerow([1, 'Alice', ' Hello World '])
writer.writerow([2, 'Bob', 'Testing 123'])
writer.writerow([3, 'Charlie', 'SQL Functions'])
writer.writerow([4, 'David', ' Spaces '])
writer.writerow([5, 'Eve', 'lowercase text'])
writer.writerow([6, 'Frank', 'UPPERCASE TEXT'])
return test_file
def test_mid_function():
test_file = setup_test_data()
query = "SELECT id, name, MID(name, 1, 3) as first_three FROM test_strings WHERE id = 1"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['first_three'] == 'Ali'
query = "SELECT id, name, MID(name, 2, 2) as middle_two FROM test_strings WHERE id = 2"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['middle_two'] == 'ob'
query = "SELECT id, name, MID(name, 10, 5) as beyond FROM test_strings WHERE id = 1"
result = run_sql_cli(test_file, query)
assert len(result) == 1
if 'beyond' in result[0]:
assert result[0]['beyond'] == ''
query = "SELECT id, name, MID(name, 4, 10) as rest FROM test_strings WHERE id = 1"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['rest'] == 'ce'
def test_upper_function():
test_file = setup_test_data()
query = "SELECT id, name, UPPER(name) as upper_name FROM test_strings WHERE id = 5"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['upper_name'] == 'EVE'
query = "SELECT id, description, UPPER(description) as upper_desc FROM test_strings WHERE id = 1"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['upper_desc'] == ' HELLO WORLD '
query = "SELECT id, description, UPPER(description) as upper_desc FROM test_strings WHERE id = 6"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['upper_desc'] == 'UPPERCASE TEXT'
def test_lower_function():
test_file = setup_test_data()
query = "SELECT id, description, LOWER(description) as lower_desc FROM test_strings WHERE id = 6"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['lower_desc'] == 'uppercase text'
query = "SELECT id, name, LOWER(name) as lower_name FROM test_strings WHERE id = 3"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['lower_name'] == 'charlie'
query = "SELECT id, description, LOWER(description) as lower_desc FROM test_strings WHERE id = 1"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['lower_desc'] == ' hello world '
def test_trim_function():
test_file = setup_test_data()
query = "SELECT id, description, TRIM(description) as trimmed FROM test_strings WHERE id = 1"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['trimmed'] == 'Hello World'
query = "SELECT id, description, TRIM(description) as trimmed FROM test_strings WHERE id = 4"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['trimmed'] == 'Spaces'
query = "SELECT id, description, TRIM(description) as trimmed FROM test_strings WHERE id = 2"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['trimmed'] == 'Testing 123'
def test_combined_string_functions():
test_file = setup_test_data()
query = "SELECT id, description, UPPER(TRIM(description)) as processed FROM test_strings WHERE id = 1"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['processed'] == 'HELLO WORLD'
query = "SELECT id, name, MID(UPPER(name), 1, 3) as first_upper FROM test_strings WHERE id = 1"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['first_upper'] == 'ALI'
query = "SELECT id, description, TRIM(LOWER(description)) as processed FROM test_strings WHERE id = 4"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['processed'] == 'spaces'
query = "SELECT id, description, MID(UPPER(TRIM(description)), 1, 5) as complex FROM test_strings WHERE id = 1"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['complex'] == 'HELLO'
def test_string_functions_with_expressions():
test_file = setup_test_data()
query = "SELECT id, name, MID(name, id, 2) as dynamic_mid FROM test_strings WHERE id = 2"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['dynamic_mid'] == 'ob'
query = "SELECT id, name, MID(name, 1, id + 1) as dynamic_length FROM test_strings WHERE id = 3"
result = run_sql_cli(test_file, query)
assert len(result) == 1
assert result[0]['dynamic_length'] == 'Char'
if __name__ == "__main__":
test_mid_function()
print("✓ MID function tests passed")
test_upper_function()
print("✓ UPPER function tests passed")
test_lower_function()
print("✓ LOWER function tests passed")
test_trim_function()
print("✓ TRIM function tests passed")
test_combined_string_functions()
print("✓ Combined string function tests passed")
test_string_functions_with_expressions()
print("✓ String functions with expressions tests passed")
print("\nAll string function tests passed!")