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
|
// Copyright 2017 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.processing;
import com.yahoo.searchdefinition.Search;
import com.yahoo.searchdefinition.SearchBuilder;
import com.yahoo.searchdefinition.document.ImportedField;
import com.yahoo.searchdefinition.parser.ParseException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.assertEquals;
import static com.yahoo.config.model.test.TestUtil.joinLines;
import static org.junit.Assert.assertNotNull;
/**
* @author geirst
*/
public class ImportedFieldsTestCase {
@Test
public void fields_can_be_imported_from_referenced_document_types() throws ParseException {
Search search = buildAdSearch(joinLines(
"search ad {",
" document ad {",
" field campaign_ref type reference<campaign> { indexing: attribute }",
" field person_ref type reference<person> { indexing: attribute }",
" }",
" import field campaign_ref.budget as my_budget {}",
" import field person_ref.name as my_name {}",
"}"));
assertEquals(2, search.importedFields().get().fields().size());
assertSearchContainsImportedField("my_budget", "campaign_ref", "campaign", "budget", search);
assertSearchContainsImportedField("my_name", "person_ref", "person", "name", search);
}
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void field_reference_spec_must_include_dot() throws ParseException {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("Illegal field reference spec 'campaignrefbudget': Does not include a single '.'");
buildAdSearch(joinLines(
"search ad {",
" document ad {}",
" import field campaignrefbudget as budget {}",
"}"));
}
private static Search buildAdSearch(String sdContent) throws ParseException {
SearchBuilder builder = new SearchBuilder();
builder.importString(joinLines("search campaign {",
" document campaign {",
" field budget type int { indexing: attribute }",
" }",
"}"));
builder.importString(joinLines("search person {",
" document person {",
" field name type string { indexing: attribute }",
" }",
"}"));
builder.importString(sdContent);
builder.build();
return builder.getSearch("ad");
}
private static void assertSearchContainsImportedField(String fieldName,
String referenceFieldName,
String referenceDocType,
String targetFieldName,
Search search) {
ImportedField importedField = search.importedFields().get().fields().get(fieldName);
assertNotNull(importedField);
assertEquals(fieldName, importedField.fieldName());
assertEquals(referenceFieldName, importedField.reference().referenceField().getName());
assertEquals(referenceDocType, importedField.reference().targetSearch().getName());
assertEquals(targetFieldName, importedField.targetField().getName());
}
}
|