import logging
import unittest

import responses

import swaggertester

TEST_SCHEMA_PATH = 'test_schema.json'
SCHEMA_URL_BASE = 'http://127.0.0.1:5000/api'
CONTENT_TYPE_JSON = 'application/json'


class EndpointCollectionTestCase(unittest.TestCase):

    def setUp(self):
        self.client = swaggertester.SwaggerClient(TEST_SCHEMA_PATH)

    def tearDown(self):
        # No teardown of test fixtures required.
        pass

    def test_schema_parse(self):
        endpoints_clctn = swaggertester.APITemplate(self.client)
        expected_endpoints = {'/schema', '/apps', '/apps/{appid}'}
        self.assertSetEqual(set(endpoints_clctn.endpoints.keys()),
                            expected_endpoints)

    @responses.activate
    def test_endpoint(self):
        endpoints_clctn = swaggertester.APITemplate(self.client)
        endpoint = endpoints_clctn.endpoints['/apps/{appid}']
        self.assertIn('get', endpoint)
        endpoint_op = endpoint['get']
        self.assertEqual(len(endpoint_op.parameters), 1)
        self.assertEqual(endpoint_op.parameters['appid'].type, 'string')
        params = {'appid': 'test_string'}

        responses.add(responses.GET, SCHEMA_URL_BASE + '/apps/test_string',
                      json={}, status=404,
                      content_type=CONTENT_TYPE_JSON)
        result = self.client.request(endpoint_op, params)
        self.assertEqual(result.status, 404)


if __name__ == '__main__':
    log_format = '%(asctime)s:%(levelname)-7s:%(funcName)s:%(message)s'
    logging.basicConfig(format=log_format, level=logging.INFO)
    unittest.main()

@@ -200,26 +200,19 @@
 ass 
-EndpointCollection
+APITemplate
 Test
@@ -740,16 +740,25 @@
 endpoint
+_manually
 (self):%0A
@@ -761,39 +761,36 @@
 f):%0A        
-endpoints_clctn
+api_template
  = swaggerte
@@ -815,32 +815,33 @@
 elf.client)%0A
+%0A
         
 endpoint = e
@@ -832,45 +832,299 @@
     
-endpoint = endpoints_clctn.endpoints%5B
+# Find the template GET operation on the /apps/%7Bappid%7D endpoint.%0A        app_id_get_op = None%0A        for operation_template in api_template.iter_template_operations():%0A            if (operation_template.operation.method == 'get' and%0A                    operation_template.operation.path == 
 '/ap
@@ -1134,18 +1134,27 @@
 %7Bappid%7D'
-%5D%0A
+):%0A        
         
@@ -1169,64 +1169,197 @@
 ertI
-n('get', endpoint)%0A        endpoint_op = endpoint%5B'get'%5D
+sNone(app_id_get_op)%0A                app_id_get_op = operation_template%0A        self.assertIsNotNone(app_id_get_op)%0A%0A        # The operation takes one parameter, 'appid', which is a string.
 %0A   
@@ -1385,18 +1385,21 @@
 al(l
-en(endpoin
+ist(app_id_ge
 t_op
@@ -1413,12 +1413,27 @@
 ters
-), 1
+.keys()), %5B'appid'%5D
 )%0A  
@@ -1455,23 +1455,25 @@
 rtEqual(
-endpoin
+app_id_ge
 t_op.par
@@ -1504,16 +1504,124 @@
 string')
+%0A%0A        # Send an example parameter in to the endpoint manually, catch the%0A        # request, and respond.
 %0A       
@@ -1655,17 +1655,16 @@
 tring'%7D%0A
-%0A
         
@@ -1865,23 +1865,25 @@
 request(
-endpoin
+app_id_ge
 t_op, pa

Update test