Skip to content

Commit

Permalink
added check csv function and the test
Browse files Browse the repository at this point in the history
  • Loading branch information
MCatherine1994 committed Jan 16, 2025
1 parent b73345c commit 3827102
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/pyeda/check_csv.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -15,4 +17,13 @@ def check_csv(file_path):
>>> from pyeda.check_csv import check_csv
>>> check_csv("../data/raw/data.csv")
"""
pass
# 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
68 changes: 67 additions & 1 deletion tests/test_check_csv.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3827102

Please sign in to comment.