def test_parse_include_metadata(self): parser = JSONParser() stream = BytesIO(self.string.encode('utf-8')) data = parser.parse(stream, None, self.parser_context) self.assertEqual(data['_meta'], {'random_key': 'random_value'})
def test_parse_invalid_data(self): parser = JSONParser() string = json.dumps([]) stream = BytesIO(string.encode('utf-8')) with self.assertRaises(ParseError): parser.parse(stream, None, self.parser_context)
def test_parse_include_metadata_format_field_names(self): parser = JSONParser() stream = BytesIO(self.string.encode('utf-8')) data = parser.parse(stream, None, self.parser_context) self.assertEqual(data['_meta'], {'random_key': 'random_value'}) self.assertEqual(data['json_value'], {'JsonKey': 'JsonValue'})
def test_parse_include_metadata(self): parser = JSONParser() string = json.dumps([]) stream = BytesIO(string.encode('utf-8')) with self.assertRaises(ParseError): parser.parse(stream, None, self.parser_context)
def test_parse_include_metadata_format_field_names(self): parser = JSONParser() stream = BytesIO(self.string.encode("utf-8")) data = parser.parse(stream, None, self.parser_context) self.assertEqual(data["_meta"], {"random_key": "random_value"}) self.assertEqual(data["json_value"], {"JsonKey": "JsonValue"})
def test_patch_doesnt_raise_attribute_error(self): parser = JSONParser() stream = BytesIO(self.string.encode('utf-8')) data = parser.parse(stream, None, self.parser_context) assert data['id'] == 123 assert data['body'] == 'hello'
def test_patch_doesnt_raise_attribute_error(self): parser = JSONParser() stream = BytesIO(self.string.encode("utf-8")) data = parser.parse(stream, None, self.parser_context) assert data["id"] == 123 assert data["body"] == "hello"
def test_parse_invalid_data_key(self): parser = JSONParser() string = json.dumps({ 'data': [{ 'id': 123, 'type': 'Blog', 'attributes': { 'json-value': {'JsonKey': 'JsonValue'} }, }] }) stream = BytesIO(string.encode('utf-8')) with self.assertRaises(ParseError): parser.parse(stream, None, self.parser_context)
def test_parse_invalid_data_key(self): parser = JSONParser() string = json.dumps( { "data": [ { "id": 123, "type": "Blog", "attributes": {"json-value": {"JsonKey": "JsonValue"}}, } ] } ) stream = BytesIO(string.encode("utf-8")) with self.assertRaises(ParseError): parser.parse(stream, None, self.parser_context)
def update_person_from_source(person): User = get_user_model() response = get_membercenter_response(person) human = response.json() parsed = JSONParser().parse_attributes(human['data']) serialized = PersonSerializer(person, data=parsed) if serialized.is_valid(): usernames = parsed['usernames'] owners = User.objects.filter(username__in=usernames).values_list( 'id', flat=True) return serialized.save(owners=owners) return serialized.errors
def parser(self): return JSONParser()