Exemple #1
0
    def test_get_document_id(self):
        # The base class doesn't implement this at all
        with self.assertRaises(NotImplementedError):
            ElasticsearchIndexMixin.get_document_id(object())

        # The Django (and Peewee) versions do - but of course
        # an object without the right attribute will fail.
        with self.assertRaises(MissingObjectError):
            DjangoElasticsearchIndexMixin.get_document_id(object())

        with self.assertRaises(MissingObjectError):
            PeeweeElasticsearchIndexMixin.get_document_id(object())

        # Now we create objects we expect to succeed:
        class Foo(object):
            pass
        foo = Foo()
        foo.pk = 1
        self.assertEqual(
            DjangoElasticsearchIndexMixin.get_document_id(foo),
            1
        )

        class Bar(object):
            pass
        bar = Bar()
        bar._meta = Bar()
        bar._meta.primary_key = Bar()
        bar._meta.primary_key.name = 'baz'
        bar.baz = 1
        self.assertEqual(
            PeeweeElasticsearchIndexMixin.get_document_id(bar),
            1
        )
Exemple #2
0
    def test_get_type_name(self):
        self.assertEqual(
            ElasticsearchIndexMixin.get_type_name(),
            'ElasticsearchIndexMixin'
        )

        class Object(object):
            pass
        DjangoElasticsearchIndexMixin._meta = Object()
        DjangoElasticsearchIndexMixin._meta.app_label = 'foo'
        DjangoElasticsearchIndexMixin._meta.model_name = 'bar'
        self.assertEqual(
            DjangoElasticsearchIndexMixin.get_type_name(),
            'foo.bar'
        )

        PeeweeElasticsearchIndexMixin.model = Object()
        PeeweeElasticsearchIndexMixin.model._meta = Object()
        PeeweeElasticsearchIndexMixin.model._meta.name = 'foo'
        self.assertEqual(
            PeeweeElasticsearchIndexMixin.get_type_name(),
            'foo'
        )