def test_missing_settings_error(self): """ Test when environment variables are not set an exception is thrown. """ company = CompanyFactory() with pytest.raises(ImproperlyConfigured): match_company(company)
def test_model_to_match_payload( self, requests_mock, build_company, expected_result, ): """ Test that the function maps the Company object to JSON correctly also stripping out falsy values. """ company = build_company() dynamic_response = HawkMockJSONResponse( api_id=settings.COMPANY_MATCHING_HAWK_ID, api_key=settings.COMPANY_MATCHING_HAWK_KEY, ) matcher = requests_mock.post( '/api/v1/company/match/', status_code=status.HTTP_200_OK, text=dynamic_response, ) match_company(company) assert matcher.called_once assert matcher.last_request.json() == { 'descriptions': [expected_result], }
def test_company_matching_service_error( self, requests_mock, response_status, ): """Test if the company matching service returns a status code that is not 200.""" requests_mock.post( '/api/v1/company/match/', status_code=response_status, ) company = CompanyFactory() with pytest.raises( CompanyMatchingServiceHTTPError, match=f'The Company matching service returned an error status: {response_status}', ): match_company(company)
def test_company_matching_service_request_error( self, requests_mock, request_exception, expected_exception, ): """ Test if there is an error connecting to company matching service the expected exception was thrown. """ requests_mock.post( '/api/v1/company/match/', exc=request_exception, ) company = CompanyFactory() with pytest.raises(expected_exception): match_company(company)
def _get_match_ids(self, target_company, request=None): """ Returns match ids matching the company and all companies that were merged into it, if there are any. """ companies = [target_company] for company in target_company.transferred_from.all(): companies.append(company) matching_response = match_company(companies, request) return self._extract_match_ids(matching_response)