|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Test the json output adapter.""" |
| 3 | + |
| 4 | +from __future__ import unicode_literals |
| 5 | + |
| 6 | +from cli_helpers.tabular_output import json_output_adapter |
| 7 | + |
| 8 | + |
| 9 | +def test_jsonl_wrapper(): |
| 10 | + """Test the jsonl output adapter.""" |
| 11 | + # Test jsonl output. |
| 12 | + data = [["ab\r\nc", 1], ["d", 456]] |
| 13 | + headers = ["letters", "number"] |
| 14 | + output = json_output_adapter.adapter(iter(data), headers, table_format="jsonl") |
| 15 | + assert ( |
| 16 | + "\n".join(output) |
| 17 | + == """{"letters":"ab\\r\\nc","number":1}\n{"letters":"d","number":456}""" |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +def test_unicode_with_jsonl(): |
| 22 | + """Test that the jsonl wrapper can pass through non-ascii characters.""" |
| 23 | + data = [["观音", 1], ["Ποσειδῶν", 456]] |
| 24 | + headers = ["letters", "number"] |
| 25 | + output = json_output_adapter.adapter(data, headers, table_format="jsonl") |
| 26 | + assert ( |
| 27 | + "\n".join(output) |
| 28 | + == """{"letters":"观音","number":1}\n{"letters":"Ποσειδῶν","number":456}""" |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +def test_unicode_with_jsonl_esc(): |
| 33 | + """Test that the jsonl_escaped wrapper JSON-escapes non-ascii characters.""" |
| 34 | + data = [["观音", 1], ["Ποσειδῶν", 456]] |
| 35 | + headers = ["letters", "number"] |
| 36 | + output = json_output_adapter.adapter(data, headers, table_format="jsonl_escaped") |
| 37 | + assert ( |
| 38 | + "\n".join(output) |
| 39 | + == """{"letters":"\\u89c2\\u97f3","number":1}\n{"letters":"\\u03a0\\u03bf\\u03c3\\u03b5\\u03b9\\u03b4\\u1ff6\\u03bd","number":456}""" |
| 40 | + ) |
0 commit comments