diff --git a/src/pyeda/check_csv.py b/src/pyeda/check_csv.py index e75bfd6..42e5a17 100644 --- a/src/pyeda/check_csv.py +++ b/src/pyeda/check_csv.py @@ -1,3 +1,5 @@ +import pandas as pd + def check_csv(file_path): """ Check if the given file is a CSV file by its extension. @@ -15,4 +17,13 @@ def check_csv(file_path): >>> from pyeda.check_csv import check_csv >>> check_csv("../data/raw/data.csv") """ - pass \ No newline at end of file + # Check if file extension is .csv + if not file_path.endswith(".csv"): + return False + + # Try to read the file using pandas (this will raise an error if it's not a CSV file) + try: + pd.read_csv(file_path) + return True + except Exception: + return False \ No newline at end of file diff --git a/tests/test_check_csv.py b/tests/test_check_csv.py index e6abe61..8d70e65 100644 --- a/tests/test_check_csv.py +++ b/tests/test_check_csv.py @@ -1 +1,67 @@ -from pyeda import check_csv +import pytest +import pandas as pd +import os +from pyeda.check_csv import check_csv + + +@pytest.fixture +def csv_content(): + # Create a mock valid CSV content + csv_content = "name,age\nJohn,25\nAlice,30" + return csv_content + + +def test_check_csv_valid_file(csv_content): + # Create a test CSV file + file_path = 'test_valid.csv' + with open(file_path, 'w') as f: + f.write(csv_content) + + # Call the check_csv function + result = check_csv(file_path) + # Assert that the function returns True for a valid CSV + assert result == True + + # Clean the test data file + if os.path.exists(file_path): + os.remove(file_path) + + +def test_check_csv_invalid_file_name(csv_content): + # Create a test txt file + file_path = 'test_invalid_name.txt' + with open(file_path, 'w') as f: + f.write(csv_content) + + # Call the check_csv function on a non-CSV file + result = check_csv(file_path) + # Assert that the function returns False for a non-CSV file + assert result == False + + # Clean the test data file + if os.path.exists(file_path): + os.remove(file_path) + + +def test_check_csv_invalid_file_content(): + # Create a mock invalid file (non-CSV content) + invalid_csv_content = """ + Name,Age,Score + Alice,25,85 + Charlie,Thirty,70, 40 + David,40 + """ + + # Create a test CSV file with invalid content + file_path = 'test_invalid_content.csv' + with open(file_path, 'w') as f: + f.write(invalid_csv_content) + + # Call the check_csv function on a non-CSV file + result = check_csv(file_path) + # Assert that the function returns False for a non-CSV file + assert result == False + + # Clean the test data file + if os.path.exists(file_path): + os.remove(file_path) \ No newline at end of file