def assertJSONNotEqual(self, raw, expected_data, msg=None): """Asserts that the JSON fragments raw and expected_data are not equal. Usual JSON non-significant whitespace rules apply as the heavyweight is delegated to the json library. """ try: data = ju.loads(raw) except ValueError: self.fail("First argument is not valid JSON: %r" % raw) if isinstance(expected_data, (str, unicode)): try: expected_data = ju.loads(expected_data) except ValueError: self.fail("Second argument is not valid JSON: %r" % expected_data) self.assertNotEqual(data, expected_data, msg=msg)
def assertAPIResponse(self, resp, content=None, metadata=None, errors=None): """Assert that the API response data has `content`, `metadata`, and `errors` that match their expected values.""" def convert_strings_to_unicode(value): if isinstance(value, basestring): return unicode(value) elif isinstance(value, collections.Mapping): return dict(map(convert_strings_to_unicode, value.iteritems())) elif isinstance(value, collections.Iterable): return type(value)(map(convert_strings_to_unicode, value)) else: return value try: data = ju.loads(resp.data.decode('utf-8')) except ValueError: self.fail("Response data is not valid JSON: {!r}".format(resp.data)) if content is not None: unicode_content = convert_strings_to_unicode(content) self.assertIn(api.ResponseConfig.CONTENT_LOCATION, data) self.assertEqual(data[api.ResponseConfig.CONTENT_LOCATION], unicode_content) if metadata is not None: self.assertIn(api.ResponseConfig.METADATA_LOCATION, data) self.assertEqual(data[api.ResponseConfig.METADATA_LOCATION], metadata) if errors is not None: self.assertIn(api.ResponseConfig.ERRORS_LOCATION, data) self.assertEqual(data[api.ResponseConfig.ERRORS_LOCATION], errors)
def __new__(cls, value): try: obj = ju.loads(value) except (JSONDecodeError, TypeError): raise ValueError('Invalid JSON string') return obj
def extract_response_metadata(resp): """Extract the "metadata" data from the response.""" return ju.loads(resp.data)[api.ResponseConfig.METADATA_LOCATION]
def extract_response_errors(resp): """Extract the "errors" data from the response.""" return ju.loads(resp.data)[api.ResponseConfig.ERRORS_LOCATION]
def extract_response_content(resp): """Extract the "content" data from the response.""" return ju.loads(resp.data)[api.ResponseConfig.CONTENT_LOCATION]