def test_parse_success(self): """The STEM parser generates a list of clients for JSON passing schema.""" data = """ { "total": 1, "clients": [ { "link": "/api/v1/clients/guid1", "id": "12345", "name": "Dave's Oyster Barn" }, { "link": "/api/v1/clients/guid2", "id": "abcde", "name": "Orlando's House of Chicken" } ], "page": "1" }""" clients = parsers.parse_clients(data) self.assertEqual(len(clients), 2) self.assertEqual(clients[0].id, "12345") self.assertEqual(clients[0].link, "/api/v1/clients/guid1") self.assertEqual(clients[0].name, "Dave's Oyster Barn")
def test_parse_fail_schema3(self): data = """ { "total": 1, "clients": [ { "link": "/api/v1/clients/guid1", "id": "12345" } ], "page": "1" }""" # Missing required name field. with self.assertRaises(SchemaValidationFailure): parsers.parse_clients(data)
def test_parse_fail_schema2(self): data = """ { "total": 1, "clients": [ { "link": "/api/v1/clients/guid1", "id": "12345", "name": "Dave's Oyster Barn" } ] }""" # Missing required page field. with self.assertRaises(SchemaValidationFailure): parsers.parse_clients(data)
def test_parse_fail_schema1(self): data = """ { "total": 1, "clients": [ { "link": "/api/v1/clients/guid1", "id": "12345", "name": "Dave's Oyster Barn", "foo": "Schema violation." } ], "page": "1" }""" with self.assertRaises(SchemaValidationFailure): parsers.parse_clients(data)
def test_parse_empty(self): data = """ { "total": 1, "clients": [], "page": "1" }""" clients = parsers.parse_clients(data) self.assertEqual(len(clients), 0)
def test_parse_fail_json(self): """The STEM parser raises an exception when the response is not JSON.""" data = """{abc123:""" with self.assertRaises(JsonParseFailure): parsers.parse_clients(data)