def test_pagination_build_query_string(self): # Test the following cases confirm expected string is returned # A) all defaults - expect next and prev page numbers only # B) non-default sort - expect page numbers with sort # C) non-default num - expect page numbers with num # D) non-default sort and num - expect page numbers with num and sort # E) no page_obj - expect None # Run where prev_page = 2 & next_page = 4 default_num = '10' default_sort = '-id' decision_list = DecisionList() decision_list.default_num_items = default_num # Set up a dummy page object class DummyPageObject: def __init__(self, **kwargs): self.__dict__.update(kwargs) page_obj = DummyPageObject(previous_page_number=lambda: 2, next_page_number=lambda: 4) test_cases = [{'name': 'Test A', 'page_obj': page_obj, 'context': {'num': default_num, 'sort': default_sort}, 'expectedprev': '?page=2', 'expectednext': '?page=4'}, {'name': 'Test B', 'page_obj': page_obj, 'context': {'num': default_num, 'sort': 'excerpt'}, 'expectedprev': '?sort=excerpt&page=2', 'expectednext': '?sort=excerpt&page=4'}, {'name': 'Test C', 'page_obj': page_obj, 'context': {'num': '25', 'sort': default_sort}, 'expectedprev': '?num=25&page=2', 'expectednext': '?num=25&page=4'}, {'name': 'Test D', 'page_obj': page_obj, 'context': {'num': '25', 'sort': 'excerpt'}, 'expectedprev': '?sort=excerpt&num=25&page=2', 'expectednext': '?sort=excerpt&num=25&page=4'}, {'name': 'Test E', 'page_obj': None, 'context': {'num': '25', 'sort': 'feedback'}, 'expectedprev': None, 'expectednext': None}] for test_case in test_cases: context = test_case['context'] context['page_obj'] = test_case['page_obj'] returned_prev_string = decision_list.build_prev_query_string(context) returned_next_string = decision_list.build_next_query_string(context) self.assertEquals(returned_prev_string, test_case['expectedprev'], 'Did not get expected previous query') self.assertEquals(returned_next_string, test_case['expectednext'], 'Did not get expected next query')
def test_all_sorts_result_in_one_arrow_present(self): """Assert only one sort class is present in the decision list view""" # Assumes CSS will be correctly displaying the sort status sort_options = DecisionList().sort_table_headers self.create_decisions_with_different_statuses() # Test Ascending Sort for page, sort_queries in sort_options.iteritems(): for sort_query in sort_queries: response = self.client.get(reverse('publicweb_item_list', args=[self.bettysorg.slug, page]), {'sort': sort_query}) html = fromstring(response.content) sort_selector = CSSSelector('table.summary-list .sort-asc') sorts = sort_selector(html) self.assertEquals(len(sorts), 1, 'Number of ascending sort arrows should be 1. But is ' + str(len(sorts)) + ' for page=' + page + ' sort_query=' + sort_query) # Test Descending Sort for page, sort_queries in sort_options.iteritems(): for sort_query in sort_queries: response = self.client.get(reverse('publicweb_item_list', args=[self.bettysorg.slug, page]), {'sort': '-' + sort_query}) html = fromstring(response.content) sort_selector = CSSSelector('table.summary-list .sort-desc') sorts = sort_selector(html) self.assertEquals(len(sorts), 1, 'Number of descending sort arrows should be 1. But is ' + str(len(sorts)) + ' for page=' + page + ' sort_query=' + sort_query)
def test_sorting_header_links(self): # Ensure that the links provided in table headers give the correct next sort link # Use default sort for each sort_option and assert that other headers are correct. self.create_decisions_with_different_statuses() decision = DecisionList() default_sort_options = deepcopy(decision.sort_options) sort_table_headers = deepcopy(decision.sort_table_headers) unsortable_headers = decision.unsortable_fields[:] for header_list in sort_table_headers.values(): for header in unsortable_headers: index = header_list.index(header) header_list.pop(index) for page, sort_queries in sort_table_headers.iteritems(): page_url = reverse('publicweb_item_list', args=[self.bettysorg.slug, page]) sort_query_defaults = {} # Build expected defaults for sort_query in sort_queries: default_sort_order = default_sort_options[sort_query] default_sort_query = default_sort_order + sort_query sort_query_defaults[sort_query] = default_sort_query sort_query_defaults['id'] = '' # Override the default sort for id for sort_query in sort_queries: sort_query_tests = sort_query_defaults.copy() test_query = sort_query_tests.pop(sort_query) response = self.client.get(page_url, {'sort': test_query}) html = fromstring(response.content) # Loop through the shortened sort_query_tests to check that default links are being given for selector, sort_query_test in sort_query_tests.iteritems(): selector = CSSSelector('.summary-header th.' + selector + ' a') link_ending = selector(html)[0].attrib['href'].split( page_url)[1] if sort_query_test == '': self.assertFalse(link_ending) else: self.assertEquals( link_ending.split('?sort=')[1], sort_query_test) # Finally check that the test_query column has the opposite sort selector = CSSSelector('.summary-header th.' + sort_query + ' a') link_ending = selector(html)[0].attrib['href'].split( page_url)[1].split('?sort=')[1] reversed_sort_order = decision.toggle_sort_order( default_sort_options[sort_query]) expected_link_ending = reversed_sort_order + sort_query self.assertEquals(expected_link_ending, link_ending)
def test_sorting_header_links(self): # Ensure that the links provided in table headers give the correct next sort link # Use default sort for each sort_option and assert that other headers are correct. self.create_decisions_with_different_statuses() decision = DecisionList() default_sort_options = deepcopy(decision.sort_options) sort_table_headers = deepcopy(decision.sort_table_headers) unsortable_headers = decision.unsortable_fields[:] for header_list in sort_table_headers.values(): for header in unsortable_headers: index = header_list.index(header) header_list.pop(index) for page, sort_queries in sort_table_headers.iteritems(): page_url = reverse('publicweb_item_list', args=[self.bettysorg.slug, page]) sort_query_defaults = {} # Build expected defaults for sort_query in sort_queries: default_sort_order = default_sort_options[sort_query] default_sort_query = default_sort_order + sort_query sort_query_defaults[sort_query] = default_sort_query sort_query_defaults['id'] = '' # Override the default sort for id for sort_query in sort_queries: sort_query_tests = sort_query_defaults.copy() test_query = sort_query_tests.pop(sort_query) response = self.client.get(page_url, {'sort': test_query}) html = fromstring(response.content) # Loop through the shortened sort_query_tests to check that default links are being given for selector, sort_query_test in sort_query_tests.iteritems(): selector = CSSSelector('.summary-header th.' + selector + ' a') link_ending = selector(html)[0].attrib['href'].split(page_url)[1] if sort_query_test == '': self.assertFalse(link_ending) else: self.assertEquals(link_ending.split('?sort=')[1], sort_query_test) # Finally check that the test_query column has the opposite sort selector = CSSSelector('.summary-header th.' + sort_query + ' a') link_ending = selector(html)[0].attrib['href'].split(page_url)[1].split('?sort=')[1] reversed_sort_order = decision.toggle_sort_order(default_sort_options[sort_query]) expected_link_ending = reversed_sort_order + sort_query self.assertEquals(expected_link_ending, link_ending)
def test_all_sorts_result_in_one_arrow_present(self): """Assert only one sort class is present in the decision list view""" # Assumes CSS will be correctly displaying the sort status sort_options = DecisionList().sort_table_headers self.create_decisions_with_different_statuses() # Test Ascending Sort for page, sort_queries in sort_options.iteritems(): for sort_query in sort_queries: response = self.client.get( reverse('publicweb_item_list', args=[self.bettysorg.slug, page]), {'sort': sort_query}) html = fromstring(response.content) sort_selector = CSSSelector('table.summary-list .sort-asc') sorts = sort_selector(html) self.assertEquals( len(sorts), 1, 'Number of ascending sort arrows should be 1. But is ' + str(len(sorts)) + ' for page=' + page + ' sort_query=' + sort_query) # Test Descending Sort for page, sort_queries in sort_options.iteritems(): for sort_query in sort_queries: response = self.client.get( reverse('publicweb_item_list', args=[self.bettysorg.slug, page]), {'sort': '-' + sort_query}) html = fromstring(response.content) sort_selector = CSSSelector('table.summary-list .sort-desc') sorts = sort_selector(html) self.assertEquals( len(sorts), 1, 'Number of descending sort arrows should be 1. But is ' + str(len(sorts)) + ' for page=' + page + ' sort_query=' + sort_query)
def test_cannot_view_decisions_when_not_member(self): request = self.factory.request() request.user = self.betty request.session = self.client.session decision = self.make_decision() kwargs = { 'org_slug': decision.organization.slug, 'status': decision.status } response = DecisionList(template_name='decision_list.html').dispatch( request, **kwargs) self.assertContains(response, decision.description) kwargs = {'pk': decision.pk} response = DecisionDetail(template_name='item_detail.html').dispatch( request, **kwargs) self.assertContains(response, decision.description) members = [x.username for x in decision.organization.users.all()] non_members = User.objects.exclude(username__in=members).exclude( username='******') self.assertTrue(non_members) self.login(non_members[0].username) request.user = self.user kwargs = { 'org_slug': decision.organization.slug, 'status': decision.status } response = DecisionList(template_name='decision_list.html').dispatch( request, **kwargs) self.assertNotContains(response, decision.description) kwargs = {'pk': decision.pk} response = DecisionDetail(template_name='item_detail.html').dispatch( request, **kwargs) self.assertNotContains(response, decision.description)
def test_status_set_in_get_and_get_context_data(self): """ More integrated test, that goes through dispatch, get, and get_context_data method to get the response object. """ org = OrganizationFactory() kwargs = {'org_slug': org.slug, 'status': Decision.PROPOSAL_STATUS} request = RequestFactory().get('/') request.user = UserFactory.build() request.session = {} dl = DecisionList.as_view() response = dl(request, **kwargs) self.assertEqual(response.context_data['tab'], Decision.PROPOSAL_STATUS)
def test_set_sorting(self): # Test the following cases confirm expected string is returned # A) None -> -id # B) random -> -id # C) valid sort option asc -> valid sort option # D) valid sort option desc -> - valid sort option valid_sort_options = DecisionList().sort_options.keys() test_c = choice(valid_sort_options) test_d = '-' + choice(valid_sort_options) test_cases = [{ 'name': 'Test A', 'sortquery': '-id', 'expectedsort': '-id', 'expectedsort_order': '-', 'expectedsort_field': 'id' }, { 'name': 'Test B', 'sortquery': self._get_random_string(10), 'expectedsort': '-id', 'expectedsort_order': '-', 'expectedsort_field': 'id' }, { 'name': 'Test C', 'sortquery': test_c, 'expectedsort': test_c, 'expectedsort_order': '', 'expectedsort_field': test_c }, { 'name': 'Test D', 'sortquery': test_d, 'expectedsort': test_d, 'expectedsort_order': '-', 'expectedsort_field': test_d[1:] }] for test_case in test_cases: response = self.client.get( reverse('publicweb_item_list', args=[self.bettysorg.slug, 'proposal']), {'sort': test_case['sortquery']}) self.assertEquals( response.context['sort'], test_case['expectedsort'], 'Did not get expected sort with sortquery ' + test_case['sortquery'])
def test_status_is_set_in_context_data_and_limits_object_list(self): """ More integrated test, that goes through dispatch, get, and get_context_data method to get the response object and test it. """ decision = DecisionFactory(status=Decision.DECISION_STATUS) org = decision.organization archive = DecisionFactory(organization=org, status=Decision.ARCHIVED_STATUS) kwargs = {'org_slug': org.slug, 'status': archive.status} request = RequestFactory().get('/') request.user = UserFactory.build() request.session = {} response = DecisionList.as_view()(request, **kwargs) self.assertIn('tab', response.context_data.keys()) self.assertEqual(response.context_data['tab'], archive.status) self.assertIn(archive, response.context_data['object_list']) self.assertNotIn(decision, response.context_data['object_list'])
def test_set_status_uses_default_when_no_kwarg_is_passed(self): dl = DecisionList() dl.set_status() default_status = Decision.PROPOSAL_STATUS self.assertEqual(dl.status, default_status)
def test_set_status_is_correctly_set_when_kwarg_is_passed(self): dl = DecisionList() kwargs = {'status': 'test status'} dl.set_status(**kwargs) self.assertEqual(dl.status, 'test status')
def test_set_status_uses_default_when_no_kwarg_is_passed(self): dl = DecisionList() dl.set_status() default_status = DecisionList.DEFAULT self.assertEqual(dl.status, default_status)
def test_toggle_sort_order(self): decision = DecisionList() self.assertEquals('', decision.toggle_sort_order('-')) self.assertEquals('-', decision.toggle_sort_order(''))
def test_pagination_build_query_string(self): # Test the following cases confirm expected string is returned # A) all defaults - expect next and prev page numbers only # B) non-default sort - expect page numbers with sort # C) non-default num - expect page numbers with num # D) non-default sort and num - expect page numbers with num and sort # E) no page_obj - expect None # Run where prev_page = 2 & next_page = 4 default_num = '10' default_sort = '-id' decision_list = DecisionList() decision_list.default_num_items = default_num # Set up a dummy page object class DummyPageObject: def __init__(self, **kwargs): self.__dict__.update(kwargs) page_obj = DummyPageObject(previous_page_number=lambda: 2, next_page_number=lambda: 4) test_cases = [{ 'name': 'Test A', 'page_obj': page_obj, 'context': { 'num': default_num, 'sort': default_sort }, 'expectedprev': '?page=2', 'expectednext': '?page=4' }, { 'name': 'Test B', 'page_obj': page_obj, 'context': { 'num': default_num, 'sort': 'excerpt' }, 'expectedprev': '?sort=excerpt&page=2', 'expectednext': '?sort=excerpt&page=4' }, { 'name': 'Test C', 'page_obj': page_obj, 'context': { 'num': '25', 'sort': default_sort }, 'expectedprev': '?num=25&page=2', 'expectednext': '?num=25&page=4' }, { 'name': 'Test D', 'page_obj': page_obj, 'context': { 'num': '25', 'sort': 'excerpt' }, 'expectedprev': '?sort=excerpt&num=25&page=2', 'expectednext': '?sort=excerpt&num=25&page=4' }, { 'name': 'Test E', 'page_obj': None, 'context': { 'num': '25', 'sort': 'feedback' }, 'expectedprev': None, 'expectednext': None }] for test_case in test_cases: context = test_case['context'] context['page_obj'] = test_case['page_obj'] returned_prev_string = decision_list.build_prev_query_string( context) returned_next_string = decision_list.build_next_query_string( context) self.assertEquals(returned_prev_string, test_case['expectedprev'], 'Did not get expected previous query') self.assertEquals(returned_next_string, test_case['expectednext'], 'Did not get expected next query')