Skip to content

Commit

Permalink
Add reverse test for CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
sbscully committed Jul 28, 2024
1 parent 0e1a8af commit 65bd30c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
1 change: 1 addition & 0 deletions test/fixtures/reverse.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1,51.9526622,7.6324709
14 changes: 7 additions & 7 deletions test/test_cli_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@pytest.fixture(autouse=True)
def around():
yield
pathlib.Path("test/fixtures/output.txt").unlink(True)
pathlib.Path("test/fixtures/output.csv").unlink(True)

def assert_parse_args_error(args, message, capfd):
with pytest.raises(SystemExit):
Expand All @@ -27,7 +27,7 @@ def test_invalid_api_key(capfd):
[
"--api-key", "invalid",
"--input", "test/fixtures/input.txt",
"--output", "test/fixtures/output.txt",
"--output", "test/fixtures/output.csv",
"--forward"
],
'invalid API key',
Expand All @@ -51,7 +51,7 @@ def test_requires_forward_or_reverse(capfd):
[
"--api-key", "oc_gc_12345678901234567890123456789012",
"--input", "test/fixtures/input.txt",
"--output", "test/fixtures/output.txt",
"--output", "test/fixtures/output.csv",
],
'one of the arguments --forward --reverse is required',
capfd
Expand All @@ -62,7 +62,7 @@ def test_argument_range(capfd):
[
"--api-key", "oc_gc_12345678901234567890123456789012",
"--input", "test/fixtures/input.txt",
"--output", "test/fixtures/output.txt",
"--output", "test/fixtures/output.csv",
"--forward",
"--workers", "200"
],
Expand All @@ -74,7 +74,7 @@ def test_full_argument_list():
args = parse_args([
"--api-key", "oc_gc_12345678901234567890123456789012",
"--input", "test/fixtures/input.txt",
"--output", "test/fixtures/output.txt",
"--output", "test/fixtures/output.csv",
"--reverse",
"--has-header",
"--input-columns", "1,2",
Expand All @@ -92,7 +92,7 @@ def test_full_argument_list():

assert args.api_key == "oc_gc_12345678901234567890123456789012"
assert args.input.name == "test/fixtures/input.txt"
assert args.output.name == "test/fixtures/output.txt"
assert args.output.name == "test/fixtures/output.csv"
assert args.reverse is True
assert args.forward is False
assert args.has_headers is True
Expand All @@ -112,7 +112,7 @@ def test_defaults():
args = parse_args([
"--api-key", "12345678901234567890123456789012",
"--input", "test/fixtures/input.txt",
"--output", "test/fixtures/output.txt",
"--output", "test/fixtures/output.csv",
"--reverse"
])

Expand Down
41 changes: 32 additions & 9 deletions test/test_cli_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,44 @@
@pytest.fixture(autouse=True)
def around():
yield
pathlib.Path("test/fixtures/output.txt").unlink(True)
pathlib.Path("test/fixtures/output.csv").unlink(True)

@pytest.mark.asyncio
def test_main():
def assert_output(path, length, lines):
assert pathlib.Path(path).exists()

with open(path, "r") as f:
actual = f.readlines()
assert len(actual) == length

for i, expected in enumerate(lines):
assert actual[i].strip() == expected

def test_forward():
main([
"--api-key", "6d0e711d72d74daeb2b0bfd2a5cdfdba",
"--input", "test/fixtures/forward.csv",
"--output", "test/fixtures/output.txt",
"--output", "test/fixtures/output.csv",
"--forward",
"--input-columns", "2,3,4",
])

assert pathlib.Path("test/fixtures/output.txt").exists()
assert_output(
path="test/fixtures/output.csv",
length=3,
lines=['1,51.9526622,7.6324709,social_facility,Germany,,Münster,48153,Friedrich-Ebert-Straße,7,9,"Chance e.V., Friedrich-Ebert-Straße 7, 48153 Münster, Germany"']
)

def test_reverse():
main([
"--api-key", "6d0e711d72d74daeb2b0bfd2a5cdfdba",
"--input", "test/fixtures/reverse.csv",
"--output", "test/fixtures/output.csv",
"--reverse",
"--add-columns", "city,postcode"
])

with open("test/fixtures/output.txt", "r") as f:
lines = f.readlines()
assert len(lines) == 3
assert lines[0].strip() == '1,51.9526622,7.6324709,social_facility,Germany,,Münster,48153,Friedrich-Ebert-Straße,7,9,"Chance e.V., Friedrich-Ebert-Straße 7, 48153 Münster, Germany"'
assert_output(
path="test/fixtures/output.csv",
length=1,
lines=['1,51.9526622,7.6324709,Münster,48153']
)

0 comments on commit 65bd30c

Please sign in to comment.