| 1 |
|
| 2 |
"""Test the json output adapter.""" |
| 3 |
|
| 4 |
from __future__ import unicode_literals |
| 5 |
|
| 6 |
from decimal import Decimal |
| 7 |
|
| 8 |
from cli_helpers.tabular_output import json_output_adapter |
| 9 |
|
| 10 |
|
| 11 |
def test_jsonl_wrapper(): |
| 12 |
"""Test the jsonl output adapter.""" |
| 13 |
|
| 14 |
data = [["ab\r\nc", 1], ["d", 456]] |
| 15 |
headers = ["letters", "number"] |
| 16 |
output = json_output_adapter.adapter(iter(data), headers, table_format="jsonl") |
| 17 |
assert ( |
| 18 |
"\n".join(output) |
| 19 |
== """{"letters":"ab\\r\\nc","number":1}\n{"letters":"d","number":456}""" |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
def test_unicode_with_jsonl(): |
| 24 |
"""Test that the jsonl wrapper can pass through non-ascii characters.""" |
| 25 |
data = [["观音", 1], ["Ποσειδῶν", 456]] |
| 26 |
headers = ["letters", "number"] |
| 27 |
output = json_output_adapter.adapter(data, headers, table_format="jsonl") |
| 28 |
assert ( |
| 29 |
"\n".join(output) |
| 30 |
== """{"letters":"观音","number":1}\n{"letters":"Ποσειδῶν","number":456}""" |
| 31 |
) |
| 32 |
|
| 33 |
|
| 34 |
def test_decimal_with_jsonl(): |
| 35 |
"""Test that the jsonl wrapper can pass through Decimal values.""" |
| 36 |
data = [["ab\r\nc", 1], ["d", Decimal(4.56)]] |
| 37 |
headers = ["letters", "number"] |
| 38 |
output = json_output_adapter.adapter(iter(data), headers, table_format="jsonl") |
| 39 |
assert ( |
| 40 |
"\n".join(output) |
| 41 |
== """{"letters":"ab\\r\\nc","number":1}\n{"letters":"d","number":4.56}""" |
| 42 |
) |
| 43 |
|
| 44 |
|
| 45 |
def test_null_with_jsonl(): |
| 46 |
"""Test that the jsonl wrapper can pass through null values.""" |
| 47 |
data = [["ab\r\nc", None], ["d", None]] |
| 48 |
headers = ["letters", "value"] |
| 49 |
output = json_output_adapter.adapter(iter(data), headers, table_format="jsonl") |
| 50 |
assert ( |
| 51 |
"\n".join(output) |
| 52 |
== """{"letters":"ab\\r\\nc","value":null}\n{"letters":"d","value":null}""" |
| 53 |
) |
| 54 |
|
| 55 |
|
| 56 |
def test_unicode_with_jsonl_esc(): |
| 57 |
"""Test that the jsonl_escaped wrapper JSON-escapes non-ascii characters.""" |
| 58 |
data = [["观音", 1], ["Ποσειδῶν", 456]] |
| 59 |
headers = ["letters", "number"] |
| 60 |
output = json_output_adapter.adapter(data, headers, table_format="jsonl_escaped") |
| 61 |
assert ( |
| 62 |
"\n".join(output) |
| 63 |
== """{"letters":"\\u89c2\\u97f3","number":1}\n{"letters":"\\u03a0\\u03bf\\u03c3\\u03b5\\u03b9\\u03b4\\u1ff6\\u03bd","number":456}""" |
| 64 |
) |