Ejemplo n.º 1
0
    def test_fails_if_entities_have_different_duns_numbers(self):
        """
        Test that if the DUNS number of the company is different from the
        one specified in the Worldbase record, a MismatchedRecordsException
        is raised.
        """
        company = CompanyFactory.build(duns_number='987654321')
        wb_record = {
            'DUNS Number': '123456789',
            'Business Name': 'WB Corp',
            'Secondary Name': '',
            'Employees Total': '0',
            'Employees Total Indicator': EmployeesIndicator.NOT_AVAILABLE,
            'Employees Here': '0',
            'Employees Here Indicator': EmployeesIndicator.NOT_AVAILABLE,
            'Annual Sales in US dollars': '0',
            'Annual Sales Indicator': TurnoverIndicator.NOT_AVAILABLE,
            'Street Address': '',
            'Street Address 2': '',
            'City Name': '',
            'State/Province Name': '',
            'Country Code': '785',
            'Postal Code for Street Address': '',
            'National Identification Number': '',
            'National Identification System Code': '',
            'Out of Business indicator':
            OutOfBusinessIndicator.OUT_OF_BUSINESS,
        }

        with pytest.raises(MismatchedRecordsException) as excinfo:
            update_company_from_wb_record(company, wb_record)

        assert str(excinfo.value) == (
            'The Worldbase record with DUNS number 123456789 cannot be used to '
            f'update company {company.id} with DUNS number 987654321.')
Ejemplo n.º 2
0
    def test_with_already_populated_billing_address(self, billing_address):
        """
        Test that if the order has some billing address fields already populated,
        none of the address fields get overridden.
        """
        company = CompanyFactory.build(
            address_1='1',
            address_2='Hello st.',
            address_town='Muckamore',
            address_county='Antrim',
            address_postcode='BT41 4QE',
            address_country_id=Country.ireland.value.id,
        )
        order = OrderFactory.build(
            company=company,
            **billing_address,
        )

        populate_billing_data(order)

        # check that the fields didn't get overridden
        actual_billing_address = {
            field_name: getattr(order, field_name)
            for field_name in billing_address
        }
        assert actual_billing_address == billing_address
Ejemplo n.º 3
0
    def test_with_empty_order(self, initial_model_values,
                              expected_billing_address_fields):
        """
        Test that an order without any of the billing fields filled in is populated
        with the company/contact details.
        """
        company = CompanyFactory.build(**initial_model_values)
        order = OrderFactory.build(
            billing_company_name='',
            billing_contact_name='',
            billing_email='',
            billing_phone='',
            billing_address_1='',
            billing_address_2='',
            billing_address_town='',
            billing_address_county='',
            billing_address_postcode='',
            billing_address_country=None,
            company=company,
            contact=ContactFactory.build(),
        )

        populate_billing_data(order)

        assert not order.billing_contact_name
        assert not order.billing_email
        assert not order.billing_phone
        assert order.billing_company_name == company.name

        actual_billing_address = {
            field_name: getattr(order, field_name)
            for field_name in expected_billing_address_fields
        }
        assert actual_billing_address == expected_billing_address_fields
Ejemplo n.º 4
0
    def test_get_group_global_headquarters(self, build_global_headquarters):
        """
        Test that `get_group_global_headquarters` returns `self` if the company has
        no `global_headquarters` or the `global_headquarters` otherwise.
        """
        company = CompanyFactory.build(
            global_headquarters=build_global_headquarters(),
        )

        expected_group_global_headquarters = company.global_headquarters or company
        assert company.get_group_global_headquarters() == expected_group_global_headquarters
Ejemplo n.º 5
0
    def test(self, initial_model_values, expected_address_values):
        """
        Test that registered address is used if defined or address otherwise.
        """
        company = CompanyFactory.build(**initial_model_values)
        address = compose_official_address(company)

        actual_address = {
            attr_name: attrgetter(attr_name)(address)
            for attr_name in expected_address_values
        }
        assert actual_address == expected_address_values
Ejemplo n.º 6
0
def get_required_company_form_data(company=None):
    """
    :returns: dict with all required fields to use in tests when posting
    to an add/edit admin company url.
    """
    if not company:
        company = CompanyFactory.build()

    data = {
        'one_list_core_team_members-TOTAL_FORMS': 0,
        'one_list_core_team_members-INITIAL_FORMS': 0,
        'one_list_core_team_members-MIN_NUM_FORMS': 0,
        'one_list_core_team_members-MAX_NUM_FORMS': 1000,
    }
    admin_form = CompanyAdmin(Company, site).get_form(mock.Mock())
    for field_name, field in admin_form.base_fields.items():
        if field.required:
            field_value = getattr(company, field_name)
            data[field_name] = field.prepare_value(field_value)
    return data
Ejemplo n.º 7
0
 def test_get_absolute_url(self):
     """Test that Company.get_absolute_url() returns the correct URL."""
     company = CompanyFactory.build()
     assert company.get_absolute_url() == (
         f'{settings.DATAHUB_FRONTEND_URL_PREFIXES["company"]}/{company.pk}'
     )
Ejemplo n.º 8
0
class TestCompany:
    """Tests for the company model."""

    def test_get_absolute_url(self):
        """Test that Company.get_absolute_url() returns the correct URL."""
        company = CompanyFactory.build()
        assert company.get_absolute_url() == (
            f'{settings.DATAHUB_FRONTEND_URL_PREFIXES["company"]}/{company.pk}'
        )

    @pytest.mark.parametrize(
        'build_global_headquarters',
        (
            lambda: CompanyFactory.build(),
            lambda: None,
        ),
        ids=('as_subsidiary', 'as_non_subsidiary'),
    )
    def test_get_group_global_headquarters(self, build_global_headquarters):
        """
        Test that `get_group_global_headquarters` returns `self` if the company has
        no `global_headquarters` or the `global_headquarters` otherwise.
        """
        company = CompanyFactory.build(
            global_headquarters=build_global_headquarters(),
        )

        expected_group_global_headquarters = company.global_headquarters or company
        assert company.get_group_global_headquarters() == expected_group_global_headquarters

    @pytest.mark.parametrize(
        'build_company',
        (
            # subsidiary with Global Headquarters on the One List
            lambda one_list_tier: CompanyFactory(
                one_list_tier=None,
                global_headquarters=CompanyFactory(one_list_tier=one_list_tier),
            ),
            # subsidiary with Global Headquarters not on the One List
            lambda one_list_tier: CompanyFactory(
                one_list_tier=None,
                global_headquarters=CompanyFactory(one_list_tier=None),
            ),
            # single company on the One List
            lambda one_list_tier: CompanyFactory(
                one_list_tier=one_list_tier,
                global_headquarters=None,
            ),
            # single company not on the One List
            lambda one_list_tier: CompanyFactory(
                one_list_tier=None,
                global_headquarters=None,
            ),
        ),
        ids=(
            'as_subsidiary_of_one_list_company',
            'as_subsidiary_of_non_one_list_company',
            'as_one_list_company',
            'as_non_one_list_company',
        ),
    )
    def test_get_one_list_group_tier(self, build_company):
        """
        Test that `get_one_list_group_tier` returns the One List Tier of `self`
        if company has no `global_headquarters` or the one of its `global_headquarters`
        otherwise.
        """
        one_list_tier = OneListTier.objects.first()

        company = build_company(one_list_tier)

        group_global_headquarters = company.global_headquarters or company
        if not group_global_headquarters.one_list_tier:
            assert not company.get_one_list_group_tier()
        else:
            assert company.get_one_list_group_tier() == one_list_tier

    @pytest.mark.parametrize(
        'build_company',
        (
            # as subsidiary
            lambda gam: CompanyFactory(
                global_headquarters=CompanyFactory(one_list_account_owner=gam),
            ),
            # as single company
            lambda gam: CompanyFactory(
                global_headquarters=None,
                one_list_account_owner=gam,
            ),
        ),
        ids=('as_subsidiary', 'as_non_subsidiary'),
    )
    @pytest.mark.parametrize(
        'with_global_account_manager',
        (True, False),
        ids=lambda val: f'{"With" if val else "Without"} global account manager',
    )
    def test_get_one_list_group_core_team(
        self,
        build_company,
        with_global_account_manager,
    ):
        """
        Test that `get_one_list_group_core_team` returns the Core Team of `self` if the company
        has no `global_headquarters` or the one of its `global_headquarters` otherwise.
        """
        team_member_advisers = AdviserFactory.create_batch(
            3,
            first_name=factory.Iterator(
                ('Adam', 'Barbara', 'Chris'),
            ),
        )
        global_account_manager = team_member_advisers[0] if with_global_account_manager else None

        company = build_company(global_account_manager)
        group_global_headquarters = company.global_headquarters or company

        OneListCoreTeamMemberFactory.create_batch(
            len(team_member_advisers),
            company=group_global_headquarters,
            adviser=factory.Iterator(team_member_advisers),
        )

        core_team = company.get_one_list_group_core_team()
        assert core_team == [
            {
                'adviser': adviser,
                'is_global_account_manager': adviser is global_account_manager,
            }
            for adviser in team_member_advisers
        ]

    @pytest.mark.parametrize(
        'build_company',
        (
            # subsidiary with Global Headquarters on the One List
            lambda one_list_tier, gam: CompanyFactory(
                one_list_tier=None,
                global_headquarters=CompanyFactory(
                    one_list_tier=one_list_tier,
                    one_list_account_owner=gam,
                ),
            ),
            # subsidiary with Global Headquarters not on the One List
            lambda one_list_tier, gam: CompanyFactory(
                one_list_tier=None,
                global_headquarters=CompanyFactory(
                    one_list_tier=None,
                    one_list_account_owner=None,
                ),
            ),
            # single company on the One List
            lambda one_list_tier, gam: CompanyFactory(
                one_list_tier=one_list_tier,
                one_list_account_owner=gam,
                global_headquarters=None,
            ),
            # single company not on the One List
            lambda one_list_tier, gam: CompanyFactory(
                one_list_tier=None,
                global_headquarters=None,
                one_list_account_owner=None,
            ),
        ),
        ids=(
            'as_subsidiary_of_one_list_company',
            'as_subsidiary_of_non_one_list_company',
            'as_one_list_company',
            'as_non_one_list_company',
        ),
    )
    def test_get_one_list_group_global_account_manager(self, build_company):
        """
        Test that `get_one_list_group_global_account_manager` returns
        the One List Global Account Manager of `self` if the company has no
        `global_headquarters` or the one of its `global_headquarters` otherwise.
        """
        global_account_manager = AdviserFactory()
        one_list_tier = OneListTier.objects.first()

        company = build_company(one_list_tier, global_account_manager)

        group_global_headquarters = company.global_headquarters or company
        actual_global_account_manager = company.get_one_list_group_global_account_manager()
        assert group_global_headquarters.one_list_account_owner == actual_global_account_manager