-
Notifications
You must be signed in to change notification settings - Fork 1
/
BobSpec.scala
76 lines (68 loc) · 2.64 KB
/
BobSpec.scala
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
67
68
69
70
71
72
73
74
75
76
package swoop.modelo
import com.github.mrpowers.spark.fast.tests.DataFrameComparer
import org.scalatest.{FunSpec, Matchers}
class BobSpec extends FunSpec with Matchers with SparkSessionTestWrapper with DataFrameComparer {
import spark.implicits._
it("dynamically builds input parameters") {
val someTool = Bob(
templates = Map("countryFiltered" -> "select * from my_table where country IN {{{countries}}}"),
baseTemplateName = "countryFiltered",
required = Set("whatever", "cool"),
paramConverters = Map("whatever" -> ParamConverters.multiMatch, "cool" -> ParamConverters.exactMatch)
)
val b = someTool
.whatever("aaa", "bbb")
.cool("ccc")
val expected = Map("whatever" -> "('aaa','bbb')", "cool" -> "'ccc'")
b.attributes should be(expected)
}
it("can dynamically run queries") {
val df = Seq(
("li", "china"),
("luis", "colombia"),
("fernanda", "brasil")
).toDF("first_name", "country")
df.createOrReplaceTempView("my_table")
// technical users construct someTool
val someTool = Bob(
templates = Map("countryFiltered.mustache" -> "select * from my_table where country IN {{{countries}}}"),
baseTemplateName = "countryFiltered.mustache",
required = Set("countries"),
paramConverters = Map("countries" -> ParamConverters.multiMatch)
)
// less technical users run queries with this interface
val b = someTool.countries("china", "colombia")
val expected = Seq(
("li", "china"),
("luis", "colombia")
).toDF("first_name", "country")
assertSmallDataFrameEquality(b.dataframe, expected, orderedComparison = false)
}
it("errors out if a required param isn't supplied") {
val someTool = Bob(
templates = Map("countryFiltered.mustache" -> "select * from my_table where country IN {{{countries}}}"),
baseTemplateName = "countryFiltered.mustache",
required = Set("countries", "cat"),
paramConverters = Map("countries" -> ParamConverters.multiMatch, "cat" -> ParamConverters.exactMatch)
)
val b = someTool
.cool("ccc")
intercept[ModeloValidationException] {
b.attributes
}
}
it("errors out if none of the required params are supplied") {
val someTool = Bob(
templates = Map("countryFiltered.mustache" -> "select * from my_table where country IN {{{countries}}}"),
baseTemplateName = "countryFiltered.mustache",
required = Set("countries"),
paramConverters = Map("countries" -> ParamConverters.multiMatch)
)
val b = someTool
.whatever("aaa", "bbb")
.cool("ccc")
intercept[ModeloValidationException] {
b.attributes
}
}
}