Last active
April 12, 2017 08:14
-
-
Save OriHoch/67947d3e7ba12e3d2265a0f9b8076314 to your computer and use it in GitHub Desktop.
test case for tabulator csv parsing text files hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tabulator | |
from functools import partial | |
def _test_from_stream(stream, expected_content): | |
try: | |
stream.open() | |
except tabulator.stream.exceptions.FormatError as e: | |
if str(e) == "Format has been detected as HTML (not supported)": | |
pass | |
else: | |
raise | |
rows = [] | |
for rownum, row in enumerate(stream, start=1): | |
if len(row) == 0: | |
rows.append("") | |
else: | |
assert len(row) == 1, "row {}: expected list with 1 item, got list with {} items".format(rownum, len(row)) | |
rows.append(row[0]) | |
actual_content = "\n".join(rows) | |
assert expected_content == actual_content | |
def _test_from_str(content, expected_content): | |
stream = tabulator.stream.Stream("text://{}".format(content), headers=["data"], format="csv", delimiter="\n") | |
_test_from_stream(stream, expected_content) | |
test_cases = (("quote_at_start_of_line_is_removed", '"line', 'line'), | |
("trailing_newlines_are_stripped", 'line\nline\n', 'line\nline')) | |
for name, content, expected_cnotent in test_cases: | |
globals()["test_stream_remote_resources_txt_{}".format(name)] = partial(_test_from_str, content, expected_cnotent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment