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_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_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 _company_factory( num_interactions=0, num_contacts=0, num_investment_projects=0, num_orders=0, num_referrals=0, num_company_list_items=0, num_pipeline_items=0, ): """ Factory for a company that has companies, interactions, investment projects and OMIS orders. """ company = CompanyFactory() ContactFactory.create_batch(num_contacts, company=company) CompanyInteractionFactory.create_batch(num_interactions, company=company) CompanyReferralFactory.create_batch(num_referrals, company=company, contact=None) OrderFactory.create_batch(num_orders, company=company) CompanyListItemFactory.create_batch(num_company_list_items, company=company) PipelineItemFactory.create_batch(num_pipeline_items, company=company) fields_iter = cycle(INVESTMENT_PROJECT_COMPANY_FIELDS) fields = islice(fields_iter, 0, num_investment_projects) InvestmentProjectFactory.create_batch( num_investment_projects, **{field: company for field in fields}, ) return company
def _company_factory(num_interactions=0, num_contacts=0, num_orders=0, num_company_list_items=0): """Factory for a company that has companies, interactions and OMIS orders.""" company = CompanyFactory() ContactFactory.create_batch(num_contacts, company=company) CompanyInteractionFactory.create_batch(num_interactions, company=company) OrderFactory.create_batch(num_orders, company=company) CompanyListItemFactory.create_batch(num_company_list_items, company=company) return company
def test_with_only_items_on_other_users_lists(self): """ Test that an empty list is returned if the user has no companies on their selected list, but other users have companies on theirs. """ CompanyListItemFactory.create_batch(5) company_list = CompanyListFactory(adviser=self.user) 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() assert response_data['results'] == []
def test_includes_accurate_item_count(self, num_items): """Test that the correct item count is returned.""" company_list = CompanyListFactory(adviser=self.user) CompanyListItemFactory.create_batch(num_items, list=company_list) # Create some unrelated items – should not affect the count CompanyListItemFactory.create_batch(7) response = self.api_client.get(list_collection_url) assert response.status_code == status.HTTP_200_OK results = response.json()['results'] result = next(result for result in results if result['id'] == str(company_list.pk)) assert result['item_count'] == num_items
def test_with_existing_item(self): """ Test that no error is returned if the specified company is already on the authenticated user's list. """ creation_date = datetime(2018, 1, 2, tzinfo=utc) modified_date = datetime(2018, 1, 5, tzinfo=utc) company = CompanyFactory() company_list = CompanyListFactory(adviser=self.user) with freeze_time(creation_date): CompanyListItemFactory(list=company_list, company=company) url = _get_list_item_url(company_list.pk, company.pk) with freeze_time(modified_date): response = self.api_client.put(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert response.content == b'' company_list_item = _get_queryset_for_list_and_company( company_list, company, ).first() assert company_list_item.created_on == creation_date assert company_list_item.modified_on == creation_date
def test_returns_no_lists_if_filtered_by_company_not_on_a_list(self): """ Test that no lists are returned when filtering lists by a company not on any of the authenticated user's lists. """ # Create some lists and list items for the user CompanyListItemFactory.create_batch(5, list__adviser=self.user) # Filter by a company not on a list response = self.api_client.get( list_collection_url, data={ 'items__company_id': CompanyFactory().pk, }, ) assert response.status_code == status.HTTP_200_OK assert response.json()['results'] == []
def test_can_delete_a_list(self): """Test that a list (including its items) can be deleted.""" company_list = CompanyListFactory(adviser=self.user) list_item = CompanyListItemFactory(list=company_list) url = _get_list_detail_url(company_list.pk) response = self.api_client.delete(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert response.content == b'' with pytest.raises(CompanyListItem.DoesNotExist): list_item.refresh_from_db() # The company should not be deleted assert Company.objects.filter(pk=list_item.company.pk).exists()
def test_does_not_overwrite_other_items(self): """Test that adding an item does not overwrite other (unrelated) items.""" existing_companies = CompanyFactory.create_batch(5) company_list = CompanyListFactory(adviser=self.user) CompanyListItemFactory.create_batch( 5, list=company_list, company=factory.Iterator(existing_companies), ) company_to_add = CompanyFactory() url = _get_list_item_url(company_list.pk, company_to_add.pk) response = self.api_client.put(url) assert response.status_code == status.HTTP_204_NO_CONTENT list_item_queryset = CompanyListItem.objects.filter(list=company_list) companies_after = {item.company for item in list_item_queryset} assert companies_after == {*existing_companies, company_to_add}
def _company_factory( num_interactions=0, num_contacts=0, num_orders=0, num_referrals=0, num_company_list_items=0, num_pipeline_items=0, ): """Factory for a company that has companies, interactions and OMIS orders.""" company = CompanyFactory() ContactFactory.create_batch(num_contacts, company=company) CompanyInteractionFactory.create_batch(num_interactions, company=company) CompanyReferralFactory.create_batch(num_referrals, company=company, contact=None) OrderFactory.create_batch(num_orders, company=company) CompanyListItemFactory.create_batch(num_company_list_items, company=company) PipelineItemFactory.create_batch(num_pipeline_items, company=company) return company
def test_other_lists_are_not_affected(self): """Test that other lists are not affected when a list is deleted.""" list_to_delete = CompanyListFactory(adviser=self.user) list_item = CompanyListItemFactory(list=list_to_delete) other_list = CompanyListFactory(adviser=self.user) other_list_item = CompanyListItemFactory(company=list_item.company) url = _get_list_detail_url(list_to_delete.pk) response = self.api_client.delete(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert response.content == b'' try: other_list.refresh_from_db() other_list_item.refresh_from_db() except (CompanyList.DoesNotExist, CompanyListItem.DoesNotExist): pytest.fail('Other lists should not be affected.')
def test_merge_when_both_companies_on_same_company_list(self): """ Test that if both the source and target company are on the same company list, the merge is successful and the two list items are also merged. """ source_company = CompanyFactory() target_company = CompanyFactory() company_list = CompanyListFactory() CompanyListItemFactory(list=company_list, company=source_company) CompanyListItemFactory(list=company_list, company=target_company) user = AdviserFactory() merge_companies(source_company, target_company, user) assert not CompanyListItem.objects.filter( list=company_list, company=source_company, ).exists() assert CompanyListItem.objects.filter(list=company_list, company=target_company).exists()
def test_filtering_by_company_id_doesnt_return_other_users_lists(self): """Test that filtering by company ID doesn't return other users' lists.""" # Create a list item belonging to another user list_item = CompanyListItemFactory() response = self.api_client.get( list_collection_url, data={ 'items__company_id': list_item.company.pk, }, ) assert response.status_code == status.HTTP_200_OK assert response.json()['results'] == []
def test_with_list_that_does_not_belong_to_user(self): """Test that a company cannot be removed from another user's lists.""" company = CompanyFactory() company_list = CompanyListFactory() CompanyListItemFactory(list=company_list, company=company) url = _get_list_item_url(company_list.pk, company.pk) response = self.api_client.delete(url) assert response.status_code == status.HTTP_404_NOT_FOUND assert response.content == b'{"detail":"Not found."}' # company has not been deleted from another user list assert CompanyListItem.objects.filter(list=company_list, company=company).exists()
def test_with_archived_company(self): """Test that no error is returned when removing an archived company.""" company = ArchivedCompanyFactory() company_list = CompanyListFactory(adviser=self.user) CompanyListItemFactory(list=company_list, company=company) assert CompanyListItem.objects.filter(list=company_list, company=company).exists() url = _get_list_item_url(company_list.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_list, company=company).exists()
def test_that_actual_company_is_not_deleted(self): """ Test that a company is not removed from database after removing from user's selected list. """ company = CompanyFactory() company_list = CompanyListFactory(adviser=self.user) CompanyListItemFactory(list=company_list, company=company) url = _get_list_item_url(company_list.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_list, company=company).exists() assert Company.objects.filter(pk=company.pk).exists()
def test_two_advisers_can_have_the_same_company(self): """Test that two advisers can have the same company on their list.""" other_user_item = CompanyListItemFactory() company = other_user_item.company company_list = CompanyListFactory(adviser=self.user) url = _get_list_item_url(company_list.pk, company.pk) response = self.api_client.put(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert _get_queryset_for_list_and_company(other_user_item.list, company).exists() assert _get_queryset_for_list_and_company(company_list, company).exists()
def test_with_existing_item(self): """Test that a company can be removed from the authenticated user's selected list.""" company = CompanyFactory() company_list = CompanyListFactory(adviser=self.user) CompanyListItemFactory(list=company_list, company=company) assert CompanyListItem.objects.filter(list=company_list, company=company).exists() url = _get_list_item_url(company_list.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_list, company=company).exists()
def test_with_item(self, company_factory): """Test serialisation of various companies.""" company = company_factory() list_item = CompanyListItemFactory(list__adviser=self.user, company=company) latest_interaction = company.interactions.order_by( '-date', '-created_by', 'pk').first() url = _get_list_item_collection_url(list_item.list.pk) response = self.api_client.get(url) assert response.status_code == status.HTTP_200_OK response_data = response.json() assert response_data['results'] == [ { 'company': { 'id': str(company.pk), 'archived': company.archived, 'name': company.name, 'trading_names': company.trading_names, }, 'created_on': format_date_or_datetime(list_item.created_on), 'latest_interaction': { 'id': str(latest_interaction.pk), 'created_on': format_date_or_datetime(latest_interaction.created_on), 'date': format_date_or_datetime(latest_interaction.date.date()), 'subject': latest_interaction.subject, 'dit_participants': [{ 'adviser': { 'id': str(dit_participant.adviser.pk), 'name': dit_participant.adviser.name, } if dit_participant.adviser else None, 'team': { 'id': str(dit_participant.team.pk), 'name': dit_participant.team.name, } if dit_participant.team else None, } for dit_participant in latest_interaction. dit_participants.order_by('pk')], } if latest_interaction else None, }, ]
def test_sorting(self): """ Test that list items are sorted in reverse order of the date of the latest interaction with the company. Note that we want companies without any interactions to be sorted last. """ # These dates are in the order we expect them to be returned # `None` represents a company without any interactions interaction_dates = [ datetime(2019, 10, 8, tzinfo=utc), datetime(2016, 9, 7, tzinfo=utc), datetime(2009, 5, 6, tzinfo=utc), None, ] shuffled_dates = sample(interaction_dates, len(interaction_dates)) company_list = CompanyListFactory(adviser=self.user) list_items = CompanyListItemFactory.create_batch( len(interaction_dates), list=company_list) for interaction_date, list_item in zip(shuffled_dates, list_items): if interaction_date: CompanyInteractionFactory(date=interaction_date, company=list_item.company) url = _get_list_item_collection_url(company_list.pk) # Make sure future interactions are also sorted correctly with freeze_time('2017-12-11 09:00:00'): response = self.api_client.get(url) assert response.status_code == status.HTTP_200_OK results = response.json()['results'] assert len(results) == len(interaction_dates) actual_interaction_dates = [ result['latest_interaction']['date'] if result['latest_interaction'] else None for result in results ] expected_interaction_dates = [ format_date_or_datetime(date_.date()) if date_ else None for date_ in interaction_dates ] assert actual_interaction_dates == expected_interaction_dates
def test_adviser_can_have_the_same_company_on_multiple_lists(self): """Tests that adviser can have the same company on multiple lists.""" company = CompanyFactory() other_list = CompanyListFactory(adviser=self.user) CompanyListItemFactory(list=other_list, company=company) company_list = CompanyListFactory(adviser=self.user) url = _get_list_item_url(company_list.pk, company.pk) response = self.api_client.put(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert response.content == b'' assert CompanyListItem.objects.filter( list__in=(other_list, company_list), company=company, ).count() == 2
def company_with_company_list_items_factory(): """Factory for a company that is on users' personal company lists.""" company = CompanyFactory() CompanyListItemFactory.create_batch(3, company=company) return company