blob: bfd87580dbe07e57e2fabc96d5c6bd96582affd5 (
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.processing;
import com.yahoo.config.model.deploy.TestProperties;
import com.yahoo.document.DataType;
import com.yahoo.document.Field;
import com.yahoo.documentmodel.NewDocumentReferenceDataType;
import com.yahoo.searchdefinition.DocumentGraphValidator;
import com.yahoo.searchdefinition.Schema;
import com.yahoo.searchdefinition.ApplicationBuilder;
import com.yahoo.searchdefinition.document.SDDocumentType;
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 org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* @author bjorncs
*/
public class ReferenceFieldTestCase {
@SuppressWarnings("deprecation")
@Rule
public final ExpectedException exceptionRule = ExpectedException.none();
@Test
public void reference_fields_are_parsed_from_search_definition() throws ParseException {
ApplicationBuilder builder = new ApplicationBuilder();
String campaignSdContent =
"search campaign {\n" +
" document campaign {\n" +
" }\n" +
"}";
String salespersonSdContent =
"search salesperson {\n" +
" document salesperson {\n" +
" }\n" +
"}";
String adSdContent =
"search ad {\n" +
" document ad {\n" +
" field campaign_ref type reference<campaign> { indexing: attribute }\n" +
" field salesperson_ref type reference<salesperson> { indexing: attribute }\n" +
" }\n" +
"}";
builder.addSchema(campaignSdContent);
builder.addSchema(salespersonSdContent);
builder.addSchema(adSdContent);
builder.build(true);
Schema schema = builder.getSchema("ad");
assertSearchContainsReferenceField("campaign_ref", "campaign", schema.getDocument());
assertSearchContainsReferenceField("salesperson_ref", "salesperson", schema.getDocument());
}
@Test
public void cyclic_document_dependencies_are_detected() throws ParseException {
var builder = new ApplicationBuilder(new TestProperties().setExperimentalSdParsing(false));
String campaignSdContent =
"search campaign {\n" +
" document campaign {\n" +
" field ad_ref type reference<ad> { indexing: attribute }\n" +
" }\n" +
"}";
String adSdContent =
"search ad {\n" +
" document ad {\n" +
" field campaign_ref type reference<campaign> { indexing: attribute }\n" +
" }\n" +
"}";
builder.addSchema(campaignSdContent);
builder.addSchema(adSdContent);
exceptionRule.expect(DocumentGraphValidator.DocumentGraphException.class);
exceptionRule.expectMessage("Document dependency cycle detected: campaign->ad->campaign.");
builder.build(true);
}
@Test
public void cyclic_document_dependencies_are_detected_new_parser() throws ParseException {
var builder = new ApplicationBuilder(new TestProperties().setExperimentalSdParsing(true));
String campaignSdContent =
"search campaign {\n" +
" document campaign {\n" +
" field ad_ref type reference<ad> { indexing: attribute }\n" +
" }\n" +
"}";
String adSdContent =
"search ad {\n" +
" document ad {\n" +
" field campaign_ref type reference<campaign> { indexing: attribute }\n" +
" }\n" +
"}";
builder.addSchema(campaignSdContent);
builder.addSchema(adSdContent);
exceptionRule.expect(IllegalArgumentException.class);
exceptionRule.expectMessage("reference cycle for documents");
builder.build(true);
}
private static void assertSearchContainsReferenceField(String expectedFieldname,
String referencedDocType,
SDDocumentType documentType) {
Field field = documentType.getDocumentType().getField(expectedFieldname);
assertNotNull("Field does not exist in document type: " + expectedFieldname, field);
DataType dataType = field.getDataType();
assertTrue(dataType instanceof NewDocumentReferenceDataType);
NewDocumentReferenceDataType refField = (NewDocumentReferenceDataType) dataType;
assertEquals(referencedDocType, refField.getTargetTypeName());
assertTrue(! refField.isTemporary());
}
}
|