Пример #1
0
def test_separators():
    h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
            {'nifty': 87}, {'field': 'yes', 'morefield': False} ]

    expect = textwrap.dedent("""\
so
  so
    "blorpie"
  many and
  so
    "whoops"
  many and
  so many and
  "d-shtaeou" and
  "d-nthiouh" and
  "i-vhbjkhnth" and
  such
    "nifty" is 87
  wow and
  such
    "field" is "yes",
    "morefield" is no
  wow
many""")


    d1 = dson.dumps(h)
    d2 = dson.dumps(h, indent=2, sort_keys=True)

    h1 = dson.loads(d1)
    h2 = dson.loads(d2)

    assert h1 == h
    assert h2 == h
    assert d2 == expect
Пример #2
0
def index(url):
    print url
    if request.method == 'GET':
        return dson.dumps(requests.get(url, params=request.args).json())
    elif request.method == 'POST':
        res = requests.post(url, data=request.json)
        return dson.dumps(res.json())
    return "such 'error' much sorry wow"
Пример #3
0
def test_highly_nested_objects_encoding():
    # See #12051
    l, d = [], {}
    for x in xrange(100000):
        l, d = [l], {'k':d}
    with pytest.raises(RuntimeError):
        dson.dumps(l)
    with pytest.raises(RuntimeError):
        dson.dumps(d)
Пример #4
0
def test_dictrecursion():
    x = {}
    x["test"] = x
    pytest.raises(ValueError, dson.dumps, x)

    x = {}
    y = {"a": x, "b": x}
    # ensure that the marker is cleared
    dson.dumps(x)
Пример #5
0
def test_dictrecursion():
    x = {}
    x["test"] = x
    pytest.raises(ValueError, dson.dumps, x)

    x = {}
    y = {"a": x, "b": x}
    # ensure that the marker is cleared
    dson.dumps(x)
Пример #6
0
def test_highly_nested_objects_encoding():
    # See #12051
    l, d = [], {}
    for x in xrange(100000):
        l, d = [l], {'k': d}
    with pytest.raises(RuntimeError):
        dson.dumps(l)
    with pytest.raises(RuntimeError):
        dson.dumps(d)
Пример #7
0
def test_listrecursion():
    x = []
    x.append(x)
    pytest.raises(ValueError, dson.dumps, x)

    x = []
    y = [x]
    x.append(y)
    pytest.raises(ValueError, dson.dumps, x)

    y = []
    x = [y, y]
    # ensure that the marker is cleared
    dson.dumps(x)
Пример #8
0
def test_listrecursion():
    x = []
    x.append(x)
    pytest.raises(ValueError, dson.dumps, x)

    x = []
    y = [x]
    x.append(y)
    pytest.raises(ValueError, dson.dumps, x)

    y = []
    x = [y, y]
    # ensure that the marker is cleared
    dson.dumps(x)
Пример #9
0
def test_allow_nan():
    val = float('nan')
    out = dson.dumps([val])
    res = dson.loads(out)
    assert len(res) == 1
    assert res[0] != res[0]
    pytest.raises(ValueError, dson.dumps, [val], allow_nan=False)
Пример #10
0
def test_allow_nan():
    val = float('nan')
    out = dson.dumps([val])
    res = dson.loads(out)
    assert len(res) == 1
    assert res[0] != res[0]
    pytest.raises(ValueError, dson.dumps, [val], allow_nan=False)
Пример #11
0
    def check(indent, expected):
        d1 = dson.dumps(h, indent=indent)
        assert d1 == expected

        sio = StringIO()
        dson.dump(h, sio, indent=indent)
        assert sio.getvalue() == expected
Пример #12
0
def test_encode_mutated():
    a = [object()] * 10

    def crasher(obj):
        del a[-1]

    assert dson.dumps(
        a, default=crasher
    ) == 'so empty and empty and empty and empty and empty many'
Пример #13
0
 def save(self, doc):
     assert self.dbm.is_transaction_writable(), "Transaction is read-only"
     if 'id' in doc:
         old_doc = self.get(doc['id'])
     else:
         doc['id'] = uniqueid.create()
         old_doc = None
     key = dson.dumpone(doc['id'])
     value = dson.dumps(doc)
     self.dbm.put(self.name, key, value)
     self.indexes.update(old_doc, doc)
     return doc
Пример #14
0
def test_separators():
    h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
         {
             'nifty': 87
         }, {
             'field': 'yes',
             'morefield': False
         }]

    expect = textwrap.dedent("""\
so
  so
    "blorpie"
  many and
  so
    "whoops"
  many and
  so many and
  "d-shtaeou" and
  "d-nthiouh" and
  "i-vhbjkhnth" and
  such
    "nifty" is 87
  wow and
  such
    "field" is "yes",
    "morefield" is no
  wow
many""")

    d1 = dson.dumps(h)
    d2 = dson.dumps(h, indent=2, sort_keys=True)

    h1 = dson.loads(d1)
    h2 = dson.loads(d2)

    assert h1 == h
    assert h2 == h
    assert d2 == expect
Пример #15
0
def output(data, **kwargs):  # pylint: disable=unused-argument
    '''
    Print the output data in JSON
    '''
    try:
        dump_opts = {'indent': 4, 'default': repr}

        if 'output_indent' in __opts__:

            indent = __opts__.get('output_indent')
            sort_keys = False

            if indent == 'pretty':
                indent = 4
                sort_keys = True

            elif isinstance(indent, six.integer_types):
                if indent >= 0:
                    indent = indent
                else:
                    indent = None

            dump_opts['indent'] = indent
            dump_opts['sort_keys'] = sort_keys

        return dson.dumps(data, **dump_opts)

    except UnicodeDecodeError as exc:
        log.error('Unable to serialize output to dson')
        return dson.dumps({
            'error': 'Unable to serialize output to DSON',
            'message': six.text_type(exc)
        })

    except TypeError:
        log.debug('An error occurred while outputting DSON', exc_info=True)
    # Return valid JSON for unserializable objects
    return dson.dumps({})
Пример #16
0
def output(data, **kwargs):  # pylint: disable=unused-argument
    """
    Print the output data in JSON
    """
    try:
        dump_opts = {"indent": 4, "default": repr}

        if "output_indent" in __opts__:

            indent = __opts__.get("output_indent")
            sort_keys = False

            if indent == "pretty":
                indent = 4
                sort_keys = True

            elif isinstance(indent, six.integer_types):
                if indent < 0:
                    indent = None

            dump_opts["indent"] = indent
            dump_opts["sort_keys"] = sort_keys

        return dson.dumps(data, **dump_opts)

    except UnicodeDecodeError as exc:
        log.error("Unable to serialize output to dson")
        return dson.dumps({
            "error": "Unable to serialize output to DSON",
            "message": six.text_type(exc),
        })

    except TypeError:
        log.debug("An error occurred while outputting DSON", exc_info=True)
    # Return valid JSON for unserializable objects
    return dson.dumps({})
Пример #17
0
 def test_all(self):
     value = {
         'string': 'I\'m a string',
         'unicode': u'h\xf6d\xf6',
         'float': 23.32,
         'int': 123,
         'realy_big_int': 1000000000000000000,
         'bool': True,
         'bool_false': False,
         'none': None,
         'datetime': datetime.utcnow().replace(tzinfo=pytz.utc),
         'list': [1,2,3,"poodles",{'foo':'bar'},["poodles",4,5,6]]
     }
     bytes = dson.dumps(value)
     deserialized_value = dson.loads(bytes)
     self.assertEquals(deserialized_value, value)
Пример #18
0
def test_allow_inf(val):
    out = dson.dumps([val])
    assert dson.loads(out) == [val]
    pytest.raises(ValueError, dson.dumps, [val], allow_nan=False)
Пример #19
0
def test_parse():
    # test in/out equivalence and parsing
    res = dson.loads(JSON)
    out = dson.dumps(res)
    assert res == dson.loads(out)
Пример #20
0
def test_encode_truefalse():
    assert dson.dumps({True: False, False: True}, sort_keys=True) == \
            'such "no" is yes, "yes" is no wow'
    assert dson.dumps(
            {2: 3.0, 4.0: long(5), False: 1, long(6): True}, sort_keys=True) == \
            'such "no" is 1, "2" is 3.0, "4.0" is 5, "6" is yes wow'
Пример #21
0
def test_dumps():
    assert dson.dumps({}) == 'such wow'
Пример #22
0
def test_ordered_dict():
    items = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
    s = dson.dumps(OrderedDict(items))
    assert s == 'such "one" is 1, "two" is 2, "three" is 3, "four" is 4, "five" is 5 wow'
Пример #23
0
def test_default():
    assert dson.dumps(type, default=repr) == dson.dumps(repr(type))
Пример #24
0
def test_encode_mutated():
    a = [object()] * 10
    def crasher(obj):
        del a[-1]
    assert dson.dumps(a, default=crasher) == 'so empty and empty and empty and empty and empty many'
Пример #25
0
def test_circular_off_default():
    dson.dumps([set()], default=default_iterable, check_circular=False)
    pytest.raises(TypeError, dson.dumps, [set()], check_circular=False)
Пример #26
0
def test_default():
    assert dson.dumps(type, default=repr) == dson.dumps(repr(type))
Пример #27
0
def test_dumps():
    assert dson.dumps({}) == 'such wow'
Пример #28
0
def test_ordered_dict():
    items = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
    s = dson.dumps(OrderedDict(items))
    assert s == 'such "one" is 1, "two" is 2, "three" is 3, "four" is 4, "five" is 5 wow'
Пример #29
0
def test_ints(num):
    assert dson.dumps(num) == str(num)
    assert int(dson.dumps(num)) == num
    assert dson.loads(dson.dumps(num)) == num
    assert dson.loads(unicode(dson.dumps(num))) == num
Пример #30
0
def test_ints(num):
    assert dson.dumps(num) == str(num)
    assert int(dson.dumps(num)) == num
    assert dson.loads(dson.dumps(num)) == num
    assert dson.loads(unicode(dson.dumps(num))) == num
Пример #31
0
def test_allow_inf(val):
    out = dson.dumps([val])
    assert dson.loads(out) == [val]
    pytest.raises(ValueError, dson.dumps, [val], allow_nan=False)
Пример #32
0
def test_floats(num):
    assert float(dson.dumps(num).replace('very', 'e')) == num
    assert dson.loads(dson.dumps(num)) == num
    assert dson.loads(unicode(dson.dumps(num))) == num
Пример #33
0
 def write_elements(self, filepath, data):
     data_set = open('%s.dson' % filepath, 'w')
     dson_data = dson.dumps(data, indent=4)
     data_set.write(dson_data)
     data_set.close()
Пример #34
0
def test_circular_default():
    dson.dumps([set()], default=default_iterable)
    pytest.raises(TypeError, dson.dumps, [set()])
Пример #35
0
def test_encode_truefalse():
    assert dson.dumps({True: False, False: True}, sort_keys=True) == \
            'such "no" is yes, "yes" is no wow'
    assert dson.dumps(
            {2: 3.0, 4.0: long(5), False: 1, long(6): True}, sort_keys=True) == \
            'such "no" is 1, "2" is 3.0, "4.0" is 5, "6" is yes wow'
Пример #36
0
def test_circular_off_default():
    dson.dumps([set()], default=default_iterable, check_circular=False)
    pytest.raises(TypeError, dson.dumps, [set()], check_circular=False)
Пример #37
0
def test_floats(num):
    assert float(dson.dumps(num).replace('very', 'e')) == num
    assert dson.loads(dson.dumps(num)) == num
    assert dson.loads(unicode(dson.dumps(num))) == num
Пример #38
0
def test_circular_default():
    dson.dumps([set()], default=default_iterable)
    pytest.raises(TypeError, dson.dumps, [set()])