File size: 3,454 Bytes
b199558 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | import os
import subprocess
from pathlib import Path
WORK = Path(os.environ["TB_WORK"])
BASH = os.environ.get("TB_BASH", "bash")
CANON = "./bin/canon"
def run_canon(source, target):
return subprocess.run(
[BASH, "-c", 'exec ./bin/canon "$@"', "canon", str(source), str(target)],
cwd=str(WORK),
capture_output=True,
text=True,
encoding="utf-8",
)
def case_dir():
path = Path(os.environ["TB_ROOT"]) / "tmp" / "cases"
path.mkdir(parents=True, exist_ok=True)
return path
def write_case(name, body, newline=""):
path = case_dir() / name
with open(path, "w", encoding="utf-8", newline=newline) as handle:
handle.write(body)
return path
def test_missing_input_exits_2():
target = case_dir() / "missing.tsv"
if target.exists():
target.unlink()
proc = run_canon("does-not-exist.csv", target)
assert proc.returncode == 2, proc.stderr
assert proc.stderr.strip() == "no such file: does-not-exist.csv"
assert not target.exists(), "output must not be created on error"
def test_duplicate_column_exits_3():
source = write_case("dup.csv", "id,ID\n1,2\n")
target = case_dir() / "dup.tsv"
if target.exists():
target.unlink()
proc = run_canon(source, target)
assert proc.returncode == 3, proc.stderr
assert proc.stderr.strip() == "duplicate column: ID"
assert not target.exists()
def test_malformed_row_exits_4_with_physical_line():
source = write_case("bad.csv", "id,sku\n1,a\n2,b,c\n")
target = case_dir() / "bad.tsv"
if target.exists():
target.unlink()
proc = run_canon(source, target)
assert proc.returncode == 4, proc.stderr
assert proc.stderr.strip() == "malformed row: line 3"
assert not target.exists()
def test_orders_sample_is_canonicalised():
target = case_dir() / "orders.tsv"
proc = run_canon("data/orders.csv", target)
assert proc.returncode == 0, proc.stderr
expected = (
"id\tsku\tqty\tprice\n"
"1\tSKU-1\t5\t0.75\n"
"1\tSKU-2\t10\t2\n"
"2\tSKU-1\t5\t1000\n"
"3\tSKU-9\t2\t1.5\n"
)
assert target.read_bytes() == expected.encode("utf-8")
def test_output_has_no_bom_and_uses_lf():
target = case_dir() / "orders.tsv"
run_canon("data/orders.csv", target)
raw = target.read_bytes()
assert not raw.startswith(b"\xef\xbb\xbf")
assert b"\r" not in raw
def test_embedded_newline_survives():
source = write_case("multi.csv", 'id,note\n2,"line one\nline two"\n1,plain\n')
target = case_dir() / "multi.tsv"
proc = run_canon(source, target)
assert proc.returncode == 0, proc.stderr
expected = "id\tnote\n1\tplain\n2\tline one\nline two\n"
assert target.read_bytes() == expected.encode("utf-8")
def test_numeric_normalisation():
source = write_case(
"nums.csv", "id,v\n1,+5\n2,007\n3,-0\n4,1.0\n5,2.5e-3\n6,abc\n"
)
target = case_dir() / "nums.tsv"
proc = run_canon(source, target)
assert proc.returncode == 0, proc.stderr
expected = "id\tv\n1\t5\n2\t7\n3\t0\n4\t1\n5\t0.0025\n6\tabc\n"
assert target.read_bytes() == expected.encode("utf-8")
def test_sorting_is_byte_order_not_numeric():
source = write_case("ids.csv", "id,x\n10,a\n9,b\n")
target = case_dir() / "ids.tsv"
proc = run_canon(source, target)
assert proc.returncode == 0, proc.stderr
assert target.read_bytes() == b"id\tx\n10\ta\n9\tb\n"
|