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)
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))
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)
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))
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))
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')
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}))
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}))
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])
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')
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)
def ud(N): return jsonutil.loads(s)
def ud(N): return jsonutil.loads(s)
def test_float(self): rval = json.loads('1', parse_int=float) self.assert_(isinstance(rval, float)) self.assertEqual(rval, 1.0)
def test_decode(self): self.failUnlessEqual(jsonutil.loads("0.1"), zero_point_one)
def test_no_exception_on_convergent_parse_float(self): self.assertEqual(jsonutil.loads("0.1", parse_float=Decimal), zero_point_one)
def test_big_unicode_decode(self): u = u'z\U0001d120x' self.assertEquals(json.loads('"' + u + '"'), u) self.assertEquals(json.loads('"z\\ud834\\udd20x"'), u)
def test_decode(self): self.assertEqual(jsonutil.loads("0.1"), zero_point_one)
def test_repr(self): body = 'this is not json' try: json.loads('this is not json') except ValueError, le: e = DecodeError(body, le)
def test_unicode_decode(self): for i in range(0, 0xd7ff): u = unichr(i) js = '"\\u%04x"' % (i,) self.assertEquals(json.loads(js), u)
def test_no_exception_on_convergent_parse_float(self): self.failUnlessEqual(jsonutil.loads("0.1", parse_float=Decimal), zero_point_one)
def loads(*args, **kwargs): dct = json.loads(*args, **kwargs) return object_from_dict(dct)
def test_big_unicode_decode(self): u = u'z\U0001d120x' self.assertEqual(json.loads('"' + u + '"'), u) self.assertEqual(json.loads('"z\\ud834\\udd20x"'), u)
def test_decode(self): self.failUnlessEqual(jsonutil.loads("0.1"), zero_point_one)
def test_float(self): rval = json.loads('1', parse_int=float) self.assert_(isinstance(rval, float)) self.assertEquals(rval, 1.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))
def loads(*args, **kwargs): dct = json.loads(*args, **kwargs) return object_from_dict(dct)
def test_unicode_decode(self): for i in range(0, 0xd7ff): u = unichr(i) js = '"\\u%04x"' % (i, ) self.assertEqual(json.loads(js), u)
def json_decode(jsonstr): try: return json.loads(jsonstr) except (ValueError, TypeError), le: raise DecodeError(jsonstr, le)
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'))
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])
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'))
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))