|
| 1 | +import pytest |
| 2 | +from django.urls import reverse |
| 3 | +from rest_framework import status |
| 4 | + |
| 5 | +from datahub.company.test.factories import CompanyFactory |
| 6 | +from datahub.core.test_utils import format_date_or_datetime |
| 7 | +from datahub.dataset.core.test import BaseDatasetViewTest |
| 8 | +from datahub.investment.project.test.factories import InvestmentProjectFactory |
| 9 | +from datahub.investment_lead.test.factories import EYBLeadFactory |
| 10 | + |
| 11 | + |
| 12 | +def get_expected_data_from_eyb(eyb_lead): |
| 13 | + """Returns EYB Lead data as a dictionary""" |
| 14 | + investment_projects = eyb_lead.investment_projects.all().order_by( |
| 15 | + 'name', |
| 16 | + 'id', |
| 17 | + ) |
| 18 | + |
| 19 | + investment_project_ids = [] |
| 20 | + if investment_projects.count() > 0: |
| 21 | + investment_project_ids = [str(investment_project.id) for investment_project in |
| 22 | + investment_projects] |
| 23 | + |
| 24 | + data = { |
| 25 | + 'modified_on': format_date_or_datetime(eyb_lead.modified_on), |
| 26 | + # Triage component |
| 27 | + 'triage_hashed_uuid': str(eyb_lead.triage_hashed_uuid), |
| 28 | + 'triage_created': format_date_or_datetime(eyb_lead.triage_created), |
| 29 | + 'triage_modified': format_date_or_datetime(eyb_lead.triage_modified), |
| 30 | + 'sector': str(eyb_lead.sector.id) if eyb_lead.sector else None, |
| 31 | + 'sector_segments': eyb_lead.sector_segments, |
| 32 | + 'intent': eyb_lead.intent, |
| 33 | + 'intent_other': eyb_lead.intent_other, |
| 34 | + 'proposed_investment_region': str(eyb_lead.proposed_investment_region.id) if ( |
| 35 | + eyb_lead.proposed_investment_region |
| 36 | + ) else None, |
| 37 | + 'proposed_investment_city': eyb_lead.proposed_investment_city, |
| 38 | + 'proposed_investment_location_none': eyb_lead.proposed_investment_location_none, |
| 39 | + 'hiring': eyb_lead.hiring, |
| 40 | + 'spend': eyb_lead.spend, |
| 41 | + 'spend_other': eyb_lead.spend_other, |
| 42 | + 'is_high_value': eyb_lead.is_high_value, |
| 43 | + |
| 44 | + # User component |
| 45 | + 'user_hashed_uuid': eyb_lead.user_hashed_uuid, |
| 46 | + 'user_created': format_date_or_datetime(eyb_lead.user_created), |
| 47 | + 'user_modified': format_date_or_datetime(eyb_lead.user_modified), |
| 48 | + 'company_name': eyb_lead.company_name, |
| 49 | + 'duns_number': eyb_lead.duns_number, |
| 50 | + 'address_1': eyb_lead.address_1, |
| 51 | + 'address_2': eyb_lead.address_2, |
| 52 | + 'address_town': eyb_lead.address_town, |
| 53 | + 'address_county': eyb_lead.address_county, |
| 54 | + 'address_country': str(eyb_lead.address_country.id) if eyb_lead.address_country else None, |
| 55 | + 'address_postcode': eyb_lead.address_postcode, |
| 56 | + 'company_website': eyb_lead.company_website, |
| 57 | + 'full_name': eyb_lead.full_name, |
| 58 | + 'role': eyb_lead.role, |
| 59 | + 'email': eyb_lead.email, |
| 60 | + 'telephone_number': eyb_lead.telephone_number, |
| 61 | + 'agree_terms': eyb_lead.agree_terms, |
| 62 | + 'agree_info_email': eyb_lead.agree_info_email, |
| 63 | + 'landing_timeframe': eyb_lead.landing_timeframe, |
| 64 | + 'company': str(eyb_lead.company.id) if eyb_lead.company else None, |
| 65 | + 'investment_project_ids': investment_project_ids, |
| 66 | + |
| 67 | + # Marketing component |
| 68 | + 'marketing_hashed_uuid': eyb_lead.marketing_hashed_uuid, |
| 69 | + 'utm_name': eyb_lead.utm_name, |
| 70 | + 'utm_source': eyb_lead.utm_source, |
| 71 | + 'utm_medium': eyb_lead.utm_medium, |
| 72 | + 'utm_content': eyb_lead.utm_content, |
| 73 | + } |
| 74 | + return data |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.django_db |
| 78 | +class TestEYBLeadsDatasetViewSet(BaseDatasetViewTest): |
| 79 | + """ |
| 80 | + Tests for EYBDatasetView |
| 81 | + """ |
| 82 | + |
| 83 | + view_url = reverse('api-v4:dataset:expand-your-business-dataset') |
| 84 | + factory = EYBLeadFactory |
| 85 | + |
| 86 | + def test_success_with_one(self, data_flow_api_client): |
| 87 | + """Test that the endpoint returns expected data for a single eyb lead.""" |
| 88 | + eyb_lead = self.factory( |
| 89 | + ) |
| 90 | + response = data_flow_api_client.get(self.view_url) |
| 91 | + assert response.status_code == status.HTTP_200_OK |
| 92 | + |
| 93 | + results_from_response = response.json()['results'] |
| 94 | + assert len(results_from_response) == 1 |
| 95 | + |
| 96 | + eyb_lead_from_response = results_from_response[0] |
| 97 | + expected_eyb_lead = get_expected_data_from_eyb(eyb_lead) |
| 98 | + assert eyb_lead_from_response == expected_eyb_lead |
| 99 | + |
| 100 | + def test_success_with_multiple(self, data_flow_api_client): |
| 101 | + """Test that the endpoint returns expected data for multiple EYB leads.""" |
| 102 | + eyb_lead_one = self.factory( |
| 103 | + investment_projects=InvestmentProjectFactory.create_batch(1), |
| 104 | + ) |
| 105 | + eyb_lead_two = self.factory( |
| 106 | + company=CompanyFactory(), |
| 107 | + ) |
| 108 | + eyb_lead_three = self.factory( |
| 109 | + ) |
| 110 | + eyb_lead_four = self.factory( |
| 111 | + investment_projects=InvestmentProjectFactory.create_batch(3), |
| 112 | + ) |
| 113 | + |
| 114 | + response = data_flow_api_client.get(self.view_url) |
| 115 | + assert response.status_code == status.HTTP_200_OK |
| 116 | + |
| 117 | + results_from_response = response.json()['results'] |
| 118 | + assert len(results_from_response) == 4 |
| 119 | + |
| 120 | + eyb_leads_from_response = results_from_response |
| 121 | + expected_eyb_leads = [ |
| 122 | + get_expected_data_from_eyb(eyb_lead) |
| 123 | + for eyb_lead |
| 124 | + in [eyb_lead_one, eyb_lead_two, eyb_lead_three, eyb_lead_four] |
| 125 | + ] |
| 126 | + assert eyb_leads_from_response == expected_eyb_leads |
0 commit comments