def test_endpoint_sort_default_strategy(self):
        """Parser should sort duplicate endpoint paths."""
        source_cells = [
            {
                "source":
                '\n```\n{"swagger":"2.0","paths":{"":{"post":{"operationId":"postRoot","parameters":[{"name":"foo"}]}},"/hello":{"post":{"operationId":"postHello","parameters":[{"name":"foo"}]},"get":{"operationId":"getHello","parameters":[{"name":"foo"}]}},"/hello/world":{"put":{"operationId":"putWorld"}}}}\n```\n'
            },
            {
                "source": '# operationId:putWorld'
            },
            {
                "source": '# operationId:getHello'
            },
            {
                "source": '# operationId:postHello'
            },
            {
                "source": '# operationId:postRoot'
            },
        ]
        parser = SwaggerCellParser(comment_prefix='#',
                                   notebook_cells=source_cells)
        endpoints = parser.endpoints(cell['source'] for cell in source_cells)

        expected_values = ['/hello/world', '/hello/:foo', '/:foo']
        try:
            for index in range(0, len(expected_values)):
                endpoint, _ = endpoints[index]
                self.assertEqual(expected_values[index], endpoint,
                                 'Endpoint was not found in expected order')
        except IndexError:
            self.fail(endpoints)
 def test_endpoint_concatenation(self):
     """Parser should concatenate multiple cells with the same verb+path."""
     cells = [{
         "source":
         '```\n{"swagger":"2.0", "paths": {"/foo": {"put": {"operationId":"putFoo","parameters": [{"name": "bar"}]},"post":{"operationId":"postFooBody"},"get": {"operationId":"getFoo","parameters": [{"name": "bar"}]}}}}\n```\n'
     }, {
         "source": '# operationId: postFooBody '
     }, {
         "source": '# unrelated comment '
     }, {
         "source": '# operationId: putFoo'
     }, {
         "source": '# operationId: puttFoo'
     }, {
         "source": '# operationId: getFoo'
     }, {
         "source": '# operationId: putFoo'
     }]
     parser = SwaggerCellParser(comment_prefix='#', notebook_cells=cells)
     endpoints = parser.endpoints(cell['source'] for cell in cells)
     self.assertEqual(len(endpoints), 2, endpoints)
     # for ease of testing
     endpoints = dict(endpoints)
     self.assertEqual(len(endpoints['/foo']), 1)
     self.assertEqual(len(endpoints['/foo/:bar']), 2)
     self.assertEqual(endpoints['/foo']['post'],
                      '# operationId: postFooBody \n')
     self.assertEqual(endpoints['/foo/:bar']['get'],
                      '# operationId: getFoo\n')
     self.assertEqual(endpoints['/foo/:bar']['put'],
                      '# operationId: putFoo\n# operationId: putFoo\n')
Example #3
0
    def test_endpoint_sort_custom_strategy(self):
        """Parser should sort duplicate endpoint paths using a custom sort
        strategy.
        """
        source_cells = [
            {"source":'```\n{"swagger": "2.0", "paths": {"/1": {"post": {"operationId": "post1"}},"/+": {"post": {"operationId": "postPlus"}},"/a": {"get": {"operationId": "getA"}}}}\n```\n'},
            {"source":'# operationId: post1'},
            {"source":'# operationId: postPlus'},
            {"source":'# operationId: getA'},
        ]

        def custom_sort_fun(endpoint):
            index = sys.maxsize
            if endpoint.find('1') >= 0:
                return 0
            elif endpoint.find('a') >= 0:
                return 1
            else:
                return 2

        parser = SwaggerCellParser(kernelspec='some_unknown_kernel', notebook_cells=source_cells)
        endpoints = parser.endpoints((cell['source'] for cell in source_cells), custom_sort_fun)
        print(str(endpoints))

        expected_values = ['/+', '/a', '/1']
        for index in range(0, len(expected_values)):
            endpoint, _ = endpoints[index]
            self.assertEqual(expected_values[index], endpoint, 'Endpoint was not found in expected order')
Example #4
0
    def test_endpoint_sort_default_strategy(self):
        """Parser should sort duplicate endpoint paths."""
        source_cells = [
            {"source":'\n```\n{"swagger":"2.0","paths":{"":{"post":{"operationId":"postRoot","parameters":[{"name":"foo"}]}},"/hello":{"post":{"operationId":"postHello","parameters":[{"name":"foo"}]},"get":{"operationId":"getHello","parameters":[{"name":"foo"}]}},"/hello/world":{"put":{"operationId":"putWorld"}}}}\n```\n'},
            {"source":'# operationId:putWorld'},
            {"source":'# operationId:getHello'},
            {"source":'# operationId:postHello'},
            {"source":'# operationId:postRoot'},
        ]
        parser = SwaggerCellParser(kernelspec='some_unknown_kernel', notebook_cells = source_cells)
        endpoints = parser.endpoints(cell['source'] for cell in source_cells)

        expected_values = ['/hello/world', '/hello/:foo', '/:foo']
        try:
            for index in range(0, len(expected_values)):
                endpoint, _ = endpoints[index]
                self.assertEqual(expected_values[index], endpoint, 'Endpoint was not found in expected order')
        except IndexError:
            self.fail(endpoints)
Example #5
0
 def test_endpoint_concatenation(self):
     """Parser should concatenate multiple cells with the same verb+path."""
     cells = [
         {"source":'```\n{"swagger":"2.0", "paths": {"/foo": {"put": {"operationId":"putFoo","parameters": [{"name": "bar"}]},"post":{"operationId":"postFooBody"},"get": {"operationId":"getFoo","parameters": [{"name": "bar"}]}}}}\n```\n'},
         {"source":'# operationId: postFooBody '},
         {"source":'# unrelated comment '},
         {"source":'# operationId: putFoo'},
         {"source":'# operationId: puttFoo'},
         {"source":'# operationId: getFoo'},
         {"source":'# operationId: putFoo'}
     ]
     parser = SwaggerCellParser(kernelspec='some_unknown_kernel', notebook_cells=cells)
     endpoints = parser.endpoints(cell['source'] for cell in cells)
     self.assertEqual(len(endpoints), 2, endpoints)
     # for ease of testing
     endpoints = dict(endpoints)
     self.assertEqual(len(endpoints['/foo']), 1)
     self.assertEqual(len(endpoints['/foo/:bar']), 2)
     self.assertEqual(endpoints['/foo']['post'], '# operationId: postFooBody \n')
     self.assertEqual(endpoints['/foo/:bar']['get'], '# operationId: getFoo\n')
     self.assertEqual(endpoints['/foo/:bar']['put'], '# operationId: putFoo\n# operationId: putFoo\n')