def test_search_method_on_collection(self):
        c = CollectionResource(MagicMock(), properties={'href': 'href'})
        ret = c.search({'q': 'some_query'})
        self.assertEqual({'q': 'some_query'}, ret._query)

        ret = c.search('some_query')
        self.assertEqual({'q': 'some_query'}, ret._query)
Exemple #2
0
    def test_get_single_app_by_indexing_and_get(self):
        ds = MagicMock()
        ds.get_resource.return_value = {
            'href': '/',
            'offset': 2,
            'limit': 25,
            'items': [{
                'href': 'test/resource'
            }, {
                'href': 'another/resource'
            }]
        }
        rl = CollectionResource(client=MagicMock(data_store=ds),
                                properties={
                                    'href':
                                    '/',
                                    'offset':
                                    0,
                                    'limit':
                                    25,
                                    'items': [{
                                        'href': 'test/resource'
                                    }, {
                                        'href': 'another/resource'
                                    }]
                                })

        a = rl[0]
        b = rl['test/resource']
        c = rl.get('another/resource')

        self.assertEqual(a.href, 'test/resource')
        self.assertEqual(b.href, 'test/resource')
        self.assertEqual(c.href, 'another/resource')
Exemple #3
0
    def test_search_method_on_collection(self):
        c = CollectionResource(MagicMock(), properties={'href': 'href'})
        ret = c.search({'q': 'some_query'})
        self.assertEqual({'q': 'some_query'}, ret._query)

        ret = c.search('some_query')
        self.assertEqual({'q': 'some_query'}, ret._query)
    def test_get_single_app_by_indexing_and_get(self):
        ds = MagicMock()
        ds.get_resource.return_value = {
            'href': '/',
            'offset': 2,
            'limit': 25,
            'items': [
                {'href': 'test/resource'},
                {'href': 'another/resource'}
            ]
        }
        rl = CollectionResource(client=MagicMock(data_store=ds), properties={
            'href': '/',
            'offset': 0,
            'limit': 25,
            'items': [
                {'href': 'test/resource'},
                {'href': 'another/resource'}
            ]
        })

        a = rl[0]
        b = rl['test/resource']
        c = rl.get('another/resource')

        self.assertEqual(a.href, 'test/resource')
        self.assertEqual(b.href, 'test/resource')
        self.assertEqual(c.href, 'another/resource')
Exemple #5
0
    def test_init_by_properties(self):
        rl = CollectionResource(client=MagicMock(),
                                properties={
                                    'href':
                                    '/',
                                    'offset':
                                    0,
                                    'limit':
                                    25,
                                    'items': [{
                                        'href': 'test/resource'
                                    }, {
                                        'href': 'another/resource'
                                    }]
                                })

        self.assertTrue(rl.is_materialized())

        # test length computation
        self.assertEqual(len(rl), 2)
        # test indexing
        self.assertEqual(rl[0].href, 'test/resource')
        self.assertEqual(rl[1].href, 'another/resource')
        # test iteration
        hrefs = [r.href for r in rl]
        self.assertTrue(hrefs, ['test/resource', 'another/resource'])
Exemple #6
0
    def test_resource_refresh(self):
        ds = MagicMock()
        ds.get_resource.return_value = {'offset': 2, 'limit': 8, 'items': []}

        rl = CollectionResource(client=MagicMock(data_store=ds),
                                href='/test_resources')

        rl.refresh()

        ds.uncache_resource.assert_called_once_with('/test_resources')
    def test_resource_refresh(self):
        ds = MagicMock()
        ds.get_resource.return_value = {
            'offset': 2,
            'limit': 8,
            'items': []
        }

        rl = CollectionResource(client=MagicMock(data_store=ds), href='/test_resources')

        rl.refresh()

        ds.uncache_resource.assert_called_once_with('/test_resources')
Exemple #8
0
    def test_query_by_slicing(self):
        ds = MagicMock()
        ds.get_resource.return_value = {
            'href': '/',
            'offset': 2,
            'limit': 8,
            'size': 0,
            'items': []
        }

        rl = CollectionResource(client=MagicMock(data_store=ds), href='/')

        rl2 = rl[2:10]
        # data store needs to be caled once because we need the size property
        self.assertEqual(ds.get_resource.call_count, 1)

        list(rl2)
        ds.get_resource.assert_called_with('/',
                                           params={
                                               'offset': 2,
                                               'limit': 8
                                           })

        self.assertEqual(rl2.offset, 2)
        self.assertEqual(rl2.limit, 8)
Exemple #9
0
    def test_pagination(self):
        ds = MagicMock()
        ds.get_resource.return_value = {
            'href':
            '/',
            'offset':
            2,
            'limit':
            2,
            'size':
            3,
            'items': [{
                'href': 'test/resource'
            }, {
                'href': 'another/resource'
            }, {
                'href': 'third/resource'
            }]
        }

        rl = CollectionResource(client=MagicMock(data_store=ds), href='/')

        hrefs = [r.href for r in rl]

        self.assertEqual(
            hrefs, ['test/resource', 'another/resource', 'third/resource'])

        ds.get_resource.assert_any_call('/', params=None)

        self.assertEqual(len(rl), 3)
        self.assertEqual(rl.offset, 2)
        self.assertEqual(rl.limit, 2)
    def test_collection_resource_to_json(self):
        ds = MagicMock()
        ds.get_resource.return_value = {
            'href': '/',
            'offset': 0,
            'limit': 25,
            'size': 2,
            'items': [{
                'href': 'test/resource'
            }, {
                'href': 'another/resource'
            }]
        }

        props = {
            'href': '/',
            'offset': 0,
            'limit': 25,
            'size': 2,
            'items': [{
                'href': 'test/resource'
            }, {
                'href': 'another/resource'
            }]
        }

        rl = CollectionResource(client=MagicMock(data_store=ds),
                                properties=props)

        self.assertEqual(props, json.loads(rl.to_json()))

        # collection resource as attribute
        rds = MagicMock()

        class Res(Resource, SaveMixin, DictMixin):
            writable_attrs = ('foo_val', 'bar', 'rl')

        res = Res(MagicMock(data_store=rds), href='test/resource1')
        res.rl = rl

        data = {'foo_val': True, 'bar': False}

        res.update(data)
        data.update({'href': 'test/resource1'})
        data.update({'rl': props})
        res._expand = Expansion(*['rl'])
        self.assertEqual(data, json.loads(res.to_json()))
    def test_collection_resource_to_json(self):
        ds = MagicMock()
        ds.get_resource.return_value = {
            'href': '/',
            'offset': 0,
            'limit': 25,
            'size': 2,
            'items': [
                {'href': 'test/resource'},
                {'href': 'another/resource'}
            ]
        }

        props = {
            'href': '/',
            'offset': 0,
            'limit': 25,
            'size': 2,
            'items': [
                {'href': 'test/resource'},
                {'href': 'another/resource'}
            ]
        }

        rl = CollectionResource(client=MagicMock(data_store=ds), properties=props)

        self.assertEqual(props, json.loads(rl.to_json()))

        # collection resource as attribute
        rds = MagicMock()

        class Res(Resource, SaveMixin, DictMixin):
            writable_attrs = ('foo_val', 'bar', 'rl')

        res = Res(MagicMock(data_store=rds), href='test/resource1')
        res.rl = rl

        data = {
            'foo_val': True,
            'bar': False
        }

        res.update(data)
        data.update({'href': 'test/resource1'})
        data.update({'rl': props})
        res._expand = Expansion(*['rl'])
        self.assertEqual(data, json.loads(res.to_json()))
Exemple #12
0
    def test_limit_offset_query(self):
        ds = MagicMock()
        ds.get_resource.return_value = {'offset': 5, 'limit': 5, 'items': []}

        rl = CollectionResource(client=MagicMock(data_store=ds), href='/')

        rl2 = rl.query(offset=5, limit=5)
        self.assertEqual(ds.get_resource.call_count, 0)

        list(rl2)
        ds.get_resource.assert_called_once_with('/',
                                                params={
                                                    'offset': 5,
                                                    'limit': 5
                                                })

        self.assertEqual(rl2.offset, 5)
        self.assertEqual(rl2.limit, 5)
    def test_limit_offset_query(self):
        ds = MagicMock()
        ds.get_resource.return_value = {
            'offset': 5,
            'limit': 5,
            'items': []
        }

        rl = CollectionResource(client=MagicMock(data_store=ds), href='/')

        rl2 = rl.query(offset=5, limit=5)
        self.assertEqual(ds.get_resource.call_count, 0)

        list(rl2)
        ds.get_resource.assert_called_once_with('/', params={
            'offset': 5,
            'limit': 5})

        self.assertEqual(rl2.offset, 5)
        self.assertEqual(rl2.limit, 5)
    def test_creation_with_expansion(self):
        ds = MagicMock()
        ds.create_resource.return_value = {
            'href': 'test/resource',
            'name': 'Test Resource',
        }

        rl = CollectionResource(
            client=MagicMock(data_store=ds,
                BASE_URL='http://www.example.com'),
            properties={
                'href': '/',
            })

        e = Expansion()
        e.add_property('bar', limit=5)

        rl.create({}, expand=e, some_param=True)

        ds.create_resource.assert_called_once_with('http://www.example.com/',
            {}, params={'someParam': True, 'expand': 'bar(limit:5)'})
    def test_creation_with_workflow_param_passing(self):
        ds = MagicMock()
        ds.create_resource.return_value = {
            'href': 'test/resource',
            'name': 'Test Resource',
        }

        rl = CollectionResource(
            client=MagicMock(data_store=ds,
                BASE_URL='http://www.example.com'),
            properties={
                'href': '/',
            })

        r = rl.create({}, some_param=True)

        ds.create_resource.assert_called_once_with('http://www.example.com/',
            {}, params={'someParam': True})

        self.assertTrue(r.href, 'test/resource')
        self.assertTrue(r.name, 'Test Resource')
    def test_init_by_properties(self):
        rl = CollectionResource(client=MagicMock(), properties={
            'href': '/',
            'offset': 0,
            'limit': 25,
            'items': [
                {'href': 'test/resource'},
                {'href': 'another/resource'}
            ]
        })

        self.assertTrue(rl.is_materialized())

        # test length computation
        self.assertEqual(len(rl), 2)
        # test indexing
        self.assertEqual(rl[0].href, 'test/resource')
        self.assertEqual(rl[1].href, 'another/resource')
        # test iteration
        hrefs = [r.href for r in rl]
        self.assertTrue(hrefs, ['test/resource', 'another/resource'])
Exemple #17
0
    def test_creation_with_workflow_param_passing(self):
        ds = MagicMock()
        ds.create_resource.return_value = {
            'href': 'test/resource',
            'name': 'Test Resource',
        }

        rl = CollectionResource(client=MagicMock(
            data_store=ds, BASE_URL='http://www.example.com'),
                                properties={
                                    'href': '/',
                                })

        r = rl.create({}, some_param=True)

        ds.create_resource.assert_called_once_with('http://www.example.com/',
                                                   {},
                                                   params={'someParam': True})

        self.assertTrue(r.href, 'test/resource')
        self.assertTrue(r.name, 'Test Resource')
Exemple #18
0
    def test_iter(self):
        ds = MagicMock()
        ds.get_resource.return_value = {
            'href': '/',
            'offset': 0,
            'limit': 25,
            'size': 2,
            'items': [{
                'href': 'test/resource'
            }, {
                'href': 'another/resource'
            }]
        }

        rl = CollectionResource(client=MagicMock(data_store=ds), href='/')

        hrefs = []
        for r in rl:
            hrefs.append(r.href)
        # assert that it will get resource from data store
        self.assertEqual(ds.get_resource.call_count, 1)
        # assert that it will get the right hrefs
        self.assertEqual(hrefs, ['test/resource', 'another/resource'])

        ds.get_resource.return_value = {
            'href':
            '/',
            'offset':
            0,
            'limit':
            25,
            'size':
            3,
            'items': [{
                'href': 'test/resource'
            }, {
                'href': 'another/resource'
            }, {
                'href': 'third/resource'
            }]
        }

        hrefs = []
        for r in rl:
            hrefs.append(r.href)
        # assert that it will get resource from data store again
        self.assertEqual(ds.get_resource.call_count, 2)
        # assert that it will get the new hrefs
        self.assertEqual(
            hrefs, ['test/resource', 'another/resource', 'third/resource'])
Exemple #19
0
    def test_creation_with_expansion(self):
        ds = MagicMock()
        ds.create_resource.return_value = {
            'href': 'test/resource',
            'name': 'Test Resource',
        }

        rl = CollectionResource(client=MagicMock(
            data_store=ds, BASE_URL='http://www.example.com'),
                                properties={
                                    'href': '/',
                                })

        e = Expansion()
        e.add_property('bar', limit=5)

        rl.create({}, expand=e, some_param=True)

        ds.create_resource.assert_called_once_with('http://www.example.com/',
                                                   {},
                                                   params={
                                                       'someParam': True,
                                                       'expand': 'bar(limit:5)'
                                                   })
Exemple #20
0
    def test_len(self):
        ds = MagicMock()
        ds.get_resource.return_value = {
            'href': '/',
            'offset': 0,
            'limit': 25,
            'size': 2,
            'items': [{
                'href': 'test/resource'
            }, {
                'href': 'another/resource'
            }]
        }

        rl = CollectionResource(client=MagicMock(data_store=ds), href='/')

        len_rl = len(rl)
        # assert that it will get resource from data store
        self.assertEqual(ds.get_resource.call_count, 1)
        # assert that it will get the right len
        self.assertEqual(len_rl, 2)

        ds.get_resource.return_value = {
            'href':
            '/',
            'offset':
            0,
            'limit':
            25,
            'size':
            3,
            'items': [{
                'href': 'test/resource'
            }, {
                'href': 'another/resource'
            }, {
                'href': 'third/resource'
            }]
        }

        len_rl = len(rl)
        # assert that it will get resource from data store again
        self.assertEqual(ds.get_resource.call_count, 2)
        # assert that it will get the new len
        self.assertEqual(len_rl, 3)
Exemple #21
0
    def test_query_by_slicing(self):
        ds = MagicMock()
        ds.get_resource.return_value = {'offset': 2, 'limit': 8, 'items': []}

        rl = CollectionResource(client=MagicMock(data_store=ds), href='/')

        rl2 = rl[2:10]
        self.assertEqual(ds.get_resource.call_count, 0)

        list(rl2)
        ds.get_resource.assert_called_once_with('/',
                                                params={
                                                    'offset': 2,
                                                    'limit': 8
                                                })

        self.assertEqual(rl2.offset, 2)
        self.assertEqual(rl2.limit, 8)
Exemple #22
0
    def test_pagination(self):
        ds = MagicMock()
        ds.get_resource.return_value = {
            'href': '/',
            'offset': 2,
            'limit': 2,
            'items': [{
                'href': 'third/resource'
            }]
        }

        rl = CollectionResource(client=MagicMock(data_store=ds),
                                properties={
                                    'href':
                                    '/',
                                    'offset':
                                    0,
                                    'limit':
                                    2,
                                    'items': [{
                                        'href': 'test/resource'
                                    }, {
                                        'href': 'another/resource'
                                    }]
                                })

        hrefs = [r.href for r in rl]

        self.assertEqual(
            hrefs, ['test/resource', 'another/resource', 'third/resource'])

        ds.get_resource.assert_called_once_with('/',
                                                params={
                                                    'offset': 2,
                                                    'limit': 2
                                                })

        self.assertEqual(len(rl), 3)
        self.assertEqual(rl.offset, 0)
        self.assertEqual(rl.limit, 3)

        # check that repeated iteration doesn't try to continue pagination
        list(rl)
        self.assertEqual(ds.get_resource.call_count, 1)
Exemple #23
0
    def test_init_by_properties(self):
        ds = MagicMock()
        ds.get_resource.return_value = {
            'href': '/',
            'offset': 0,
            'limit': 25,
            'size': 2,
            'items': [{
                'href': 'test/resource'
            }, {
                'href': 'another/resource'
            }]
        }

        rl = CollectionResource(client=MagicMock(data_store=ds),
                                properties={
                                    'href':
                                    '/',
                                    'offset':
                                    0,
                                    'limit':
                                    25,
                                    'size':
                                    2,
                                    'items': [{
                                        'href': 'test/resource'
                                    }, {
                                        'href': 'another/resource'
                                    }]
                                })

        # test length computation
        self.assertEqual(len(rl), 2)
        # test indexing
        self.assertEqual(rl[0].href, 'test/resource')
        self.assertEqual(rl[1].href, 'another/resource')
        # test iteration
        hrefs = [r.href for r in rl]
        self.assertTrue(hrefs, ['test/resource', 'another/resource'])