summaryrefslogtreecommitdiffstats
path: root/sample-apps/blog-tutorial-shared/src/test/scala/com/yahoo/example/blog/CollaborativeFilteringTest.scala
blob: c660b45630a6d9854976c1d3f4fb59dc9c787a56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.yahoo.example.blog

import org.apache.spark.ml.recommendation.ALSModel
import org.apache.spark.sql.SparkSession
import org.scalatest.Matchers._
import org.scalatest._

class CollaborativeFilteringTest extends FunSuite with BeforeAndAfter {

  var ss: SparkSession = _

  before {

    ss = SparkSession
      .builder()
      .appName("Unit Test")
      .master("local[*]")
      .getOrCreate()

  }

  after {
    ss.stop()
  }

  test("run method returns a MatrixFactorizationModel with latent factors of size 10 to user and item") {

    val file_path = getClass.getResource("/trainingSetIndicesSample.txt")

    val cf = new CollaborativeFiltering(ss)

    val model = cf.run(
      input_path = file_path.toString,
      rank = 10,
      numIterations = 10,
      lambda = 0.01)

    model shouldBe a [ALSModel]

    val product_feature_array = model.itemFactors.first().getSeq(1)
    assertResult(10){product_feature_array.length}

    val user_feature_array = model.userFactors.first().getSeq(1)
    assertResult(10){user_feature_array.length}

  }

  test("run_pipeline method returns a MatrixFactorizationModel with latent factors of size 10 to user and item") {

    val file_path = getClass.getResource("/trainingSetIndicesSample.txt")

    val cf = new CollaborativeFiltering(ss)

    val model = cf.run_pipeline(input_path = file_path.toString, numIterations = 10)

    model shouldBe a [ALSModel]

    val product_feature_array = model.itemFactors.first().getSeq(1)
    assertResult(10){product_feature_array.length}

    val user_feature_array = model.userFactors.first().getSeq(1)
    assertResult(10){user_feature_array.length}

  }

}