Example #1
0
def setup_data(setup_es):
    """Sets up data for the tests."""
    companies = (
        CompaniesHouseCompanyFactory(
            name='Pallas',
            company_number='111',
            incorporation_date=dateutil_parse('2012-09-12T00:00:00Z'),
            company_status='jumping',
        ),
        CompaniesHouseCompanyFactory(
            name='Jaguarundi',
            company_number='222',
            incorporation_date=dateutil_parse('2015-09-12T00:00:00Z'),
            company_status='sleeping',
        ),
        CompaniesHouseCompanyFactory(
            name='Cheetah',
            company_number='333',
            incorporation_date=dateutil_parse('2016-09-12T00:00:00Z'),
            company_status='purring',
        ),
    )

    for company in companies:
        sync_object_async(ESCompaniesHouseCompany, DBCompaniesHouseCompany,
                          company.pk)

    setup_es.indices.refresh()
Example #2
0
    def test_list_ch_companies(self):
        """List the companies house companies."""
        CompaniesHouseCompanyFactory()
        CompaniesHouseCompanyFactory()

        url = reverse('api-v3:ch-company:collection')
        response = self.api_client.get(url)

        assert response.status_code == status.HTTP_200_OK
        assert response.json()['count'] == CompaniesHouseCompany.objects.all(
        ).count()
def test_indexed_doc(setup_es):
    """Test the ES data of an indexed companies house company."""
    ch_company = CompaniesHouseCompanyFactory()

    doc = ESCompaniesHouseCompany.es_document(ch_company)
    elasticsearch.bulk(actions=(doc, ), chunk_size=1)

    setup_es.indices.refresh()

    indexed_ch_company = setup_es.get(
        index=ESCompaniesHouseCompany.get_write_index(),
        doc_type=CompaniesHouseCompanySearchApp.name,
        id=ch_company.pk,
    )

    assert indexed_ch_company == {
        '_index': ESCompaniesHouseCompany.get_target_index_name(),
        '_type': CompaniesHouseCompanySearchApp.name,
        '_id': str(ch_company.pk),
        '_version': indexed_ch_company['_version'],
        'found': True,
        '_source': {
            'id':
            str(ch_company.pk),
            'name':
            ch_company.name,
            'registered_address_1':
            ch_company.registered_address_1,
            'registered_address_2':
            ch_company.registered_address_2,
            'registered_address_town':
            ch_company.registered_address_town,
            'registered_address_county':
            ch_company.registered_address_county,
            'registered_address_postcode':
            ch_company.registered_address_postcode,
            'registered_address_country': {
                'id': str(ch_company.registered_address_country.pk),
                'name': ch_company.registered_address_country.name,
            },
            'company_number':
            ch_company.company_number,
            'company_category':
            ch_company.company_category,
            'company_status':
            ch_company.company_status,
            'sic_code_1':
            ch_company.sic_code_1,
            'sic_code_2':
            ch_company.sic_code_2,
            'sic_code_3':
            ch_company.sic_code_3,
            'sic_code_4':
            ch_company.sic_code_4,
            'uri':
            ch_company.uri,
            'incorporation_date':
            format_date_or_datetime(ch_company.incorporation_date),
        },
    }
Example #4
0
    def test_promoting_a_ch_company_creates_a_new_version(self):
        """Test that promoting a CH company to full company creates a new version."""
        assert Version.objects.count() == 0
        CompaniesHouseCompanyFactory(company_number=1234567890)

        response = self.api_client.post(
            reverse('api-v4:company:collection'),
            data={
                'name': 'Acme',
                'company_number': 1234567890,
                'business_type': BusinessTypeConstant.company.value.id,
                'sector': random_obj_for_model(Sector).id,
                'address': {
                    'line_1': '75 Stramford Road',
                    'town': 'London',
                    'country': {
                        'id': Country.united_kingdom.value.id,
                    },
                },
                'uk_region': UKRegion.england.value.id,
            },
        )

        assert response.status_code == status.HTTP_201_CREATED

        company = Company.objects.get(pk=response.json()['id'])

        # check version created
        assert Version.objects.get_for_object(company).count() == 1
        version = Version.objects.get_for_object(company).first()
        assert version.field_dict['company_number'] == '1234567890'
Example #5
0
    def test_response_body(self, setup_es):
        """Tests the response body of a search query."""
        company = CompaniesHouseCompanyFactory(
            name='Pallas',
            company_number='111',
            incorporation_date=dateutil_parse('2012-09-12T00:00:00Z'),
            company_status='jumping',
        )
        sync_object(CompaniesHouseCompanySearchApp, company.pk)
        setup_es.indices.refresh()

        url = reverse('api-v3:search:companieshousecompany')
        response = self.api_client.post(url)

        assert response.status_code == status.HTTP_200_OK
        assert response.json() == {
            'count':
            1,
            'results': [
                {
                    'id':
                    str(company.pk),
                    'name':
                    company.name,
                    'company_category':
                    company.company_category,
                    'incorporation_date':
                    company.incorporation_date.date().isoformat(),
                    'company_number':
                    company.company_number,
                    'company_status':
                    company.company_status,
                    'registered_address_1':
                    company.registered_address_1,
                    'registered_address_2':
                    company.registered_address_2,
                    'registered_address_town':
                    company.registered_address_town,
                    'registered_address_county':
                    company.registered_address_county,
                    'registered_address_postcode':
                    company.registered_address_postcode,
                    'registered_address_country': {
                        'id': str(company.registered_address_country.id),
                        'name': company.registered_address_country.name,
                    },
                    'sic_code_1':
                    company.sic_code_1,
                    'sic_code_2':
                    company.sic_code_2,
                    'sic_code_3':
                    company.sic_code_3,
                    'sic_code_4':
                    company.sic_code_4,
                    'uri':
                    company.uri,
                },
            ],
        }
    def test_ch_item_cannot_be_written(self, verb):
        """Test verbs not allowed on CH company item endpoint."""
        ch_company = CompaniesHouseCompanyFactory()
        url = reverse(
            'api-v4:ch-company:item',
            kwargs={'company_number': ch_company.company_number},
        )
        response = getattr(self.api_client, verb)(url)

        assert response.status_code == status.HTTP_403_FORBIDDEN
Example #7
0
class CompanyWithRegAddressFactory(CompanyFactory):
    """Factory for a company with all registered address filled in."""

    registered_address_1 = factory.Faker('text')
    registered_address_2 = factory.Faker('text')
    registered_address_town = factory.Faker('text')
    registered_address_county = factory.Faker('text')
    registered_address_postcode = factory.Faker('text')
    registered_address_country_id = constants.Country.japan.value.id
    company_number = factory.LazyFunction(
        lambda: CompaniesHouseCompanyFactory().company_number, )
Example #8
0
    def test_get_ch_company_alphanumeric(self):
        """Test retrieving a single CH company where the company number contains letters."""
        CompaniesHouseCompanyFactory(company_number='SC00001234')
        url = reverse(
            'api-v3:ch-company:item',
            kwargs={'company_number': 'SC00001234'},
        )
        response = self.api_client.get(url)

        assert response.status_code == status.HTTP_200_OK
        response_data = response.json()
        assert response_data['company_number'] == 'SC00001234'
Example #9
0
    def test_get_ch_company(self):
        """Test retrieving a single CH company."""
        ch_company = CompaniesHouseCompanyFactory()
        url = reverse(
            'api-v3:ch-company:item',
            kwargs={'company_number': ch_company.company_number},
        )
        response = self.api_client.get(url)

        assert response.status_code == status.HTTP_200_OK
        response_data = response.json()
        assert response_data == {
            'id':
            ch_company.id,
            'business_type': {
                'id': str(ch_company.business_type.id),
                'name': ch_company.business_type.name,
            },
            'company_number':
            ch_company.company_number,
            'company_category':
            ch_company.company_category,
            'company_status':
            ch_company.company_status,
            'incorporation_date':
            format_date_or_datetime(ch_company.incorporation_date),
            'name':
            ch_company.name,
            'registered_address_1':
            ch_company.registered_address_1,
            'registered_address_2':
            ch_company.registered_address_2,
            'registered_address_town':
            ch_company.registered_address_town,
            'registered_address_county':
            ch_company.registered_address_county,
            'registered_address_postcode':
            ch_company.registered_address_postcode,
            'registered_address_country': {
                'id': str(ch_company.registered_address_country.id),
                'name': ch_company.registered_address_country.name,
            },
            'sic_code_1':
            ch_company.sic_code_1,
            'sic_code_2':
            ch_company.sic_code_2,
            'sic_code_3':
            ch_company.sic_code_3,
            'sic_code_4':
            ch_company.sic_code_4,
            'uri':
            ch_company.uri,
        }
    def test_list(self):
        """Test listing CH companies."""
        companies = CompaniesHouseCompanyFactory.create_batch(2)

        url = reverse('api-v4:ch-company:collection')
        response = self.api_client.get(url)

        assert response.status_code == status.HTTP_200_OK
        response_data = response.json()
        assert response_data['count'] == len(companies)

        actual_ids = {result['id'] for result in response_data['results']}
        expected_ids = {company.id for company in companies}
        assert actual_ids == expected_ids
Example #11
0
    def test_content(self):
        """Test that the quote content is populated as expected."""
        hourly_rate = HourlyRateFactory(rate_value=1250,
                                        vat_value=Decimal(17.5))
        company = CompanyFactory(
            name='My *Coorp',
            registered_address_1='line 1',
            registered_address_2='*line 2',
            registered_address_town='London',
            registered_address_county='County',
            registered_address_postcode='SW1A 1AA',
            registered_address_country_id=Country.united_kingdom.value.id,
            company_number=CompaniesHouseCompanyFactory().company_number,
        )
        contact = ContactFactory(
            company=company,
            first_name='John',
            last_name='*Doe',
        )
        order = OrderFactory(
            delivery_date=dateutil_parse('2017-06-20'),
            company=company,
            contact=contact,
            reference='ABC123',
            primary_market_id=Country.france.value.id,
            description='lorem *ipsum',
            discount_value=100,
            hourly_rate=hourly_rate,
            assignees=[],
            vat_status=VATStatus.uk,
            contact_email='*****@*****.**',
        )
        OrderAssigneeFactory(
            order=order,
            adviser=AdviserFactory(
                first_name='Foo',
                last_name='*Bar',
            ),
            estimated_time=150,
            is_lead=True,
        )

        content = generate_quote_content(
            order=order,
            expires_on=dateutil_parse('2017-05-18').date(),
        )
        with open(COMPILED_QUOTE_TEMPLATE, 'r', encoding='utf-8') as f:
            expected_content = f.read()

        assert content == expected_content
def setup_data(setup_es):
    """Sets up data for the tests."""
    companies = (
        CompaniesHouseCompanyFactory(
            name='Pallas',
            company_number='111',
            incorporation_date=dateutil_parse('2012-09-12T00:00:00Z'),
            registered_address_postcode='SW1A 1AA',
            company_status='jumping',
        ),
        CompaniesHouseCompanyFactory(
            name='Jaguarundi',
            company_number='222',
            incorporation_date=dateutil_parse('2015-09-12T00:00:00Z'),
            registered_address_postcode='E1 6JE',
            company_status='sleeping',
        ),
        CompaniesHouseCompanyFactory(
            name='Cheetah',
            company_number='333',
            incorporation_date=dateutil_parse('2016-09-12T00:00:00Z'),
            registered_address_postcode='SW1A 0PW',
            company_status='purring',
        ),
        CompaniesHouseCompanyFactory(
            name='Pallas Second',
            company_number='444',
            incorporation_date=dateutil_parse('2019-09-12T00:00:00Z'),
            registered_address_postcode='WC1B 3DG',
            company_status='crying',
        ),
    )

    for company in companies:
        sync_object(CompaniesHouseCompanySearchApp, company.pk)

    setup_es.indices.refresh()
Example #13
0
    def test_ignored_models_excluded_from_aggregations(self, setup_es):
        """That that companieshousecompany is not included in aggregations."""
        ch_company = CompaniesHouseCompanyFactory()
        sync_object(CompaniesHouseCompanySearchApp, ch_company.pk)

        setup_es.indices.refresh()

        url = reverse('api-v3:search:basic')
        response = self.api_client.get(
            url,
            data={
                'term': '',
            },
        )

        assert response.status_code == status.HTTP_200_OK
        response_data = response.json()
        assert response_data['count'] == 0
        assert response_data['aggregations'] == []
Example #14
0
def test_process_rows_record():
    """Tests that records are updated or inserted as needed."""
    existing_ch_company = CompaniesHouseCompanyFactory(
        company_number='00000001',
        name='old name',
    )

    existing_record_update_data = {
        'company_number': '00000001',
        'company_category': 'company cat 1',
        'company_status': 'status 1',
        'incorporation_date': date(2017, 1, 1),
        'name': 'name 1',
        'registered_address_1': 'add 1-1',
        'registered_address_2': 'add 1-2',
        'registered_address_country_id': UUID(Country.united_kingdom.value.id),
        'registered_address_county': 'county 1',
        'registered_address_postcode': 'postcode 1',
        'registered_address_town': 'town 1',
        'sic_code_1': 'sic code 1-1',
        'sic_code_2': 'sic code 1-2',
        'sic_code_3': 'sic code 1-3',
        'sic_code_4': 'sic code 1-4',
        'uri': 'uri 1',
    }
    new_record_data = {
        'company_number': '00000002',
        'company_category': 'company cat 2',
        'company_status': 'status 2',
        'incorporation_date': date(2016, 12, 1),
        'name': 'name 2',
        'registered_address_1': 'add 2-1',
        'registered_address_2': 'add 2-2',
        'registered_address_country_id': UUID(Country.united_kingdom.value.id),
        'registered_address_county': 'county 2',
        'registered_address_postcode': 'postcode 2',
        'registered_address_town': 'town 2',
        'sic_code_1': 'sic code 2-1',
        'sic_code_2': 'sic code 2-2',
        'sic_code_3': 'sic code 2-3',
        'sic_code_4': 'sic code 2-4',
        'uri': 'uri 2',
    }

    with connection.cursor() as cursor:
        syncer = sync_ch.CHSynchroniser()
        syncer._process_batch(cursor,
                              [existing_record_update_data, new_record_data])

    existing_ch_company.refresh_from_db()
    actual_updated_data = {
        field: getattr(existing_ch_company, field)
        for field in existing_record_update_data
    }

    new_ch_company = CompaniesHouseCompany.objects.get(
        company_number=new_record_data['company_number'], )
    actual_new_data = {
        field: getattr(new_ch_company, field)
        for field in new_record_data
    }

    assert CompaniesHouseCompany.objects.count() == 2
    assert actual_updated_data == existing_record_update_data
    assert actual_new_data == new_record_data
    def test_response_body(self, setup_es):
        """Tests the response body of a search query."""
        ch_company = CompaniesHouseCompanyFactory(company_number='123', )
        company = CompanyFactory(
            company_number='123',
            trading_names=['Xyz trading', 'Abc trading'],
            global_headquarters=None,
            one_list_tier=None,
            one_list_account_owner=None,
        )
        setup_es.indices.refresh()

        url = reverse('api-v3:search:company')
        response = self.api_client.post(url)

        assert response.status_code == status.HTTP_200_OK
        assert response.json() == {
            'count':
            1,
            'results': [
                {
                    'id':
                    str(company.pk),
                    'companies_house_data': {
                        'id': str(ch_company.id),
                        'company_number': ch_company.company_number,
                    },
                    'created_on':
                    company.created_on.isoformat(),
                    'modified_on':
                    company.modified_on.isoformat(),
                    'name':
                    company.name,
                    'reference_code':
                    company.reference_code,
                    'company_number':
                    company.company_number,
                    'vat_number':
                    company.vat_number,
                    'duns_number':
                    company.duns_number,
                    'trading_names':
                    company.trading_names,
                    'registered_address_1':
                    company.registered_address_1,
                    'registered_address_2':
                    company.registered_address_2,
                    'registered_address_town':
                    company.registered_address_town,
                    'registered_address_county':
                    company.registered_address_county,
                    'registered_address_postcode':
                    company.registered_address_postcode,
                    'registered_address_country': {
                        'id': str(Country.united_kingdom.value.id),
                        'name': Country.united_kingdom.value.name,
                    },
                    'trading_address_1':
                    company.trading_address_1,
                    'trading_address_2':
                    company.trading_address_2,
                    'trading_address_country': {
                        'id': str(company.trading_address_country.id),
                        'name': company.trading_address_country.name,
                    },
                    'trading_address_county':
                    company.trading_address_county,
                    'trading_address_postcode':
                    company.trading_address_postcode,
                    'trading_address_town':
                    company.trading_address_town,
                    'uk_based': (company.address_country.id == uuid.UUID(
                        Country.united_kingdom.value.id)),
                    'uk_region': {
                        'id': str(company.uk_region.id),
                        'name': company.uk_region.name,
                    },
                    'business_type': {
                        'id': str(company.business_type.id),
                        'name': company.business_type.name,
                    },
                    'description':
                    company.description,
                    'employee_range': {
                        'id': str(company.employee_range.id),
                        'name': company.employee_range.name,
                    },
                    'export_experience_category': {
                        'id': str(company.export_experience_category.id),
                        'name': company.export_experience_category.name,
                    },
                    'export_to_countries': [],
                    'future_interest_countries': [],
                    'headquarter_type':
                    company.headquarter_type,
                    'sector': {
                        'id':
                        str(company.sector.id),
                        'name':
                        company.sector.name,
                        'ancestors': [{
                            'id': str(ancestor.id)
                        } for ancestor in company.sector.get_ancestors()],
                    },
                    'turnover_range': {
                        'id': str(company.turnover_range.id),
                        'name': company.turnover_range.name,
                    },
                    'website':
                    company.website,
                    'global_headquarters':
                    None,
                    'archived':
                    False,
                    'archived_by':
                    None,
                    'archived_on':
                    None,
                    'archived_reason':
                    None,
                    'suggest':
                    get_suggestions(company),
                },
            ],
        }