def test_construct_pagination_single_page(self): """ Test that the pages are created correct if only a single page is required """ paginator = CustomPaginator('run', self.data, 5, 2, 1) self.assertEqual(len(paginator.page_list), 1) self.assertIsInstance(paginator.page_list[0], RunPage) self.assertTrue(paginator.page_list[0].is_visible)
def test_set_next_and_previous_only_next(self): """ Ensure that only a next page is set when on the max page """ paginator = CustomPaginator('run', self.data, 1, 1, 1) self.assertTrue(paginator.has_next) self.assertFalse(paginator.has_previous) self.assertEqual(paginator.next_page_index, 2) self.assertEqual(paginator.previous_page_index, 0)
def test_set_next_and_previous_with_both(self): """ Ensure that each the next and previous page are correctly set for the current page """ paginator = CustomPaginator('run', self.data, 1, 1, 2) self.assertTrue(paginator.has_next) self.assertTrue(paginator.has_previous) self.assertEqual(paginator.next_page_index, 3) self.assertEqual(paginator.previous_page_index, 1)
def test_create_display(self): """ Ensure that the page display is made correctly """ paginator = CustomPaginator('run', self.data, 1, 1, 1) self.assertEqual(len(paginator.display_list), 3) self.assertEqual(paginator.display_list[0].records[0].run_number, 1) self.assertEqual(paginator.display_list[1].records[0].run_number, 2) self.assertEqual(paginator.display_list[2], '...')
def test_create_display_date(self): """ Ensure that the page display is made correctly for date filter """ paginator = CustomPaginator('date', self.data, 1, 1, 1) self.assertEqual(len(paginator.display_list), 3) self.assertEqual(paginator.display_list[0].records[0].last_updated, self.data[0].last_updated) self.assertEqual(paginator.display_list[1].records[0].last_updated, self.data[1].last_updated) self.assertEqual(paginator.display_list[2], '...')
def test_construct_pagination_visibility(self): """ Test that the pages visibility are set correctly """ paginator = CustomPaginator('run', self.data, 1, 1, 1) self.assertEqual(len(paginator.page_list), 5) self.assertFalse(paginator.page_list[4].is_visible) self.assertFalse(paginator.page_list[3].is_visible) self.assertFalse(paginator.page_list[2].is_visible) self.assertTrue(paginator.page_list[1].is_visible) self.assertTrue(paginator.page_list[0].is_visible)
def test_init(self, mock_create_display_list, mock_set_next_and_previous, mock_construct_pagination, mock_validate_current_page): """ Ensure that all variables are set as expected in the __init__ and the functions to populate / validate / render the paginator are run """ paginator = CustomPaginator('run', self.data, 3, 2, 1) self.assertEqual(paginator.query_set, self.data) self.assertEqual(paginator.items_per_page, 3) self.assertEqual(paginator.page_tolerance, 2) self.assertEqual(paginator.current_page_index, 1) self.assertEqual(paginator.next_page_index, 0) self.assertEqual(paginator.has_next, False) self.assertEqual(paginator.has_previous, False) self.assertEqual(paginator.previous_page_index, 0) self.assertEqual(paginator.page_list, []) self.assertEqual(paginator.display_list, []) mock_validate_current_page.assert_called_once() mock_construct_pagination.assert_called_once() mock_set_next_and_previous.assert_called_once() mock_create_display_list.assert_called_once()
def test_construct_pagination_multi_page(self): """ Test that the pages are created correctly if multiple pages are required """ paginator = CustomPaginator('run', self.data, 2, 2, 1) self.assertEqual(len(paginator.page_list), 3)
def test_validate_current_page_high(self): """ Ensure that the current page is set to maximum page number if larger than maximum pages """ paginator = CustomPaginator('run', self.data, 3, 2, 10) self.assertEqual(paginator.current_page_index, 2)
def test_validate_current_page_low(self): """ Ensure that the current page is at least 1 when a number less than 1 is provided """ paginator = CustomPaginator('run', self.data, 3, 2, -1) self.assertEqual(paginator.current_page_index, 1)
def test_validate_current_page_valid(self): """ Ensure that current page index does not change if it is valid """ paginator = CustomPaginator('run', self.data, 3, 2, 1) self.assertEqual(paginator.current_page_index, 1)
def runs_list(request, instrument=None): """ Render instrument summary """ try: filter_by = request.GET.get('filter', 'run') instrument_obj = Instrument.objects.get(name=instrument) except Instrument.DoesNotExist: return {'message': "Instrument not found."} try: sort_by = request.GET.get('sort', 'run') if sort_by == 'run': runs = (ReductionRun.objects.only( 'status', 'last_updated', 'run_number', 'run_version', 'run_name').select_related('status').filter( instrument=instrument_obj).order_by( '-run_number', 'run_version')) else: runs = (ReductionRun.objects.only( 'status', 'last_updated', 'run_number', 'run_version', 'run_name').select_related('status').filter( instrument=instrument_obj).order_by('-last_updated')) if len(runs) == 0: return {'message': "No runs found for instrument."} try: current_variables = VariableUtils.get_default_variables( instrument_obj.name) error_reason = "" except FileNotFoundError: current_variables = {} error_reason = "reduce_vars.py is missing for this instrument" except (ImportError, SyntaxError): current_variables = {} error_reason = "reduce_vars.py has an import or syntax error" has_variables = bool(current_variables) context_dictionary = { 'instrument': instrument_obj, 'instrument_name': instrument_obj.name, 'runs': runs, 'last_instrument_run': runs[0], 'processing': runs.filter(status=STATUS.get_processing()), 'queued': runs.filter(status=STATUS.get_queued()), 'filtering': filter_by, 'sort': sort_by, 'has_variables': has_variables, 'error_reason': error_reason } if filter_by == 'experiment': experiments_and_runs = {} experiments = Experiment.objects.filter(reduction_runs__instrument=instrument_obj). \ order_by('-reference_number').distinct() for experiment in experiments: associated_runs = runs.filter(experiment=experiment). \ order_by('-created') experiments_and_runs[experiment] = associated_runs context_dictionary['experiments'] = experiments_and_runs else: max_items_per_page = request.GET.get('pagination', 10) custom_paginator = CustomPaginator( page_type=sort_by, query_set=runs, items_per_page=max_items_per_page, page_tolerance=3, current_page=request.GET.get('page', 1)) context_dictionary['paginator'] = custom_paginator context_dictionary['last_page_index'] = len( custom_paginator.page_list) context_dictionary['max_items'] = max_items_per_page # pylint:disable=broad-except except Exception as exception: LOGGER.error(exception) return { 'message': "An unexpected error has occurred when loading the instrument." } return context_dictionary