Exemple #1
0
class SnippetDocument(DocType):
    """
    Document for the Snippet model for elasticsearch with fields to index
    """
    desc = TextField(
        attr='desc',
        fields={
            'suggest': Completion(),
        }
    )
    user = ObjectField(
        properties={
            'username': TextField(),
            'id': IntegerField(),
        }
    )

    class Django:
        model = Snippet
        fields = [
            'id',
            'is_visible'
        ]

        related_models = [settings.AUTH_USER_MODEL]

    def get_queryset(self):
        return super().get_queryset().select_related(
            'user'
        )

    def get_instances_from_related(self, related_instance):
        if isinstance(related_instance, settings.AUTH_USER_MODEL):
            return related_instance.snippet_set.all()
    def test_get_value_from_iterable(self):
        field = ObjectField(attr='person', properties={
            'first_name': TextField(analyzer='foo'),
            'last_name': TextField()
        })

        instance = NonCallableMock(
            person=[
                NonCallableMock(
                    first_name="foo1", last_name="bar1"
                ),
                NonCallableMock(
                    first_name="foo2", last_name="bar2"
                )
            ]
        )

        self.assertEqual(field.get_value_from_instance(instance), [
            {
                'first_name': "foo1",
                'last_name': "bar1",
            },
            {
                'first_name': "foo2",
                'last_name': "bar2",
            }
        ])
    def test_get_value_from_instance(self):
        field = ObjectField(attr='person', properties={
            'first_name': TextField(analyzer='foo'),
            'last_name': TextField()
        })

        instance = NonCallableMock(person=NonCallableMock(
            first_name='foo', last_name='bar'))

        self.assertEqual(field.get_value_from_instance(instance), {
            'first_name': "foo",
            'last_name': "bar",
        })
    def test_get_mapping(self):
        field = ObjectField(attr='person', properties={
            'first_name': TextField(analyzer='foo'),
            'last_name': TextField()
        })

        expected_type = 'string' if ES_MAJOR_VERSION == 2 else 'text'

        self.assertEqual({
            'type': 'object',
            'properties': {
                'first_name': {'type': expected_type, 'analyzer': 'foo'},
                'last_name': {'type': expected_type},
            }
        }, field.to_dict())
 def test_get_value_from_instance(self):
     instance = NonCallableMock(
         foo=NonCallableMock(bar=["alpha", "beta", "gamma"])
     )
     field = ListField(TextField(attr='foo.bar'))
     self.assertEqual(
         field.get_value_from_instance(instance), instance.foo.bar)
    def test_get_mapping(self):
        field = TextField()

        expected_type = 'string' if ES_MAJOR_VERSION == 2 else 'text'

        self.assertEqual({
            'type': expected_type,
        }, field.to_dict())
    def test_get_value_from_instance_with_none_inner_objectfield(self):
        field = ObjectField(attr='person', properties={
            'first_name': TextField(analyzer='foo'),
            'last_name': TextField(),
            'additional': ObjectField(properties={
                'age': IntegerField()
            })
        })

        instance = NonCallableMock(person=NonCallableMock(
            first_name="foo", last_name="bar",
            additional=None
        ))

        self.assertEqual(field.get_value_from_instance(instance), {
            'first_name': "foo",
            'last_name': "bar",
            'additional': {}
        })
        def test_get_mapping(self):
            field = TextField()

            self.assertEqual({
                'type': 'text',
            }, field.to_dict())