Exemplo n.º 1
0
def test_investment_project_dbmodels_to_documents(opensearch):
    """Tests conversion of db models to OpenSearch documents."""
    projects = InvestmentProjectFactory.create_batch(2)

    result = SearchInvestmentProject.db_objects_to_documents(projects)

    assert len(list(result)) == len(projects)
Exemplo n.º 2
0
def test_investment_project_dbmodels_to_es_documents(es):
    """Tests conversion of db models to Elasticsearch documents."""
    projects = InvestmentProjectFactory.create_batch(2)

    result = ESInvestmentProject.db_objects_to_es_documents(projects)

    assert len(list(result)) == len(projects)
Exemplo n.º 3
0
def test_max_values_of_doubles_gross_value_added_and_foreign_equity_investment(
    es_with_signals,
    project_with_max_gross_value_added,
):
    """
    Tests the max value of gross value added and foreign equity investment.

    Both gross_value_added and foreign_equity_investment are decimal fields but the elasticsearch
    library casts them to floats so are treated as floats.

    The test highlights a known inaccuracy when dealing with large floating point numbers.

    https://docs.python.org/3.6/tutorial/floatingpoint.html

    This inaccuracy is going to be ignored as although possible this shouldn't happen in
    the real world with the gva multiplier value unlikely to exceed 2.

    """
    foreign_equity_investment_value = 9999999999999999999
    less_accurate_expected_foreign_equity_investment_value = 10000000000000000000

    expected_gross_value_added_value = 99999989999999999990
    less_accurate_expected_gross_value_added = 99999989999999991808

    project = project_with_max_gross_value_added
    assert project.gross_value_added == expected_gross_value_added_value

    result = ESInvestmentProject.db_object_to_dict(project)
    assert result[
        'foreign_equity_investment'] == foreign_equity_investment_value
    assert result['gross_value_added'] == expected_gross_value_added_value

    # Re-fetch the project from elasticsearch and
    # re-check the values against the less accurate values.
    project_in_es = ESInvestmentProject.get(
        id=project.pk,
        index=ESInvestmentProject.get_read_alias(),
    )

    assert (project_in_es['foreign_equity_investment'] ==
            less_accurate_expected_foreign_equity_investment_value)
    assert project_in_es[
        'gross_value_added'] == less_accurate_expected_gross_value_added
Exemplo n.º 4
0
def test_investment_project_to_dict(es):
    """Tests conversion of db model to dict."""
    project = InvestmentProjectFactory()
    result = ESInvestmentProject.db_object_to_dict(project)

    keys = {
        '_document_type',
        'id',
        'allow_blank_estimated_land_date',
        'allow_blank_possible_uk_regions',
        'business_activities',
        'client_contacts',
        'client_relationship_manager',
        'investor_company',
        'investor_company_country',
        'investor_type',
        'level_of_involvement',
        'investment_type',
        'stage',
        'referral_source_activity',
        'referral_source_adviser',
        'sector',
        'project_code',
        'created_on',
        'created_by',
        'modified_on',
        'archived',
        'archived_on',
        'archived_reason',
        'archived_by',
        'name',
        'description',
        'comments',
        'anonymous_description',
        'estimated_land_date',
        'actual_land_date',
        'approved_commitment_to_invest',
        'approved_fdi',
        'approved_good_value',
        'approved_high_value',
        'approved_landed',
        'approved_non_fdi',
        'intermediate_company',
        'referral_source_activity_website',
        'referral_source_activity_marketing',
        'referral_source_activity_event',
        'fdi_type',
        'fdi_value',
        'client_cannot_provide_total_investment',
        'total_investment',
        'client_cannot_provide_foreign_investment',
        'foreign_equity_investment',
        'gross_value_added',
        'government_assistance',
        'some_new_jobs',
        'specific_programme',
        'number_new_jobs',
        'will_new_jobs_last_two_years',
        'average_salary',
        'number_safeguarded_jobs',
        'r_and_d_budget',
        'non_fdi_r_and_d_budget',
        'associated_non_fdi_r_and_d_project',
        'new_tech_to_uk',
        'export_revenue',
        'client_requirements',
        'uk_region_locations',
        'actual_uk_regions',
        'delivery_partners',
        'site_decided',
        'address_1',
        'address_2',
        'address_town',
        'address_postcode',
        'uk_company_decided',
        'uk_company',
        'project_manager',
        'proposal_deadline',
        'project_assurance_adviser',
        'team_members',
        'likelihood_to_land',
        'project_arrived_in_triage_on',
        'quotable_as_public_case_study',
        'other_business_activity',
        'status',
        'reason_delayed',
        'reason_abandoned',
        'date_abandoned',
        'reason_lost',
        'date_lost',
        'country_lost_to',
        'country_investment_originates_from',
        'level_of_involvement_simplified',
    }

    assert set(result.keys()) == keys
Exemplo n.º 5
0
def test_mapping(es):
    """Test the ES mapping for an investment project."""
    mapping = Mapping.from_es(
        ESInvestmentProject.get_write_index(),
        DEFAULT_MAPPING_TYPE,
    )

    assert mapping.to_dict() == {
        DEFAULT_MAPPING_TYPE: {
            'dynamic': 'false',
            'properties': {
                '_document_type': {
                    'type': 'keyword',
                },
                'actual_land_date': {
                    'type': 'date'
                },
                'actual_uk_regions': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'address_1': {
                    'type': 'text'
                },
                'address_2': {
                    'type': 'text'
                },
                'address_postcode': {
                    'type': 'text'
                },
                'address_town': {
                    'normalizer': 'lowercase_asciifolding_normalizer',
                    'type': 'keyword',
                },
                'allow_blank_estimated_land_date': {
                    'index': False,
                    'type': 'boolean',
                },
                'allow_blank_possible_uk_regions': {
                    'index': False,
                    'type': 'boolean',
                },
                'anonymous_description': {
                    'analyzer': 'english_analyzer',
                    'type': 'text',
                },
                'approved_commitment_to_invest': {
                    'type': 'boolean'
                },
                'approved_fdi': {
                    'type': 'boolean'
                },
                'approved_good_value': {
                    'type': 'boolean'
                },
                'approved_high_value': {
                    'type': 'boolean'
                },
                'approved_landed': {
                    'type': 'boolean'
                },
                'approved_non_fdi': {
                    'type': 'boolean'
                },
                'archived': {
                    'type': 'boolean'
                },
                'archived_by': {
                    'properties': {
                        'first_name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'id': {
                            'type': 'keyword'
                        },
                        'last_name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'name': {
                            'type': 'text',
                            'fields': {
                                'keyword': {
                                    'normalizer':
                                    'lowercase_asciifolding_normalizer',
                                    'type': 'keyword',
                                },
                                'trigram': {
                                    'analyzer': 'trigram_analyzer',
                                    'type': 'text',
                                },
                            },
                        },
                    },
                    'type': 'object',
                },
                'archived_on': {
                    'type': 'date'
                },
                'archived_reason': {
                    'type': 'text'
                },
                'associated_non_fdi_r_and_d_project': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'project_code': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'average_salary': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'business_activities': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'client_cannot_provide_foreign_investment': {
                    'type': 'boolean'
                },
                'client_cannot_provide_total_investment': {
                    'type': 'boolean'
                },
                'client_contacts': {
                    'properties': {
                        'first_name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'id': {
                            'type': 'keyword'
                        },
                        'last_name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'name': {
                            'type': 'text',
                            'fields': {
                                'keyword': {
                                    'normalizer':
                                    'lowercase_asciifolding_normalizer',
                                    'type': 'keyword',
                                },
                                'trigram': {
                                    'analyzer': 'trigram_analyzer',
                                    'type': 'text',
                                },
                            },
                        },
                    },
                    'type': 'object',
                },
                'client_relationship_manager': {
                    'properties': {
                        'dit_team': {
                            'properties': {
                                'id': {
                                    'type': 'keyword'
                                },
                                'name': {
                                    'normalizer':
                                    'lowercase_asciifolding_normalizer',
                                    'type': 'keyword',
                                },
                            },
                            'type': 'object',
                        },
                        'first_name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'id': {
                            'type': 'keyword'
                        },
                        'last_name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'name': {
                            'type': 'text',
                            'fields': {
                                'keyword': {
                                    'normalizer':
                                    'lowercase_asciifolding_normalizer',
                                    'type': 'keyword',
                                },
                                'trigram': {
                                    'analyzer': 'trigram_analyzer',
                                    'type': 'text',
                                },
                            },
                        },
                    },
                    'type': 'object',
                },
                'client_requirements': {
                    'type': 'text',
                    'index': False,
                },
                'comments': {
                    'analyzer': 'english_analyzer',
                    'type': 'text',
                },
                'country_investment_originates_from': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'country_lost_to': {
                    'properties': {
                        'id': {
                            'type': 'keyword',
                            'index': False,
                        },
                        'name': {
                            'type': 'text',
                            'index': False,
                        },
                    },
                    'type': 'object',
                },
                'created_by': {
                    'properties': {
                        'dit_team': {
                            'properties': {
                                'id': {
                                    'type': 'keyword'
                                },
                                'name': {
                                    'normalizer':
                                    'lowercase_asciifolding_normalizer',
                                    'type': 'keyword',
                                },
                            },
                            'type': 'object',
                        },
                        'first_name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'id': {
                            'type': 'keyword'
                        },
                        'last_name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'name': {
                            'type': 'text',
                            'fields': {
                                'keyword': {
                                    'normalizer':
                                    'lowercase_asciifolding_normalizer',
                                    'type': 'keyword',
                                },
                                'trigram': {
                                    'analyzer': 'trigram_analyzer',
                                    'type': 'text',
                                },
                            },
                        },
                    },
                    'type': 'object',
                },
                'created_on': {
                    'type': 'date'
                },
                'date_abandoned': {
                    'type': 'date'
                },
                'date_lost': {
                    'type': 'date'
                },
                'delivery_partners': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'description': {
                    'analyzer': 'english_analyzer',
                    'type': 'text',
                },
                'estimated_land_date': {
                    'type': 'date'
                },
                'export_revenue': {
                    'type': 'boolean'
                },
                'fdi_type': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'fdi_value': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'foreign_equity_investment': {
                    'type': 'double'
                },
                'government_assistance': {
                    'type': 'boolean'
                },
                'gross_value_added': {
                    'type': 'double'
                },
                'id': {
                    'type': 'keyword'
                },
                'intermediate_company': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'investment_type': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'investor_company': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'type': 'text',
                            'fields': {
                                'keyword': {
                                    'normalizer':
                                    'lowercase_asciifolding_normalizer',
                                    'type': 'keyword',
                                },
                                'trigram': {
                                    'analyzer': 'trigram_analyzer',
                                    'type': 'text',
                                },
                            },
                        },
                    },
                    'type': 'object',
                },
                'investor_company_country': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'investor_type': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'level_of_involvement': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'level_of_involvement_simplified': {
                    'type': 'keyword'
                },
                'likelihood_to_land': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'modified_on': {
                    'type': 'date'
                },
                'name': {
                    'type': 'text',
                    'fields': {
                        'keyword': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'trigram': {
                            'analyzer': 'trigram_analyzer',
                            'type': 'text',
                        },
                    },
                },
                'new_tech_to_uk': {
                    'type': 'boolean'
                },
                'non_fdi_r_and_d_budget': {
                    'type': 'boolean'
                },
                'number_new_jobs': {
                    'type': 'integer'
                },
                'number_safeguarded_jobs': {
                    'type': 'long'
                },
                'other_business_activity': {
                    'type': 'text',
                    'index': False,
                },
                'project_arrived_in_triage_on': {
                    'type': 'date'
                },
                'project_assurance_adviser': {
                    'properties': {
                        'dit_team': {
                            'properties': {
                                'id': {
                                    'type': 'keyword'
                                },
                                'name': {
                                    'normalizer':
                                    'lowercase_asciifolding_normalizer',
                                    'type': 'keyword',
                                },
                            },
                            'type': 'object',
                        },
                        'first_name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'id': {
                            'type': 'keyword'
                        },
                        'last_name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'name': {
                            'type': 'text',
                            'fields': {
                                'keyword': {
                                    'normalizer':
                                    'lowercase_asciifolding_normalizer',
                                    'type': 'keyword',
                                },
                                'trigram': {
                                    'analyzer': 'trigram_analyzer',
                                    'type': 'text',
                                },
                            },
                        },
                    },
                    'type': 'object',
                },
                'project_code': {
                    'normalizer': 'lowercase_asciifolding_normalizer',
                    'type': 'keyword',
                    'fields': {
                        'trigram': {
                            'analyzer': 'trigram_analyzer',
                            'type': 'text',
                        },
                    },
                },
                'project_manager': {
                    'properties': {
                        'dit_team': {
                            'properties': {
                                'id': {
                                    'type': 'keyword'
                                },
                                'name': {
                                    'normalizer':
                                    'lowercase_asciifolding_normalizer',
                                    'type': 'keyword',
                                },
                            },
                            'type': 'object',
                        },
                        'first_name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'id': {
                            'type': 'keyword'
                        },
                        'last_name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'name': {
                            'type': 'text',
                            'fields': {
                                'keyword': {
                                    'normalizer':
                                    'lowercase_asciifolding_normalizer',
                                    'type': 'keyword',
                                },
                                'trigram': {
                                    'analyzer': 'trigram_analyzer',
                                    'type': 'text',
                                },
                            },
                        },
                    },
                    'type': 'object',
                },
                'proposal_deadline': {
                    'type': 'date'
                },
                'quotable_as_public_case_study': {
                    'type': 'boolean'
                },
                'r_and_d_budget': {
                    'type': 'boolean'
                },
                'reason_abandoned': {
                    'type': 'text',
                    'index': False,
                },
                'reason_delayed': {
                    'type': 'text',
                    'index': False,
                },
                'reason_lost': {
                    'type': 'text',
                    'index': False,
                },
                'referral_source_activity': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'referral_source_activity_event': {
                    'normalizer': 'lowercase_asciifolding_normalizer',
                    'type': 'keyword',
                },
                'referral_source_activity_marketing': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'referral_source_activity_website': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'referral_source_adviser': {
                    'properties': {
                        'first_name': {
                            'type': 'text',
                            'index': False,
                        },
                        'id': {
                            'type': 'keyword',
                            'index': False,
                        },
                        'last_name': {
                            'type': 'text',
                            'index': False,
                        },
                        'name': {
                            'type': 'text',
                            'index': False,
                        },
                    },
                    'type': 'object',
                },
                'sector': {
                    'properties': {
                        'ancestors': {
                            'properties': {
                                'id': {
                                    'type': 'keyword'
                                }
                            },
                            'type': 'object',
                        },
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'site_decided': {
                    'type': 'boolean'
                },
                'some_new_jobs': {
                    'type': 'boolean'
                },
                'specific_programme': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'stage': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'status': {
                    'normalizer': 'lowercase_asciifolding_normalizer',
                    'type': 'keyword',
                },
                'team_members': {
                    'properties': {
                        'dit_team': {
                            'properties': {
                                'id': {
                                    'type': 'keyword'
                                },
                                'name': {
                                    'normalizer':
                                    'lowercase_asciifolding_normalizer',
                                    'type': 'keyword',
                                },
                            },
                            'type': 'object',
                        },
                        'first_name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'id': {
                            'type': 'keyword'
                        },
                        'last_name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                        'name': {
                            'type': 'text',
                            'fields': {
                                'keyword': {
                                    'normalizer':
                                    'lowercase_asciifolding_normalizer',
                                    'type': 'keyword',
                                },
                                'trigram': {
                                    'analyzer': 'trigram_analyzer',
                                    'type': 'text',
                                },
                            },
                        },
                    },
                    'type': 'object',
                },
                'total_investment': {
                    'type': 'double'
                },
                'uk_company': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'type': 'text',
                            'fields': {
                                'keyword': {
                                    'normalizer':
                                    'lowercase_asciifolding_normalizer',
                                    'type': 'keyword',
                                },
                                'trigram': {
                                    'analyzer': 'trigram_analyzer',
                                    'type': 'text',
                                },
                            },
                        },
                    },
                    'type': 'object',
                },
                'uk_company_decided': {
                    'type': 'boolean'
                },
                'uk_region_locations': {
                    'properties': {
                        'id': {
                            'type': 'keyword'
                        },
                        'name': {
                            'normalizer': 'lowercase_asciifolding_normalizer',
                            'type': 'keyword',
                        },
                    },
                    'type': 'object',
                },
                'will_new_jobs_last_two_years': {
                    'type': 'boolean'
                },
            },
        },
    }