diff options
author | Jon Bratseth <bratseth@vespa.ai> | 2023-06-05 13:10:06 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@vespa.ai> | 2023-06-05 13:10:06 +0200 |
commit | 3ecff4fb067f090789f2e85e0e195c1fd69e3c3e (patch) | |
tree | e0b68ac002cb604d3725afd356fbebb0cfcfa861 /container-search/src/test/java/com/yahoo/search/searchers | |
parent | 4864d94e48919a8cb734191ab90b80738e843d08 (diff) |
Validate prefix matching
Diffstat (limited to 'container-search/src/test/java/com/yahoo/search/searchers')
-rw-r--r-- | container-search/src/test/java/com/yahoo/search/searchers/test/QueryValidatorTestCase.java | 49 |
1 files changed, 36 insertions, 13 deletions
diff --git a/container-search/src/test/java/com/yahoo/search/searchers/test/QueryValidatorTestCase.java b/container-search/src/test/java/com/yahoo/search/searchers/test/QueryValidatorTestCase.java index 64fb4354003..8c525b2975a 100644 --- a/container-search/src/test/java/com/yahoo/search/searchers/test/QueryValidatorTestCase.java +++ b/container-search/src/test/java/com/yahoo/search/searchers/test/QueryValidatorTestCase.java @@ -18,7 +18,7 @@ import static org.junit.jupiter.api.Assertions.fail; public class QueryValidatorTestCase { @Test - void testValidation() { + void testTensorsCannotBeSearchedForTerms() { SearchDefinition sd = new SearchDefinition("test"); sd.addCommand("mytensor1", "type tensor(x[100]"); sd.addCommand("mytensor2", "type tensor<float>(x[100]"); @@ -27,24 +27,47 @@ public class QueryValidatorTestCase { IndexFacts indexFacts = new IndexFacts(model); Execution execution = new Execution(Execution.Context.createContextStub(indexFacts)); - new QueryValidator().search(new Query("?query=mystring:foo"), execution); + assertSucceeds("?query=mystring:foo", execution); + assertFails("Cannot search for terms in 'mytensor1': It is a tensor field", + "?query=mytensor1:foo", execution); + assertFails("Cannot search for terms in 'mytensor2': It is a tensor field", + "?query=mytensor2:foo", execution); + } - try { - new QueryValidator().search(new Query("?query=mytensor1:foo"), execution); - fail("Expected validation error"); - } - catch (IllegalArgumentException e) { - // success - assertEquals("Cannot search 'mytensor1': It is a tensor field", e.getMessage()); - } + @Test + void testPrefixRequiresAttribute() { + SearchDefinition sd = new SearchDefinition("test"); + sd.addCommand("attributeOnly", "type string") + .addCommand("attribute"); + sd.addCommand("indexOnly", "type string") + .addCommand("index"); + sd.addCommand("attributeAndIndex", "type string") + .addCommand("attribute") + .addCommand("index"); + IndexModel model = new IndexModel(sd); + + IndexFacts indexFacts = new IndexFacts(model); + Execution execution = new Execution(Execution.Context.createContextStub(indexFacts)); + + assertSucceeds("?query=attributeOnly:foo*", execution); + assertFails("'indexOnly' is not an attribute field: Prefix matching is not supported", + "?query=indexOnly:foo*", execution); + assertFails("'attributeAndIndex' is an index field: Prefix matching is not supported even when it is also an attribute", + "?query=attributeAndIndex:foo*", execution); + } + + private void assertSucceeds(String query, Execution execution) { + new QueryValidator().search(new Query(query), execution); + } + private void assertFails(String expectedError, String query, Execution execution) { try { - new QueryValidator().search(new Query("?query=mytensor2:foo"), execution); - fail("Expected validation error"); + new QueryValidator().search(new Query(query), execution); + fail("Expected validation error from " + query); } catch (IllegalArgumentException e) { // success - assertEquals("Cannot search 'mytensor2': It is a tensor field", e.getMessage()); + assertEquals(expectedError, e.getMessage()); } } |