|
import org.scalatest.FunSuite |
|
import com.twitter.scalding.{FieldConversions, TupleConversions, Tsv, JobTest} |
|
import scala.collection.mutable |
|
import org.scalatest.matchers.ShouldMatchers |
|
|
|
class FixedPointJobTest extends FunSuite with TupleConversions with FieldConversions with ShouldMatchers { |
|
|
|
test("should work for the example in the book") { |
|
val exampleGraphInput = List( |
|
(1,4), (4,3), (4,5), (5,2), (5,11) |
|
) |
|
|
|
val graphAfterIteration1 = List( |
|
(1,4), (1,3), (1,5), (3,4), (2,4), (2,5), (2, 11), (5, 11) |
|
) |
|
|
|
val graphAfterIteration2 = List( |
|
(1,3), (1,4), (1,5), (1,11), (1,2), (2,5), (2, 4), (2,11) |
|
) |
|
|
|
val finalExpectedGraph = List( |
|
(1,2), (1,3), (1,4), (1,5), (1,11) |
|
) |
|
|
|
def validateOutput(runString: String, expectedGraph: List[(Int, Int)])(buffer: mutable.Buffer[(Int, Int)]) { |
|
println(runString) |
|
buffer.filterNot(expectedGraph.contains).size should be(0) |
|
} |
|
|
|
JobTest(new FixedPointJob(_)) |
|
.arg("input", "input") |
|
.arg("output-base-dir", "output-base-dir") |
|
.arg("progress", "progress") |
|
.arg("iteration", "1") |
|
.source(Tsv("input", fields = ('n1, 'n2)), exampleGraphInput) |
|
.source(Tsv("output-base-dir/run-1", fields = ('n1, 'n2)), graphAfterIteration1) |
|
.source(Tsv("output-base-dir/run-2", fields = ('n1, 'n2)), graphAfterIteration2) |
|
.source(Tsv("output-base-dir/run-3", fields = ('n1, 'n2)), finalExpectedGraph) |
|
.source(Tsv("output-base-dir/run-4", fields = ('n1, 'n2)), Iterable()) |
|
.sink(Tsv("output-base-dir/run-1"))(validateOutput("Validating Run 1", graphAfterIteration1)) |
|
.sink(Tsv("output-base-dir/run-2"))(validateOutput("Validating Run 2", graphAfterIteration2)) |
|
.sink(Tsv("output-base-dir/run-3"))(validateOutput("Validating Run 3", finalExpectedGraph)) |
|
.sink(Tsv("output-base-dir/run-4"))(validateOutput("Validating Run 4", finalExpectedGraph)) |
|
.sink(Tsv("progress"))(doNothing) |
|
.run |
|
.finish |
|
} |
|
|
|
def doNothing(buffer: mutable.Buffer[(Int, Int)]) {} |
|
|
|
} |