diff options
author | Harald Musum <musum@yahooinc.com> | 2023-06-08 23:38:27 +0200 |
---|---|---|
committer | Harald Musum <musum@yahooinc.com> | 2023-06-08 23:38:27 +0200 |
commit | fa0044d92067bfe91a104d0a6bb6b085c0b9439e (patch) | |
tree | afed4f72ba4271b76ef8867c2917e21a54196bb8 /config-model/src/test/java/com/yahoo/vespa/model/container/search | |
parent | e3bda63e89a8b75edba41ec589de5b7ce1c934ba (diff) |
Validate semantic rules when building config model
Diffstat (limited to 'config-model/src/test/java/com/yahoo/vespa/model/container/search')
4 files changed, 48 insertions, 17 deletions
diff --git a/config-model/src/test/java/com/yahoo/vespa/model/container/search/SemanticRulesTest.java b/config-model/src/test/java/com/yahoo/vespa/model/container/search/SemanticRulesTest.java index d9e2ae59ef6..f0e28f9df09 100644 --- a/config-model/src/test/java/com/yahoo/vespa/model/container/search/SemanticRulesTest.java +++ b/config-model/src/test/java/com/yahoo/vespa/model/container/search/SemanticRulesTest.java @@ -2,52 +2,66 @@ package com.yahoo.vespa.model.container.search; import com.yahoo.config.model.application.provider.FilesApplicationPackage; -import com.yahoo.language.simple.SimpleLinguistics; import com.yahoo.prelude.semantics.RuleBase; -import com.yahoo.prelude.semantics.RuleImporter; import com.yahoo.prelude.semantics.SemanticRulesConfig; import com.yahoo.prelude.semantics.parser.ParseException; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - import java.io.File; import java.io.IOException; -import java.util.HashMap; import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + /** * @author bratseth */ public class SemanticRulesTest { - private final static String root = "src/test/java/com/yahoo/vespa/model/container/search/semanticrules"; + private static final String basePath = "src/test/java/com/yahoo/vespa/model/container/search/"; + private static final String root = basePath + "semanticrules"; + private static final String rootWithErrors = basePath + "semanticrules_with_errors"; + private static final String rootWithDuplicateDefault = basePath + "semanticrules_with_duplicate_default_rule"; @Test - void semanticRulesTest() throws ParseException, IOException { + void semanticRulesTest() throws ParseException, IOException { SemanticRuleBuilder ruleBuilder = new SemanticRuleBuilder(); SemanticRules rules = ruleBuilder.build(FilesApplicationPackage.fromFile(new File(root))); SemanticRulesConfig.Builder configBuilder = new SemanticRulesConfig.Builder(); rules.getConfig(configBuilder); SemanticRulesConfig config = new SemanticRulesConfig(configBuilder); - Map<String, RuleBase> ruleBases = toMap(config); + Map<String, RuleBase> ruleBases = SemanticRuleBuilder.toMap(config); assertEquals(2, ruleBases.size()); assertTrue(ruleBases.containsKey("common")); assertTrue(ruleBases.containsKey("other")); assertFalse(ruleBases.get("common").isDefault()); assertTrue(ruleBases.get("other").isDefault()); + assertTrue(ruleBases.get("other").includes("common")); + assertNotNull(ruleBases.get("other").getCondition("stopword")); } - private static Map<String, RuleBase> toMap(SemanticRulesConfig config) throws ParseException, IOException { - RuleImporter ruleImporter = new RuleImporter(config, new SimpleLinguistics()); - Map<String, RuleBase> ruleBaseMap = new HashMap<>(); - for (SemanticRulesConfig.Rulebase ruleBaseConfig : config.rulebase()) { - RuleBase ruleBase = ruleImporter.importConfig(ruleBaseConfig); - if (ruleBaseConfig.isdefault()) - ruleBase.setDefault(true); - ruleBaseMap.put(ruleBase.getName(), ruleBase); + @Test + void rulesWithErrors() { + try { + new SemanticRuleBuilder().build(FilesApplicationPackage.fromFile(new File(rootWithErrors))); + fail("should fail with exception"); + } catch (Exception e) { + assertEquals("com.yahoo.prelude.semantics.parser.ParseException: Could not parse 'semantic-rules.cfg'", e.getMessage()); + } + } + + @Test + void rulesWithDuplicateDefault() { + try { + new SemanticRuleBuilder().build(FilesApplicationPackage.fromFile(new File(rootWithDuplicateDefault))); + fail("should fail with exception"); + } catch (Exception e) { + assertEquals("Both 'one' and 'other' is marked as default rule, there can only be one", e.getMessage()); } - return ruleBaseMap; } } diff --git a/config-model/src/test/java/com/yahoo/vespa/model/container/search/semanticrules_with_duplicate_default_rule/rules/one.sr b/config-model/src/test/java/com/yahoo/vespa/model/container/search/semanticrules_with_duplicate_default_rule/rules/one.sr new file mode 100644 index 00000000000..4f2271e91ba --- /dev/null +++ b/config-model/src/test/java/com/yahoo/vespa/model/container/search/semanticrules_with_duplicate_default_rule/rules/one.sr @@ -0,0 +1,5 @@ +# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +@default + +# Spelling correction +bahc -> bach; diff --git a/config-model/src/test/java/com/yahoo/vespa/model/container/search/semanticrules_with_duplicate_default_rule/rules/other.sr b/config-model/src/test/java/com/yahoo/vespa/model/container/search/semanticrules_with_duplicate_default_rule/rules/other.sr new file mode 100644 index 00000000000..29f7e85967f --- /dev/null +++ b/config-model/src/test/java/com/yahoo/vespa/model/container/search/semanticrules_with_duplicate_default_rule/rules/other.sr @@ -0,0 +1,5 @@ +# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +@default + +# Spelling correction +list-> liszt; diff --git a/config-model/src/test/java/com/yahoo/vespa/model/container/search/semanticrules_with_errors/rules/invalid.sr b/config-model/src/test/java/com/yahoo/vespa/model/container/search/semanticrules_with_errors/rules/invalid.sr new file mode 100644 index 00000000000..9d89cab7e31 --- /dev/null +++ b/config-model/src/test/java/com/yahoo/vespa/model/container/search/semanticrules_with_errors/rules/invalid.sr @@ -0,0 +1,7 @@ +# Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +# Stopwords +[stopword] -> ; +[stopword] :- and, or, the, what, why, how; + +# Synonyms, with wrong character at end of line +[bill] :- Bill, bill, William: |