Ejemplo n.º 1
0
    def setUp(self):
        self.registry = DocumentRegistry()
        self.index_1 = Mock()
        self.index_2 = Mock()

        self.doc_a1 = Mock()
        self.doc_a1._doc_type.model = self.ModelA
        self.doc_a1._doc_type.ignore_signals = False

        self.doc_a2 = Mock()
        self.doc_a2._doc_type.model = self.ModelA
        self.doc_a2._doc_type.ignore_signals = False

        self.doc_b1 = Mock()
        self.doc_b1._doc_type.model = self.ModelB
        self.doc_b1._doc_type.ignore_signals = False

        self.doc_c1 = Mock()
        self.doc_c1._doc_type.model = self.ModelC
        self.doc_c1._doc_type.ignore_signals = False

        self.registry.register(self.index_1, self.doc_a1)
        self.registry.register(self.index_1, self.doc_a2)
        self.registry.register(self.index_2, self.doc_b1)
        self.registry.register(self.index_1, self.doc_c1)
Ejemplo n.º 2
0
    def setUp(self):
        self.out = StringIO()
        self.registry = DocumentRegistry()
        self.index_a = Mock()
        self.index_b = Mock()

        self.doc_a1 = Mock()
        self.doc_a1._doc_type.model = self.ModelA
        self.doc_a1_qs = Mock()
        self.doc_a1.get_queryset = Mock(return_value=self.doc_a1_qs)

        self.doc_a2 = Mock()
        self.doc_a2._doc_type.model = self.ModelA
        self.doc_a2_qs = Mock()
        self.doc_a2.get_queryset = Mock(return_value=self.doc_a2_qs)

        self.doc_b1 = Mock()
        self.doc_b1._doc_type.model = self.ModelB
        self.doc_b1_qs = Mock()
        self.doc_b1.get_queryset = Mock(return_value=self.doc_b1_qs)

        self.doc_c1 = Mock()
        self.doc_c1._doc_type.model = self.ModelC
        self.doc_c1_qs = Mock()
        self.doc_c1.get_queryset = Mock(return_value=self.doc_c1_qs)

        self.registry.register(self.index_a, self.doc_a1)
        self.registry.register(self.index_a, self.doc_a2)
        self.registry.register(self.index_a, self.doc_b1)
        self.registry.register(self.index_b, self.doc_c1)
    def setUp(self):
        self.registry = DocumentRegistry()
        self.index_1 = Index(name='index_1')
        self.index_2 = Index(name='index_2')

        self.doc_a1 = self._generate_doc_mock(self.ModelA, self.index_1)
        self.doc_a2 = self._generate_doc_mock(self.ModelA, self.index_1)
        self.doc_b1 = self._generate_doc_mock(self.ModelB, self.index_2)
        self.doc_c1 = self._generate_doc_mock(self.ModelC, self.index_1)
    def test_documents_add_to_register(self):
        registry = DocumentRegistry()
        with patch('django_elasticsearch_dsl.indices.registry', new=registry):
            index = Index('test')
            index.doc_type(self.DocA1)
            docs = list(registry.get_documents())
            self.assertEqual(len(docs), 1)
            self.assertIsInstance(docs[0], self.DocA1)

            index.doc_type(self.DocA2)
            docs = list(registry.get_documents())
            self.assertEqual(len(docs), 2)
            self.assertIsInstance(docs[0], self.DocA1)
            self.assertIsInstance(docs[1], self.DocA2)
    def setUp(self):
        self.out = StringIO()
        self.registry = DocumentRegistry()
        self.index_a = Index('foo')
        self.index_b = Index('bar')

        self.doc_a1_qs = Mock()
        self.doc_a1 = self._generate_doc_mock(
            self.ModelA, self.index_a, self.doc_a1_qs
        )

        self.doc_a2_qs = Mock()
        self.doc_a2 = self._generate_doc_mock(
            self.ModelA, self.index_a, self.doc_a2_qs
        )

        self.doc_b1_qs = Mock()
        self.doc_b1 = self._generate_doc_mock(
            self.ModelB, self.index_a, self.doc_b1_qs
        )

        self.doc_c1_qs = Mock()
        self.doc_c1 = self._generate_doc_mock(
            self.ModelC, self.index_b, self.doc_c1_qs
        )

        self._mock_setup()
Ejemplo n.º 6
0
    def setUp(self):
        self.out = StringIO()
        search_index.registry = self.registry = DocumentRegistry()
        self.index_a = Mock()
        self.index_a.__str__ = Mock(return_value='index_a')
        self.index_b = Mock()
        self.index_b.__str__ = Mock(return_value='index_b')

        self.doc_a1_qs = Mock()
        self.doc_a1 = self._generate_doc_mock(self.ModelA, self.index_a,
                                              self.doc_a1_qs)

        self.doc_a2_qs = Mock()
        self.doc_a2 = self._generate_doc_mock(self.ModelA, self.index_a,
                                              self.doc_a2_qs)

        self.doc_b1_qs = Mock()
        self.doc_b1 = self._generate_doc_mock(self.ModelB, self.index_a,
                                              self.doc_b1_qs)

        self.doc_c1_qs = Mock()
        self.doc_c1 = self._generate_doc_mock(self.ModelC, self.index_b,
                                              self.doc_c1_qs)

        self.patcher = patch(
            'lily.management.commands.search_index.connections', MagicMock())
        connections = self.patcher.start()
        self.indices_client = connections.get_connection().indices

        def side_effect(value):
            if value.startswith('index_a'):
                return {'index_a.1': {'aliases': ['index_a']}, 'index_a.2': {}}
            else:
                return {'index_b.0': {}}

        self.indices_client.get.side_effect = side_effect
Ejemplo n.º 7
0
class DocumentRegistryTestCase(TestCase):
    class ModelA():
        pass

    class ModelB():
        pass

    class ModelC():
        pass

    def setUp(self):
        self.registry = DocumentRegistry()
        self.index_1 = Mock()
        self.index_2 = Mock()

        self.doc_a1 = Mock()
        self.doc_a1._doc_type.model = self.ModelA
        self.doc_a1._doc_type.ignore_signals = False

        self.doc_a2 = Mock()
        self.doc_a2._doc_type.model = self.ModelA
        self.doc_a2._doc_type.ignore_signals = False

        self.doc_b1 = Mock()
        self.doc_b1._doc_type.model = self.ModelB
        self.doc_b1._doc_type.ignore_signals = False

        self.doc_c1 = Mock()
        self.doc_c1._doc_type.model = self.ModelC
        self.doc_c1._doc_type.ignore_signals = False

        self.registry.register(self.index_1, self.doc_a1)
        self.registry.register(self.index_1, self.doc_a2)
        self.registry.register(self.index_2, self.doc_b1)
        self.registry.register(self.index_1, self.doc_c1)

    def test_empty_registry(self):
        registry = DocumentRegistry()
        self.assertEqual(registry._indices, {})
        self.assertEqual(registry._models, {})

    def test_register(self):
        self.assertEqual(self.registry._models[self.ModelA],
                         set([self.doc_a1, self.doc_a2]))
        self.assertEqual(self.registry._models[self.ModelB],
                         set([self.doc_b1]))

        self.assertEqual(self.registry._indices[self.index_1],
                         set([self.doc_a1, self.doc_a2, self.doc_c1]))
        self.assertEqual(self.registry._indices[self.index_2],
                         set([self.doc_b1]))

    def test_get_models(self):
        self.assertEqual(self.registry.get_models(),
                         set([self.ModelA, self.ModelB, self.ModelC]))

    def test_get_documents(self):
        self.assertEqual(
            self.registry.get_documents(),
            set([self.doc_a1, self.doc_a2, self.doc_b1, self.doc_c1]))

    def test_get_documents_by_model(self):
        self.assertEqual(self.registry.get_documents([self.ModelA]),
                         set([self.doc_a1, self.doc_a2]))

    def test_get_documents_by_unregister_model(self):
        ModelC = Mock()
        self.assertFalse(self.registry.get_documents([ModelC]))

    def test_get_indices(self):
        self.assertEqual(self.registry.get_indices(),
                         set([self.index_1, self.index_2]))

    def test_get_indices_by_model(self):
        self.assertEqual(self.registry.get_indices([self.ModelA]),
                         set([self.index_1]))

    def test_get_indices_by_unregister_model(self):
        ModelC = Mock()
        self.assertFalse(self.registry.get_indices([ModelC]))

    def test_update_instance(self):
        doc_a3 = Mock()
        doc_a3._doc_type.model = self.ModelA
        doc_a3._doc_type.ignore_signals = True

        self.registry.register(self.index_1, doc_a3)

        instance = self.ModelA()
        self.registry.update(instance)

        self.assertFalse(doc_a3.update.called)
        self.assertFalse(self.doc_b1.update.called)
        self.doc_a1.update.assert_called_once_with(instance)
        self.doc_a2.update.assert_called_once_with(instance)

    def test_delete_instance(self):
        doc_a3 = Mock()
        doc_a3._doc_type.model = self.ModelA
        doc_a3._doc_type.ignore_signals = True

        self.registry.register(self.index_1, doc_a3)

        instance = self.ModelA()
        self.registry.delete(instance)

        self.assertFalse(doc_a3.update.called)
        self.assertFalse(self.doc_b1.update.called)
        self.doc_a1.update.assert_called_once_with(instance, action='delete')
        self.doc_a2.update.assert_called_once_with(instance, action='delete')

    def test_autosync(self):
        settings.ELASTICSEARCH_DSL_AUTOSYNC = False

        instance = self.ModelA()
        self.registry.update(instance)
        self.assertFalse(self.doc_a1.update.called)

        settings.ELASTICSEARCH_DSL_AUTOSYNC = True
Ejemplo n.º 8
0
 def test_empty_registry(self):
     registry = DocumentRegistry()
     self.assertEqual(registry._indices, {})
     self.assertEqual(registry._models, {})
Ejemplo n.º 9
0
class SearchIndexTestCase(TestCase):
    class ModelA(models.Model):
        class Meta:
            app_label = 'foo'

    class ModelB(models.Model):
        class Meta:
            app_label = 'foo'

    class ModelC(models.Model):
        class Meta:
            app_label = 'bar'

    def setUp(self):
        self.out = StringIO()
        self.registry = DocumentRegistry()
        self.index_a = Mock()
        self.index_b = Mock()

        self.doc_a1 = Mock()
        self.doc_a1._doc_type.model = self.ModelA
        self.doc_a1_qs = Mock()
        self.doc_a1.get_queryset = Mock(return_value=self.doc_a1_qs)

        self.doc_a2 = Mock()
        self.doc_a2._doc_type.model = self.ModelA
        self.doc_a2_qs = Mock()
        self.doc_a2.get_queryset = Mock(return_value=self.doc_a2_qs)

        self.doc_b1 = Mock()
        self.doc_b1._doc_type.model = self.ModelB
        self.doc_b1_qs = Mock()
        self.doc_b1.get_queryset = Mock(return_value=self.doc_b1_qs)

        self.doc_c1 = Mock()
        self.doc_c1._doc_type.model = self.ModelC
        self.doc_c1_qs = Mock()
        self.doc_c1.get_queryset = Mock(return_value=self.doc_c1_qs)

        self.registry.register(self.index_a, self.doc_a1)
        self.registry.register(self.index_a, self.doc_a2)
        self.registry.register(self.index_a, self.doc_b1)
        self.registry.register(self.index_b, self.doc_c1)

    def test_get_models(self):
        cmd = Command()
        with patch(
                'django_elasticsearch_dsl.management.commands.'
                'search_index.registry', self.registry):
            self.assertEqual(cmd._get_models(['foo']),
                             set([self.ModelA, self.ModelB]))

            self.assertEqual(cmd._get_models(['foo', 'bar.ModelC']),
                             set([self.ModelA, self.ModelB, self.ModelC]))

            self.assertEqual(cmd._get_models([]),
                             set([self.ModelA, self.ModelB, self.ModelC]))
            with self.assertRaises(CommandError):
                cmd._get_models(['unknown'])

    def test_no_action_error(self):
        cmd = Command()
        with self.assertRaises(CommandError):
            cmd.handle(action="")

    def test_delete_foo_index(self):

        with patch(
                'django_elasticsearch_dsl.management.commands.'
                'search_index.registry', self.registry):
            with patch(
                    'django_elasticsearch_dsl.management.commands.'
                    'search_index.input', Mock(return_value="y")):
                call_command('search_index',
                             stdout=self.out,
                             action='delete',
                             models=['foo'])
                self.index_a.delete.assert_called_once()
                self.assertFalse(self.index_b.delete.called)

    def test_force_delete_all_indices(self):

        with patch(
                'django_elasticsearch_dsl.management.commands.'
                'search_index.registry', self.registry):
            call_command('search_index',
                         stdout=self.out,
                         action='delete',
                         force=True)
            self.index_a.delete.assert_called_once()
            self.index_b.delete.assert_called_once()

    def test_force_delete_bar_model_c_index(self):

        with patch(
                'django_elasticsearch_dsl.management.commands.'
                'search_index.registry', self.registry):
            call_command('search_index',
                         stdout=self.out,
                         models=['bar.ModelC'],
                         action='delete',
                         force=True)
            self.index_b.delete.assert_called_once()
            self.assertFalse(self.index_a.delete.called)

    def test_create_all_indices(self):

        with patch(
                'django_elasticsearch_dsl.management.commands.'
                'search_index.registry', self.registry):
            call_command('search_index', stdout=self.out, action='create')
            self.index_a.create.assert_called_once()
            self.index_b.create.assert_called_once()

    def test_populate_all_doc_type(self):

        with patch(
                'django_elasticsearch_dsl.management.commands.'
                'search_index.registry', self.registry):
            call_command('search_index', stdout=self.out, action='populate')
            self.doc_a1.get_queryset.assert_called_once()
            self.doc_a1.update.assert_called_once_with(
                self.doc_a1_qs.iterator())
            self.doc_a2.get_queryset.assert_called_once()
            self.doc_a2.update.assert_called_once_with(
                self.doc_a2_qs.iterator())
            self.doc_b1.get_queryset.assert_called_once()
            self.doc_b1.update.assert_called_once_with(
                self.doc_b1_qs.iterator())
            self.doc_c1.get_queryset.assert_called_once()
            self.doc_c1.update.assert_called_once_with(
                self.doc_c1_qs.iterator())

    def test_rebuild_indices(self):
        Command.old_handle = Command.handle
        with patch.object(Command, 'handle', Mock()) as handle:
            cmd = Command()
            cmd.old_handle(stdout=self.out,
                           action='rebuild',
                           models=['foo'],
                           force=True)
            handle.assert_any_call(stdout=self.out,
                                   action='delete',
                                   models=['foo'],
                                   force=True)
            handle.assert_any_call(stdout=self.out,
                                   action='create',
                                   models=['foo'],
                                   force=True)
            handle.assert_any_call(stdout=self.out,
                                   action='populate',
                                   models=['foo'],
                                   force=True)
class DocumentRegistryTestCase(WithFixturesMixin, TestCase):
    def setUp(self):
        self.registry = DocumentRegistry()
        self.index_1 = Index(name='index_1')
        self.index_2 = Index(name='index_2')

        self.doc_a1 = self._generate_doc_mock(self.ModelA, self.index_1)
        self.doc_a2 = self._generate_doc_mock(self.ModelA, self.index_1)
        self.doc_b1 = self._generate_doc_mock(self.ModelB, self.index_2)
        self.doc_c1 = self._generate_doc_mock(self.ModelC, self.index_1)

    def test_empty_registry(self):
        registry = DocumentRegistry()
        self.assertEqual(registry._indices, {})
        self.assertEqual(registry._models, {})

    def test_register(self):
        self.assertEqual(self.registry._models[self.ModelA],
                         set([self.doc_a1, self.doc_a2]))
        self.assertEqual(self.registry._models[self.ModelB],
                         set([self.doc_b1]))

        self.assertEqual(self.registry._indices[self.index_1],
                         set([self.doc_a1, self.doc_a2, self.doc_c1]))
        self.assertEqual(self.registry._indices[self.index_2],
                         set([self.doc_b1]))

    def test_get_models(self):
        self.assertEqual(self.registry.get_models(),
                         set([self.ModelA, self.ModelB, self.ModelC]))

    def test_get_documents(self):
        self.assertEqual(self.registry.get_documents(),
                         set([self.doc_a1, self.doc_a2,
                              self.doc_b1, self.doc_c1]))

    def test_get_documents_by_model(self):
        self.assertEqual(self.registry.get_documents([self.ModelA]),
                         set([self.doc_a1, self.doc_a2]))

    def test_get_documents_by_unregister_model(self):
        ModelC = Mock()
        self.assertFalse(self.registry.get_documents([ModelC]))

    def test_get_indices(self):
        self.assertEqual(self.registry.get_indices(),
                         set([self.index_1, self.index_2]))

    def test_get_indices_by_model(self):
        self.assertEqual(self.registry.get_indices([self.ModelA]),
                         set([self.index_1]))

    def test_get_indices_by_unregister_model(self):
        ModelC = Mock()
        self.assertFalse(self.registry.get_indices([ModelC]))

    def test_update_instance(self):
        doc_a3 = self._generate_doc_mock(
            self.ModelA, self.index_1, _ignore_signals=True
        )

        instance = self.ModelA()
        self.registry.update(instance)

        self.assertFalse(doc_a3.update.called)
        self.assertFalse(self.doc_b1.update.called)
        self.doc_a1.update.assert_called_once_with(instance)
        self.doc_a2.update.assert_called_once_with(instance)

    def test_update_related_instances(self):
        doc_d1 = self._generate_doc_mock(
            self.ModelD, self.index_1,
            _related_models=[self.ModelE, self.ModelB]
        )
        doc_d2 = self._generate_doc_mock(
            self.ModelD, self.index_1, _related_models=[self.ModelE]
        )

        instance_e = self.ModelE()
        instance_b = self.ModelB()
        related_instance = self.ModelD()

        doc_d2.get_instances_from_related.return_value = related_instance
        doc_d1.get_instances_from_related.return_value = related_instance
        self.registry.update_related(instance_e)

        doc_d1.get_instances_from_related.assert_called_once_with(instance_e)
        doc_d1.update.assert_called_once_with(related_instance)
        doc_d2.get_instances_from_related.assert_called_once_with(instance_e)
        doc_d2.update.assert_called_once_with(related_instance)

        doc_d1.get_instances_from_related.reset_mock()
        doc_d1.update.reset_mock()
        doc_d2.get_instances_from_related.reset_mock()
        doc_d2.update.reset_mock()

        self.registry.update_related(instance_b)
        doc_d1.get_instances_from_related.assert_called_once_with(instance_b)
        doc_d1.update.assert_called_once_with(related_instance)
        doc_d2.get_instances_from_related.assert_not_called()
        doc_d2.update.assert_not_called()

    def test_update_related_instances_not_defined(self):
        doc_d1 = self._generate_doc_mock(_model=self.ModelD, index=self.index_1,
                                         _related_models=[self.ModelE])

        instance = self.ModelE()

        doc_d1.get_instances_from_related.return_value = None
        self.registry.update_related(instance)

        doc_d1.get_instances_from_related.assert_called_once_with(instance)
        doc_d1.update.assert_not_called()

    def test_delete_instance(self):
        doc_a3 = self._generate_doc_mock(
            self.ModelA, self.index_1, _ignore_signals=True
        )

        instance = self.ModelA()
        self.registry.delete(instance)

        self.assertFalse(doc_a3.update.called)
        self.assertFalse(self.doc_b1.update.called)
        self.doc_a1.update.assert_called_once_with(instance, action='delete')
        self.doc_a2.update.assert_called_once_with(instance, action='delete')

    def test_autosync(self):
        settings.ELASTICSEARCH_DSL_AUTOSYNC = False

        instance = self.ModelA()
        self.registry.update(instance)
        self.assertFalse(self.doc_a1.update.called)

        settings.ELASTICSEARCH_DSL_AUTOSYNC = True
Ejemplo n.º 11
0
 def setUp(self):
     self.registry = DocumentRegistry()
Ejemplo n.º 12
0
class DocumentRegistryTestCase(TestCase):
    class ModelA():
        pass

    class ModelB():
        pass

    class ModelC():
        pass

    def _generate_doc_mock(self,
                           model,
                           index=None,
                           ignore_signals=False,
                           related_models=None):
        doc = Mock()
        doc._doc_type.model = model
        doc._doc_type.ignore_signals = ignore_signals
        doc._doc_type.related_models = related_models if (
            related_models) is not None else []

        if index:
            self.registry.register(index, doc)

        return doc

    def setUp(self):
        self.registry = DocumentRegistry()
        self.index_1 = Mock()
        self.index_2 = Mock()

        self.doc_a1 = self._generate_doc_mock(self.ModelA, self.index_1)
        self.doc_a2 = self._generate_doc_mock(self.ModelA, self.index_1)
        self.doc_b1 = self._generate_doc_mock(self.ModelB, self.index_2)
        self.doc_c1 = self._generate_doc_mock(self.ModelC, self.index_1)

    def test_empty_registry(self):
        registry = DocumentRegistry()
        self.assertEqual(registry._indices, {})
        self.assertEqual(registry._models, {})

    def test_register(self):
        self.assertEqual(self.registry._models[self.ModelA],
                         set([self.doc_a1, self.doc_a2]))
        self.assertEqual(self.registry._models[self.ModelB],
                         set([self.doc_b1]))

        self.assertEqual(self.registry._indices[self.index_1],
                         set([self.doc_a1, self.doc_a2, self.doc_c1]))
        self.assertEqual(self.registry._indices[self.index_2],
                         set([self.doc_b1]))

    def test_get_models(self):
        self.assertEqual(self.registry.get_models(),
                         set([self.ModelA, self.ModelB, self.ModelC]))

    def test_get_documents(self):
        self.assertEqual(
            self.registry.get_documents(),
            set([self.doc_a1, self.doc_a2, self.doc_b1, self.doc_c1]))

    def test_get_documents_by_model(self):
        self.assertEqual(self.registry.get_documents([self.ModelA]),
                         set([self.doc_a1, self.doc_a2]))

    def test_get_documents_by_unregister_model(self):
        ModelC = Mock()
        self.assertFalse(self.registry.get_documents([ModelC]))

    def test_get_indices(self):
        self.assertEqual(self.registry.get_indices(),
                         set([self.index_1, self.index_2]))

    def test_get_indices_by_model(self):
        self.assertEqual(self.registry.get_indices([self.ModelA]),
                         set([self.index_1]))

    def test_get_indices_by_unregister_model(self):
        ModelC = Mock()
        self.assertFalse(self.registry.get_indices([ModelC]))

    def test_update_instance(self):
        doc_a3 = self._generate_doc_mock(self.ModelA,
                                         self.index_1,
                                         ignore_signals=True)

        instance = self.ModelA()
        self.registry.update(instance)

        self.assertFalse(doc_a3.update.called)
        self.assertFalse(self.doc_b1.update.called)
        self.doc_a1.update.assert_called_once_with(instance)
        self.doc_a2.update.assert_called_once_with(instance)

    def test_update_related_instances(self):
        class ModelD():
            pass

        class ModelE():
            pass

        doc_d1 = self._generate_doc_mock(ModelD,
                                         self.index_1,
                                         related_models=[ModelE])

        instance = ModelE()
        related_instance = ModelD()

        doc_d1.get_instances_from_related.return_value = related_instance
        self.registry.update(instance)

        doc_d1.get_instances_from_related.assert_called_once_with(instance)
        doc_d1.update.assert_called_once_with(related_instance)

    def test_update_related_isntances_not_defined(self):
        class ModelD():
            pass

        class ModelE():
            pass

        doc_d1 = self._generate_doc_mock(ModelD,
                                         self.index_1,
                                         related_models=[ModelE])

        instance = ModelE()

        doc_d1.get_instances_from_related.return_value = None
        self.registry.update(instance)

        doc_d1.get_instances_from_related.assert_called_once_with(instance)
        doc_d1.update.assert_not_called()

    def test_delete_instance(self):
        doc_a3 = self._generate_doc_mock(self.ModelA,
                                         self.index_1,
                                         ignore_signals=True)

        instance = self.ModelA()
        self.registry.delete(instance)

        self.assertFalse(doc_a3.update.called)
        self.assertFalse(self.doc_b1.update.called)
        self.doc_a1.update.assert_called_once_with(instance, action='delete')
        self.doc_a2.update.assert_called_once_with(instance, action='delete')

    def test_autosync(self):
        settings.ELASTICSEARCH_DSL_AUTOSYNC = False

        instance = self.ModelA()
        self.registry.update(instance)
        self.assertFalse(self.doc_a1.update.called)

        settings.ELASTICSEARCH_DSL_AUTOSYNC = True