diff options
author | Jon Bratseth <bratseth@gmail.com> | 2022-02-10 14:08:04 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@gmail.com> | 2022-02-10 14:08:04 +0100 |
commit | a1ff60175c83e27d2ff36996abf90f318b709c6a (patch) | |
tree | bb46d1e1e8da18c9cf5f96dddfdacb3e5c4af0dd /integration | |
parent | 5dddb73757af51138187df1a911ecaf9f6992b09 (diff) |
Add unit testing framework
Diffstat (limited to 'integration')
11 files changed, 396 insertions, 0 deletions
diff --git a/integration/intellij/src/main/java/ai/vespa/intellij/schema/model/RankProfile.java b/integration/intellij/src/main/java/ai/vespa/intellij/schema/model/RankProfile.java index 1cc6374f812..fd30a0fbe53 100644 --- a/integration/intellij/src/main/java/ai/vespa/intellij/schema/model/RankProfile.java +++ b/integration/intellij/src/main/java/ai/vespa/intellij/schema/model/RankProfile.java @@ -1,7 +1,17 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package ai.vespa.intellij.schema.model; +import ai.vespa.intellij.schema.psi.SdFile; import ai.vespa.intellij.schema.psi.SdRankProfileDefinition; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiManager; +import com.intellij.psi.search.FilenameIndex; +import com.intellij.psi.search.GlobalSearchScope; +import com.intellij.psi.util.PsiTreeUtil; + +import java.util.Optional; /** * A rank profile @@ -16,4 +26,29 @@ public class RankProfile { this.definition = definition; } + public SdRankProfileDefinition definition() { return definition; } + + /** + * Returns the profile of the given name from the given file. + * + * @throws IllegalArgumentException if not found + */ + public static RankProfile fromProjectFile(Project project, String filePath, String profileName) { + PsiFile[] psiFile = FilenameIndex.getFilesByName(project, filePath, GlobalSearchScope.allScope(project)); + if (psiFile.length == 0) + throw new IllegalArgumentException(filePath + " could not be opened"); + if (psiFile.length > 1) + throw new IllegalArgumentException("Multiple files matches " + filePath); + if ( ! (psiFile[0] instanceof SdFile)) + throw new IllegalArgumentException(filePath + " is not a schema or profile"); + Optional<SdRankProfileDefinition> definition = + PsiTreeUtil.collectElementsOfType(psiFile[0], SdRankProfileDefinition.class) + .stream() + .filter(p -> p.getName().equals(profileName)) + .findAny(); + if (definition.isEmpty()) + throw new IllegalArgumentException("Rank profile '" + profileName + "' is not present in " + filePath); + return new RankProfile(definition.get()); + } + } diff --git a/integration/intellij/src/test/applications/rankprofilemodularity/test.sd b/integration/intellij/src/test/applications/rankprofilemodularity/test.sd new file mode 100644 index 00000000000..34414414d6c --- /dev/null +++ b/integration/intellij/src/test/applications/rankprofilemodularity/test.sd @@ -0,0 +1,49 @@ +schema test { + + document test { + + field title type string { + indexing: index + } + + } + + rank-profile default inherits outside_schema2 { + } + + rank-profile in_schema0 inherits outside_schema2 { + } + + rank-profile in_schema1 { + + first-phase { + expression: nativeRank + } + + } + + rank-profile in_schema2 inherits outside_schema2 { + + function f2() { + expression: fieldMatch(title) + fo2 + } + + first-phase { + expression: f2 + } + + } + + rank-profile in_schema3 inherits outside_schema1, outside_schema2 { + + function f2() { + expression: fieldMatch(title) + fo2 + } + + first-phase { + expression: f2 + } + + } + +}
\ No newline at end of file diff --git a/integration/intellij/src/test/applications/rankprofilemodularity/test/outside_schema1.profile b/integration/intellij/src/test/applications/rankprofilemodularity/test/outside_schema1.profile new file mode 100644 index 00000000000..a8092689b7f --- /dev/null +++ b/integration/intellij/src/test/applications/rankprofilemodularity/test/outside_schema1.profile @@ -0,0 +1,7 @@ +rank-profile outside_schema1 inherits in_schema1 { + + function fo1() { + expression: now + } + +}
\ No newline at end of file diff --git a/integration/intellij/src/test/applications/rankprofilemodularity/test/outside_schema2.profile b/integration/intellij/src/test/applications/rankprofilemodularity/test/outside_schema2.profile new file mode 100644 index 00000000000..8cf3fcfbb78 --- /dev/null +++ b/integration/intellij/src/test/applications/rankprofilemodularity/test/outside_schema2.profile @@ -0,0 +1,11 @@ +rank-profile outside_schema2 { + + function fo2() { + expression: random + } + + first-phase { + expression: fieldMatch(title).completeness + } + +}
\ No newline at end of file diff --git a/integration/intellij/src/test/applications/schemainheritance/child.sd b/integration/intellij/src/test/applications/schemainheritance/child.sd new file mode 100644 index 00000000000..a209919763d --- /dev/null +++ b/integration/intellij/src/test/applications/schemainheritance/child.sd @@ -0,0 +1,43 @@ +schema child inherits parent { + + document child inherits parent { + + field cf1 type string { + indexing: summary + } + + } + + fieldset child_set { + fields: cf1, pf1 + } + + stemming: shortest + + index child_index { + stemming: shortest + } + + field child_field type string { + indexing: input pf1 | lowercase | index | attribute | summary + } + + rank-profile child_profile inherits parent_profile { + } + + constant child_constant { + file: constants/my_constant_tensor_file.json + type: tensor<float>(x{},y{}) + } + + onnx-model child_model { + file: small_constants_and_functions.onnx + } + + document-summary child_summary inherits parent_summary { + summary cf1 type string {} + } + + import field importedschema_ref.importedfield2 as child_imported {} + +} diff --git a/integration/intellij/src/test/applications/schemainheritance/importedschema.sd b/integration/intellij/src/test/applications/schemainheritance/importedschema.sd new file mode 100644 index 00000000000..1b5acff8a26 --- /dev/null +++ b/integration/intellij/src/test/applications/schemainheritance/importedschema.sd @@ -0,0 +1,15 @@ +schema importedschema { + + document importedschema { + + field importedfield1 type string { + indexing: attribute + } + + field importedfield2 type string { + indexing: attribute + } + + } + +}
\ No newline at end of file diff --git a/integration/intellij/src/test/applications/schemainheritance/parent.sd b/integration/intellij/src/test/applications/schemainheritance/parent.sd new file mode 100644 index 00000000000..51b11dad444 --- /dev/null +++ b/integration/intellij/src/test/applications/schemainheritance/parent.sd @@ -0,0 +1,41 @@ +schema parent { + + document parent { + + field pf1 type string { + indexing: summary + } + + field importedschema_ref type reference<importedschema> { + indexing: attribute + } + + } + + fieldset parent_set { + fields: pf1 + } + stemming: none + index parent_index { + stemming: best + } + field parent_field type string { + indexing: input pf1 | lowercase | index | attribute | summary + } + rank-profile parent_profile { + } + constant parent_constant { + file: constants/my_constant_tensor_file.json + type: tensor<float>(x{},y{}) + } + onnx-model parent_model { + file: small_constants_and_functions.onnx + } + document-summary parent_summary { + summary pf1 type string { + } + } + import field importedschema_ref.importedfield1 as parent_imported { + } + raw-as-base64-in-summary +} diff --git a/integration/intellij/src/test/applications/schemainheritance/small_constants_and_functions.onnx b/integration/intellij/src/test/applications/schemainheritance/small_constants_and_functions.onnx Binary files differnew file mode 100644 index 00000000000..0d4bffa5b57 --- /dev/null +++ b/integration/intellij/src/test/applications/schemainheritance/small_constants_and_functions.onnx diff --git a/integration/intellij/src/test/java/ai/vespa/intellij/model/MockProject.java b/integration/intellij/src/test/java/ai/vespa/intellij/model/MockProject.java new file mode 100644 index 00000000000..f54dfdee2ef --- /dev/null +++ b/integration/intellij/src/test/java/ai/vespa/intellij/model/MockProject.java @@ -0,0 +1,147 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.intellij.model; + +import com.intellij.diagnostic.ActivityCategory; +import com.intellij.openapi.extensions.PluginDescriptor; +import com.intellij.openapi.extensions.PluginId; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Condition; +import com.intellij.openapi.util.Key; +import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.messages.MessageBus; +import org.picocontainer.PicoContainer; + +import java.io.File; +import java.util.Map; + +public class MockProject implements Project { + + private final File schemasDir; + + public MockProject(File schemasDir) { + this.schemasDir = schemasDir; + } + + @Override + public String getName() { return "mock project"; } + + @Override + public VirtualFile getBaseDir() { + throw new UnsupportedOperationException(); + } + + @Override + public String getBasePath() { + throw new UnsupportedOperationException(); + } + + @Override + public VirtualFile getProjectFile() { + throw new UnsupportedOperationException(); + } + + @Override + public String getProjectFilePath() { + throw new UnsupportedOperationException(); + } + + @Override + public VirtualFile getWorkspaceFile() { + throw new UnsupportedOperationException(); + } + + @Override + public String getLocationHash() { + throw new UnsupportedOperationException(); + } + + @Override + public void save() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isOpen() { return true; } + + @Override + public boolean isInitialized() { return true; } + + @Override + public <T> T getComponent(Class<T> interfaceClass) { + throw new UnsupportedOperationException(); + } + + @Override + public <T> T[] getComponents(Class<T> baseClass) { + throw new UnsupportedOperationException(); + } + + @Override + public PicoContainer getPicoContainer() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isInjectionForExtensionSupported() { return false; } + + @Override + public MessageBus getMessageBus() { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isDisposed() { return false; } + + @Override + public Condition<?> getDisposed() { + throw new UnsupportedOperationException(); + } + + @Override + public <T> T getService(Class<T> serviceClass) { + throw new UnsupportedOperationException(); + } + + @Override + public <T> T instantiateClassWithConstructorInjection(Class<T> aClass, Object key, PluginId pluginId) { + throw new UnsupportedOperationException(); + } + + @Override + public RuntimeException createError(Throwable error, PluginId pluginId) { + throw new UnsupportedOperationException(); + } + + @Override + public RuntimeException createError(String message, PluginId pluginId) { + throw new UnsupportedOperationException(); + } + + @Override + public RuntimeException createError(String message, Throwable error, PluginId pluginId, Map<String, String> attachments) { + throw new UnsupportedOperationException(); + } + + @Override + public <T> Class<T> loadClass(String className, PluginDescriptor pluginDescriptor) throws ClassNotFoundException { + throw new UnsupportedOperationException(); + } + + @Override + public ActivityCategory getActivityCategory(boolean isExtension) { + throw new UnsupportedOperationException(); + } + + @Override + public void dispose() { + } + + @Override + public <T> T getUserData(Key<T> key) { return null; } + + @Override + public <T> void putUserData(Key<T> key, T value) { + throw new UnsupportedOperationException(); + } + +} diff --git a/integration/intellij/src/test/java/ai/vespa/intellij/model/RankProfileTest.java b/integration/intellij/src/test/java/ai/vespa/intellij/model/RankProfileTest.java new file mode 100644 index 00000000000..c0b4902c002 --- /dev/null +++ b/integration/intellij/src/test/java/ai/vespa/intellij/model/RankProfileTest.java @@ -0,0 +1,32 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.intellij.model; + +import ai.vespa.intellij.schema.model.RankProfile; +import com.intellij.testFramework.LightProjectDescriptor; +import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase; +import org.junit.Test; + +import java.io.File; + +/** + * @author bratseth + */ +public class RankProfileTest extends LightJavaCodeInsightFixtureTestCase { + + @Override + protected LightProjectDescriptor getProjectDescriptor() { + // Store the descriptor between tests to make this faster + return new TestProjectDescriptor(); + } + + @Override + protected String getTestDataPath() { return "."; } + + @Test + public void testFindDefinition() { + super.myFixture.copyDirectoryToProject("src/test/applications/schemainheritance", "/"); + RankProfile profile = RankProfile.fromProjectFile(getProject(), "child.sd", "child_profile"); + assertEquals("child_profile", profile.definition().getName()); + } + +} diff --git a/integration/intellij/src/test/java/ai/vespa/intellij/model/TestProjectDescriptor.java b/integration/intellij/src/test/java/ai/vespa/intellij/model/TestProjectDescriptor.java new file mode 100644 index 00000000000..ae36c16ce67 --- /dev/null +++ b/integration/intellij/src/test/java/ai/vespa/intellij/model/TestProjectDescriptor.java @@ -0,0 +1,16 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.intellij.model; + +import com.intellij.testFramework.LightProjectDescriptor; + +import java.io.File; + +/** + * Describes a project used in unit tests. + * https://plugins.jetbrains.com/docs/intellij/light-and-heavy-tests.html + * + * @author bratseth + */ +public class TestProjectDescriptor extends LightProjectDescriptor { + +} |