def testGetNToMRelationship(self): result = self.app.get("/photos/1/tags") JsonAPIValidator.validate(result.json) self.assertIs(len(result.json["data"]), 2) for tag in result.json["data"]: self.assertIn(tag["attributes"]["name"], TAG_NAMES)
def testPatchingRelatedOneToMResource(self): result = self.app.get("/articles/1/relationships/comments") self.assertIsInstance(result.json["data"], list) del result.json["data"][0] request = {u"data": result.json["data"]} JsonAPIValidator.validate(request) result = self.app.patch_json("/articles/1/relationships/comments", params=request)
def testPatchingRelatedOneToMResource(self): result = self.app.get("/articles/1/relationships/comments") self.assertIsInstance(result.json["data"], list) del result.json["data"][0] request = { u"data": result.json["data"] } JsonAPIValidator.validate(request) result = self.app.patch_json( "/articles/1/relationships/comments", params=request )
def testIncludeParameterForwardRelationship(self): result = self.app.get("/articles/2?include=cover") JsonAPIValidator.validate(result.json) self.assertIn("included", result.json) # the server must not return any other fields than requested self.assertIs(len(result.json["included"]), 1) ref = result.json["data"]["relationships"]["cover"]["data"] inc = result.json["included"][0] self.assertEqual(inc["type"], ref["type"]) self.assertEqual(inc["id"], ref["id"]) # the self link must be valid and refer to the same object subresult = self.app.get(inc["links"]["self"]) self.assertEqual(subresult.json["data"], inc)
def testIncludeParameterReverseRelationship(self): result = self.app.get("/articles/1?include=comments") JsonAPIValidator.validate(result.json) self.assertIn("included", result.json) # the server must not return any other fields than requested self.assertIs(len(result.json["included"]), len(COMMENT_BODIES)) refs = result.json["data"]["relationships"]["comments"]["data"] for inc in result.json["included"]: self.assertIn({"id": inc["id"], "type": inc["type"]}, refs) # the self link must be valid and refer to the same object subresult = self.app.get(inc["links"]["self"]) self.assertEqual(subresult.json["data"], inc) self.assertEqual(subresult.json["links"]["self"], inc["links"]["self"])
def testSparseFieldsetsWithIncludedObjects(self): result = self.app.get("/articles/1?include=comments&fields[comment]=") JsonAPIValidator.validate(result.json) for inc in result.json["included"]: self.assertNotIn("attributes", inc) result = self.app.get( "/comments/1?include=article.author&fields[person]=age") JsonAPIValidator.validate(result.json) is_included = False for inc in result.json["included"]: if inc["type"] == "person": is_included = True self.assertIn("age", inc["attributes"]) self.assertNotIn("name", inc["attributes"]) self.assertIsNotNone(is_included)
def testSparseFieldsetsWithIncludedObjects(self): result = self.app.get("/articles/1?include=comments&fields[comment]=") JsonAPIValidator.validate(result.json) for inc in result.json["included"]: self.assertNotIn("attributes", inc) result = self.app.get( "/comments/1?include=article.author&fields[person]=age" ) JsonAPIValidator.validate(result.json) is_included = False for inc in result.json["included"]: if inc["type"] == "person": is_included = True self.assertIn("age", inc["attributes"]) self.assertNotIn("name", inc["attributes"]) self.assertIsNotNone(is_included)
def testIncludeParameterReverseRelationship(self): result = self.app.get("/articles/1?include=comments") JsonAPIValidator.validate(result.json) self.assertIn("included", result.json) # the server must not return any other fields than requested self.assertIs(len(result.json["included"]), len(COMMENT_BODIES)) refs = result.json["data"]["relationships"]["comments"]["data"] for inc in result.json["included"]: self.assertIn({"id": inc["id"], "type": inc["type"]}, refs) # the self link must be valid and refer to the same object subresult = self.app.get(inc["links"]["self"]) self.assertEqual(subresult.json["data"], inc) self.assertEqual( subresult.json["links"]["self"], inc["links"]["self"] )
def testLinkWithSpecifiedField(self): result = self.app.get("/articles/1/relationships/revisions") JsonAPIValidator.validate(result.json)
def testSparseFieldsets(self): result = self.app.get("/people/1?fields[person]=age") JsonAPIValidator.validate(result.json) self.assertNotIn("name", result.json["data"]["attributes"]) self.assertIn("age", result.json["data"]["attributes"])