示例#1
0
def test_computed_field_function():
    """Tests if provided function is being called and dictionary created."""
    obj = construct_mock(
        get_cats_name=lambda: construct_mock(id='cat-01', name='Mittens'),
    )

    result = dict_utils.computed_field_function('get_cats_name', dict_utils.id_name_dict)(obj)
    assert result == {'id': 'cat-01', 'name': 'Mittens'}
示例#2
0
def test_computed_field_function_not_a_function():
    """Tests when provided function is missing, ValueError is raised."""
    obj = construct_mock(get_cats_name='tabby', )

    with raises(ValueError):
        dict_utils.computed_field_function('get_cats_name',
                                           dict_utils.id_name_dict)(obj)
示例#3
0
def test_address_dict_raises_error_with_invalid_prefix():
    """
    Tests that if address_dict is called with a prefix that
    cannot be found on the object, an AttributeError is raised.
    """
    obj = construct_mock(
        primary_address_1='1',
        primary_address_2='Main Road',
        primary_address_town='London',
        primary_address_county='Greenwich',
        primary_address_postcode='SE10 9NN',
        primary_address_country=construct_mock(
            id='80756b9a-5d95-e211-a939-e4115bead28a',
            name='United Kingdom',
        ),
    )
    with pytest.raises(AttributeError):
        dict_utils.address_dict(obj, prefix='secondary_address')
示例#4
0
def test_id_name_dict():
    """Tests _id_name_dict."""
    obj = construct_mock(id=123, name='test')

    res = dict_utils.id_name_dict(obj)

    assert res == {
        'id': str(obj.id),
        'name': obj.name,
    }
示例#5
0
def test_id_uri_dict():
    """Tests id_uri_dict."""
    obj = construct_mock(id=123, uri='test')

    res = dict_utils.id_uri_dict(obj)

    assert res == {
        'id': str(obj.id),
        'uri': obj.uri,
    }
示例#6
0
def test_id_type_dict():
    """Tests _id_type_dict."""
    obj = construct_mock(id=123, type='test')

    res = dict_utils.id_type_dict(obj)

    assert res == {
        'id': str(obj.id),
        'type': obj.type,
    }
示例#7
0
def test_ch_company_dict():
    """Tests ch_company_dict."""
    obj = construct_mock(id=123, company_number='01234567')

    res = dict_utils.ch_company_dict(obj)

    assert res == {
        'id': str(obj.id),
        'company_number': obj.company_number,
    }
示例#8
0
def test_interaction_dict():
    """Interaction dict should serialize id, date and subject"""
    obj = construct_mock(
        id=1234,
        date='2018-01-01',
        subject='mock interaction',
    )
    res = dict_utils.interaction_dict(obj)
    assert res == {
        'id': str(obj.id),
        'date': obj.date,
        'subject': obj.subject,
    }
示例#9
0
def test_contact_or_adviser_list_of_dicts():
    """Test that contact_or_adviser_list_of_dicts returns a list of person dicts."""
    data = [
        {'id': '12', 'first_name': 'first A', 'last_name': 'last A', 'name': 'test A'},
        {'id': '99', 'first_name': 'first B', 'last_name': 'last B', 'name': 'testing B'},
    ]
    objects = [
        construct_mock(**data_item)
        for data_item in data
    ]

    manager = mock.Mock(
        all=mock.Mock(return_value=objects),
    )

    assert dict_utils.contact_or_adviser_list_of_dicts(manager) == data
示例#10
0
def test_id_name_list_of_dicts():
    """Test that id_name_list_of_dicts returns a list of dicts with ID and name keys."""
    data = [
        {'id': '12', 'name': 'test A'},
        {'id': '99', 'name': 'testing B'},
    ]
    objects = [
        construct_mock(**mock_data)
        for mock_data in data
    ]

    manager = mock.Mock(
        all=mock.Mock(return_value=objects),
    )

    assert dict_utils.id_name_list_of_dicts(manager) == data
示例#11
0
def test_contact_or_adviser_dict():
    """Tests contact_or_adviser_dict."""
    obj = construct_mock(
        id=123,
        first_name='First',
        last_name='Last',
        name='First Last',
    )

    res = dict_utils.contact_or_adviser_dict(obj)

    assert res == {
        'id': str(obj.id),
        'first_name': obj.first_name,
        'last_name': obj.last_name,
        'name': obj.name,
    }
示例#12
0
import pytest

from datahub.core.test_utils import construct_mock
from datahub.search.contact import dict_utils


@pytest.mark.parametrize(
    'obj,field_name,expected_output',
    (
        # address_same_as_company = False and char field
        (
            construct_mock(
                address_same_as_company=False,
                address_1='2',
            ),
            'address_1',
            '2',
        ),

        # address_same_as_company = False and nested field
        (
            construct_mock(
                address_same_as_company=False,
                address_country=construct_mock(
                    id='80756b9a-5d95-e211-a939-e4115bead28a',
                    name='United Kingdom',
                ),
            ),
            'address_country',
            {
                'id': '80756b9a-5d95-e211-a939-e4115bead28a',
示例#13
0
    res = dict_utils.id_uri_dict(obj)

    assert res == {
        'id': str(obj.id),
        'uri': obj.uri,
    }


@pytest.mark.parametrize(
    'obj,expected_dict',
    (
        # complete object
        (
            construct_mock(
                id=123,
                name='Name',
                trading_names=['Trading 1', 'Trading 2'],
            ),
            {
                'id': '123',
                'name': 'Name',
                'trading_names': ['Trading 1', 'Trading 2'],
            },
        ),

        # minimal object
        (
            construct_mock(
                id=123,
                name='Name',
                trading_names=[],