Skip to content

Commit

Permalink
Add documentation for anonymous JobTest
Browse files Browse the repository at this point in the history
  • Loading branch information
RustedBones committed Dec 5, 2023
1 parent daa9ff7 commit c19f88c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ import org.apache.beam.sdk.values.KV

object ObjectFileJob {

// #JobTestTest_io_pipeline_section
def pipeline(sc: ScioContext, input: String, output: String): Unit = {
sc.objectFile[Int](input)
.map(_ * 10)
.saveAsObjectFile(output)
}
// #JobTestTest_io_pipeline_section

def main(cmdlineArgs: Array[String]): Unit = {
val (sc, args) = ContextAndArgs(cmdlineArgs)
Expand Down Expand Up @@ -351,10 +353,13 @@ class JobTestTest extends PipelineSpec {
}

it should "execute anonymous job" in {
JobTest(ObjectFileJob.pipeline(_, "in.avro", "out.avro"))
import ObjectFileJob.pipeline
// #JobTestTest_anonymous_job_test
JobTest(pipeline(_, "in.avro", "out.avro"))
.input(ObjectFileIO[Int]("in.avro"), Seq(1, 2, 3))
.output(ObjectFileIO[Int]("out.avro"))(_ should containInAnyOrder(Seq(10, 20, 30)))
.run()
// #JobTestTest_anonymous_job_test
}

def testSpecificAvroFileJob(xs: Seq[TestRecord]): Unit =
Expand Down
11 changes: 11 additions & 0 deletions site/src/main/paradox/Scio-Unit-Tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ The data variable hold the test data and `sc.parallelize` will transform the inp

@@snip [TriggerExampleTest.scala](/scio-examples/src/test/scala/com/spotify/scio/examples/cookbook/TriggerExampleTest.scala) { #TriggerExampleTest_example }

When your pipeline section contains input and/or output, you can also create an anonymous `JobTest` to inject the test data.

If we have the following pipeline section:

@@snip [JobTestTest.scala](/scio-test/src/test/scala/com/spotify/scio/testing/JobTestTest.scala) { #JobTestTest_io_pipeline_section }

It can be tested with:

@@snip [JobTestTest.scala](/scio-test/src/test/scala/com/spotify/scio/testing/JobTestTest.scala) { #JobTestTest_anonymous_job_test }


### Test for pipeline with windowing
We will use the LeaderBoardTest to explain how to test Windowing in Scio. The full example code is found @github[here](/scio-examples/src/test/scala/com/spotify/scio/examples/complete/game/LeaderBoardTest.scala). LeaderBoardTest also extends `PipelineSpec`. The function under test is the @github[LeaderBoard.calculateTeamScores](/scio-examples/src/main/scala/com/spotify/scio/examples/complete/game/LeaderBoard.scala#L131). This function calculates teams scores within a fixed window with the following the window options:

Expand Down

0 comments on commit c19f88c

Please sign in to comment.