Exemplo n.º 1
0
def test_rounding_tuples(values, expected, precision):
    before, after = values
    if simplejson_dumps:
        # simplejson supports `namedtuple_as_object` param unlike json
        assert dumps(before, precision=precision) == expected["as_object"]
        assert (dumps(before, precision=precision,
                      namedtuple_as_object=False) == expected["as_list"])
    else:
        assert dumps(before, precision=precision) == expected["as_list"]
    assert before == after
Exemplo n.º 2
0
def test_raw_encoder_with_date_unix_time_default():
    class Foo(object):
        def __repr__(self):
            return "<Foo>"

    # by default `raw_encoder` encodes dates as ISO
    assert dumps({
        "foo": Foo(),
        "bar": datetime.date(2018, 1, 1)
    },
                 default=raw_encoder) == dumps({
                     "foo": "<Foo>",
                     "bar": "2018-01-01"
                 })
Exemplo n.º 3
0
def test_dump_with_default_and_date_as_unix_time():
    with pytest.raises(TypeError):
        # `date_as_unix_time` leads to failing when `default` is specified
        dumps(datetime.date(2018, 1, 1), default=str, date_as_unix_time=True)
Exemplo n.º 4
0
def test_dump_with_default():
    # by default `dumps` encodes dates as ISO
    assert dumps(datetime.date(2018, 1, 1), default=str) == '"2018-01-01"'
Exemplo n.º 5
0
def test_date_as_unix_time_default():
    # When `date_as_unix_time` is not passed, then it is disabled and dates are converted as ISO
    assert dumps(datetime.date(2018, 1, 1)) == '"2018-01-01"'
Exemplo n.º 6
0
def test_rounding(values, expected, precision):
    before, after = values
    assert dumps(before, precision=precision) == expected
    if after:
        # check if object was not modified by rounding and removing of dict_items
        assert before == after
Exemplo n.º 7
0
def test_dumps(value, expected, date_as_unix_time):
    assert dumps(value, date_as_unix_time=date_as_unix_time) == expected
Exemplo n.º 8
0
def test_json_with_use_decimal_argument():
    with pytest.raises(KiwiJsonError, match=r".*Decimal.*"):
        dumps(Decimal(0), use_decimal=True)
Exemplo n.º 9
0
def test_simplejson_encoder_with_decimal(value, expected):
    assert dumps(value, use_decimal=True) == expected