def test_enquiry_list_html(self): """Test retrieving enquiry list and ensure we get expected count""" enquiries = EnquiryFactory.create_batch(2) response = self.client.get(reverse("index"), **headers) soup = BeautifulSoup(response.content, "html.parser") enquiry_els = soup.select(".entity__list-item") self.assertEqual(response.status_code, status.HTTP_200_OK) count = len(enquiry_els) self.assertEqual(count, len(enquiries))
def test_enquiries_list_pagination(self): """ Tests pagination of enquiries list view. Creates enquiries and retrieves single page of results each time and ensures we get the expected number of results for that page. It will be same for all pages except for the last page if num_enquiries is not a multiple of page_size """ num_enquiries = 123 enquiries = EnquiryFactory.create_batch(num_enquiries) # sort enquiries to match the default sort enquiries.sort(key=lambda x: x.date_received, reverse=True) ids = [e.id for e in enquiries] page_size = settings.REST_FRAMEWORK["PAGE_SIZE"] total_pages = (num_enquiries + page_size - 1) // page_size for page in range(total_pages): start = page * page_size end = start + page_size response = self.client.get(reverse("index"), {"page": page + 1}, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual([enq["id"] for enq in response.data["results"]], ids[start:end]) self.assertEqual(response.data["current_page"], page + 1) response = self.client.get(reverse("index"), **headers) pages = response.context["pages"] assert response.context["total_pages"] == 13 assert pages[0]["page_number"] == 1 assert pages[0]["link"] == ("/?page=1") page_labels = [page["page_number"] for page in pages] assert page_labels == [1, 2, 3, 4, "...", 13] # Ensure accesing the page after the last page should return 404 response = self.client.get(reverse("index"), {"page": total_pages + 1}) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)