Пример #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 test_object_pairs_hook():
    s = ('such "xkd" is 1, "kcw" is 2, "art" is 3, "hxm" is 4, '
            '"qrt" is 5, "pad" is 6, "hoy" is 7 wow')
    p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
            ("qrt", 5), ("pad", 6), ("hoy", 7)]
    assert dson.loads(s) == dict(p)
    assert dson.loads(s, object_pairs_hook=lambda x: x) == p
    assert dson.load(StringIO(s), object_pairs_hook=lambda x: x) == p
    od = dson.loads(s, object_pairs_hook=OrderedDict)
    assert od == OrderedDict(p)
    assert type(od) == OrderedDict
    # the object_pairs_hook takes priority over the object_hook
    assert dson.loads(s, object_pairs_hook=OrderedDict,
                                object_hook=lambda x: None) == OrderedDict(p)
Пример #3
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)
Пример #4
0
 def get(self, id):
     key = dson.dumpone(id)
     value = self.dbm.get(self.name, key)
     if value is None:
         return None
     else:
         return dson.loads(value)
Пример #5
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)
Пример #6
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
Пример #7
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)
Пример #8
0
def render(dson_input, saltenv='base', sls='', **kwargs):
    '''
    Accepts DSON data as a string or as a file object and runs it through the
    JSON parser.

    :rtype: A Python data structure
    '''
    if not isinstance(dson_input, six.string_types):
        dson_input = dson_input.read()

    log.debug('DSON input = %s', dson_input)

    if dson_input.startswith('#!'):
        dson_input = dson_input[(dson_input.find('\n') + 1):]
    if not dson_input.strip():
        return {}
    return dson.loads(dson_input)
Пример #9
0
def render(dson_input, saltenv="base", sls="", **kwargs):
    """
    Accepts DSON data as a string or as a file object and runs it through the
    JSON parser.

    :rtype: A Python data structure
    """
    if not isinstance(dson_input, str):
        dson_input = dson_input.read()

    log.debug("DSON input = %s", dson_input)

    if dson_input.startswith("#!"):
        dson_input = dson_input[(dson_input.find("\n") + 1):]
    if not dson_input.strip():
        return {}
    return dson.loads(dson_input)
Пример #10
0
def render(dson_input, saltenv="base", sls="", **kwargs):
    """
    Accepts DSON data as a string or as a file object and runs it through the
    JSON parser.

    :rtype: A Python data structure
    """
    if not isinstance(dson_input, six.string_types):
        dson_input = dson_input.read()

    log.debug("DSON input = %s", dson_input)

    if dson_input.startswith("#!"):
        dson_input = dson_input[(dson_input.find("\n") + 1) :]
    if not dson_input.strip():
        return {}
    return dson.loads(dson_input)
Пример #11
0
def test_extra_data():
    s = 'so 1 and 2 also 3 many 5'
    with pytest.raises(ValueError) as e: dson.loads(s)
    assert 'Extra data' in str(e)
Пример #12
0
def test_empty_object_pairs_hook():
    assert dson.loads('such wow', object_pairs_hook=OrderedDict) == OrderedDict()
    assert dson.loads('such "empty" is such wow wow',
        object_pairs_hook=OrderedDict) == OrderedDict([('empty', OrderedDict())])
Пример #13
0
 def load(self, data):
     return dson.loads(data)
Пример #14
0
def test_empty_objects():
    assert dson.loads('such wow') == {}
    assert dson.loads('so many') == []
    assert dson.loads('""') == u""
    assert isinstance(dson.loads('""'), unicode)
Пример #15
0
def test_out_of_range(doc, expected):
    assert dson.loads(doc) == expected
Пример #16
0
def test_out_of_range(doc, expected):
    assert dson.loads(doc) == expected
Пример #17
0
def test_highly_nested_objects_decoding():
    # test that loading highly-nested objects doesn't segfault when C
    # accelerations are used. See #12017
    # str
    with pytest.raises(RuntimeError):
        dson.loads('such "a" is ' * 100000 + ' 1 ' + ' wow ' * 100000)
    with pytest.raises(RuntimeError):
        dson.loads('such "a" is ' * 100000 + ' so 1 many ' + ' wow ' * 100000)
    with pytest.raises(RuntimeError):
        dson.loads('so ' * 100000 + ' 1 ' + ' many ' * 100000)
    # unicode
    with pytest.raises(RuntimeError):
        dson.loads(u'such "a" is ' * 100000 + u' 1 ' + u' wow ' * 100000)
    with pytest.raises(RuntimeError):
        dson.loads(u'such "a" is ' * 100000 + u' so 1 many' +
                   u' wow ' * 100000)
    with pytest.raises(RuntimeError):
        dson.loads(u'so ' * 100000 + u' 1 ' + u' many ' * 100000)
Пример #18
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
Пример #19
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
Пример #20
0
def test_parse():
    # test in/out equivalence and parsing
    res = dson.loads(JSON)
    out = dson.dumps(res)
    assert res == dson.loads(out)
Пример #21
0
def test_highly_nested_objects_decoding():
    # test that loading highly-nested objects doesn't segfault when C
    # accelerations are used. See #12017
    # str
    with pytest.raises(RuntimeError):
        dson.loads('such "a" is ' * 100000 + ' 1 ' + ' wow ' * 100000)
    with pytest.raises(RuntimeError):
        dson.loads('such "a" is ' * 100000 + ' so 1 many ' + ' wow ' * 100000)
    with pytest.raises(RuntimeError):
        dson.loads('so ' * 100000 + ' 1 ' + ' many ' * 100000)
    # unicode
    with pytest.raises(RuntimeError):
        dson.loads(u'such "a" is ' * 100000 + u' 1 ' + u' wow ' * 100000)
    with pytest.raises(RuntimeError):
        dson.loads(u'such "a" is ' * 100000 + u' so 1 many' + u' wow ' * 100000)
    with pytest.raises(RuntimeError):
        dson.loads(u'so ' * 100000 + u' 1 ' + u' many ' * 100000)
Пример #22
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
Пример #23
0
def test_invalid_escape():
    s = 'so "abc\\y" many'
    with pytest.raises(ValueError) as e: dson.loads(s)
    assert 'escape' in str(e)
Пример #24
0
def test_allow_inf(val):
    out = dson.dumps([val])
    assert dson.loads(out) == [val]
    pytest.raises(ValueError, dson.dumps, [val], allow_nan=False)
Пример #25
0
def test_decimal():
    rval = dson.loads('1.1', parse_float=decimal.Decimal)
    assert isinstance(rval, decimal.Decimal)
    assert rval == decimal.Decimal('1.1')
Пример #26
0
def test_float():
    rval = dson.loads('1', parse_int=float)
    assert isinstance(rval, float)
    assert rval == 1.0
Пример #27
0
def test_decoder_optimizations():
    # Several optimizations were made that skip over calls to
    # the whitespace regex, so this test is designed to try and
    # exercise the uncommon cases. The array cases are already covered.
    rval = dson.loads('such   "key"    is    "value"    .  "k"is"v"    wow')
    assert rval == {"key":"value",  "k":"v"}
Пример #28
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
Пример #29
0
def test_allow_inf(val):
    out = dson.dumps([val])
    assert dson.loads(out) == [val]
    pytest.raises(ValueError, dson.dumps, [val], allow_nan=False)