From f0e2d44c5d63207bc31a23ca5f895e6256091e7f Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Mon, 4 Nov 2019 09:41:47 +0000 Subject: [PATCH] Make API Gateway tests Python2/3 compatible --- moto/apigateway/models.py | 14 ++++++++------ tests/test_apigateway/test_apigateway.py | 8 ++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/moto/apigateway/models.py b/moto/apigateway/models.py index 022601b93..dca80a234 100644 --- a/moto/apigateway/models.py +++ b/moto/apigateway/models.py @@ -8,7 +8,10 @@ import requests import time from boto3.session import Session -from urlparse import urlparse +try: + from urlparse import urlparse +except ImportError: + from urllib.parse import urlparse import responses from moto.core import BaseBackend, BaseModel from .utils import create_id @@ -728,16 +731,15 @@ class APIGatewayBackend(BaseBackend): stage_variables = {} api = self.get_rest_api(function_id) methods = [ - res.resource_methods.values()[0] for res in self.list_resources(function_id) - ] + list(res.resource_methods.values()) for res in self.list_resources(function_id) + ][0] if not any(methods): raise NoMethodDefined() method_integrations = [ - method["methodIntegration"] + method["methodIntegration"] if "methodIntegration" in method else None for method in methods - if "methodIntegration" in method ] - if not all(method_integrations): + if not any(method_integrations): raise NoIntegrationDefined() deployment = api.create_deployment(name, description, stage_variables) return deployment diff --git a/tests/test_apigateway/test_apigateway.py b/tests/test_apigateway/test_apigateway.py index acc26bf6f..2d72c3efe 100644 --- a/tests/test_apigateway/test_apigateway.py +++ b/tests/test_apigateway/test_apigateway.py @@ -814,7 +814,7 @@ def test_put_integration_response_requires_responseTemplate(): # Works fine if responseTemplate is defined client.put_integration_response( restApiId=api_id, - resourceId=resource["id"], + resourceId=root_id, httpMethod="GET", statusCode="200", responseTemplates={}, @@ -1018,15 +1018,15 @@ def test_delete_stage(): variables={"env": "dev"}, ) stages = client.get_stages(restApiId=api_id)["item"] - [stage["stageName"] for stage in stages].should.equal( - [new_stage_name, new_stage_name_with_vars, stage_name] + sorted([stage["stageName"] for stage in stages]).should.equal( + sorted([new_stage_name, new_stage_name_with_vars, stage_name]) ) # delete stage response = client.delete_stage(restApiId=api_id, stageName=new_stage_name_with_vars) response["ResponseMetadata"]["HTTPStatusCode"].should.equal(202) # verify other stage still exists stages = client.get_stages(restApiId=api_id)["item"] - [stage["stageName"] for stage in stages].should.equal([new_stage_name, stage_name]) + sorted([stage["stageName"] for stage in stages]).should.equal(sorted([new_stage_name, stage_name])) @mock_apigateway