Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions csvapi/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

from csvapi.utils import get_db_info
from csvapi.type_tester import agate_tester
import logging

logging.captureWarnings(True)
logging.getLogger("py.warnings").setLevel(logging.ERROR)

SNIFF_LIMIT = 4096
CSV_FILETYPES = ('text/plain', 'application/csv', 'text/csv')
Expand All @@ -28,6 +32,11 @@ def from_csv(filepath, encoding='utf-8', sniff_limit=SNIFF_LIMIT):
'encoding': encoding,
'column_types': agate_tester()
}

with open(filepath, 'rb') as fp:
if len(fp.readlines()) < 2:
raise ValueError

try:
return agate.Table.from_csv(filepath, **kwargs)
except ValueError:
Expand Down
15 changes: 14 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ def csv_custom_types_double_cr():
"""


@pytest.fixture
def one_line_json_file():
return '''{ "property1": 1, "property2": 2}'''


def random_url():
return f"https://example.com/{uuid.uuid4()}.csv"

Expand Down Expand Up @@ -629,4 +634,12 @@ async def test_no_analysis_when_excel(client, rmock, extension):
print(jsonres)
assert jsonres['columns'] == ['rowid', 'col a', 'col b', 'col c']
assert jsonres['general_infos'] == { 'filetype': 'excel' }
assert jsonres['columns_infos'] == {}
assert jsonres['columns_infos'] == {}


async def test_fail_one_line_json_file(rmock, one_line_json_file, client):
content = one_line_json_file
url = random_url()
rmock.get(url, body=content)
res = await client.get(f"/apify?url={url}&analysis=yes")
assert res.status_code == 500