Пример #1
0
    def test_dict_getter_v2_0(self):
        """ make sure 'DictGetter' works the same as 'LocalGetter'
        for Swagger 2.0
        """

        #
        # loading via DictGetter
        #
        path = get_test_data_folder(
            version='2.0',
            which='wordnik'
        )

        origin_app = App.create(path)

        with open(os.path.join(path, 'swagger.json'), 'r') as f:
            spec = json.loads(f.read())

        getter = DictGetter([path], {
            os.path.join(path, 'swagger.json'): spec
        })
        app = App.load(path, resolver=Resolver(default_getter=getter))
        app.prepare()

        # make sure it produce the same App in default way
        self.assertEqual(sorted(_diff_(app.dump(), origin_app.dump())), [])

        #
        # loading via wrong path, should be ok when all internal $ref are not absoluted
        #
        getter = DictGetter([''], {
            '': spec
        })
        app = App.load('', resolver=Resolver(default_getter=getter))
        app.prepare()

        # make sure it produce the same App in default way
        self.assertEqual(sorted(_diff_(app.dump(), origin_app.dump(), exclude=['$ref'])), [])

        #
        # faking http path
        #
        getter = DictGetter(['https://petstore.com'], {
            'https://petstore.com': spec
        })
        app = App.load('https://petstore.com', resolver=Resolver(default_getter=getter))
        app.prepare()

        # make sure it produce the same App in default way
        self.assertEqual(sorted(_diff_(app.dump(), origin_app.dump(), exclude=['$ref'])), [])
Пример #2
0
    def test_dict_getter_v2_0(self):
        """ make sure 'DictGetter' works the same as 'LocalGetter'
        for Swagger 2.0
        """

        #
        # loading via DictGetter
        #
        path = get_test_data_folder(version='2.0', which='wordnik')

        origin_app = App.create(path)

        with open(os.path.join(path, 'swagger.json'), 'r') as f:
            spec = json.loads(f.read())

        getter = DictGetter([path], {os.path.join(path, 'swagger.json'): spec})
        app = App.load(path, resolver=Resolver(default_getter=getter))
        app.prepare()

        # make sure it produce the same App in default way
        self.assertEqual(sorted(_diff_(app.dump(), origin_app.dump())), [])

        #
        # loading via wrong path, should be ok when all internal $ref are not absoluted
        #
        getter = DictGetter([''], {'': spec})
        app = App.load('', resolver=Resolver(default_getter=getter))
        app.prepare()

        # make sure it produce the same App in default way
        self.assertEqual(
            sorted(_diff_(app.dump(), origin_app.dump(), exclude=['$ref'])),
            [])

        #
        # faking http path
        #
        getter = DictGetter(['https://petstore.com'],
                            {'https://petstore.com': spec})
        app = App.load('https://petstore.com',
                       resolver=Resolver(default_getter=getter))
        app.prepare()

        # make sure it produce the same App in default way
        self.assertEqual(
            sorted(_diff_(app.dump(), origin_app.dump(), exclude=['$ref'])),
            [])
Пример #3
0
    def test_diff(self):
        dict1 = dict(a=1, b=[1, 2, 3])
        dict2 = dict(a=1, b=[1, 3])
        dict3 = dict(
            a=dict(a=1, b=[1, 2, 3], c=4),
            b=dict(a=2, b=[1, 2, 3], c=4),
        )
        dict4 = dict(
            a=dict(a=2, b=[1, 3], c=5),
            b=dict(a=2, b=[1, 2], c=4),
        )

        list1 = [dict1, dict3]
        list2 = [dict2, dict4]

        self.assertEqual(utils._diff_(dict1, dict2), [
            ('b', 3, 2),
        ])

        self.assertEqual(utils._diff_(dict2, dict1), [
            ('b', 2, 3),
        ])

        self.assertEqual(sorted(utils._diff_(dict3, dict4)), sorted([
            ('a/a', 1, 2), ('a/b', 3, 2), ('a/c', 4, 5), ('b/b', 3, 2)
        ]))

        self.assertEqual(sorted(utils._diff_(list1, list2)), sorted([
            ('0/b', 3, 2),
            ('1/a/a', 1, 2),
            ('1/a/b', 3, 2),
            ('1/a/c', 4, 5),
            ('1/b/b', 3, 2)
        ]))

        # test include
        self.assertEqual(sorted(utils._diff_(dict3, dict4, include=['a'])), sorted([
            ('a/a', 1, 2), ('a/b', 3, 2), ('a/c', 4, 5)
        ]))
        # test exclude
        self.assertEqual(sorted(utils._diff_(dict3, dict4, exclude=['a'])), sorted([
            ('b/b', 3, 2)
        ]))
        # test include and exclude
        self.assertEqual(sorted(utils._diff_(dict3, dict4, include=['a', 'b'], exclude=['a'])), sorted([
            ('b/b', 3, 2)
        ]))
Пример #4
0
    def test_dict_getter_v1_2(self):
        """ make sure 'DictGetter' works the same as 'LocalGetter'
        for Swagger 1.2
        """

        #
        # loading via DictGetter
        #
        path = get_test_data_folder(
            version='1.2',
            which='wordnik'
        )

        path_resource_list = os.path.join(path, 'resource_list.json')
        path_pet = os.path.join(path, 'pet.json')
        path_store = os.path.join(path, 'store.json')
        path_user = os.path.join(path, 'user.json')
        with open(path_resource_list, 'r') as f:
            resource_list = json.loads(f.read())
        with open(path_pet, 'r') as f:
            pet = json.loads(f.read())
        with open(path_store, 'r') as f:
            store = json.loads(f.read())
        with open(path_user, 'r') as f:
            user = json.loads(f.read())

        getter = DictGetter([
            path_resource_list,
            path_pet,
            path_user,
            path_store,
        ], {
            path_resource_list: resource_list,
            path_pet: pet,
            path_store: store,
            path_user: user,
        })
        app = App.load(path, resolver=Resolver(default_getter=getter))
        app.prepare()

        # make sure it produce the same App in default way
        self.assertEqual(sorted(_diff_(app.dump(), App.create(path).dump())), [])

        #
        # different path, mocking an url
        #
        getter = DictGetter([
            'http://petstore.com',
            'http://petstore.com/pet.json',
            'http://petstore.com/user.json',
            'http://petstore.com/store.json',
        ], {
            'http://petstore.com': resource_list,
            'http://petstore.com/pet.json': pet,
            'http://petstore.com/store.json': store,
            'http://petstore.com/user.json': user
        })
        app = App.load('http://petstore.com', resolver=Resolver(default_getter=getter))
        app.prepare()

        # make sure it produce the same App in default way
        self.assertEqual(sorted(_diff_(app.dump(), App.create(path).dump(), exclude=['$ref'])), [])

        #
        # provide empty path
        #
        getter = DictGetter([
            '',
            'pet.json',
            'user.json',
            'store.json',
        ], {
            '': resource_list,
            'pet.json': pet,
            'store.json': store,
            'user.json': user
        })
        app = App.load('http://petstore.com', resolver=Resolver(default_getter=getter))
        app.prepare()

        # make sure it produce the same App in default way
        self.assertEqual(sorted(_diff_(app.dump(), App.create(path).dump(), exclude=['$ref'])), [])
Пример #5
0
    def test_dict_getter_v1_2(self):
        """ make sure 'DictGetter' works the same as 'LocalGetter'
        for Swagger 1.2
        """

        #
        # loading via DictGetter
        #
        path = get_test_data_folder(version='1.2', which='wordnik')

        path_resource_list = os.path.join(path, 'resource_list.json')
        path_pet = os.path.join(path, 'pet.json')
        path_store = os.path.join(path, 'store.json')
        path_user = os.path.join(path, 'user.json')
        with open(path_resource_list, 'r') as f:
            resource_list = json.loads(f.read())
        with open(path_pet, 'r') as f:
            pet = json.loads(f.read())
        with open(path_store, 'r') as f:
            store = json.loads(f.read())
        with open(path_user, 'r') as f:
            user = json.loads(f.read())

        getter = DictGetter(
            [
                path_resource_list,
                path_pet,
                path_user,
                path_store,
            ], {
                path_resource_list: resource_list,
                path_pet: pet,
                path_store: store,
                path_user: user,
            })
        app = App.load(path, resolver=Resolver(default_getter=getter))
        app.prepare()

        # make sure it produce the same App in default way
        self.assertEqual(sorted(_diff_(app.dump(),
                                       App.create(path).dump())), [])

        #
        # different path, mocking an url
        #
        getter = DictGetter(
            [
                'http://petstore.com',
                'http://petstore.com/pet.json',
                'http://petstore.com/user.json',
                'http://petstore.com/store.json',
            ], {
                'http://petstore.com': resource_list,
                'http://petstore.com/pet.json': pet,
                'http://petstore.com/store.json': store,
                'http://petstore.com/user.json': user
            })
        app = App.load('http://petstore.com',
                       resolver=Resolver(default_getter=getter))
        app.prepare()

        # make sure it produce the same App in default way
        self.assertEqual(
            sorted(
                _diff_(app.dump(), App.create(path).dump(), exclude=['$ref'])),
            [])

        #
        # provide empty path
        #
        getter = DictGetter(
            [
                '',
                'pet.json',
                'user.json',
                'store.json',
            ], {
                '': resource_list,
                'pet.json': pet,
                'store.json': store,
                'user.json': user
            })
        app = App.load('http://petstore.com',
                       resolver=Resolver(default_getter=getter))
        app.prepare()

        # make sure it produce the same App in default way
        self.assertEqual(
            sorted(
                _diff_(app.dump(), App.create(path).dump(), exclude=['$ref'])),
            [])