Beispiel #1
0
    def test_format_response_for_a_list_of_objects(self):

        data = {
            'limit': 20,
            'offset': 0,
            'total': 200,
            'objects': [{
                'id': 1,
                'data': 1
            }]
        }

        self.req.path = "/api/v1/packages/"
        self.config.add_route('Attempt', '/api/v1/attempts/{id}/')
        self.config.add_route('Ticket', '/api/v1/tickets/{id}/')

        renderer = GtwMetaFactory()
        renderer.request = self.req
        renderer._current_resource_path = lambda *args, **kwargs: self.req.path + '?limit=20&offset=20'

        self.assertEqual(
            renderer.format_response(data), {
                'meta': {
                    'total': 200,
                    'limit': 20,
                    'offset': 0,
                    'next': "/api/v1/packages/?limit=20&offset=20",
                    'previous': None,
                },
                'objects': [{
                    'id': 1,
                    'data': 1,
                    'resource_uri': '/api/v1/packages/1/'
                }]
            })
Beispiel #2
0
    def test_format_response_for_a_list_of_objects(self):
        
        data = {'limit': 20,
                'offset': 0,
                'total': 200,
                'objects': [{'id':1, 'data':1}]}

        self.req.path = "/api/v1/packages/"
        self.config.add_route('Attempt', '/api/v1/attempts/{id}/')
        self.config.add_route('Ticket', '/api/v1/tickets/{id}/')

        renderer = GtwMetaFactory()
        renderer.request = self.req
        renderer._current_resource_path = lambda *args, **kwargs: self.req.path + '?limit=20&offset=20' 

        self.assertEqual(renderer.format_response(data), {
                'meta':{
                        'total': 200,
                        'limit': 20,
                        'offset': 0,
                        'next': "/api/v1/packages/?limit=20&offset=20",
                        'previous': None,
                        },
                'objects':
                    [
                        {'id':1, 
                            'data':1,
                            'resource_uri': '/api/v1/packages/1/'
                        }
                    ]
                    })
Beispiel #3
0
    def test_format_response_for_a_single_object(self):
        data = AttemptStub().to_dict()
        expected = data
        expected['resource_uri'] = '/api/v1/attempts/1/'

        renderer = GtwMetaFactory()
        self.req.path = "/api/v1/attempts/1/"
        renderer.request = self.req

        self.assertEqual(renderer.format_response(data), expected)
Beispiel #4
0
    def test_format_response_for_a_single_object(self):
        data = AttemptStub().to_dict()
        expected = data
        expected['resource_uri'] = '/api/v1/attempts/1/'

        renderer = GtwMetaFactory()
        self.req.path = "/api/v1/attempts/1/"
        renderer.request = self.req

        self.assertEqual(renderer.format_response(data), 
                        expected
                       )
Beispiel #5
0
    def test_current_resource_path(self):
        from pyramid.interfaces import IRoutesMapper
        route = DummyRoute('/1/2/3')
        mapper = DummyRoutesMapper(route=route)
        self.req.matched_route = route
        self.req.matchdict = {}
        self.req.script_name = '/script_name'
        self.req.registry.registerUtility(mapper, IRoutesMapper)

        renderer = GtwMetaFactory()
        renderer.request = self.req
        result = renderer._current_resource_path({'foo': 'bar'}, limit=15, offset=50)

        self.assertEqual(result, '/script_name/1/2/3?foo=bar&limit=15&offset=50')
Beispiel #6
0
    def test_current_resource_path(self):
        from pyramid.interfaces import IRoutesMapper
        route = DummyRoute('/1/2/3')
        mapper = DummyRoutesMapper(route=route)
        self.req.matched_route = route
        self.req.matchdict = {}
        self.req.script_name = '/script_name'
        self.req.registry.registerUtility(mapper, IRoutesMapper)

        renderer = GtwMetaFactory()
        renderer.request = self.req
        result = renderer._current_resource_path({'foo': 'bar'},
                                                 limit=15,
                                                 offset=50)

        self.assertEqual(result,
                         '/script_name/1/2/3?foo=bar&limit=15&offset=50')
Beispiel #7
0
    def test_add_resource_to_object_which_has_related_resources(self):
        data = ArticlePkgStub().to_dict()
        expected = {}
        expected.update(data)
        expected.update({'tickets':  ['/api/v1/tickets/11/', '/api/v1/tickets/12/']})
        expected.update({'attempts': ['/api/v1/attempts/1/', '/api/v1/attempts/2/']})
        expected.update({'resource_uri': '/api/v1/packages/1/'})
        del expected['related_resources']

        renderer = GtwMetaFactory()
        self.req.path = "/api/v1/packages/"
        self.config.add_route('Attempt', '/api/v1/attempts/{id}/')
        self.config.add_route('Ticket', '/api/v1/tickets/{id}/')

        renderer.request = self.req
        renderer._current_resource_path = lambda *args, **kwargs: None

        self.assertEqual(renderer.add_resource_uri(data), expected)
Beispiel #8
0
    def test_add_resource_to_object_without_related_resources(self):
        data = {
                    'collection_uri': '/api/v1/collection/xxx/',
                    'filepath': '/tmp/foo/bar.zip',
                    'finished_at': None,
                    'articlepkg_id': 1,
                    'is_valid': True,
                    'started_at': '2013-09-18 14:11:04.129956',
                    'id': 1,
                    'package_checksum': 'ol9j27n3f52kne7hbn',
                }
        expected = {'resource_uri': '/api/v1/attempts/1/'}
        expected.update(data)

        renderer = GtwMetaFactory()
        self.req.path = "/api/v1/attempts/"
        renderer.request = self.req

        self.assertEqual(renderer.add_resource_uri(data), expected)
Beispiel #9
0
    def test_add_resource_to_object_without_related_resources(self):
        data = {
            'collection_uri': '/api/v1/collection/xxx/',
            'filepath': '/tmp/foo/bar.zip',
            'finished_at': None,
            'articlepkg_id': 1,
            'is_valid': True,
            'started_at': '2013-09-18 14:11:04.129956',
            'id': 1,
            'package_checksum': 'ol9j27n3f52kne7hbn',
        }
        expected = {'resource_uri': '/api/v1/attempts/1/'}
        expected.update(data)

        renderer = GtwMetaFactory()
        self.req.path = "/api/v1/attempts/"
        renderer.request = self.req

        self.assertEqual(renderer.add_resource_uri(data), expected)
Beispiel #10
0
    def test_add_resource_to_object_which_has_related_resources(self):
        data = ArticlePkgStub().to_dict()
        expected = {}
        expected.update(data)
        expected.update(
            {'tickets': ['/api/v1/tickets/11/', '/api/v1/tickets/12/']})
        expected.update(
            {'attempts': ['/api/v1/attempts/1/', '/api/v1/attempts/2/']})
        expected.update({'resource_uri': '/api/v1/packages/1/'})
        del expected['related_resources']

        renderer = GtwMetaFactory()
        self.req.path = "/api/v1/packages/"
        self.config.add_route('Attempt', '/api/v1/attempts/{id}/')
        self.config.add_route('Ticket', '/api/v1/tickets/{id}/')

        renderer.request = self.req
        renderer._current_resource_path = lambda *args, **kwargs: None

        self.assertEqual(renderer.add_resource_uri(data), expected)
Beispiel #11
0
 def test_positive_int_or_zero_string_not_number(self):
     renderer = GtwMetaFactory()
     self.assertEqual(renderer._positive_int_or_zero('bla'), 0)
Beispiel #12
0
 def test_positive_int_or_zero_negative(self):
     renderer = GtwMetaFactory()
     self.assertEqual(renderer._positive_int_or_zero(-10), 0)
Beispiel #13
0
 def test_positive_int_or_zero_string(self):
     renderer = GtwMetaFactory()
     self.assertEqual(renderer._positive_int_or_zero('10'), 10)
Beispiel #14
0
 def test_positive_int_or_zero_string(self):
     renderer = GtwMetaFactory()
     self.assertEqual(renderer._positive_int_or_zero('10'), 10)
Beispiel #15
0
 def test_int(self):
     renderer = GtwMetaFactory()
     self.assertEqual(renderer._positive_int_or_zero(10), 10)
Beispiel #16
0
 def test_previous_offset_offset_20_limit_20(self):
     renderer = GtwMetaFactory()
     self.req.path = "/api/v1/attempts/"
     renderer.request = self.req
     self.assertEqual(renderer._prev_offset(offset=20, limit=20), 0)
Beispiel #17
0
 def test_next_offset_offset_50_limit_50(self):
     renderer = GtwMetaFactory()
     self.req.path = "/api/v1/attempts/"
     renderer.request = self.req
     self.assertEqual(renderer._next_offset(offset=50, limit=50, total=101),
                      100)
Beispiel #18
0
 def test_next_offset_offset_20_limit_20_total_39(self):
     renderer = GtwMetaFactory()
     self.req.path = "/api/v1/attempts/"
     renderer.request = self.req
     self.assertEqual(renderer._next_offset(offset=20, limit=20, total=39), None)
Beispiel #19
0
 def test_int(self):
     renderer = GtwMetaFactory()
     self.assertEqual(renderer._positive_int_or_zero(10), 10)
Beispiel #20
0
 def test_next_offset_offset_50_limit_50(self):
     renderer = GtwMetaFactory()
     self.req.path = "/api/v1/attempts/"
     renderer.request = self.req
     self.assertEqual(renderer._next_offset(offset=50, limit=50, total=101), 100)
Beispiel #21
0
 def test_next_offset_offset_None_limit_40(self):
     renderer = GtwMetaFactory()
     self.req.path = "/api/v1/attempts/"
     renderer.request = self.req
     self.assertEqual(renderer._next_offset(offset=None, limit=40, total=100), 40)
Beispiel #22
0
 def test_previous_offset_offset_20_limit_20(self):
     renderer = GtwMetaFactory()
     self.req.path = "/api/v1/attempts/"
     renderer.request = self.req
     self.assertEqual(renderer._prev_offset(offset=20, limit=20), 0)
Beispiel #23
0
 def test_positive_int_or_zero_string_not_number(self):
     renderer = GtwMetaFactory()
     self.assertEqual(renderer._positive_int_or_zero('bla'), 0)
Beispiel #24
0
 def test_positive_int_or_zero_negative(self):
     renderer = GtwMetaFactory()
     self.assertEqual(renderer._positive_int_or_zero(-10), 0)
Beispiel #25
0
 def test_next_offset_offset_None_limit_40(self):
     renderer = GtwMetaFactory()
     self.req.path = "/api/v1/attempts/"
     renderer.request = self.req
     self.assertEqual(
         renderer._next_offset(offset=None, limit=40, total=100), 40)
Beispiel #26
0
 def test_positive_int_or_zero_None(self):
     renderer = GtwMetaFactory()
     self.assertEqual(renderer._positive_int_or_zero(None), 0)
Beispiel #27
0
 def test_next_offset_offset_20_limit_20_total_39(self):
     renderer = GtwMetaFactory()
     self.req.path = "/api/v1/attempts/"
     renderer.request = self.req
     self.assertEqual(renderer._next_offset(offset=20, limit=20, total=39),
                      None)
Beispiel #28
0
 def test_positive_int_or_zero_None(self):
     renderer = GtwMetaFactory()
     self.assertEqual(renderer._positive_int_or_zero(None), 0)