def test_unsupported_method(): event = { "requestContext": { "http": { "method": "AA" } }, "queryStringParameters": { "mal_id": "123" } } with pytest.raises(UnsupportedMethod): handle(event, None)
def test_invalid_body(self, mocked_shows_db, mocked_episodes_db): mocked_episodes_db.table.query.return_value = { "Items": [ { "mal_id": 123 } ] } mocked_shows_db.table.get_item.return_value = { "Item": { "id": TEST_SHOW_UUID } } event = copy.deepcopy(self.event) event["body"] = '{"aa": "bb"}' res = handle(event, None) exp = { 'body': '{"message": "Invalid post schema", ' '"error": "Additional properties are not allowed (\'aa\' was unexpected)"}', 'statusCode': 400 } assert res == exp
def test_not_found(self, mocked_shows_db, mocked_episodes_db): mocked_episodes_db.table.query.side_effect = mocked_episodes_db.NotFoundError res = handle(self.event, None) exp = { "statusCode": 404, } assert res == exp
def test_invalid_api_name(self): event = copy.deepcopy(self.event) event["queryStringParameters"]["api_name"] = "INVALID" res = handle(event, None) exp = { "statusCode": 400, "body": json.dumps({"error": "Unsupported api_name"}) } assert res == exp
def test_missing_api_name(self): event = copy.deepcopy(self.event) del event["queryStringParameters"]["api_name"] res = handle(event, None) exp = { "statusCode": 400, "body": json.dumps({"error": "Missing api_name query parameter"}) } assert res == exp
def test_empty_query_params(self): event = copy.deepcopy(self.event) event["queryStringParameters"] = {} res = handle(event, None) exp = { "statusCode": 400, "body": json.dumps({"error": "Please specify query parameters"}) } assert res == exp
def test_invalid_query_params(self): event = copy.deepcopy(self.event) event["queryStringParameters"] = { "abc": "123" } res = handle(event, None) exp = { "statusCode": 400, "body": json.dumps({"error": "Missing api_id query parameter"}) } assert res == exp
def test_success(self, mocked_shows_db, mocked_episodes_db): mocked_episodes_db.table.query.return_value = {"Items": []} mocked_shows_db.table.get_item.return_value = { "Item": { "id": TEST_SHOW_UUID } } res = handle(self.event, None) res_body = json.loads(res["body"]) assert res["statusCode"] == 200 assert res_body["id"] == "20e10800-b2e2-5079-90b5-243647854ef2" assert res_body["tvmaze_data"]["id"] == 456 assert res_body["tvmaze_data"]["name"] == "The Waking Dead"
def test_not_found(self, mocked_shows_db, mocked_episodes_db): mocked_episodes_db.table.query.return_value = { "Items": [ { "mal_id": 123 } ] } mocked_shows_db.table.get_item.side_effect = mocked_shows_db.NotFoundError res = handle(self.event, None) exp = { 'body': '{"message": "Show not found"}', 'statusCode': 404 } assert res == exp
def test_success(self, mocked_shows_db, mocked_episodes_db): exp_res = { "id": "123", "tvmaze_id": 456, } mocked_episodes_db.table.query.return_value = { "Items": [ exp_res ], "Count": 1 } res = handle(self.event, None) res_body = json.loads(res["body"]) assert res["statusCode"] == 200 assert res_body["id"] == exp_res["id"] assert res_body["tvmaze_data"]["id"] == exp_res["tvmaze_id"] assert res_body["tvmaze_data"]["name"] == "The Waking Dead" # From real tvmaze api
def test_missing_path_id(self, mocked_shows_db, mocked_episodes_db): mocked_episodes_db.table.query.return_value = { "Items": [ { "mal_id": 123 } ] } event = copy.deepcopy(self.event) del event["pathParameters"] res = handle(event, None) exp = { 'body': 'Missing id query param', 'statusCode': 400 } assert res == exp
def test_no_body(self, mocked_shows_db, mocked_episodes_db): mocked_episodes_db.table.query.return_value = { "Items": [ { "tvmaze_id": "123" }, ] } mocked_shows_db.table.get_item.return_value = { "Item": { "id": TEST_SHOW_UUID } } event = copy.deepcopy(self.event) del event["body"] res = handle(event, None) exp = { 'body': '{"message": "Invalid post body"}', 'statusCode': 400 } assert res == exp