Example #1
0
def test_interaction_coi_extraction_when_interaction_does_not_have_a_note(
    add_objects, add_side_effect, mock_datetime
):
    # Test mapping
    add_objects(
        Interactions,
        [
            {
                'datahub_interaction_id': '18c3e449-ce56-45b4-93d3-362960622ca2',
                'datahub_company_id': 'df14625d-c89b-4efd-bc5a-9b46f68b71bb',
                'notes': None,
            }
        ],
    )

    output = [()]
    add_side_effect(output)

    mock_datetime.datetime.now.return_value = datetime.datetime(2020, 1, 1)

    mapper.analyse_interactions()

    expected_rows = []
    assert rows_equal_query_results(
        flask_app.dbi,
        expected_rows,
        f'SELECT * FROM "{mapper.output_schema}"."{mapper.output_table}"',
    )

    session = flask_app.db.session
    interaction_ids = session.query(InteractionsAnalysedInteractionIdLog).all()
    assert len(interaction_ids) == 1
    assert str(interaction_ids[0].datahub_interaction_id) == '18c3e449-ce56-45b4-93d3-362960622ca2'
    assert interaction_ids[0].analysed_at == datetime.datetime(2020, 1, 1)
Example #2
0
def test_interaction_coi_extraction(add_objects, add_side_effect, mock_datetime):
    # Test mapping
    add_objects(
        Interactions,
        [
            {
                'datahub_interaction_id': '52552367-436f-4a5d-84a2-dbf4ffeddb76',
                'datahub_company_id': 'df14625d-c89b-4efd-bc5a-9b46f68b71bb',
                'notes': 'Google exports to Brussels',
            }
        ],
    )

    output = [[('Brussels', 'Belgium', 'exported', 'GPE', ['export'], False)]]
    add_side_effect(output)

    mock_datetime.datetime.now.return_value = datetime.datetime(2020, 1, 1)

    mapper.analyse_interactions()

    expected_rows = [
        (
            1,
            '52552367-436f-4a5d-84a2-dbf4ffeddb76',
            'Brussels',
            'Belgium',
            'exported',
            'GPE',
            ['export'],
            False,
        )
    ]
    assert rows_equal_query_results(
        flask_app.dbi,
        expected_rows,
        f'SELECT * FROM "{mapper.output_schema}"."{mapper.output_table}"',
    )

    session = flask_app.db.session
    interaction_ids = session.query(InteractionsAnalysedInteractionIdLog).all()
    assert len(interaction_ids) == 1
    assert str(interaction_ids[0].datahub_interaction_id) == '52552367-436f-4a5d-84a2-dbf4ffeddb76'
    assert interaction_ids[0].analysed_at == datetime.datetime(2020, 1, 1)
Example #3
0
def test_company_matching(
    mocker,
    app,
    add_datahub_company,
    add_datahub_contact,
    add_export_wins,
    add_countries_and_sectors_of_interest,
):
    add_datahub_company([
        {
            'company_name': 'company_name1',
            'datahub_company_id': 'f17645ba-c38e-4215-b09e-47451edb9125',
            'companies_house_id': '90821308',
            'sector': 'Food',
            'reference_code': 'ORG-01233',
            'postcode': 'GS3 3PL',
            'modified_on': '2018-01-01 00:00:00',
        },
        {
            'company_name': 'company_name2',
            'datahub_company_id': '5965b49e-dae5-4a06-81fa-c894e652cd52',
            'companies_house_id': '92890123',
            'sector': 'Retail',
            'reference_code': 'ORG-01244',
            'postcode': 'GH3 3PL',
            'modified_on': '2017-01-01 00:00:00',
        },
    ])
    add_datahub_contact([{
        'datahub_contact_id': '93041a63-57d3-4b6b-a83d-172ba854f770',
        'datahub_company_id': '5965b49e-dae5-4a06-81fa-c894e652cd52',
        'email': '*****@*****.**',
    }])
    add_export_wins([
        {
            'export_wins_company_id': 'ORG-01244',
            'contact_email_address': '*****@*****.**',
            'sector': 'Aerospace',
            'company_name': 'company_name2',
            'country': 'China',
            'export_wins_id': 'ffa75985-7bc0-4e9f-8d58-28a7f234b7fc',
            'created_on': '2011-10-10 12:12:12',
            'date_won': '2010-11-11 12:12:12',
        },
    ])
    add_countries_and_sectors_of_interest(
        [
            {
                'service_company_id': 'f17645ba-c38e-4215-b09e-47451edb9125',
                'company_match_id': None,
                'country': 'country1',
                'sector': 'sector1',
                'type': 'interested',
                'service': 'datahub',
                'source': 'source1',
                'source_id': 'f17645ba-c38e-4215-b09e-47451edb9125',
                'timestamp': '2009-10-10T12:12:12',
            },
            {
                'service_company_id': '5965b49e-dae5-4a06-81fa-c894e652cd52',
                'company_match_id': None,
                'country': 'country2',
                'sector': None,
                'type': 'exported',
                'service': 'datahub',
                'source': 'source2',
                'source_id': '5965b49e-dae5-4a06-81fa-c894e652cd52',
                'timestamp': '2009-10-10T13:00:00',
            },
            {
                'service_company_id': 'ORG-01244',
                'company_match_id': None,
                'country': 'China',
                'sector': 'Aerospace',
                'type': 'exported',
                'service': 'export_wins',
                'source': 'export_wins',
                'source_id': 'ffa75985-7bc0-4e9f-8d58-28a7f234b7fc',
                'timestamp': '2010-11-11 12:12:12',
            },
        ],
        temp=True,
    )

    _api_mock(
        mocker,
        {
            "matches": [
                {
                    "id": 'f17645ba-c38e-4215-b09e-47451edb9125',
                    "match_id": 1,
                    "similarity": "101010",
                },
                {
                    "id": '5965b49e-dae5-4a06-81fa-c894e652cd52',
                    "match_id": 2,
                    "similarity": "101110",
                },
                {
                    "id": 'ffa75985-7bc0-4e9f-8d58-28a7f234b7fc',
                    "match_id": 2,
                    "similarity": "001110",
                },
            ]
        },
        200,
    )
    app.config['cms']['base_url'] = 'https://test.com'
    task = Task()
    task()

    expected_rows = [
        (
            1,
            'f17645ba-c38e-4215-b09e-47451edb9125',
            1,
            'country1',
            'sector1',
            'interested',
            'datahub',
            'source1',
            'f17645ba-c38e-4215-b09e-47451edb9125',
            '2009-10-10 12:12:12',
        ),
        (
            2,
            '5965b49e-dae5-4a06-81fa-c894e652cd52',
            2,
            'country2',
            None,
            'exported',
            'datahub',
            'source2',
            '5965b49e-dae5-4a06-81fa-c894e652cd52',
            '2009-10-10 13:00:00',
        ),
        (
            3,
            'ORG-01244',
            2,
            'China',
            'Aerospace',
            'exported',
            'export_wins',
            'export_wins',
            'ffa75985-7bc0-4e9f-8d58-28a7f234b7fc',
            '2010-11-11 12:12:12',
        ),
    ]

    assert rows_equal_query_results(
        flask_app.dbi,
        expected_rows,
        f'SELECT * FROM {CountriesAndSectorsInterest.get_fq_table_name()}',
    )
Example #4
0
def test_interaction_already_seen(add_objects, add_side_effect):

    add_objects(
        Interactions,
        [
            {
                'id': 1,
                'datahub_interaction_id': '52552367-436f-4a5d-84a2-dbf4ffeddb76',
                'datahub_company_id': 'df14625d-c89b-4efd-bc5a-9b46f68b71bb',
                'notes': 'Google exports to Brussels',
            },
            {
                'id': 2,
                'datahub_interaction_id': '52552367-436f-4a5d-84a2-dbf4ffeddb77',
                'datahub_company_id': 'df14625d-c89b-4efd-bc5a-9b46f68b71bb',
                'notes': 'nothing mentioning a location',
            },
            {
                'id': 3,
                'datahub_interaction_id': '52552367-436f-4a5d-84a2-dbf4ffeddb78',
                'datahub_company_id': 'df14625d-c89b-4efd-bc5a-9b46f68b71bb',
                'notes': 'China',
            },
        ],
    )

    add_objects(
        InteractionsAnalysedInteractionIdLog,
        [{'datahub_interaction_id': '52552367-436f-4a5d-84a2-dbf4ffeddb76'}],
    )

    outputs = [
        [],  # no country information
        [('China', 'China', 'exported', 'GPE', ['export'], False)],
    ]
    add_side_effect(outputs)

    mapper.analyse_interactions()

    expected_rows = [
        (
            '52552367-436f-4a5d-84a2-dbf4ffeddb78',
            'China',
            'China',
            'exported',
            'GPE',
            ['export'],
            False,
        ),
    ]

    assert rows_equal_query_results(
        flask_app.dbi,
        expected_rows,
        f'SELECT datahub_interaction_id,place,'
        f'standardized_place,action,type,context,negation'
        f' FROM "{mapper.output_schema}"."{mapper.output_table}"',
    )

    session = flask_app.db.session
    interaction_ids = session.query(InteractionsAnalysedInteractionIdLog).all()
    assert len(interaction_ids) == 3
def test_country_mapping(
    add_datahub_company_export_country,
    add_datahub_company_export_country_history,
    add_datahub_omis,
    add_export_wins,
    add_country_territory_registry,
):

    add_datahub_company_export_country([
        {
            'company_export_country_id':
            '9c83fe1f-24c9-4f44-8d04-3a6b82d6ca34',
            'company_id': '25262ffe-e062-49af-a620-a84d4f3feb8b',
            'country': 'afganistan',
            'country_iso_alpha2_code': 'AF',
            'created_on': '2020-01-01',
            'modified_on': '2020-01-02',
            'status': 'currently_exporting',
        },
        {
            'company_export_country_id':
            '8272b09f-f778-417f-a613-daae5c8a50c3',
            'company_id': '25262ffe-e062-49af-a620-a84d4f3feb8b',
            'country': 'netherlands antilles',
            'country_iso_alpha2_code': 'AN',
            'created_on': '2020-02-01',
            'modified_on': '2020-02-02',
            'status': 'future_interest',
        },
    ])

    add_datahub_company_export_country_history([{
        'company_id': 'd584c5e2-ef16-4aba-91d4-71949078831f',
        'country': 'Andorra',
        'country_iso_alpha2_code': 'AD',
        'history_date': '2020-01-01',
        'history_id': 'd413d348-8828-48a7-88a1-1e8a1d7a2f1f',
        'history_type': 'insert',
        'status': 'future_interest',
    }])

    add_datahub_omis([{
        'company_id': '7ff93060-1f05-4bbc-a58a-65ae7d7e8ba8',
        'created_date': '2009-10-10',
        'datahub_omis_order_id': 'a0e38b4f-f4c9-4ebc-b196-208972268efb',
        'market': 'usa',
        'sector': 'Aerospace',
    }])

    add_export_wins([
        {
            'export_wins_company_id': '12345678',
            'contact_email_address': '*****@*****.**',
            'sector': 'Aerospace',
            'company_name': 'Test Company',
            'country': 'uae',
            'export_wins_id': 'ffa75985-7bc0-4e9f-8d58-28a7f234b7fc',
            'created_on': '2009-10-10 12:12:12',
            'date_won': '2010-11-11 12:12:12',
        },
        {
            'export_wins_company_id': '12345678',
            'contact_email_address': '*****@*****.**',
            'sector': 'Aerospace',
            'company_name': 'Company testing',
            'country': 'unknown',
            'export_wins_id': 'ffa75985-7bc0-4e9f-8d58-28a7f234b7fc',
            'created_on': '2009-10-10 12:12:12',
            'date_won': '2010-11-11 12:12:12',
        },
    ])

    # Populate registry
    countries = [
        ('AF', 'Afghanistan'),
        ('AL', 'Albania'),
        ('DZ', 'Algeria'),
        ('AD', 'Andorra'),
        ('AO', 'Angola'),
        ('AG', 'Antigua and Barbuda'),
        ('BE', 'Belgium'),
        ('CL', 'Chile'),
        ('FR', 'France'),
        ('NZ', 'New Zealand'),
        ('CA', 'Canada'),
        ('US', 'United States'),
        ('CN', 'China'),
        ('DE', 'Germany'),
        ('BS', 'The Bahamas'),
        ('IN', 'India'),
        ('JP', 'Japan'),
        ('US', 'United States'),
        ('AU', 'Australia'),
        ('NZ', 'New Zealand'),
        ('CA', 'Canada'),
        ('AE', 'United Arab Emirates'),
    ]
    country_territory_entries = []
    for iso_alpha2_code, country in countries:
        entry = {'country_iso_alpha2_code': iso_alpha2_code, 'name': country}
        country_territory_entries.append(entry)
    add_country_territory_registry(country_territory_entries)

    # Standardise
    mapper.standardise_countries()

    # TODO: uncomment once export wins is included
    expected_rows = [
        (1, 'afganistan', 'Afghanistan', 91),
        (2, 'Andorra', 'Andorra', 100),
        (3, 'netherlands antilles', 'Bonaire', 100),
        (4, 'netherlands antilles', 'Saint Eustatius', 100),
        (5, 'netherlands antilles', 'Saba', 100),
        (6, 'netherlands antilles', 'CuraƧao', 100),
        (7, 'netherlands antilles', 'Sint Maarten (Dutch part)', 100),
        (8, 'uae', 'United Arab Emirates', 100),
        (9, 'usa', 'United States', 100),
    ]
    assert rows_equal_query_results(
        flask_app.dbi,
        expected_rows,
        f'SELECT * FROM "{mapper.output_schema}"."{mapper.output_table}"',
    )