File: test_json_output_adapter.py

package info (click to toggle)
cli-helpers 2.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 356 kB
  • sloc: python: 2,208; makefile: 16
file content (64 lines) | stat: -rw-r--r-- 2,264 bytes parent folder | download
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
# -*- coding: utf-8 -*-
"""Test the json output adapter."""

from __future__ import unicode_literals

from decimal import Decimal

from cli_helpers.tabular_output import json_output_adapter


def test_jsonl_wrapper():
    """Test the jsonl output adapter."""
    # Test jsonl output.
    data = [["ab\r\nc", 1], ["d", 456]]
    headers = ["letters", "number"]
    output = json_output_adapter.adapter(iter(data), headers, table_format="jsonl")
    assert (
        "\n".join(output)
        == """{"letters":"ab\\r\\nc","number":1}\n{"letters":"d","number":456}"""
    )


def test_unicode_with_jsonl():
    """Test that the jsonl wrapper can pass through non-ascii characters."""
    data = [["观音", 1], ["Ποσειδῶν", 456]]
    headers = ["letters", "number"]
    output = json_output_adapter.adapter(data, headers, table_format="jsonl")
    assert (
        "\n".join(output)
        == """{"letters":"观音","number":1}\n{"letters":"Ποσειδῶν","number":456}"""
    )


def test_decimal_with_jsonl():
    """Test that the jsonl wrapper can pass through Decimal values."""
    data = [["ab\r\nc", 1], ["d", Decimal(4.56)]]
    headers = ["letters", "number"]
    output = json_output_adapter.adapter(iter(data), headers, table_format="jsonl")
    assert (
        "\n".join(output)
        == """{"letters":"ab\\r\\nc","number":1}\n{"letters":"d","number":4.56}"""
    )


def test_null_with_jsonl():
    """Test that the jsonl wrapper can pass through null values."""
    data = [["ab\r\nc", None], ["d", None]]
    headers = ["letters", "value"]
    output = json_output_adapter.adapter(iter(data), headers, table_format="jsonl")
    assert (
        "\n".join(output)
        == """{"letters":"ab\\r\\nc","value":null}\n{"letters":"d","value":null}"""
    )


def test_unicode_with_jsonl_esc():
    """Test that the jsonl_escaped wrapper JSON-escapes non-ascii characters."""
    data = [["观音", 1], ["Ποσειδῶν", 456]]
    headers = ["letters", "number"]
    output = json_output_adapter.adapter(data, headers, table_format="jsonl_escaped")
    assert (
        "\n".join(output)
        == """{"letters":"\\u89c2\\u97f3","number":1}\n{"letters":"\\u03a0\\u03bf\\u03c3\\u03b5\\u03b9\\u03b4\\u1ff6\\u03bd","number":456}"""
    )