Ejemplo n.º 1
0
    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(),
        )
Ejemplo n.º 2
0
    def test_get_value_from_iterable(self):
        field = ObjectField(
            attr="person",
            properties={
                "first_name": TextField(analyzier="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",
                },
            ],
        )
Ejemplo n.º 3
0
    def test_get_value_from_instance_with_inner_objectfield(self):
        field = ObjectField(
            attr="person",
            properties={
                "first_name": TextField(analyzier="foo"),
                "last_name": TextField(),
                "aditional": ObjectField(properties={"age": IntegerField()}),
            },
        )

        instance = NonCallableMock(
            person=NonCallableMock(first_name="foo",
                                   last_name="bar",
                                   aditional=NonCallableMock(age=12)))

        self.assertEqual(
            field.get_value_from_instance(instance),
            {
                "first_name": "foo",
                "last_name": "bar",
                "aditional": {
                    "age": 12
                }
            },
        )
Ejemplo n.º 4
0
    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())
Ejemplo n.º 5
0
 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)