Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
106 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
version = 2.6.3 | ||
|
||
align = more | ||
maxColumn = 150 | ||
docstrings = JavaDoc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package swoop.modelo | ||
|
||
object ParamConverters { | ||
|
||
def multiMatch(strs: List[String]): String = { | ||
strs.map(str => s"'${str}'").mkString("(", ",", ")") | ||
} | ||
|
||
def exactMatch(strs: List[String]): String = { | ||
"'" + strs(0) + "'" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package swoop.modelo | ||
|
||
case class ModeloValidationException(smth: String) extends Exception(smth) | ||
|
||
object ParamValidators { | ||
|
||
def requireAtLeastOne(params: Map[String, List[String]], required: Set[String]): Unit = { | ||
if (required.nonEmpty && required.intersect(params.keys.toSet).isEmpty) | ||
throw ModeloValidationException(s"You supplied these params [${params.keys}] but at least one of these are required [${required}]") | ||
} | ||
|
||
def requireParams(params: Map[String, List[String]], required: Set[String]): Unit = { | ||
if (!required.subsetOf(params.keys.toSet)) | ||
throw ModeloValidationException(s"You supplied these params [${params.keys}] but all these are required [${required}]") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,76 @@ | ||
package swoop.modelo | ||
|
||
import com.github.mrpowers.spark.fast.tests.DataFrameComparer | ||
import org.scalatest.{FunSpec, Matchers} | ||
|
||
class BobSpec extends FunSpec with Matchers { | ||
class BobSpec extends FunSpec with Matchers with SparkSessionTestWrapper with DataFrameComparer { | ||
|
||
import spark.implicits._ | ||
|
||
it("dynamically builds input parameters") { | ||
val b = Bob(required = Set("whatever", "cool")) | ||
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" -> List("aaa", "bbb"), "cool" -> List("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 b = Bob(required = Set("fun")) | ||
.whatever("aaa", "bbb") | ||
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[ModeloBobException] { | ||
intercept[ModeloValidationException] { | ||
b.attributes | ||
} | ||
} | ||
|
||
it("errors out if none of the required params are supplied") { | ||
val b = Bob(requireAtLeastOne = Set("fun")) | ||
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[ModeloBobException] { | ||
intercept[ModeloValidationException] { | ||
b.attributes | ||
} | ||
} | ||
|
||
it("does not error out if at least one of the required params is supplied") { | ||
val b = Bob(requireAtLeastOne = Set("cool")) | ||
.whatever("aaa", "bbb") | ||
.cool("ccc") | ||
val expected = Map("whatever" -> List("aaa", "bbb"), "cool" -> List("ccc")) | ||
b.attributes should be(expected) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters