summaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/searchdefinition/derived/AttributeListTestCase.java
blob: d36a165ceb5190b4dc5cba30886276728e89effa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.derived;

import com.yahoo.searchdefinition.Search;
import com.yahoo.searchdefinition.SearchBuilder;
import com.yahoo.searchdefinition.SearchDefinitionTestCase;
import com.yahoo.searchdefinition.document.Attribute;
import com.yahoo.searchdefinition.parser.ParseException;
import org.junit.Test;

import java.io.IOException;
import java.util.Iterator;

import static com.yahoo.config.model.test.TestUtil.joinLines;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Tests attribute deriving
 *
 * @author bratseth
 */
public class AttributeListTestCase extends SearchDefinitionTestCase {

    @Test
    public void testDeriving() throws IOException, ParseException {
        // Test attribute importing
        Search search = SearchBuilder.buildFromFile("src/test/examples/simple.sd");

        // Test attribute deriving
        AttributeFields attributeFields = new AttributeFields(search);
        Iterator attributes = attributeFields.attributeIterator();
        Attribute attribute;
        attribute = (Attribute)attributes.next();
        assertEquals("popularity", attribute.getName());
        assertEquals(Attribute.Type.INTEGER, attribute.getType());
        assertEquals(Attribute.CollectionType.SINGLE, attribute.getCollectionType());

        attribute = (Attribute)attributes.next();
        assertEquals("measurement", attribute.getName());
        assertEquals(Attribute.Type.INTEGER, attribute.getType());
        assertEquals(Attribute.CollectionType.SINGLE, attribute.getCollectionType());

        attribute = (Attribute)attributes.next();
        assertEquals("smallattribute", attribute.getName());
        assertEquals(Attribute.Type.BYTE, attribute.getType());
        assertEquals(Attribute.CollectionType.ARRAY, attribute.getCollectionType());

        attribute = (Attribute)attributes.next();
        assertEquals("access", attribute.getName());
        assertEquals(Attribute.Type.BYTE, attribute.getType());
        assertEquals(Attribute.CollectionType.SINGLE, attribute.getCollectionType());

        attribute = (Attribute)attributes.next();
        assertEquals("category_arr", attribute.getName());
        assertEquals(Attribute.Type.STRING, attribute.getType());
        assertEquals(Attribute.CollectionType.ARRAY, attribute.getCollectionType());

        attribute = (Attribute)attributes.next();
        assertEquals("measurement_arr", attribute.getName());
        assertEquals(Attribute.Type.INTEGER, attribute.getType());
        assertEquals(Attribute.CollectionType.ARRAY, attribute.getCollectionType());

        attribute = (Attribute)attributes.next();
        assertEquals("popsiness", attribute.getName());
        assertEquals(Attribute.Type.INTEGER, attribute.getType());
        assertEquals(Attribute.CollectionType.SINGLE, attribute.getCollectionType());

        assertTrue(!attributes.hasNext());
    }

    @Test
    public void array_of_struct_field_is_derived_into_array_attributes() throws IOException, ParseException {
        Search search = SearchBuilder.buildFromFile("src/test/derived/array_of_struct_attribute/test.sd");
        Iterator<Attribute> attributes = new AttributeFields(search).attributeIterator();

        assertAttribute("elem_array.name", Attribute.Type.STRING, Attribute.CollectionType.ARRAY, attributes.next());
        assertAttribute("elem_array.weight", Attribute.Type.INTEGER, Attribute.CollectionType.ARRAY, attributes.next());
        assertTrue(!attributes.hasNext());
    }

    @Test
    public void map_of_struct_fields_are_derived_into_array_attributes() throws IOException, ParseException {
        Search search = SearchBuilder.buildFromFile("src/test/derived/map_of_struct_attribute/test.sd");
        Iterator<Attribute> attributes = new AttributeFields(search).attributeIterator();

        assertAttribute("str_elem_map.key", Attribute.Type.STRING, Attribute.CollectionType.ARRAY, attributes.next());
        assertAttribute("str_elem_map.value.name", Attribute.Type.STRING, Attribute.CollectionType.ARRAY, attributes.next());
        assertAttribute("str_elem_map.value.weight", Attribute.Type.INTEGER, Attribute.CollectionType.ARRAY, attributes.next());
        assertAttribute("int_elem_map.key", Attribute.Type.INTEGER, Attribute.CollectionType.ARRAY, attributes.next());
        assertAttribute("int_elem_map.value.name", Attribute.Type.STRING, Attribute.CollectionType.ARRAY, attributes.next());
        assertTrue(!attributes.hasNext());
    }

    private static void assertAttribute(String name, Attribute.Type type, Attribute.CollectionType collection, Attribute attr) {
        assertEquals(name, attr.getName());
        assertEquals(type, attr.getType());
        assertEquals(collection, attr.getCollectionType());
    }

    @Test
    public void only_zcurve_attribute_is_derived_from_array_of_position_field() throws ParseException {
        Search search = SearchBuilder.createFromString(
                joinLines("search test {",
                          "  document test {",
                          "    field pos_array type array<position> {",
                          "      indexing: attribute",
                          "    }",
                          "  }",
                          "}")).getSearch();
        Iterator<Attribute> attributes = new AttributeFields(search).attributeIterator();

        assertAttribute("pos_array_zcurve", Attribute.Type.LONG, Attribute.CollectionType.ARRAY, attributes.next());
        assertTrue(!attributes.hasNext());
    }

}