import subprocess
import json
import os
import tempfile
import pytest
import shutil
from pathlib import Path
SQL_CLI = Path(__file__).parent.parent.parent / "target" / "release" / "sql-cli"
def run_query(csv_file, query, date_notation="us"):
cmd = [str(SQL_CLI), csv_file, "-q", query, "-o", "json"]
env = os.environ.copy()
env["SQL_CLI_DATE_NOTATION"] = date_notation
result = subprocess.run(cmd, capture_output=True, text=True, env=env)
if result.returncode != 0:
raise Exception(f"Query failed: {result.stderr}")
lines = result.stdout.strip().split('\n')
json_lines = [line for line in lines if line and not line.startswith('#')]
json_text = '\n'.join(json_lines)
if not json_text or json_text == '[]':
return []
return json.loads(json_text)
def create_test_csv(filename, content):
with open(filename, 'w') as f:
f.write(content)
class TestDateFormats:
@classmethod
def setup_class(cls):
cls.test_csv = "test_dates.csv"
create_test_csv(cls.test_csv, """id,start_date,end_date,description
1,01/02/2024 09:00:00,03/02/2024 17:00:00,February in US / January-February in EU
2,02/01/2024 10:00:00,04/01/2024 18:00:00,January in US / February in EU
3,12/06/2024 14:00:00,15/06/2024 16:00:00,June in US / December in EU
4,06/12/2024 15:00:00,08/12/2024 17:00:00,December in US / June in EU
5,15/03/2024 11:00:00,20/03/2024 12:00:00,March unambiguous
6,25/12/2024 08:00:00,31/12/2024 23:59:59,December unambiguous""")
@classmethod
def teardown_class(cls):
if os.path.exists(cls.test_csv):
os.remove(cls.test_csv)
def test_datediff_us_format(self):
query = """
SELECT id, start_date, end_date,
DATEDIFF('day', start_date, end_date) as days_diff
FROM test_dates
"""
results = run_query(self.test_csv, query, "us")
assert len(results) == 6
assert results[0]['days_diff'] == 60 assert results[1]['days_diff'] == 60 assert results[2]['days_diff'] == -173 assert results[3]['days_diff'] == 61 assert results[4]['days_diff'] == 5 assert results[5]['days_diff'] == 6
def test_datediff_eu_format(self):
query = """
SELECT id, start_date, end_date,
DATEDIFF('day', start_date, end_date) as days_diff
FROM test_dates
"""
results = run_query(self.test_csv, query, "european")
assert len(results) == 6
assert results[0]['days_diff'] == 2 assert results[1]['days_diff'] == 2 assert results[2]['days_diff'] == 3 assert results[3]['days_diff'] == 2 assert results[4]['days_diff'] == 5 assert results[5]['days_diff'] == 6
def test_where_clause_us_format(self):
query = """
SELECT id, start_date FROM test_dates
WHERE start_date > '06/01/2024 00:00:00'
ORDER BY id
"""
results = run_query(self.test_csv, query, "us")
assert len(results) == 3
assert results[0]['id'] == 3
assert results[1]['id'] == 4
assert results[2]['id'] == 6
def test_where_clause_eu_format(self):
query = """
SELECT id, start_date FROM test_dates
WHERE start_date > '01/06/2024 00:00:00'
ORDER BY id
"""
results = run_query(self.test_csv, query, "european")
assert len(results) == 3
assert results[0]['id'] == 3
assert results[1]['id'] == 4
assert results[2]['id'] == 6
def test_dateadd_us_format(self):
query = """
SELECT id, start_date,
DATEADD('day', 10, start_date) as plus_10_days
FROM test_dates
WHERE id = 1
"""
results = run_query(self.test_csv, query, "us")
assert len(results) == 1
assert '2024-01-12' in results[0]['plus_10_days']
def test_dateadd_eu_format(self):
query = """
SELECT id, start_date,
DATEADD('day', 10, start_date) as plus_10_days
FROM test_dates
WHERE id = 1
"""
results = run_query(self.test_csv, query, "european")
assert len(results) == 1
assert '2024-02-11' in results[0]['plus_10_days']
def test_mixed_operations_us(self):
query = """
SELECT id,
DATEDIFF('hour', start_date, end_date) as hours_diff,
DATEADD('month', 1, start_date) as next_month
FROM test_dates
WHERE start_date >= '01/01/2024 00:00:00'
AND start_date <= '03/31/2024 23:59:59'
ORDER BY id
"""
results = run_query(self.test_csv, query, "us")
assert len(results) == 3
assert results[0]['id'] == 1
assert results[1]['id'] == 2
assert results[2]['id'] == 5
def test_mixed_operations_eu(self):
query = """
SELECT id,
DATEDIFF('hour', start_date, end_date) as hours_diff,
DATEADD('month', 1, start_date) as next_month
FROM test_dates
WHERE start_date >= '01/01/2024 00:00:00'
AND start_date <= '31/03/2024 23:59:59'
ORDER BY id
"""
results = run_query(self.test_csv, query, "european")
assert len(results) == 3
assert results[0]['id'] == 1
assert results[1]['id'] == 2
assert results[2]['id'] == 5
def test_iso_dates_consistent(self):
query = """
SELECT DATEDIFF('day', '2024-03-15 10:00:00', '2024-03-20 10:00:00') as diff
"""
us_results = run_query(self.test_csv, query, "us")
eu_results = run_query(self.test_csv, query, "european")
assert us_results[0]['diff'] == 5
assert eu_results[0]['diff'] == 5
assert us_results[0]['diff'] == eu_results[0]['diff']
if __name__ == "__main__":
pytest.main([__file__, "-v"])