| 1 |
"""Every format, against one result set holding every type that is hard. |
| 2 |
|
| 3 |
The point of routing both files and text layouts through duckdb is that a value |
| 4 |
looks the same however it leaves: `--format table` and `--format csv` are |
| 5 |
supposed to agree cell for cell, and a Postgres timestamp and a DuckDB timestamp |
| 6 |
are supposed to agree with each other. Neither property is visible from inside |
| 7 |
one format, so these are snapshots -- what changes here is a change to |
| 8 |
Harlequin's output contract, and it should be read in a diff rather than |
| 9 |
discovered by an agent. |
| 10 |
|
| 11 |
They are single-file snapshots, one per format, in binary write mode: the bytes |
| 12 |
are the contract, so nothing here may re-encode a newline on the way to disk or |
| 13 |
back. Regenerate them the way every other snapshot in this repo is regenerated, |
| 14 |
with `--snapshot-update` on Python 3.10, and read the diff before committing it. |
| 15 |
""" |
| 16 |
|
| 17 |
from __future__ import annotations |
| 18 |
|
| 19 |
import csv |
| 20 |
import io |
| 21 |
from pathlib import Path |
| 22 |
from typing import Callable |
| 23 |
|
| 24 |
import duckdb |
| 25 |
import pytest |
| 26 |
from syrupy.assertion import SnapshotAssertion |
| 27 |
from syrupy.extensions.single_file import SingleFileSnapshotExtension |
| 28 |
|
| 29 |
from harlequin.export import write_stream |
| 30 |
from harlequin.layout import get_layout |
| 31 |
from harlequin.query import ResultSet |
| 32 |
|
| 33 |
ResultSetFactory = Callable[..., ResultSet] |
| 34 |
|
| 35 |
TYPE_COVERAGE_SQL = """ |
| 36 |
select |
| 37 |
1::integer as id, |
| 38 |
'ünïcode, "quoted"' as label, |
| 39 |
12345.6789::decimal(18, 4) as amount, |
| 40 |
1234567.89::double as approx, |
| 41 |
true as flag, |
| 42 |
date '2024-03-01' as day, |
| 43 |
timestamp '2024-03-01 12:30:00.123456' as at, |
| 44 |
timestamptz '2024-03-01 12:30:00+00' as at_tz, |
| 45 |
interval '1 day 2 hours' as span, |
| 46 |
'\\x00\\x01\\xFF'::blob as payload, |
| 47 |
[1, 2, 3] as items, |
| 48 |
{'a': 1, 'b': 'two'} as record, |
| 49 |
map([1, 2], ['one', 'two']) as lookup |
| 50 |
union all |
| 51 |
select |
| 52 |
2, 'a value considerably wider than its column name', null, null, false, |
| 53 |
null, null, null, null, null, null, null, null |
| 54 |
order by id |
| 55 |
""" |
| 56 |
|
| 57 |
LAYOUTS = ["table", "markdown", "vertical"] |
| 58 |
|
| 59 |
|
| 60 |
# One snapshot file per format, named like the format, so a reviewer opening |
| 61 |
# the diff sees a csv as a csv. The default `.raw` would make every one of them |
| 62 |
# look alike. |
| 63 |
class CsvSnapshot(SingleFileSnapshotExtension): |
| 64 |
file_extension = "csv" |
| 65 |
|
| 66 |
|
| 67 |
class TsvSnapshot(SingleFileSnapshotExtension): |
| 68 |
file_extension = "tsv" |
| 69 |
|
| 70 |
|
| 71 |
class JsonSnapshot(SingleFileSnapshotExtension): |
| 72 |
file_extension = "json" |
| 73 |
|
| 74 |
|
| 75 |
class JsonlSnapshot(SingleFileSnapshotExtension): |
| 76 |
file_extension = "jsonl" |
| 77 |
|
| 78 |
|
| 79 |
class TextSnapshot(SingleFileSnapshotExtension): |
| 80 |
file_extension = "txt" |
| 81 |
|
| 82 |
|
| 83 |
FILE_FORMATS = { |
| 84 |
"csv": CsvSnapshot, |
| 85 |
"tsv": TsvSnapshot, |
| 86 |
"json": JsonSnapshot, |
| 87 |
"jsonl": JsonlSnapshot, |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
@pytest.fixture(scope="module", autouse=True) |
| 92 |
def utc() -> None: |
| 93 |
"""Pin the connection that does the serializing. |
| 94 |
|
| 95 |
duckdb renders a `timestamptz` in the session's time zone, and the session |
| 96 |
here is duckdb's default connection -- the one both `write_stream()` and |
| 97 |
`text_columns()` reach through. Pinning it is what makes these snapshots the |
| 98 |
same on a runner in Denver and a runner in Berlin. |
| 99 |
""" |
| 100 |
duckdb.execute("set TimeZone='UTC'") |
| 101 |
|
| 102 |
|
| 103 |
@pytest.fixture |
| 104 |
def result(result_set: ResultSetFactory) -> ResultSet: |
| 105 |
return result_set(TYPE_COVERAGE_SQL) |
| 106 |
|
| 107 |
|
| 108 |
@pytest.mark.parametrize("format_name", list(FILE_FORMATS)) |
| 109 |
def test_file_formats( |
| 110 |
result: ResultSet, snapshot: SnapshotAssertion, format_name: str |
| 111 |
) -> None: |
| 112 |
out = io.BytesIO() |
| 113 |
write_stream(result.arrow_table(), out, format_name) |
| 114 |
assert out.getvalue() == snapshot(extension_class=FILE_FORMATS[format_name]) |
| 115 |
|
| 116 |
|
| 117 |
@pytest.mark.parametrize("name", LAYOUTS) |
| 118 |
def test_layouts(result: ResultSet, snapshot: SnapshotAssertion, name: str) -> None: |
| 119 |
out = io.StringIO() |
| 120 |
get_layout(name).write(result, out) |
| 121 |
assert out.getvalue().encode() == snapshot(extension_class=TextSnapshot) |
| 122 |
|
| 123 |
|
| 124 |
def test_the_snapshots_were_checked_out_with_lf(request: pytest.FixtureRequest) -> None: |
| 125 |
"""Otherwise every line of every snapshot above differs, invisibly. |
| 126 |
|
| 127 |
Windows checkouts default to `core.autocrlf`, and these are compared byte |
| 128 |
for byte. `.gitattributes` pins them; this says so when it hasn't. |
| 129 |
""" |
| 130 |
if request.config.option.update_snapshots: |
| 131 |
# syrupy writes the collection at the end of the session, so there is |
| 132 |
# nothing on disk to look at yet -- and it is about to be rewritten. |
| 133 |
pytest.skip("snapshots are rewritten at the end of this run") |
| 134 |
|
| 135 |
directory = Path(request.path).parent / "__snapshots__" / Path(request.path).stem |
| 136 |
snapshots = sorted(directory.iterdir()) |
| 137 |
assert snapshots, f"no snapshots in {directory}" |
| 138 |
mangled = [p.name for p in snapshots if b"\r\n" in p.read_bytes()] |
| 139 |
assert not mangled, ( |
| 140 |
f"{mangled} were checked out with CRLF line endings. Newlines are part " |
| 141 |
"of what these snapshots assert; .gitattributes pins them to LF." |
| 142 |
) |
| 143 |
|
| 144 |
|
| 145 |
def test_the_layouts_and_the_files_agree_cell_for_cell(result: ResultSet) -> None: |
| 146 |
"""The reason `text_columns()` casts through duckdb rather than calling |
| 147 |
`str()`: every string `--format table` prints is a string `--format csv` |
| 148 |
writes. |
| 149 |
|
| 150 |
Parsed back with the stdlib reader, not split on the delimiter: quoting is |
| 151 |
csv's framing of a value, not part of it, and unwrapping it is the only way |
| 152 |
to compare the values themselves. |
| 153 |
""" |
| 154 |
out = io.BytesIO() |
| 155 |
write_stream(result.arrow_table(), out, "tsv", {"header": False, "na_rep": "∅"}) |
| 156 |
from_csv = list(csv.reader(out.getvalue().decode().splitlines(), delimiter="\t")) |
| 157 |
|
| 158 |
text = result.text_columns() |
| 159 |
from_text = [ |
| 160 |
["∅" if value is None else value for value in row] |
| 161 |
for row in zip(*[column.to_pylist() for column in text.columns], strict=False) |
| 162 |
] |
| 163 |
|
| 164 |
assert from_text == from_csv |