def test_filtering_by_country_excludes_non_matches(self,
                                                       es_with_collector):
        """Test that filtering by country excludes non-matching objects."""
        countries = list(Country.objects.order_by('?')[:2])
        filter_country = countries[0]
        other_country = countries[1]

        # Non-export country interactions should be excluded
        CompanyInteractionFactory()

        # Unrelated countries should be excluded
        CompanyExportCountryHistoryFactory(country=other_country,
                                           history_type=HistoryType.INSERT)
        ExportCountriesInteractionFactory(
            export_countries__country=other_country)

        es_with_collector.flush_and_refresh()

        response = self.api_client.post(
            export_country_history_search_url,
            data={
                'country': filter_country.pk,
            },
        )
        assert response.status_code == status.HTTP_200_OK

        response_data = response.json()
        assert response_data['count'] == 0
        assert response_data['results'] == []
Example #2
0
    def test_sorts_results(self, opensearch_with_collector, request_args,
                           is_reversed):
        """
        Test sorting in various cases.

        Note that a filter is mandatory in this view, hence the test filters by company.
        """
        datetimes = [
            datetime(2001, 1, 22, tzinfo=utc),
            datetime(2002, 2, 23, 1, 2, 3, tzinfo=utc),
            datetime(2003, 3, 24, tzinfo=utc),
            datetime(2004, 4, 25, 1, 2, 3, tzinfo=utc),
        ]
        company = CompanyFactory()

        objects = [
            ExportCountriesInteractionFactory(date=datetimes.pop(0),
                                              company=company),
            _make_dated_export_country_history(datetimes.pop(0),
                                               company=company),
            ExportCountriesInteractionFactory(date=datetimes.pop(0),
                                              company=company),
            _make_dated_export_country_history(datetimes.pop(0),
                                               company=company),
        ]

        if is_reversed:
            objects.reverse()

        opensearch_with_collector.flush_and_refresh()

        response = self.api_client.post(
            export_country_history_search_url,
            data={
                'company': company.pk,
                **request_args,
            },
        )
        assert response.status_code == status.HTTP_200_OK

        expected_result_ids = [str(obj.pk) for obj in objects]
        actual_result_ids = [
            result['id'] for result in response.json()['results']
        ]

        assert actual_result_ids == expected_result_ids
    def test_interaction_response_body(self, es_with_collector):
        """Test the format of an interaction result in the response body."""
        interaction = ExportCountriesInteractionFactory()
        es_with_collector.flush_and_refresh()

        response = self.api_client.post(
            export_country_history_search_url,
            data={
                # The view requires a filter
                'company': interaction.company.pk,
            },
        )
        assert response.status_code == status.HTTP_200_OK

        results = response.json()['results']
        assert len(results) == 1

        result = results[0]
        result['contacts'].sort(key=itemgetter('id'))
        result['dit_participants'].sort(
            key=lambda participant: participant['adviser']['id'])
        result['export_countries'].sort(
            key=lambda export_country: export_country['country']['id'])

        assert result == {
            'company': {
                'id': str(interaction.company.pk),
                'name': interaction.company.name,
                'trading_names': interaction.company.trading_names,
            },
            'contacts': [{
                'id': str(contact.pk),
                'first_name': contact.first_name,
                'name': contact.name,
                'last_name': contact.last_name,
            } for contact in sorted(interaction.contacts.all(),
                                    key=attrgetter('id'))],
            'date':
            interaction.date.isoformat(),
            'dit_participants': [{
                'adviser': {
                    'id': str(dit_participant.adviser.pk),
                    'first_name': dit_participant.adviser.first_name,
                    'name': dit_participant.adviser.name,
                    'last_name': dit_participant.adviser.last_name,
                },
                'team': {
                    'id': str(dit_participant.team.pk),
                    'name': dit_participant.team.name,
                },
            } for dit_participant in interaction.dit_participants.order_by(
                'adviser__pk')],
            'export_countries': [{
                'country': {
                    'id': str(export_country.country.pk),
                    'name': export_country.country.name,
                },
                'status': export_country.status,
            } for export_country in interaction.export_countries.order_by(
                'country__pk')],
            'kind':
            interaction.kind,
            'id':
            str(interaction.pk),
            'service': {
                'id': str(interaction.service.pk),
                'name': interaction.service.name,
            },
            'subject':
            interaction.subject,
        }