Exemplo n.º 1
0
    def test_separators(self):
        h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
             {'nifty': 87}, {'field': 'yes', 'morefield': False} ]

        expect = textwrap.dedent("""\
        [
          [
            "blorpie"
          ] ,
          [
            "whoops"
          ] ,
          [] ,
          "d-shtaeou" ,
          "d-nthiouh" ,
          "i-vhbjkhnth" ,
          {
            "nifty" : 87
          } ,
          {
            "field" : "yes" ,
            "morefield" : false
          }
        ]""")


        d1 = json.dumps(h)
        d2 = json.dumps(h, indent=2, sort_keys=True, separators=(' ,', ' : '))

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

        self.assertEquals(h1, h)
        self.assertEquals(h2, h)
        self.assertEquals(d2, expect)
Exemplo n.º 2
0
 def test_parse(self):
     # test in/out equivalence and parsing
     res = json.loads(JSON)
     out = json.dumps(res)
     self.assertEqual(res, json.loads(out))
     self.assertTrue(
         "2.3456789012E+676" in json.dumps(res, allow_nan=False))
Exemplo n.º 3
0
    def test_separators(self):
        h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth',
             {'nifty': 87}, {'field': 'yes', 'morefield': False} ]

        expect = textwrap.dedent("""\
        [
          [
            "blorpie"
          ] ,
          [
            "whoops"
          ] ,
          [] ,
          "d-shtaeou" ,
          "d-nthiouh" ,
          "i-vhbjkhnth" ,
          {
            "nifty" : 87
          } ,
          {
            "field" : "yes" ,
            "morefield" : false
          }
        ]""")


        d1 = json.dumps(h)
        d2 = json.dumps(h, indent=2, sort_keys=True, separators=(' ,', ' : '))

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

        self.assertEquals(h1, h)
        self.assertEquals(h2, h)
        self.assertEquals(d2, expect)
Exemplo n.º 4
0
 def test_failures(self):
     for idx, doc in enumerate(JSONDOCS):
         idx = idx + 1
         if idx in SKIPS:
             json.loads(doc)
             continue
         try:
             json.loads(doc)
         except ValueError:
             pass
         else:
             self.fail("Expected failure for fail%d.json: %r" % (idx, doc))
Exemplo n.º 5
0
 def test_failures(self):
     for idx, doc in enumerate(JSONDOCS):
         idx = idx + 1
         if idx in SKIPS:
             json.loads(doc)
             continue
         try:
             json.loads(doc)
         except ValueError:
             pass
         else:
             self.fail("Expected failure for fail%d.json: %r" % (idx, doc))
Exemplo n.º 6
0
    def test_update_feature(self):
        handle = 'SG_abcdefghijklmnopqrstuv'
        rec = Feature((D('11.03'), D('10.04')), simplegeohandle=handle)

        mockhttp = mock.Mock()
        mockhttp.request.return_value = ({
            'status': '200',
            'content-type': 'application/json',
        }, {
            'token': "this is your polling token"
        })
        self.client.places.http = mockhttp

        res = self.client.places.update_feature(rec)
        self.failUnless(isinstance(res, dict), res)
        self.failUnless(res.has_key('token'), res)

        self.assertEqual(mockhttp.method_calls[0][0], 'request')
        self.assertEqual(
            mockhttp.method_calls[0][1][0],
            'http://api.simplegeo.com:80/%s/features/%s.json' %
            (API_VERSION, handle))
        self.assertEqual(mockhttp.method_calls[0][1][1], 'POST')
        bodyjson = mockhttp.method_calls[0][2]['body']
        self.failUnless(isinstance(bodyjson, basestring),
                        (repr(bodyjson), type(bodyjson)))
        # If it decoded as valid json then check for some expected fields
        bodyobj = json.loads(bodyjson)
        self.failUnless(
            bodyobj.get('geometry').has_key('coordinates'), bodyobj)
        self.failUnless(bodyobj.get('geometry').has_key('type'), bodyobj)
        self.failUnlessEqual(bodyobj.get('geometry')['type'], 'Point')
Exemplo n.º 7
0
 def mockrequest(*args, **kwargs):
     self.failUnlessEqual(args[0], 'http://api.simplegeo.com:80/%s/places' % (API_VERSION,))
     self.failUnlessEqual(args[1], 'POST')
     bodyobj = json.loads(kwargs['body'])
     self.failUnlessEqual(bodyobj['properties'].get('record_id'), record_id)
     methods_called.append(('request', args, kwargs))
     mockhttp.request = mockrequest2
     return ({'status': '202', 'content-type': 'application/json', 'location': newloc}, json.dumps({'id': handle}))
Exemplo n.º 8
0
        def mockrequest(*args, **kwargs):
            self.assertEqual(
                args[0],
                'http://api.simplegeo.com:80/%s/places' % (API_VERSION, ))
            self.assertEqual(args[1], 'POST')

            bodyobj = json.loads(kwargs['body'])
            self.failUnlessEqual(bodyobj['id'], None)
            methods_called.append(('request', args, kwargs))
            mockhttp.request = mockrequest2
            return ({
                'status': '202',
                'content-type': 'application/json',
                'location': newloc
            }, json.dumps({'id': handle}))
Exemplo n.º 9
0
    def annotate(self, simplegeohandle, annotations, private):
        if not isinstance(annotations, dict):
            raise TypeError('annotations must be of type dict')
        if not len(annotations.keys()):
            raise ValueError('annotations dict is empty')
        for annotation_type in annotations.keys():
            if not len(annotations[annotation_type].keys()):
                raise ValueError('annotation type "%s" is empty' % annotation_type)
        if not isinstance(private, bool):
            raise TypeError('private must be of type bool')

        data = {'annotations': annotations,
                'private': private}

        endpoint = self._endpoint('annotations', simplegeohandle=simplegeohandle)
        return json.loads(self._request(endpoint,
                                        'POST',
                                        data=json.dumps(data))[1])
Exemplo n.º 10
0
    def test_update_feature(self):
        handle = 'SG_abcdefghijklmnopqrstuv'
        rec = Feature((D('11.03'), D('10.04')), simplegeohandle=handle)

        mockhttp = mock.Mock()
        mockhttp.request.return_value = ({'status': '200', 'content-type': 'application/json', }, {'token': "this is your polling token"})
        self.client.places.http = mockhttp

        res = self.client.places.update_feature(rec)
        self.failUnless(isinstance(res, dict), res)
        self.failUnless(res.has_key('token'), res)

        self.assertEqual(mockhttp.method_calls[0][0], 'request')
        self.assertEqual(mockhttp.method_calls[0][1][0], 'http://api.simplegeo.com:80/%s/features/%s.json' % (API_VERSION, handle))
        self.assertEqual(mockhttp.method_calls[0][1][1], 'POST')
        bodyjson = mockhttp.method_calls[0][2]['body']
        self.failUnless(isinstance(bodyjson, basestring), (repr(bodyjson), type(bodyjson)))
        # If it decoded as valid json then check for some expected fields
        bodyobj = json.loads(bodyjson)
        self.failUnless(bodyobj.get('geometry').has_key('coordinates'), bodyobj)
        self.failUnless(bodyobj.get('geometry').has_key('type'), bodyobj)
        self.failUnlessEqual(bodyobj.get('geometry')['type'], 'Point')
Exemplo n.º 11
0
def json_decode(jsonstr):
    try:
        return json.loads(jsonstr)
    except (ValueError, TypeError), le:
        raise DecodeError(jsonstr, le)
 def test_repr(self):
     body = 'this is not json'
     try:
         json.loads('this is not json')
     except ValueError, le:
         e = DecodeError(body, le)
Exemplo n.º 13
0
def ud(N):
    return jsonutil.loads(s)
Exemplo n.º 14
0
def ud(N):
    return jsonutil.loads(s)
Exemplo n.º 15
0
 def test_float(self):
     rval = json.loads('1', parse_int=float)
     self.assert_(isinstance(rval, float))
     self.assertEqual(rval, 1.0)
Exemplo n.º 16
0
 def test_decode(self):
     self.failUnlessEqual(jsonutil.loads("0.1"), zero_point_one)
Exemplo n.º 17
0
 def test_no_exception_on_convergent_parse_float(self):
     self.assertEqual(jsonutil.loads("0.1", parse_float=Decimal),
                      zero_point_one)
Exemplo n.º 18
0
 def test_big_unicode_decode(self):
     u = u'z\U0001d120x'
     self.assertEquals(json.loads('"' + u + '"'), u)
     self.assertEquals(json.loads('"z\\ud834\\udd20x"'), u)
Exemplo n.º 19
0
 def test_decode(self):
     self.assertEqual(jsonutil.loads("0.1"), zero_point_one)
Exemplo n.º 20
0
 def test_repr(self):
     body = 'this is not json'
     try:
         json.loads('this is not json')
     except ValueError, le:
         e = DecodeError(body, le)
Exemplo n.º 21
0
 def test_unicode_decode(self):
     for i in range(0, 0xd7ff):
         u = unichr(i)
         js = '"\\u%04x"' % (i,)
         self.assertEquals(json.loads(js), u)
Exemplo n.º 22
0
 def test_no_exception_on_convergent_parse_float(self):
     self.failUnlessEqual(jsonutil.loads("0.1", parse_float=Decimal), zero_point_one)
Exemplo n.º 23
0
def loads(*args, **kwargs):
    dct = json.loads(*args, **kwargs)
    return object_from_dict(dct)
Exemplo n.º 24
0
 def test_big_unicode_decode(self):
     u = u'z\U0001d120x'
     self.assertEqual(json.loads('"' + u + '"'), u)
     self.assertEqual(json.loads('"z\\ud834\\udd20x"'), u)
Exemplo n.º 25
0
 def test_decode(self):
     self.failUnlessEqual(jsonutil.loads("0.1"), zero_point_one)
Exemplo n.º 26
0
 def test_float(self):
     rval = json.loads('1', parse_int=float)
     self.assert_(isinstance(rval, float))
     self.assertEquals(rval, 1.0)
Exemplo n.º 27
0
 def test_parse(self):
     # test in/out equivalence and parsing
     res = json.loads(JSON)
     out = json.dumps(res)
     self.assertEquals(res, json.loads(out))
Exemplo n.º 28
0
def loads(*args, **kwargs):
    dct = json.loads(*args, **kwargs)
    return object_from_dict(dct)
Exemplo n.º 29
0
 def test_unicode_decode(self):
     for i in range(0, 0xd7ff):
         u = unichr(i)
         js = '"\\u%04x"' % (i, )
         self.assertEqual(json.loads(js), u)
Exemplo n.º 30
0
def json_decode(jsonstr):
    try:
        return json.loads(jsonstr)
    except (ValueError, TypeError), le:
        raise DecodeError(jsonstr, le)
Exemplo n.º 31
0
 def test_decimal(self):
     rval = json.loads('1.1', parse_float=decimal.Decimal)
     self.assert_(isinstance(rval, decimal.Decimal))
     self.assertEquals(rval, decimal.Decimal('1.1'))
Exemplo n.º 32
0
 def get_annotations(self, simplegeohandle):
     if not is_simplegeohandle(simplegeohandle):
         raise TypeError("simplegeohandle is required to match the regex %s, but it was %s :: %r" % (SIMPLEGEOHANDLE_RSTR, type(simplegeohandle), simplegeohandle))
     endpoint = self._endpoint('annotations', simplegeohandle=simplegeohandle)
     return json.loads(self._request(endpoint, 'GET')[1])
Exemplo n.º 33
0
 def test_decimal(self):
     rval = json.loads('1.1', parse_float=decimal.Decimal)
     self.assert_(isinstance(rval, decimal.Decimal))
     self.assertEqual(rval, decimal.Decimal('1.1'))
Exemplo n.º 34
0
 def test_parse(self):
     # test in/out equivalence and parsing
     res = json.loads(JSON)
     out = json.dumps(res)
     self.assertEquals(res, json.loads(out))
     self.failUnless("2.3456789012E+676" in json.dumps(res, allow_nan=False))