You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by se...@apache.org on 2020/09/17 19:34:48 UTC

[incubator-nlpcraft] 01/06: WIP.

This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git

commit bdfe2bc536bc6067dd0d30466c5e2b43eaa2da80
Author: Sergey Kamov <se...@apache.org>
AuthorDate: Wed Sep 16 19:12:47 2020 +0300

    WIP.
---
 ...CConversationSpec.scala => NCTestContext.scala} | 52 +++++++++-------------
 .../org/apache/nlpcraft/NCTestContextModel.java    | 32 +++++++++++++
 .../model/conversation/NCConversationSpec.scala    | 27 ++---------
 3 files changed, 56 insertions(+), 55 deletions(-)

diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/conversation/NCConversationSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContext.scala
similarity index 52%
copy from nlpcraft/src/test/scala/org/apache/nlpcraft/model/conversation/NCConversationSpec.scala
copy to nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContext.scala
index 198f47c..05d1937 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/conversation/NCConversationSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContext.scala
@@ -15,57 +15,47 @@
  * limitations under the License.
  */
 
-package org.apache.nlpcraft.model.conversation
+package org.apache.nlpcraft
 
 import java.io.IOException
 
 import org.apache.nlpcraft.common.NCException
-import org.apache.nlpcraft.examples.weather.WeatherModel
+import org.apache.nlpcraft.model.NCModel
 import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe
 import org.apache.nlpcraft.model.tools.test.{NCTestClient, NCTestClientBuilder}
-import org.junit.jupiter.api.Assertions.assertTrue
-import org.junit.jupiter.api.{AfterEach, BeforeEach, Test}
+import org.apache.nlpcraft.probe.mgrs.model.NCModelManager
+import org.junit.jupiter.api.{AfterEach, BeforeEach, TestInfo}
 
 /**
-  * @see WeatherModel
+  *
   */
-class NCConversationSpec {
-    private var cli: NCTestClient = _
+class NCTestContext {
+    protected var cli: NCTestClient = _
 
     @BeforeEach
     @throws[NCException]
     @throws[IOException]
-    private[conversation] def setUp(): Unit = {
-        NCEmbeddedProbe.start(classOf[WeatherModel])
+    private def setUp(ti: TestInfo): Unit = {
+        if (ti.getTestMethod.isPresent) {
+            val a = ti.getTestMethod.get().getAnnotation(classOf[NCTestContextModel])
 
-        cli = new NCTestClientBuilder().newBuilder.build
+            if (a != null) {
+                NCEmbeddedProbe.start(a.value().asInstanceOf[Class[NCModel]])
 
-        cli.open("nlpcraft.weather.ex") // See weather_model.json
+                cli = new NCTestClientBuilder().newBuilder.build
+
+                cli.open(NCModelManager.getAllModels().head.model.getId)
+            }
+        }
     }
 
     @AfterEach
     @throws[NCException]
     @throws[IOException]
-    private[conversation] def tearDown(): Unit = {
-        if (cli != null)
+    private def tearDown(): Unit =
+        if (cli != null) {
             cli.close()
 
-        NCEmbeddedProbe.stop()
-    }
-
-    @Test
-    @throws[NCException]
-    @throws[IOException]
-    private[conversation] def test(): Unit = {
-        assertTrue(cli.ask("What's the weather in Moscow?").isOk)
-
-        // Can be answered with conversation.
-        assertTrue(cli.ask("Chance of snow?").isOk)
-        assertTrue(cli.ask("Moscow").isOk)
-
-        cli.clearConversation()
-
-        // Cannot be answered without conversation.
-        assertTrue(cli.ask("Moscow").isFailed)
-    }
+            NCEmbeddedProbe.stop()
+        }
 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContextModel.java b/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContextModel.java
new file mode 100644
index 0000000..c58bd5f
--- /dev/null
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContextModel.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nlpcraft;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+@Documented
+@Retention(value=RUNTIME)
+@Target(value=METHOD)
+public @interface NCTestContextModel {
+    Class value();
+}
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/conversation/NCConversationSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/conversation/NCConversationSpec.scala
index 198f47c..61bee87 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/conversation/NCConversationSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/conversation/NCConversationSpec.scala
@@ -19,6 +19,7 @@ package org.apache.nlpcraft.model.conversation
 
 import java.io.IOException
 
+import org.apache.nlpcraft.{NCTestContext, NCTestContextModel}
 import org.apache.nlpcraft.common.NCException
 import org.apache.nlpcraft.examples.weather.WeatherModel
 import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe
@@ -29,33 +30,11 @@ import org.junit.jupiter.api.{AfterEach, BeforeEach, Test}
 /**
   * @see WeatherModel
   */
-class NCConversationSpec {
-    private var cli: NCTestClient = _
-
-    @BeforeEach
-    @throws[NCException]
-    @throws[IOException]
-    private[conversation] def setUp(): Unit = {
-        NCEmbeddedProbe.start(classOf[WeatherModel])
-
-        cli = new NCTestClientBuilder().newBuilder.build
-
-        cli.open("nlpcraft.weather.ex") // See weather_model.json
-    }
-
-    @AfterEach
-    @throws[NCException]
-    @throws[IOException]
-    private[conversation] def tearDown(): Unit = {
-        if (cli != null)
-            cli.close()
-
-        NCEmbeddedProbe.stop()
-    }
-
+class NCConversationSpec extends NCTestContext {
     @Test
     @throws[NCException]
     @throws[IOException]
+    @NCTestContextModel(value = classOf[WeatherModel])
     private[conversation] def test(): Unit = {
         assertTrue(cli.ask("What's the weather in Moscow?").isOk)