From a4f8950ec3e5be15b6c10220fc9b7f4201d6e885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Chantepie?= Date: Sun, 29 Aug 2021 17:49:32 +0200 Subject: [PATCH] Failing test in LitTest: ``` - support value class *** FAILED *** (101 milliseconds) [info] Array(Q(1,[0,1000000005,6d65726f4c]), Q(2,[0,1000000005,6d65726f4c])) was not equal to List(Q(1,Lorem), Q(2,Lorem)) (LitTests.scala:70) ``` --- .../main/scala/frameless/RecordEncoder.scala | 3 +- .../src/test/scala/frameless/LitTests.scala | 33 ++++++++++++++++--- .../scala/frameless/RecordEncoderTests.scala | 4 ++- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/dataset/src/main/scala/frameless/RecordEncoder.scala b/dataset/src/main/scala/frameless/RecordEncoder.scala index 096b7cb92..2f293e297 100644 --- a/dataset/src/main/scala/frameless/RecordEncoder.scala +++ b/dataset/src/main/scala/frameless/RecordEncoder.scala @@ -176,7 +176,8 @@ class RecordEncoder[F, G <: HList, H <: HList] } } -final class RecordFieldEncoder[T](val encoder: TypedEncoder[T]) +final class RecordFieldEncoder[T]( + val encoder: TypedEncoder[T]) extends Serializable object RecordFieldEncoder extends RecordFieldEncoderLowPriority { diff --git a/dataset/src/test/scala/frameless/LitTests.scala b/dataset/src/test/scala/frameless/LitTests.scala index 4d9760c65..0c81f65f6 100644 --- a/dataset/src/test/scala/frameless/LitTests.scala +++ b/dataset/src/test/scala/frameless/LitTests.scala @@ -2,10 +2,13 @@ package frameless import frameless.functions.lit -import org.scalacheck.Prop -import org.scalacheck.Prop._ +import org.scalatest.matchers.should.Matchers -class LitTests extends TypedDatasetSuite { +import org.scalacheck.{ Arbitrary, Gen, Prop }, Prop._ + +import RecordEncoderTests.Name + +class LitTests extends TypedDatasetSuite with Matchers { def prop[A: TypedEncoder](value: A): Prop = { val df: TypedDataset[Int] = TypedDataset.create(1 :: Nil) @@ -21,7 +24,6 @@ class LitTests extends TypedDatasetSuite { .run() .toVector - (localElems ?= Vector(value)) && (elems ?= Vector(value)) } @@ -45,10 +47,29 @@ class LitTests extends TypedDatasetSuite { check(prop[Food] _) + implicit def nameArb: Arbitrary[Name] = + Arbitrary(Gen.alphaStr.map(new Name(_))) + + check(prop[Name] _) + // doesn't work, object has to be serializable // check(prop[frameless.LocalDateTime] _) } + test("support value class") { + val initial = Seq( + Q(name = new Name("Foo"), id = 1), + Q(name = new Name("Bar"), id = 2)) + val ds = TypedDataset.create(initial) + + ds.collect.run() shouldBe initial + + val lorem = new Name("Lorem") + + ds.withColumnReplaced('name, lit(lorem)). + collect.run() shouldBe initial.map(_.copy(name = lorem)) + } + test("#205: comparing literals encoded using Injection") { import org.apache.spark.sql.catalyst.util.DateTimeUtils implicit val dateAsInt: Injection[java.sql.Date, Int] = @@ -58,8 +79,10 @@ class LitTests extends TypedDatasetSuite { val data = Vector(P(42, today)) val tds = TypedDataset.create(data) - tds.filter(tds('d) === today).collect().run() + tds.filter(tds('d) === today).collect.run().map(_.i) shouldBe Seq(42) } } final case class P(i: Int, d: java.sql.Date) + +final case class Q(id: Int, name: Name) diff --git a/dataset/src/test/scala/frameless/RecordEncoderTests.scala b/dataset/src/test/scala/frameless/RecordEncoderTests.scala index 384aeb3db..5e4a905f4 100644 --- a/dataset/src/test/scala/frameless/RecordEncoderTests.scala +++ b/dataset/src/test/scala/frameless/RecordEncoderTests.scala @@ -25,7 +25,9 @@ object RecordEncoderTests { case class B(a: Seq[A]) case class C(b: B) - class Name(val value: String) extends AnyVal with Serializable + class Name(val value: String) extends AnyVal with Serializable { + override def toString = value + } case class Person(name: Name, age: Int)