šŸ” Code Extractor

function test_european_with_thousands

Maturity: 42

A unit test function that validates the smart_read_csv function's ability to correctly parse European-formatted CSV files with thousand separators (dots) and decimal commas.

File:
/tf/active/vicechatdev/vice_ai/test_regional_formats.py
Lines:
84 - 115
Complexity:
moderate

Purpose

This test function verifies that the smart_read_csv utility can properly handle European number formatting conventions where dots are used as thousand separators (e.g., 1.234) and commas as decimal separators (e.g., 1.234,56 = 1234.56). It creates a temporary CSV file with European-formatted numbers, reads it using smart_read_csv, and asserts that the numeric columns are correctly converted to float types with proper values.

Source Code

def test_european_with_thousands():
    """Test European format with thousand separators"""
    print("\n" + "="*60)
    print("Test 3: European CSV with thousand separators")
    print("="*60)
    
    # Create test CSV with European thousand separators (dots) and decimal commas
    test_file = Path('/tmp/test_european_thousands.csv')
    european_data = """Product;Price;Quantity;Revenue
Widget A;1.234,56;100;123.456,00
Widget B;2.567,89;50;128.394,50
Widget C;987,65;200;197.530,00"""
    
    test_file.write_text(european_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"\nPrice column (should be numeric):")
    print(f"  Type: {df['Price'].dtype}")
    print(f"  Values: {df['Price'].tolist()}")
    
    # Verify conversion - "1.234,56" should become 1234.56
    assert df['Price'].dtype in ['float64', 'Float64'], f"Price should be numeric, got {df['Price'].dtype}"
    assert 1234.0 < df['Price'].iloc[0] < 1235.0, f"Widget A price should be ~1234.56, got {df['Price'].iloc[0]}"
    
    print("\nāœ“ European thousands separator 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

  • pandas
  • pathlib
  • smartstat_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_european_with_thousands()

# Or as part of a test suite
import pytest

def test_suite():
    test_european_with_thousands()
    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 verify both data type conversion and numerical accuracy within a tolerance range
  • The test uses semicolon as delimiter which is common in European CSV formats
  • Always ensure the smartstat_service module is properly installed and accessible before running
  • The test expects specific numeric ranges (1234.0 < value < 1235.0) to validate conversion accuracy

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_tab_delimited_european 89.8% similar

    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).

    From: /tf/active/vicechatdev/vice_ai/test_regional_formats.py
  • function test_european_csv 89.5% similar

    A test function that validates the ability to read and parse European-formatted CSV files (semicolon delimiters, comma decimal separators) and convert them to proper numeric types.

    From: /tf/active/vicechatdev/vice_ai/test_regional_formats.py
  • function test_us_with_thousands 88.6% similar

    A unit test function that validates the smart_read_csv function's ability to correctly parse US-formatted CSV files containing numbers with thousand separators (commas) and decimal points.

    From: /tf/active/vicechatdev/vice_ai/test_regional_formats.py
  • function test_us_csv 78.5% similar

    A unit test function that validates the smart_read_csv function's ability to correctly parse US-formatted CSV files with comma delimiters and point decimal separators.

    From: /tf/active/vicechatdev/vice_ai/test_regional_formats.py
  • function smart_read_csv 71.4% similar

    Automatically detects CSV file delimiters (comma, semicolon, tab) and handles regional decimal formats (European comma vs US/UK point) to reliably parse CSV files from different locales.

    From: /tf/active/vicechatdev/vice_ai/smartstat_service.py
← Back to Browse