Skip to content

Instantly share code, notes, and snippets.

@nicmarti
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicmarti/545fabf5922cfe66a8d6 to your computer and use it in GitHub Desktop.
Save nicmarti/545fabf5922cfe66a8d6 to your computer and use it in GitHub Desktop.
Sample scala test
package roger
import org.scalatest.{FlatSpec, ShouldMatchers}
import roger.AggregationOperation.AggregationOperation
case class Metric(id: String, aggregationOperation: AggregationOperation)
case class Dimension(id: String)
object AggregationOperation extends Enumeration {
type AggregationOperation = Value
val Sum, Average, Min, Max, Count = Value
}
/**
* Think of a RowSelection as a typed SQL table. We use this selection to
* represent OLAP cubes.
*
* Headers are the names of each column, with an additional information about
* either we are dealing with a dimension or a metric.
*
* Rows are double dimensions arrays.
* Each row is viewed as 2 concatenated sequence: one of strings for dimension values, the others of numerics.
*
* Ex:
* ["ga:origin", "ga:browser"] ["ga:visits", "ga:timeOnSite"]
* ["France", "Chrome"] [20.0, 345.5]
*
*/
case class RowSelection(
metricHeaders: Seq[Metric],
dimensionHeaders: Seq[Dimension],
metricRows: Seq[IndexedSeq[Double]],
dimensionRows: Seq[IndexedSeq[String]]) {
def take(n: Int): RowSelection = {
/// To be implemented
???
}
def ++(other: RowSelection): RowSelection = {
/// To be implemented
???
}
def filter(col: Dimension, f: (String) => Boolean): RowSelection = {
/// To be implemented
???
}
def groupByColumns(cols: Seq[Dimension]): RowSelection = {
///To be implemented
???
}
}
class RowSelectionSpec extends FlatSpec with ShouldMatchers {
val metric1 = Metric("m1", AggregationOperation.Sum)
val metric2 = Metric("m2", AggregationOperation.Average)
val dimension1 = Dimension("d1")
val dimension2 = Dimension("d2")
val selection = RowSelection(
Seq(metric1, metric2),
Seq(dimension1, dimension2),
Seq(IndexedSeq(1.0, 3.0), IndexedSeq(2.0, 9.0)),
Seq(IndexedSeq("dimVal11", "dimVal12"), IndexedSeq("dimVal21", "dimVal22"))
)
"A RowSelection" should "take the n first rows" in {
val taken = selection.take(1)
taken.metricRows should have size 1
taken.dimensionRows should have size 1
}
it should "add a selection to another selection" in {
val other = selection
val added = selection ++ other
added.metricHeaders should equal(selection.metricHeaders)
added.dimensionHeaders should equal(selection.dimensionHeaders)
added.metricRows should equal(Seq(IndexedSeq(1.0, 3.0), IndexedSeq(2.0, 9.0), IndexedSeq(1.0, 3.0), IndexedSeq(2.0, 9.0)))
added.dimensionRows should equal(Seq(IndexedSeq("dimVal11", "dimVal12"), IndexedSeq("dimVal21", "dimVal22"), IndexedSeq("dimVal11", "dimVal12"), IndexedSeq("dimVal21", "dimVal22")))
}
it should "filter the selection" in {
val f: (String => Boolean) = dimVal => dimVal == "dimVal11"
val filtered = selection.filter(dimension1, f)
filtered.metricRows should equal(Seq(IndexedSeq(1.0, 3.0)))
filtered.dimensionRows should equal(Seq(IndexedSeq("dimVal11", "dimVal12")))
}
it should "group by columns" in {
val s = selection.copy(dimensionRows = Seq(IndexedSeq("dimVal1", "dimVal2"), IndexedSeq("dimVal1", "dimVal2")))
val grouped = s.groupByColumns(Seq(dimension1, dimension2))
grouped.dimensionRows should equal(Seq(IndexedSeq("dimVal1", "dimVal2")))
grouped.metricRows should equal(Seq(IndexedSeq(3.0, 6.0)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment