<filename>datahub/company/test/test_contact_matching.py<gh_stars>1-10
import pytest

from datahub.company.contact_matching import (
    ContactMatchingStatus,
    find_active_contact_by_email_address,
    MatchStrategy,
)
from datahub.company.test.factories import ContactFactory
from datahub.interaction.test.factories import CompanyInteractionFactory


EMAIL_MATCHING_CONTACT_TEST_DATA = [
    {
        'email': '<EMAIL>',
        'email_alternative': '<EMAIL>',
        'archived': False,
    },
    {
        'email': '<EMAIL>',
        'email_alternative': '',
        'archived': False,
        'interactions': 10,
    },
    {
        'email': '<EMAIL>',
        'email_alternative': '',
        'archived': False,
    },
    {
        'email': '<EMAIL>',
        'email_alternative': '<EMAIL>',
        'archived': False,
        'interactions': 10,
    },
    {
        'email': '<EMAIL>',
        'email_alternative': '<EMAIL>',
        'archived': False,
    },
    {
        'email': '<EMAIL>',
        'email_alternative': '<EMAIL>',
        'archived': True,
    },
]


@pytest.mark.django_db
@pytest.mark.parametrize(
    'email,expected_matching_status,match_on_alternative,match_strategy',
    (
        # same case, match on email
        ('<EMAIL>', ContactMatchingStatus.matched, False, MatchStrategy.DEFAULT),
        # same case, match on email_alternative
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            True,
            MatchStrategy.DEFAULT,
        ),
        # different case, match on email
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            False,
            MatchStrategy.DEFAULT,
        ),
        # different case, match on email_alternative
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            True,
            MatchStrategy.DEFAULT,
        ),
        # different
        (
            '<EMAIL>',
            ContactMatchingStatus.unmatched,
            None,
            MatchStrategy.DEFAULT,
        ),
        # duplicate on email
        (
            '<EMAIL>',
            ContactMatchingStatus.multiple_matches,
            None,
            MatchStrategy.DEFAULT,
        ),
        # duplicate on email_alternative
        (
            '<EMAIL>',
            ContactMatchingStatus.multiple_matches,
            None,
            MatchStrategy.DEFAULT,
        ),
        # archived contact ignored (email value specified)
        (
            '<EMAIL>',
            ContactMatchingStatus.unmatched,
            None,
            MatchStrategy.DEFAULT,
        ),
        # archived contact ignored (email_alternative value specified)
        (
            '<EMAIL>',
            ContactMatchingStatus.unmatched,
            None,
            MatchStrategy.DEFAULT,
        ),
        # same case, match on email
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            False,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # same case, match on email_alternative
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            True,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # different case, match on email
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            False,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # different case, match on email_alternative
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            True,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # different
        (
            '<EMAIL>',
            ContactMatchingStatus.unmatched,
            None,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # duplicate on email
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            None,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # duplicate on email_alternative
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            True,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # archived contact ignored (email value specified)
        (
            '<EMAIL>',
            ContactMatchingStatus.unmatched,
            None,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # archived contact ignored (email_alternative value specified)
        (
            '<EMAIL>',
            ContactMatchingStatus.unmatched,
            None,
            MatchStrategy.MAX_INTERACTIONS,
        ),
    ),
)
def test_find_active_contact_by_email_address(
    email,
    expected_matching_status,
    match_on_alternative,
    match_strategy,
):
    """Test finding a contact by email address for various scenarios."""
    for factory_kwargs in EMAIL_MATCHING_CONTACT_TEST_DATA:
        interaction_count = 0
        if factory_kwargs.get('interactions'):
            interaction_count = factory_kwargs.pop('interactions')
        created_contact = ContactFactory(**factory_kwargs)
        for _ in range(interaction_count):
            CompanyInteractionFactory(contacts=[created_contact])

    contact, actual_matching_status = find_active_contact_by_email_address(email, match_strategy)

    assert actual_matching_status == expected_matching_status

    if actual_matching_status == ContactMatchingStatus.matched:
        assert contact
        actual_email = contact.email_alternative if match_on_alternative else contact.email
        assert actual_email.lower() == email.lower()
    else:
        assert not contact

<filename>datahub/company/test/test_contact_matching.py<gh_stars>1-10
import pytest

from datahub.company.contact_matching import (
    ContactMatchingStatus,
    find_active_contact_by_email_address,
    MatchStrategy,
)
from datahub.company.test.factories import ContactFactory
from datahub.interaction.test.factories import CompanyInteractionFactory


EMAIL_MATCHING_CONTACT_TEST_DATA = [
    {
        'email': '<EMAIL>',
        'email_alternative': '<EMAIL>',
        'archived': False,
    },
    {
        'email': '<EMAIL>',
        'email_alternative': '',
        'archived': False,
        'interactions': 10,
    },
    {
        'email': '<EMAIL>',
        'email_alternative': '',
        'archived': False,
    },
    {
        'email': '<EMAIL>',
        'email_alternative': '<EMAIL>',
        'archived': False,
        'interactions': 10,
    },
    {
        'email': '<EMAIL>',
        'email_alternative': '<EMAIL>',
        'archived': False,
    },
    {
        'email': '<EMAIL>',
        'email_alternative': '<EMAIL>',
        'archived': True,
    },
]


@pytest.mark.django_db
@pytest.mark.parametrize(
    'email,expected_matching_status,match_on_alternative,match_strategy',
    (
        # same case, match on email
        ('<EMAIL>', ContactMatchingStatus.matched, False, MatchStrategy.DEFAULT),
        # same case, match on email_alternative
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            True,
            MatchStrategy.DEFAULT,
        ),
        # different case, match on email
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            False,
            MatchStrategy.DEFAULT,
        ),
        # different case, match on email_alternative
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            True,
            MatchStrategy.DEFAULT,
        ),
        # different
        (
            '<EMAIL>',
            ContactMatchingStatus.unmatched,
            None,
            MatchStrategy.DEFAULT,
        ),
        # duplicate on email
        (
            '<EMAIL>',
            ContactMatchingStatus.multiple_matches,
            None,
            MatchStrategy.DEFAULT,
        ),
        # duplicate on email_alternative
        (
            '<EMAIL>',
            ContactMatchingStatus.multiple_matches,
            None,
            MatchStrategy.DEFAULT,
        ),
        # archived contact ignored (email value specified)
        (
            '<EMAIL>',
            ContactMatchingStatus.unmatched,
            None,
            MatchStrategy.DEFAULT,
        ),
        # archived contact ignored (email_alternative value specified)
        (
            '<EMAIL>',
            ContactMatchingStatus.unmatched,
            None,
            MatchStrategy.DEFAULT,
        ),
        # same case, match on email
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            False,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # same case, match on email_alternative
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            True,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # different case, match on email
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            False,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # different case, match on email_alternative
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            True,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # different
        (
            '<EMAIL>',
            ContactMatchingStatus.unmatched,
            None,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # duplicate on email
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            None,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # duplicate on email_alternative
        (
            '<EMAIL>',
            ContactMatchingStatus.matched,
            True,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # archived contact ignored (email value specified)
        (
            '<EMAIL>',
            ContactMatchingStatus.unmatched,
            None,
            MatchStrategy.MAX_INTERACTIONS,
        ),
        # archived contact ignored (email_alternative value specified)
        (
            '<EMAIL>',
            ContactMatchingStatus.unmatched,
            None,
            MatchStrategy.MAX_INTERACTIONS,
        ),
    ),
)
def test_find_active_contact_by_email_address(
    email,
    expected_matching_status,
    match_on_alternative,
    match_strategy,
):
    """Test finding a contact by email address for various scenarios."""
    for factory_kwargs in EMAIL_MATCHING_CONTACT_TEST_DATA:
        interaction_count = 0
        if factory_kwargs.get('interactions'):
            interaction_count = factory_kwargs.pop('interactions')
        created_contact = ContactFactory(**factory_kwargs)
        for _ in range(interaction_count):
            CompanyInteractionFactory(contacts=[created_contact])

    contact, actual_matching_status = find_active_contact_by_email_address(email, match_strategy)

    assert actual_matching_status == expected_matching_status

    if actual_matching_status == ContactMatchingStatus.matched:
        assert contact
        actual_email = contact.email_alternative if match_on_alternative else contact.email
        assert actual_email.lower() == email.lower()
    else:
        assert not contact

# same case, match on email # same case, match on email_alternative # different case, match on email # different case, match on email_alternative # different # duplicate on email # duplicate on email_alternative # archived contact ignored (email value specified) # archived contact ignored (email_alternative value specified) # same case, match on email # same case, match on email_alternative # different case, match on email # different case, match on email_alternative # different # duplicate on email # duplicate on email_alternative # archived contact ignored (email value specified) # archived contact ignored (email_alternative value specified) Test finding a contact by email address for various scenarios.