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()
def test_collector(monkeypatch, setup_es): """ Test that the collector collects and deletes all the django objects deleted. """ obj = SimpleModel.objects.create() sync_object_async(ESSimpleModel, SimpleModel, str(obj.pk)) setup_es.indices.refresh() search_app = get_search_app_by_model(SimpleModel) es_doc = ESSimpleModel.es_document(obj) assert SimpleModel.objects.count() == 1 collector = Collector() # check that the post/pre_delete callbacks of SimpleModel are in the collected # signal receivers to disable simplemodel_receivers = [ receiver for receiver in collector.signal_receivers_to_disable if receiver.sender is SimpleModel ] assert simplemodel_receivers assert {receiver.signal for receiver in simplemodel_receivers } == {post_delete, pre_delete} # mock the receiver methods so that we can check they are called for receiver in collector.signal_receivers_to_disable: monkeypatch.setattr(receiver, 'connect', mock.Mock()) monkeypatch.setattr(receiver, 'disconnect', mock.Mock()) collector.connect() # check that the existing signal receivers are disconnected for receiver in collector.signal_receivers_to_disable: assert receiver.disconnect.called assert not receiver.connect.called obj.delete() collector.disconnect() # check that the existing signal receivers are connected back for receiver in collector.signal_receivers_to_disable: assert receiver.connect.called assert collector.deletions == { SimpleModel: [es_doc], } read_alias = search_app.es_model.get_read_alias() assert SimpleModel.objects.count() == 0 assert setup_es.count(read_alias, doc_type=search_app.name)['count'] == 1 collector.delete_from_es() setup_es.indices.refresh() assert setup_es.count(read_alias, doc_type=search_app.name)['count'] == 0
def sync_es_wrapper(): if isinstance(instance, InvestmentProjectTeamMember): pk = instance.investment_project.pk else: pk = instance.pk sync_object_async( ESInvestmentProject, DBInvestmentProject, str(pk), )
def sync_es_wrapper(): queryset = DBInvestmentProject.objects.filter( Q(created_by_id=instance.pk) | Q(client_relationship_manager_id=instance.pk) | Q(project_manager_id=instance.pk) | Q(project_assurance_adviser_id=instance.pk) | Q(team_members__adviser_id=instance.pk), ) for project in queryset: sync_object_async( ESInvestmentProject, DBInvestmentProject, str(project.pk), )
def test_update_es_after_deletions(setup_es): """ Test that the context manager update_es_after_deletions collects and deletes all the django objects deleted. """ obj = SimpleModel.objects.create() sync_object_async(ESSimpleModel, SimpleModel, str(obj.pk)) setup_es.indices.refresh() search_app = get_search_app_by_model(SimpleModel) read_alias = search_app.es_model.get_read_alias() assert SimpleModel.objects.count() == 1 assert setup_es.count(read_alias, doc_type=search_app.name)['count'] == 1 with update_es_after_deletions(): obj.delete() setup_es.indices.refresh() assert setup_es.count(read_alias, doc_type=search_app.name)['count'] == 0
def test_creates_user_event_log_entries(self, setup_es): """Tests that when an export is performed, a user event is recorded.""" user = create_test_user(permission_codenames=['view_simplemodel']) api_client = self.create_api_client(user=user) url = reverse('api-v3:search:simplemodel-export') simple_obj = SimpleModel(name='test') simple_obj.save() sync_object_async(ESSimpleModel, SimpleModel, simple_obj.pk) setup_es.indices.refresh() frozen_time = datetime.datetime(2018, 1, 2, 12, 30, 50, tzinfo=utc) with freeze_time(frozen_time): response = api_client.post( url, data={ 'name': 'test', }, ) assert response.status_code == status.HTTP_200_OK assert UserEvent.objects.count() == 1 user_event = UserEvent.objects.first() assert user_event.adviser == user assert user_event.type == USER_EVENT_TYPES.search_export assert user_event.timestamp == frozen_time assert user_event.api_url_path == '/v3/search/simplemodel/export' assert user_event.data == { 'args': { 'limit': 100, 'name': 'test', 'offset': 0, 'original_query': '', 'sortby': None, }, 'num_results': 1, }
def order_sync_es(sender, instance, **kwargs): """Sync an order to the Elasticsearch.""" transaction.on_commit( lambda: sync_object_async(ESOrder, DBOrder, str(instance.pk)), )
def sync_interaction_to_es(sender, instance, **kwargs): """Sync interaction to the Elasticsearch.""" transaction.on_commit( lambda: sync_object_async(ESInteraction, DBInteraction, str(instance.pk )), )
def sync_event_to_es(sender, instance, **kwargs): """Sync event to the Elasticsearch.""" transaction.on_commit( lambda: sync_object_async(ESEvent, DBEvent, str(instance.pk)), )
def contact_sync_es(sender, instance, **kwargs): """Sync contact to the Elasticsearch.""" transaction.on_commit( lambda: sync_object_async(ESContact, DBContact, str(instance.pk)), )