Repository: incubator-predictionio-template-skeleton
Updated Branches:
  refs/heads/develop 856348430 -> 5494f06a0


Example Tests

- Switch to `EngineFactory` from deprecated `IEngineFactory`
- Implement tests in template skeleton.

Closes #5


Project: 
http://git-wip-us.apache.org/repos/asf/incubator-predictionio-template-skeleton/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-predictionio-template-skeleton/commit/349b8878
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-predictionio-template-skeleton/tree/349b8878
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-predictionio-template-skeleton/diff/349b8878

Branch: refs/heads/develop
Commit: 349b8878d27a3eabf91d126162222d1b95900722
Parents: 8563484
Author: Mars Hall <[email protected]>
Authored: Wed May 3 15:51:35 2017 -0700
Committer: Donald Szeto <[email protected]>
Committed: Wed May 3 15:51:35 2017 -0700

----------------------------------------------------------------------
 build.sbt                                   |  6 +++++-
 src/main/scala/Engine.scala                 |  4 ++--
 src/test/scala/AlgorithmTest.scala          | 26 ++++++++++++++++++++++++
 src/test/scala/DataSourceTest.scala         | 15 ++++++++++++++
 src/test/scala/EngineTest.scala             | 15 ++++++++++++++
 src/test/scala/PreparatorTest.scala         | 23 +++++++++++++++++++++
 src/test/scala/ServingTest.scala            | 23 +++++++++++++++++++++
 src/test/scala/SharedSingletonContext.scala | 24 ++++++++++++++++++++++
 8 files changed, 133 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-predictionio-template-skeleton/blob/349b8878/build.sbt
----------------------------------------------------------------------
diff --git a/build.sbt b/build.sbt
index eed327e..7615d32 100644
--- a/build.sbt
+++ b/build.sbt
@@ -9,4 +9,8 @@ organization := "org.apache.predictionio"
 libraryDependencies ++= Seq(
   "org.apache.predictionio" %% "apache-predictionio-core" % 
"0.10.0-incubating" % "provided",
   "org.apache.spark"        %% "spark-core"               % "1.3.0" % 
"provided",
-  "org.apache.spark"        %% "spark-mllib"              % "1.3.0" % 
"provided")
+  "org.apache.spark"        %% "spark-mllib"              % "1.3.0" % 
"provided",
+  "org.scalatest"           %% "scalatest"                % "2.2.1" % "test")
+
+// SparkContext is shared between all tests via SharedSingletonContext
+parallelExecution in Test := false
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-predictionio-template-skeleton/blob/349b8878/src/main/scala/Engine.scala
----------------------------------------------------------------------
diff --git a/src/main/scala/Engine.scala b/src/main/scala/Engine.scala
index bfecacc..6090772 100644
--- a/src/main/scala/Engine.scala
+++ b/src/main/scala/Engine.scala
@@ -1,13 +1,13 @@
 package org.template.vanilla
 
-import org.apache.predictionio.controller.IEngineFactory
+import org.apache.predictionio.controller.EngineFactory
 import org.apache.predictionio.controller.Engine
 
 case class Query(q: String) extends Serializable
 
 case class PredictedResult(p: String) extends Serializable
 
-object VanillaEngine extends IEngineFactory {
+object VanillaEngine extends EngineFactory {
   def apply() = {
     new Engine(
       classOf[DataSource],

http://git-wip-us.apache.org/repos/asf/incubator-predictionio-template-skeleton/blob/349b8878/src/test/scala/AlgorithmTest.scala
----------------------------------------------------------------------
diff --git a/src/test/scala/AlgorithmTest.scala 
b/src/test/scala/AlgorithmTest.scala
new file mode 100644
index 0000000..2c533e0
--- /dev/null
+++ b/src/test/scala/AlgorithmTest.scala
@@ -0,0 +1,26 @@
+package org.template.vanilla
+
+import org.scalatest.FlatSpec
+import org.scalatest.Matchers
+
+import org.apache.predictionio.data.storage.Event
+
+class AlgorithmTest
+  extends FlatSpec with SharedSingletonContext with Matchers {
+
+  val params = AlgorithmParams(mult = 7)
+  val algorithm = new Algorithm(params)
+  val dataSource = Seq(
+    Event(event = "test", entityType = "example", entityId = "1"),
+    Event(event = "test", entityType = "example", entityId = "1"),
+    Event(event = "test", entityType = "example", entityId = "1"),
+    Event(event = "test", entityType = "example", entityId = "1"),
+    Event(event = "test", entityType = "example", entityId = "1"))
+
+  "train" should "return a model" in {
+    val dataSourceRDD = sparkContext.parallelize(dataSource)
+    val preparedData = new PreparedData(events = dataSourceRDD)
+    val model = algorithm.train(sparkContext, preparedData)
+    model shouldBe a [Model]
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-predictionio-template-skeleton/blob/349b8878/src/test/scala/DataSourceTest.scala
----------------------------------------------------------------------
diff --git a/src/test/scala/DataSourceTest.scala 
b/src/test/scala/DataSourceTest.scala
new file mode 100644
index 0000000..9098946
--- /dev/null
+++ b/src/test/scala/DataSourceTest.scala
@@ -0,0 +1,15 @@
+package org.template.vanilla
+
+import org.scalatest.FlatSpec
+import org.scalatest.Matchers
+
+class DataSourceTest
+  extends FlatSpec with SharedSingletonContext with Matchers {
+
+  ignore should "return the data" in {
+    val dataSource = new DataSource(
+      new DataSourceParams(appName = "test"))
+    val data = dataSource.readTraining(sc = sparkContext)
+    data shouldBe a [TrainingData]
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-predictionio-template-skeleton/blob/349b8878/src/test/scala/EngineTest.scala
----------------------------------------------------------------------
diff --git a/src/test/scala/EngineTest.scala b/src/test/scala/EngineTest.scala
new file mode 100644
index 0000000..26d9e2c
--- /dev/null
+++ b/src/test/scala/EngineTest.scala
@@ -0,0 +1,15 @@
+package org.template.vanilla
+
+import org.scalatest.FlatSpec
+import org.scalatest.Matchers
+
+import org.apache.predictionio.controller.Engine
+
+class EngineTest
+  extends FlatSpec with Matchers {
+
+  "apply" should "return a new engine instance" in {
+    val engine = VanillaEngine.apply()
+    engine shouldBe an [Engine[_,_,_,_,_,_]]
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-predictionio-template-skeleton/blob/349b8878/src/test/scala/PreparatorTest.scala
----------------------------------------------------------------------
diff --git a/src/test/scala/PreparatorTest.scala 
b/src/test/scala/PreparatorTest.scala
new file mode 100644
index 0000000..d7b5ccd
--- /dev/null
+++ b/src/test/scala/PreparatorTest.scala
@@ -0,0 +1,23 @@
+package org.template.vanilla
+
+import org.scalatest.FlatSpec
+import org.scalatest.Matchers
+
+import org.apache.predictionio.data.storage.Event
+
+class PreparatorTest
+  extends FlatSpec with SharedSingletonContext with Matchers {
+
+  val dataSource = Seq(
+    Event(event = "test", entityType = "example", entityId = "1"),
+    Event(event = "test", entityType = "example", entityId = "1"),
+    Event(event = "test", entityType = "example", entityId = "1"),
+    Event(event = "test", entityType = "example", entityId = "1"),
+    Event(event = "test", entityType = "example", entityId = "1"))
+
+  "prepare" should "return the events" in {
+    val dataSourceRDD = sparkContext.parallelize(dataSource)
+    val preparedData = new PreparedData(events = dataSourceRDD)
+    preparedData shouldBe a [PreparedData]
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-predictionio-template-skeleton/blob/349b8878/src/test/scala/ServingTest.scala
----------------------------------------------------------------------
diff --git a/src/test/scala/ServingTest.scala b/src/test/scala/ServingTest.scala
new file mode 100644
index 0000000..8571105
--- /dev/null
+++ b/src/test/scala/ServingTest.scala
@@ -0,0 +1,23 @@
+package org.template.vanilla
+
+import org.scalatest.FlatSpec
+import org.scalatest.Matchers
+
+class ServingTest
+  extends FlatSpec with Matchers {
+
+  val query = Query(q = "5")
+  val predictedResults = Seq(
+    PredictedResult(p = "25"),
+    PredictedResult(p = "50"),
+    PredictedResult(p = "75"))
+
+  "serve" should "return the first prediction" in {
+    val serving = new Serving()
+    val prediction = serving.serve(
+      query = query,
+      predictedResults = predictedResults)
+    prediction shouldBe a [PredictedResult]
+    prediction.p shouldEqual "25"
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-predictionio-template-skeleton/blob/349b8878/src/test/scala/SharedSingletonContext.scala
----------------------------------------------------------------------
diff --git a/src/test/scala/SharedSingletonContext.scala 
b/src/test/scala/SharedSingletonContext.scala
new file mode 100644
index 0000000..92443d6
--- /dev/null
+++ b/src/test/scala/SharedSingletonContext.scala
@@ -0,0 +1,24 @@
+package org.template.vanilla
+
+import org.apache.spark.{SparkConf, SparkContext}
+import org.scalatest.{BeforeAndAfterAll, Suite}
+
+trait SharedSingletonContext extends BeforeAndAfterAll {
+  this: Suite =>
+
+  private var _sparkContext: Option[SparkContext] = None
+  def sparkContext = _sparkContext.get
+  val sparkConf = new SparkConf(false)
+
+  override def beforeAll() {
+    _sparkContext = Some(new SparkContext("local", "test", sparkConf))
+    super.beforeAll()
+  }
+
+  override def afterAll() {
+    super.afterAll()
+    sparkContext.stop()
+    _sparkContext = None
+    System.clearProperty("spark.driver.port")
+  }
+}
\ No newline at end of file

Reply via email to