aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/searchdefinition/document/ComplexAttributeFieldUtilsTestCase.java
blob: 4cdc48b330e425cbef6effc046f78cf7a455101e (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.yahoo.searchdefinition.document;

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

import static com.yahoo.config.model.test.TestUtil.joinLines;
import static com.yahoo.searchdefinition.document.ComplexAttributeFieldUtils.isArrayOfSimpleStruct;
import static com.yahoo.searchdefinition.document.ComplexAttributeFieldUtils.isComplexFieldWithOnlyStructFieldAttributes;
import static com.yahoo.searchdefinition.document.ComplexAttributeFieldUtils.isMapOfPrimitiveType;
import static com.yahoo.searchdefinition.document.ComplexAttributeFieldUtils.isMapOfSimpleStruct;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class ComplexAttributeFieldUtilsTestCase {

    private static ImmutableSDField createField(String fieldName, String sdFieldContent) throws ParseException {
        String sdContent = joinLines("search test {",
                "  document test {",
                "    struct elem {",
                "      field name type string {}",
                "      field weight type string {}",
                "    }",
                sdFieldContent,
                "  }",
                "}");
        Search search = SearchBuilder.createFromString(sdContent).getSearch();
        return search.getConcreteField(fieldName);
    }

    @Test
    public void array_of_struct_with_only_struct_field_attributes_is_tagged_as_such() throws ParseException {
        ImmutableSDField field = createField("elem_array",
                joinLines("field elem_array type array<elem> {",
                        "  indexing: summary",
                        "  struct-field name { indexing: attribute }",
                        "  struct-field weight { indexing: attribute }",
                        "}"));
        assertTrue(isArrayOfSimpleStruct(field));
        assertTrue(isComplexFieldWithOnlyStructFieldAttributes(field));
    }

    @Test
    public void array_of_struct_with_some_struct_field_attributes_is_tagged_as_such() throws ParseException {
        ImmutableSDField field = createField("elem_array",
                joinLines("field elem_array type array<elem> {",
                        "  indexing: summary",
                        "  struct-field weight { indexing: attribute }",
                        "}"));
        assertTrue(isArrayOfSimpleStruct(field));
        assertFalse(isComplexFieldWithOnlyStructFieldAttributes(field));
    }

    @Test
    public void map_of_struct_with_only_struct_field_attributes_is_tagged_as_such() throws ParseException {
        ImmutableSDField field = createField("elem_map",
                joinLines("field elem_map type map<string, elem> {",
                        "  indexing: summary",
                        "  struct-field key { indexing: attribute }",
                        "  struct-field value.name { indexing: attribute }",
                        "  struct-field value.weight { indexing: attribute }",
                        "}"));
        assertTrue(isMapOfSimpleStruct(field));
        assertFalse(isMapOfPrimitiveType(field));
        assertTrue(isComplexFieldWithOnlyStructFieldAttributes(field));
    }

    @Test
    public void map_of_struct_with_some_struct_field_attributes_is_tagged_as_such() throws ParseException {
        {
            ImmutableSDField field = createField("elem_map",
                    joinLines("field elem_map type map<int, elem> {",
                            "  indexing: summary",
                            "  struct-field value.name { indexing: attribute }",
                            "  struct-field value.weight { indexing: attribute }",
                            "}"));
            assertTrue(isMapOfSimpleStruct(field));
            assertFalse(isMapOfPrimitiveType(field));
            assertFalse(isComplexFieldWithOnlyStructFieldAttributes(field));
        }
        {
            ImmutableSDField field = createField("elem_map",
                    joinLines("field elem_map type map<int, elem> {",
                            "  indexing: summary",
                            "  struct-field key { indexing: attribute }",
                            "  struct-field value.weight { indexing: attribute }",
                            "}"));
            assertTrue(isMapOfSimpleStruct(field));
            assertFalse(isMapOfPrimitiveType(field));
            assertFalse(isComplexFieldWithOnlyStructFieldAttributes(field));
        }
    }

    @Test
    public void map_of_primitive_type_with_only_struct_field_attributes_is_tagged_as_such() throws ParseException {
         ImmutableSDField field = createField("str_map",
                 joinLines("field str_map type map<string, string> {",
                         "  indexing: summary",
                         "  struct-field key { indexing: attribute }",
                         "  struct-field value { indexing: attribute }",
                         "}"));
         assertTrue(isMapOfPrimitiveType(field));
         assertFalse(isMapOfSimpleStruct(field));
         assertTrue(isComplexFieldWithOnlyStructFieldAttributes(field));
    }

    @Test
    public void map_of_primitive_type_with_some_struct_field_attributes_is_tagged_as_such() throws ParseException {
        {
            ImmutableSDField field = createField("int_map",
                    joinLines("field int_map type map<int, int> {",
                            "  indexing: summary",
                            "  struct-field key { indexing: attribute }",
                            "}"));
            assertTrue(isMapOfPrimitiveType(field));
            assertFalse(isMapOfSimpleStruct(field));
            assertFalse(isComplexFieldWithOnlyStructFieldAttributes(field));
        }
        {
            ImmutableSDField field = createField("int_map",
                    joinLines("field int_map type map<int, int> {",
                            "  indexing: summary",
                            "  struct-field value { indexing: attribute }",
                            "}"));
            assertTrue(isMapOfPrimitiveType(field));
            assertFalse(isMapOfSimpleStruct(field));
            assertFalse(isComplexFieldWithOnlyStructFieldAttributes(field));
        }
    }
}