function test_tab_delimited_european
A unit test function that validates the smart_read_csv function's ability to correctly parse tab-delimited CSV files containing European-style decimal numbers (using commas instead of periods).
/tf/active/vicechatdev/vice_ai/test_regional_formats.py
152 - 183
simple
Purpose
This test function verifies that the smart_read_csv utility can automatically detect and convert European decimal notation (e.g., '65,5' instead of '65.5') in tab-delimited CSV files. It creates a temporary test file with tab-separated values and comma decimals, reads it using smart_read_csv, and asserts that numeric columns are properly converted to float types with correct values. This ensures data integrity when processing international CSV formats.
Source Code
def test_tab_delimited_european():
"""Test tab-delimited with European decimals"""
print("\n" + "="*60)
print("Test 5: Tab-delimited CSV with European decimals")
print("="*60)
# Create test tab-delimited CSV with comma decimals
test_file = Path('/tmp/test_tab_european.csv')
tab_data = """Name\tWeight\tHeight\tTemperature
Alice\t65,5\t170,2\t36,6
Bob\t78,3\t182,5\t37,1
Charlie\t71,2\t175,8\t36,9"""
test_file.write_text(tab_data)
# Read with smart_read_csv
df = smart_read_csv(str(test_file))
print(f"\nLoaded DataFrame:")
print(df)
print(f"\nColumn types:")
print(df.dtypes)
print(f"\nWeight column (should be numeric):")
print(f" Type: {df['Weight'].dtype}")
print(f" Values: {df['Weight'].tolist()}")
# Verify conversion
assert df['Weight'].dtype in ['float64', 'Float64'], f"Weight should be numeric, got {df['Weight'].dtype}"
assert 65.4 < df['Weight'].iloc[0] < 65.6, f"Alice's weight should be ~65.5, got {df['Weight'].iloc[0]}"
print("\nā Tab-delimited European test PASSED")
test_file.unlink()
Return Value
This function does not return any value (implicitly returns None). It performs assertions and prints test results to stdout. If assertions fail, it raises an AssertionError.
Dependencies
pandaspathlibsmartstat_service
Required Imports
import pandas as pd
from pathlib import Path
from smartstat_service import smart_read_csv
Usage Example
# Run the test function directly
test_tab_delimited_european()
# Or as part of a test suite
import pytest
def test_suite():
test_tab_delimited_european()
print('All tests passed')
if __name__ == '__main__':
test_suite()
Best Practices
- This is a test function and should be run in a testing environment, not in production code
- The function creates temporary files in /tmp which are cleaned up after the test
- Assertions use tolerance ranges (65.4 < value < 65.6) to account for floating-point precision
- The test prints detailed output for debugging purposes, making it easy to diagnose failures
- Always ensure the smartstat_service module is properly installed before running this test
- The test file is explicitly deleted with unlink() to prevent accumulation of test files
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_european_csv 91.0% similar
-
function test_european_with_thousands 89.8% similar
-
function test_us_csv 81.6% similar
-
function smart_read_csv 78.6% similar
-
function test_us_with_thousands 78.1% similar