-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add zio spark test assertion system #307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dylandoamaral
wants to merge
10
commits into
master
Choose a base branch
from
feat/add-zio-spark-test-assertion-system
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
94646d7
feat: add zio spark test assertion system
dylandoamaral 9baebf0
chore: make progress
dylandoamaral 3abd567
fix: style
dylandoamaral 6a5c091
wip
dylandoamaral 21d6c57
fix: scala 3 macro
dylandoamaral 7905243
feat: implement assertSpark
dylandoamaral 132377e
fix: scala 3 macro
dylandoamaral 809c336
refactor: remove extra macro elements for showExpression by reusing m…
ahoy-jon 1cc3548
feat: remove useless scala3 function
dylandoamaral 5185166
Merge branch 'master' of github.com:univalence/zio-spark into feat/ad…
dylandoamaral File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
11 changes: 11 additions & 0 deletions
11
zio-spark-test/src/main/scala-2/zio/spark/test/CompileVariants.scala
This file contains hidden or 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,11 @@ | ||
| package zio.spark.test | ||
|
|
||
| import zio.spark.sql.SIO | ||
| import zio.test.TestResult | ||
|
|
||
| trait CompileVariants { | ||
| def assertSpark[A, B](value: => A)(assertion: SparkAssertion[A, B]): SIO[TestResult] = macro Macros.assert_impl | ||
|
|
||
| def assertZIOSpark[A, B](value: SIO[A])(assertion: SparkAssertion[A, B]): SIO[TestResult] = | ||
| macro Macros.assertZIO_impl | ||
| } |
77 changes: 77 additions & 0 deletions
77
zio-spark-test/src/main/scala-2/zio/spark/test/Macros.scala
This file contains hidden or 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,77 @@ | ||
| package zio.spark.test | ||
|
|
||
| import scala.reflect.macros._ | ||
|
|
||
| // Pilfered (with immense gratitude & minor modifications) | ||
| // from https://github.com/zio/zio/blob/series/2.x/test/shared/src/main/scala-2/zio/test/Macros.scala | ||
|
|
||
| // scalafix:off | ||
| private[test] object Macros { | ||
| def assert_impl(c: blackbox.Context)(value: c.Tree)(assertion: c.Tree): c.Tree = { | ||
| import c.universe._ | ||
|
|
||
| // Pilfered (with immense gratitude & minor modifications) | ||
| // from https://github.com/com-lihaoyi/sourcecode | ||
| def text[T: c.WeakTypeTag](tree: c.Tree): (Int, Int, String) = { | ||
| val fileContent = new String(tree.pos.source.content) | ||
|
|
||
| var start = | ||
| tree.collect { case treeVal => | ||
| treeVal.pos match { | ||
| case NoPosition => Int.MaxValue | ||
| case p => p.start | ||
| } | ||
| }.min | ||
| val initialStart = start | ||
|
|
||
| // Moves to the true beginning of the expression, in the case where the | ||
| // internal expression is wrapped in parens. | ||
| while ((start - 2) >= 0 && fileContent(start - 2) == '(') | ||
| start -= 1 | ||
|
|
||
| val g = c.asInstanceOf[reflect.macros.runtime.Context].global | ||
| val parser = g.newUnitParser(fileContent.drop(start)) | ||
| parser.expr() | ||
| val end = parser.in.lastOffset | ||
| (initialStart - start, start, fileContent.slice(start, start + end)) | ||
| } | ||
|
|
||
| val codeString = text(value)._3 | ||
| val assertionString = text(assertion)._3 | ||
| q"_root_.zio.spark.test.assertSparkImpl($value, $codeString, $assertionString)($assertion)" | ||
| } | ||
|
|
||
| def assertZIO_impl(c: blackbox.Context)(value: c.Tree)(assertion: c.Tree): c.Tree = { | ||
| import c.universe._ | ||
|
|
||
| // Pilfered (with immense gratitude & minor modifications) | ||
| // from https://github.com/com-lihaoyi/sourcecode | ||
| def text[T: c.WeakTypeTag](tree: c.Tree): (Int, Int, String) = { | ||
| val fileContent = new String(tree.pos.source.content) | ||
|
|
||
| var start = | ||
| tree.collect { case treeVal => | ||
| treeVal.pos match { | ||
| case NoPosition => Int.MaxValue | ||
| case p => p.start | ||
| } | ||
| }.min | ||
| val initialStart = start | ||
|
|
||
| // Moves to the true beginning of the expression, in the case where the | ||
| // internal expression is wrapped in parens. | ||
| while ((start - 2) >= 0 && fileContent(start - 2) == '(') | ||
| start -= 1 | ||
|
|
||
| val g = c.asInstanceOf[reflect.macros.runtime.Context].global | ||
| val parser = g.newUnitParser(fileContent.drop(start)) | ||
| parser.expr() | ||
| val end = parser.in.lastOffset | ||
| (initialStart - start, start, fileContent.slice(start, start + end)) | ||
| } | ||
|
|
||
| val codeString = text(value)._3 | ||
| val assertionString = text(assertion)._3 | ||
| q"_root_.zio.spark.test.assertZIOSparkImpl($value, $codeString, $assertionString)($assertion)" | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
zio-spark-test/src/main/scala-3/zio/spark/test/CompileVariants.scala
This file contains hidden or 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,38 @@ | ||
| package zio.spark.test | ||
|
|
||
| import zio.Trace | ||
| import zio.spark.sql.SIO | ||
| import zio.test.TestResult | ||
| import zio.internal.stacktracer.SourceLocation | ||
|
|
||
| trait CompileVariants { | ||
| inline def assertSpark[A, B](inline value: => A) (inline assertion: SparkAssertion[A, B])(implicit trace: Trace, sourceLocation: SourceLocation):SIO[TestResult] = | ||
| ${Macros.assert_impl('value)('assertion, 'trace, 'sourceLocation)} | ||
|
|
||
| inline def assertZIOSpark[A, B](inline value: SIO[A])(inline assertion: SparkAssertion[A, B])(implicit trace: Trace, sourceLocation: SourceLocation): SIO[TestResult] = | ||
| ${Macros.assertZIO_impl('value)('assertion, 'trace, 'sourceLocation)} | ||
| } | ||
|
|
||
| object CompileVariants { | ||
| def assertSparkProxy[A, B]( | ||
| value: => A, | ||
| codePart: String, | ||
| assertionPart: String | ||
| )( | ||
| assertion: SparkAssertion[A, B] | ||
| )(implicit | ||
| trace: Trace, | ||
| sourceLocation: SourceLocation | ||
| ) = zio.spark.test.assertSparkImpl(value, codePart, assertionPart)(assertion) | ||
|
|
||
| def assertZIOSparkProxy[A, B]( | ||
| value: SIO[A], | ||
| codePart: String, | ||
| assertionPart: String | ||
| )( | ||
| assertion: SparkAssertion[A, B] | ||
| )(implicit | ||
| trace: Trace, | ||
| sourceLocation: SourceLocation | ||
| ) = zio.spark.test.assertZIOSparkImpl(value, codePart, assertionPart)(assertion) | ||
| } |
38 changes: 38 additions & 0 deletions
38
zio-spark-test/src/main/scala-3/zio/spark/test/Macros.scala
This file contains hidden or 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,38 @@ | ||
| package zio.spark.test | ||
|
|
||
| import scala.reflect.macros._ | ||
|
|
||
| import zio._ | ||
| import zio.test.TestResult | ||
| import zio.test.Macros.showExpr | ||
| import zio.internal.stacktracer.SourceLocation | ||
|
|
||
| import zio.spark.sql.SIO | ||
| import zio.spark.test.SparkAssertion | ||
|
|
||
| import scala.quoted._ | ||
|
|
||
| // Pilfered (with immense gratitude & minor modifications) | ||
| // from https://github.com/zio/zio/blob/series/2.x/test/shared/src/main/scala-3/zio/test/Macros.scala | ||
| object Macros { | ||
| def assert_impl[A: Type, B: Type](value: Expr[A])(assertion: Expr[SparkAssertion[A, B]], trace: Expr[Trace], sourceLocation: Expr[SourceLocation])(using Quotes): Expr[SIO[TestResult]] = { | ||
| import quotes.reflect._ | ||
| val codeString = showExpr(value) | ||
| val assertionString = showExpr(assertion) | ||
| '{_root_.zio.spark.test.CompileVariants.assertSparkProxy($value, ${Expr(codeString)}, ${Expr(assertionString)})($assertion)($trace, $sourceLocation) | ||
| } | ||
| } | ||
|
|
||
| def assertZIO_impl[A: Type, B: Type](value: Expr[SIO[A]])(assertion: Expr[SparkAssertion[A, B]], trace: Expr[Trace], sourceLocation: Expr[SourceLocation])(using Quotes): Expr[SIO[TestResult]] = { | ||
| import quotes.reflect._ | ||
| val codeString = showExpr(value) | ||
| val assertionString = showExpr(assertion) | ||
| '{_root_.zio.spark.test.CompileVariants.assertZIOSparkProxy($value, ${Expr(codeString)}, ${Expr(assertionString)})($assertion)($trace, $sourceLocation) | ||
| } | ||
| } | ||
|
|
||
| def showExpr[A](expr: Expr[A])(using Quotes): String = { | ||
| import quotes.reflect._ | ||
| expr.asTerm.pos.sourceCode.get | ||
| } | ||
| } |
This file contains hidden or 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
41 changes: 41 additions & 0 deletions
41
zio-spark-test/src/main/scala/zio/spark/test/SparkAssertion.scala
This file contains hidden or 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,41 @@ | ||
| package zio.spark.test | ||
|
|
||
| import zio.internal.ansi.AnsiStringOps | ||
| import zio.internal.stacktracer.SourceLocation | ||
| import zio.spark.sql.{Dataset, SIO} | ||
| import zio.spark.sql.TryAnalysis.syntax.throwAnalysisException | ||
| import zio.test.{Assertion, TestArrow, TestResult} | ||
|
|
||
| /** | ||
| * Todo: instruction is just the content of f as a string such as | ||
| * {{{_.isEmpty}}} becomes {{{"isEmpty"}}}. We should use a macro | ||
| * instead. | ||
| */ | ||
| final case class SparkAssertion[A, B](f: A => SIO[B], assertion: Assertion[B], instruction: String) | ||
|
|
||
| object SparkAssertion { | ||
| private[test] def smartAssert[A]( | ||
| expr: => A, | ||
| codePart: String, | ||
| assertionPart: String, | ||
| instructionPart: String | ||
| )( | ||
| assertion: Assertion[A] | ||
| )(implicit sourceLocation: SourceLocation): TestResult = { | ||
| lazy val value0 = expr | ||
| val completeString = codePart.blue + " did not satisfy " + assertionPart.cyan | ||
| val instructionString = s"$codePart.$instructionPart" | ||
|
|
||
| TestResult( | ||
| (TestArrow.succeed(value0).withCode(instructionString) >>> assertion.arrow).withLocation | ||
| .withCompleteCode(completeString) | ||
| ) | ||
| } | ||
|
|
||
| def isEmpty[A]: SparkAssertion[Dataset[A], Boolean] = SparkAssertion(_.isEmpty, Assertion.isTrue, "isEmpty") | ||
| def shouldExist[A](expr: String): SparkAssertion[Dataset[A], Boolean] = | ||
| SparkAssertion(_.filter(expr).isEmpty, Assertion.isFalse, s"""filter("$expr").isEmpty""") | ||
|
|
||
| def shouldNotExist[A](expr: String): SparkAssertion[Dataset[A], Boolean] = | ||
| shouldExist[A](expr).copy(assertion = Assertion.isTrue) | ||
| } |
This file contains hidden or 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
38 changes: 38 additions & 0 deletions
38
zio-spark-test/src/test/scala/zio/spark/test/SparkAssertionSpec.scala
This file contains hidden or 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,38 @@ | ||
| package zio.spark.test | ||
|
|
||
| import scala3encoders.given // scalafix:ok | ||
|
|
||
| import zio.spark.sql.implicits._ | ||
| import zio.spark.test.SparkAssertion._ | ||
| import zio.test.TestAspect.failing | ||
|
|
||
| object SparkAssertionSpec extends SharedZIOSparkSpecDefault { | ||
|
|
||
| override def spec = | ||
| suite("SparkAssertion spec")( | ||
| test("Assertions should work with assertSpark") { | ||
| for { | ||
| df <- Dataset[Int]() | ||
| result <- assertSpark(df)(isEmpty) | ||
| } yield result | ||
| }, | ||
| test("It should assert that a dataset is empty") { | ||
| assertZIOSpark(Dataset[Int]())(isEmpty) | ||
| }, | ||
| test("It should fail asserting that a dataset is empty") { | ||
| assertZIOSpark(Dataset(1, 2, 3))(isEmpty) | ||
| } @@ failing, | ||
| test("It should assert that at least a row respect the predicate") { | ||
| assertZIOSpark(Dataset(1, 2, 3))(shouldExist("value == 1")) | ||
| }, | ||
| test("It should fail asserting that at least a row respect the predicate") { | ||
| assertZIOSpark(Dataset(1, 2, 3))(shouldExist("value == 4")) | ||
| } @@ failing, | ||
| test("It should assert that no rows respect the predicate") { | ||
| assertZIOSpark(Dataset(1, 2, 3))(shouldNotExist("value == 4")) | ||
| }, | ||
| test("It should fail asserting that no rows respect the predicate") { | ||
| assertZIOSpark(Dataset(1, 2, 3))(shouldNotExist("value == 1")) | ||
| } @@ failing | ||
| ) | ||
| } |
24 changes: 0 additions & 24 deletions
24
zio-spark-test/src/test/scala/zio/spark/test/ZIOSparkSpecDefaultSpec.scala
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pb, the user need the macro as a dependencie, as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it it the same expectation for zio: https://github.com/zio/zio/blob/series/2.x/project/BuildHelper.scala#L254.