def test_can_filter_by_valid_company_id(self): """Test that a user's lists can be filtered to those containing a particular company.""" company_to_filter_by = CompanyFactory() # Create some lists containing `company_to_filter_by` expected_lists = CompanyListFactory.create_batch(3, adviser=self.user) CompanyListItemFactory.create_batch( len(expected_lists), list=factory.Iterator(expected_lists), company=company_to_filter_by, ) # Create some lists without `company_to_filter_by` that should not be returned CompanyListFactory.create_batch(3, adviser=self.user) response = self.api_client.get( list_collection_url, data={ 'items__company_id': company_to_filter_by.pk, }, ) assert response.status_code == status.HTTP_200_OK results = response.json()['results'] assert len(results) == len(expected_lists) expected_list_ids = {str(list_.pk) for list_ in expected_lists} actual_list_ids = {result['id'] for result in results} assert actual_list_ids == expected_list_ids
def test_doesnt_return_other_users_lists(self): """Test that other users' company lists are not returned.""" # Create some lists belonging to other users CompanyListFactory.create_batch(5) response = self.api_client.get(list_collection_url) assert response.status_code == status.HTTP_200_OK response_data = response.json() assert response_data['results'] == []
def test_with_non_existent_list(self): """Test that you cannot add an item to a list that does not exist.""" # Existing company lists for other users should not matter CompanyListFactory.create_batch(5) company = CompanyFactory() url = _get_list_item_url(uuid4(), company.pk) response = self.api_client.put(url) assert response.status_code == status.HTTP_404_NOT_FOUND assert not CompanyListItem.objects.filter( list__adviser=self.user, company=company, ).exists()
def test_with_multiple_user_lists(self): """Test that user who owns multiple lists can list all their contents.""" lists = CompanyListFactory.create_batch(5, adviser=self.user) company_list_companies = {} # add multiple companies to user's lists for company_list in lists: companies = CompanyFactory.create_batch(5) company_list_companies[company_list.pk] = companies CompanyListItemFactory.create_batch( len(companies), list=company_list, company=factory.Iterator(companies), ) # check if contents of each user's list can be listed for company_list in lists: url = _get_list_item_collection_url(company_list.pk) response = self.api_client.get(url) assert response.status_code == status.HTTP_200_OK response_data = response.json() result_company_ids = { result['company']['id'] for result in response_data['results'] } assert result_company_ids == { str(company.id) for company in company_list_companies[company_list.pk] }
def test_with_multiple_lists(self): """Test that deleting company from one list will not delete it from the other lists.""" company = CompanyFactory() company_lists = CompanyListFactory.create_batch(5, adviser=self.user) CompanyListItemFactory.create_batch( 5, list=factory.Iterator(company_lists), company=company, ) # company exists on 5 user's lists assert CompanyListItem.objects.filter(list__in=company_lists, company=company).count() == 5 url = _get_list_item_url(company_lists[0].pk, company.pk) response = self.api_client.delete(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert response.content == b'' assert not CompanyListItem.objects.filter(list=company_lists[0], company=company).exists() # The company exists on 4 other lists assert CompanyListItem.objects.filter(list__in=company_lists, company=company).count() == 4
def test_lists_are_sorted_by_name(self): """Test that returned lists are sorted by name.""" expected_list_names = ['A list', 'B list', 'C list', 'D list'] shuffled_list_names = sample(expected_list_names, len(expected_list_names)) CompanyListFactory.create_batch( len(expected_list_names), adviser=self.user, name=factory.Iterator(shuffled_list_names), ) response = self.api_client.get(list_collection_url) assert response.status_code == status.HTTP_200_OK response_data = response.json() actual_list_names = [ result['name'] for result in response_data['results'] ] assert actual_list_names == expected_list_names