import pytest
import linreg_core
class TestCSVParsingNative:
def test_parse_basic_csv_native(self):
csv_content = """name,value,category
Alice,42.5,A
Bob,17.3,B
Charlie,99.9,A"""
result = linreg_core.parse_csv(csv_content)
assert hasattr(result, 'headers')
assert hasattr(result, 'data')
assert hasattr(result, 'numeric_columns')
assert hasattr(result, 'n_rows')
assert hasattr(result, 'n_cols')
assert result.headers == ["name", "value", "category"]
assert "value" in result.numeric_columns
assert result.n_rows == 3
assert result.n_cols == 3
assert len(result.data) == 3
def test_csv_result_object_attributes(self):
csv_content = """x,y,z
1.5,2.5,hello
3.5,4.5,world"""
result = linreg_core.parse_csv(csv_content)
assert isinstance(result.headers, list)
assert isinstance(result.data, list)
assert isinstance(result.numeric_columns, list)
assert isinstance(result.n_rows, int)
assert isinstance(result.n_cols, int)
def test_parse_csv_identifies_numeric_columns(self):
csv_content = """x,y,z
1.5,2.5,hello
3.5,4.5,world"""
result = linreg_core.parse_csv(csv_content)
numeric_set = set(result.numeric_columns)
assert "x" in numeric_set
assert "y" in numeric_set
assert "z" not in numeric_set
def test_csv_summary_method(self):
csv_content = """x,y
1.0,2.0
3.0,4.0"""
result = linreg_core.parse_csv(csv_content)
summary = result.summary()
assert isinstance(summary, str)
assert "CSV Parsing Results" in summary
assert "Rows:" in summary
assert "Columns:" in summary
def test_csv_to_dict_method(self):
csv_content = """x,y
1.0,2.0
3.0,4.0"""
result = linreg_core.parse_csv(csv_content)
d = result.to_dict()
assert isinstance(d, dict)
assert "headers" in d
assert "data" in d
assert "numeric_columns" in d
assert "n_rows" in d
assert "n_cols" in d
def test_csv_repr_and_str(self):
csv_content = """x,y
1.0,2.0"""
result = linreg_core.parse_csv(csv_content)
str_result = str(result)
assert "CSV Parsing Results" in str_result
repr_result = repr(result)
assert "CSVResult" in repr_result
def test_parse_csv_multiple_rows(self):
csv_content = """a,b,c
1,2,3
4,5,6
7,8,9
10,11,12"""
result = linreg_core.parse_csv(csv_content)
assert result.n_rows == 4
assert result.n_cols == 3
assert len(result.data) == 4
def test_parse_csv_with_whitespace(self):
csv_content = """x, y , z
1.5 , 2.5 , hello
3.5 , 4.5 , world"""
result = linreg_core.parse_csv(csv_content)
assert result.headers == ["x", " y ", " z"]
assert " y " in result.numeric_columns
def test_parse_csv_numeric_data_types(self):
csv_content = """int_val,float_val,neg_val
42,3.14,-1.5
100,2.71,-999"""
result = linreg_core.parse_csv(csv_content)
numeric_set = set(result.numeric_columns)
assert "int_val" in numeric_set
assert "float_val" in numeric_set
assert "neg_val" in numeric_set
class TestCSVParsingEdgeCases:
def test_empty_csv(self):
result = linreg_core.parse_csv("")
assert hasattr(result, 'headers')
assert hasattr(result, 'data')
assert hasattr(result, 'numeric_columns')
assert result.n_rows == 0
def test_csv_with_only_headers(self):
csv_content = """x,y,z"""
result = linreg_core.parse_csv(csv_content)
assert result.headers == ["x", "y", "z"]
assert result.n_rows == 0
assert result.n_cols == 3
def test_csv_with_empty_rows(self):
csv_content = """x,y
1,2
3,4
5,6"""
result = linreg_core.parse_csv(csv_content)
assert result.n_rows >= 2
def test_csv_with_quotes(self):
csv_content = '''name,value
"John Doe",42.5
"Jane, Smith",17.3'''
result = linreg_core.parse_csv(csv_content)
assert result.headers == ["name", "value"]
assert "value" in result.numeric_columns
assert result.n_rows == 2
def test_csv_with_mixed_types(self):
csv_content = """x,y,z
1,hello,3.5
2,world,4.5
NA,data,5.5"""
result = linreg_core.parse_csv(csv_content)
numeric_set = set(result.numeric_columns)
assert "x" in numeric_set
assert "z" in numeric_set
def test_csv_with_special_characters(self):
csv_content = """name,value
test_1,1.5
test-2,2.5
test.3,3.5"""
result = linreg_core.parse_csv(csv_content)
assert "value" in result.numeric_columns
assert result.n_rows == 3
def test_csv_with_leading_trailing_spaces(self):
csv_content = """ x , y , z
1.5,2.5,hello"""
result = linreg_core.parse_csv(csv_content)
assert len(result.headers) == 3
def test_csv_with_inconsistent_column_counts(self):
csv_content = """x,y,z
1,2,3
4,5
6,7,8"""
result = linreg_core.parse_csv(csv_content)
assert hasattr(result, 'data')
def test_csv_with_unicode(self):
csv_content = """name,value
café,42.5
日本語,17.3
مرحبا,99.9"""
result = linreg_core.parse_csv(csv_content)
assert result.headers == ["name", "value"]
assert result.n_rows == 3
def test_csv_with_zero_values(self):
csv_content = """x,y
0,0.0
0,0"""
result = linreg_core.parse_csv(csv_content)
numeric_set = set(result.numeric_columns)
assert "x" in numeric_set
assert "y" in numeric_set
def test_csv_with_boolean_strings(self):
csv_content = """x,y
true,1.5
false,2.5"""
result = linreg_core.parse_csv(csv_content)
numeric_set = set(result.numeric_columns)
assert "y" in numeric_set