예제 #1
0
 def setUp(self):
     self.reader = RequestPaginationReader(
         PropertyNamingMock('post_page_number', 'post_page_size',
                            'post_sort_property', 'post_sort_direction'),
         PropertyNamingMock('param_page_number', 'param_page_size',
                            'param_sort_property', 'param_sort_direction'),
         PropertyNamingMock('cookie_page_number', 'cookie_page_size',
                            'cookie_sort_property',
                            'cookie_sort_direction'))
예제 #2
0
    def test_call(self):
        config = testing.setUp()
        config.add_route('some_route', '/')

        pagination_request_writer = PaginationRequestWriterMock()
        param_property_naming = PropertyNamingMock('page-number', 'page-size',
                                                   'sort-property',
                                                   'sort-direction')
        url_creator_factory = UrlCreatorFactoryMock('http://example.com')
        request = DummyRequest()
        request.registry.pagination = {
            'some_route': {
                'pagination_request_writer': pagination_request_writer,
                'param_property_naming': param_property_naming,
                'url_creator_factory': url_creator_factory,
            }
        }
        request.pagination = Pagination(Paging(100, PagingRequest(3, 12)),
                                        Sorting('some_property', 'desc'))
        request.matched_route = RouteMock('some_route')
        event = BeforeRenderEventMock()
        event['request'] = request
        subscriber = PaginationBeforeRenderSubscriber()
        subscriber(event)
        self.assertEqual(pagination_request_writer.call_count, 1)

        pagination = event.rendering_val['pagination']
        self.assertEqual('http://example.com', str(pagination.url))
예제 #3
0
    def test_call(self):
        writer = DefaultPaginationRequestWriter(
            PropertyNamingMock(
                'cookie_prefix.page_number',
                'cookie_prefix.page_size',
                'cookie_prefix.sort_property',
                'cookie_prefix.sort_direction'
            )
        )
        request = DummyRequest()
        request.response = ResponseMock()
        pagination_request = PaginationRequest(
            PagingRequest(3, 24),
            Sorting('some_sort_property', 'desc'))
        writer(request, pagination_request)

        self.assertEqual(
            request.response.get_cookie('cookie_prefix.page_number'),
            '3')
        self.assertEqual(
            request.response.get_cookie('cookie_prefix.page_size'),
            '24')
        self.assertEqual(
            request.response.get_cookie('cookie_prefix.sort_property'),
            'some_sort_property')
        self.assertEqual(
            request.response.get_cookie('cookie_prefix.sort_direction'),
            'desc')
 def test_call(self):
     request_dummy = DummyRequest()
     property_naming = PropertyNamingMock('page-number', 'page-size',
                                          'sort-property', 'sort-direction')
     creator_factory = RequestUrlCreatorFactory(property_naming)
     url_creator = creator_factory(request_dummy, 'route_name')
     self.assertTrue(isinstance(url_creator, IUrlCreator))
    def test_create_url(self):
        config = testing.setUp()
        config.add_route('route_name', 'route_name')
        request = DummyRequest()
        property_naming = PropertyNamingMock('page-number', 'page-size',
                                             'sort-property', 'sort-direction')
        pagination_url_creator = RequestUrlCreator(
            request,
            'route_name',
            property_naming,
        )
        url = pagination_url_creator(
            PaginationRequest(PagingRequest(3, 10), Sorting('id', 'desc')))
        self.assertTrue(url.find('http://example.com/route_name') == 0)
        self.assertTrue(url.find('page-number=3') > 0)
        self.assertTrue(url.find('page-size=10') > 0)
        self.assertTrue(url.find('sort-property=id') > 0)
        self.assertTrue(url.find('sort-direction=desc') > 0)

        config.add_route('match_route', 'match_route/{page_number}')
        request = DummyRequest()
        request.matchdict['page_number'] = 'some_page'
        request.params['test'] = 'some_value'
        pagination_url_creator = RequestUrlCreator(
            request,
            'match_route',
            property_naming,
        )
        url = pagination_url_creator(
            PaginationRequest(PagingRequest(3, 10), Sorting('id', 'desc')))
        self.assertTrue(url.find('match_route/some_page') > 0)
        self.assertTrue(url.find('test=some_value') > 0)
예제 #6
0
    def test_write(self):
        request = DummyRequest()
        response_mock = ResponseMock()
        request.response = response_mock
        sorting = Sorting('id', 'desc')
        sorting_writer = CookieSortingWriter(
            request,
            PropertyNamingMock(
                'page_number',
                'page_size',
                'sort_property',
                'sort_direction'
            )
        )
        sorting_writer(sorting)
        self.assertEqual(response_mock.get_cookie('sort_property'), 'id')
        self.assertEqual(response_mock.get_cookie('sort_direction'), 'desc')
        self.assertEqual(response_mock.call_count(), 2)

        request = DummyRequest()
        response_mock = ResponseMock()
        request.response = response_mock
        sorting = Sorting('id', 'desc')
        sorting_writer = CookieSortingWriter(
            request,
            PropertyNamingMock(
                'cookie_prefix.page_number',
                'cookie_prefix.page_size',
                'cookie_prefix.sort_property',
                'cookie_prefix.sort_direction'
            )
        )
        sorting_writer(sorting)
        self.assertEqual(response_mock.get_cookie('cookie_prefix.sort_property'), 'id')
        self.assertEqual(response_mock.get_cookie('cookie_prefix.sort_direction'), 'desc')
        self.assertEqual(response_mock.call_count(), 2)
예제 #7
0
 def test_write(self):
     request = DummyRequest()
     response_mock = ResponseMock()
     request.response = response_mock
     pagination_request = PagingRequest(3, 12)
     pagination_request_writer = CookiePagingRequestWriter(
         request,
         PropertyNamingMock(
             'page_number',
             'page_size',
             'sort_property',
             'sort_direction'
         )
     )
     pagination_request_writer(pagination_request)
     self.assertEqual(response_mock.get_cookie('page_number'), '3')
     self.assertEqual(response_mock.get_cookie('page_size'), '12')
     self.assertEqual(response_mock.call_count(), 2)