Skip to content

Commit

Permalink
new tests taking sortWithConstructors into account
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolanrensen committed Apr 9, 2024
1 parent 89ddd20 commit b918f1b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class JavaPojo {

public JavaPojo() {}

public JavaPojo(int a, String b) {
public JavaPojo(String b, int a) {
this.a = a;
this.b = b;
}
Expand Down Expand Up @@ -51,8 +51,8 @@ public int hashCode() {
@Override
public String toString() {
return "TestPojo{" +
"a=" + a +
", b='" + b + '\'' +
'}';
"a=" + a +
", b='" + b + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class CreateDataFrameTests {
fun treatErasedGenericAsAny() {
class IncompatibleVersionErrorData<T>(val expected: T, val actual: T)
class DeserializedContainerSource(val incompatibility: IncompatibleVersionErrorData<*>)

val functions = listOf(DeserializedContainerSource(IncompatibleVersionErrorData(1, 2)))

val df = functions.toDataFrame(maxDepth = 2)
Expand Down Expand Up @@ -320,11 +321,19 @@ class CreateDataFrameTests {

constructor()

constructor(a: Int, b: String) {
constructor(b: String, a: Int) {
this.a = a
this.b = b
}

constructor(b: String) {
this.b = b
}

constructor(a: Int) {
this.a = a
}

fun getA(): Int = a
fun setA(a: Int) {
this.a = a
Expand Down Expand Up @@ -358,18 +367,21 @@ class CreateDataFrameTests {

@Test
fun `convert POJO to DF`() {
listOf(KotlinPojo(1, "a")).toDataFrame() shouldBe dataFrameOf("a", "b")(1, "a")
listOf(JavaPojo(1, "a")).toDataFrame() shouldBe dataFrameOf("a", "b")(1, "a")
// even though the names b, a, follow the constructor order
listOf(KotlinPojo("bb", 1)).toDataFrame() shouldBe dataFrameOf("b", "a")("bb", 1)

// cannot read java constructor parameter names with reflection, so sort lexigographically
listOf(JavaPojo("bb", 1)).toDataFrame() shouldBe dataFrameOf("a", "b")(1, "bb")

listOf(KotlinPojo(1, "a")).toDataFrame { properties(KotlinPojo::getA) } shouldBe dataFrameOf("a")(1)
listOf(KotlinPojo(1, "a")).toDataFrame { properties(KotlinPojo::getB) } shouldBe dataFrameOf("b")("a")
listOf(KotlinPojo("bb", 1)).toDataFrame { properties(KotlinPojo::getA) } shouldBe dataFrameOf("a")(1)
listOf(KotlinPojo("bb", 1)).toDataFrame { properties(KotlinPojo::getB) } shouldBe dataFrameOf("b")("bb")

listOf(JavaPojo(1, "a")).toDataFrame {
listOf(JavaPojo("bb", 1)).toDataFrame {
properties(JavaPojo::getA)
} shouldBe dataFrameOf("a")(1)

listOf(JavaPojo(1, "a")).toDataFrame {
listOf(JavaPojo("bb", 1)).toDataFrame {
properties(JavaPojo::getB)
} shouldBe dataFrameOf("b")("a")
} shouldBe dataFrameOf("b")("bb")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class JavaPojo {

public JavaPojo() {}

public JavaPojo(int a, String b) {
public JavaPojo(String b, int a) {
this.a = a;
this.b = b;
}
Expand Down Expand Up @@ -51,8 +51,8 @@ public int hashCode() {
@Override
public String toString() {
return "TestPojo{" +
"a=" + a +
", b='" + b + '\'' +
'}';
"a=" + a +
", b='" + b + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class CreateDataFrameTests {
fun treatErasedGenericAsAny() {
class IncompatibleVersionErrorData<T>(val expected: T, val actual: T)
class DeserializedContainerSource(val incompatibility: IncompatibleVersionErrorData<*>)

val functions = listOf(DeserializedContainerSource(IncompatibleVersionErrorData(1, 2)))

val df = functions.toDataFrame(maxDepth = 2)
Expand Down Expand Up @@ -320,11 +321,19 @@ class CreateDataFrameTests {

constructor()

constructor(a: Int, b: String) {
constructor(b: String, a: Int) {
this.a = a
this.b = b
}

constructor(b: String) {
this.b = b
}

constructor(a: Int) {
this.a = a
}

fun getA(): Int = a
fun setA(a: Int) {
this.a = a
Expand Down Expand Up @@ -358,18 +367,21 @@ class CreateDataFrameTests {

@Test
fun `convert POJO to DF`() {
listOf(KotlinPojo(1, "a")).toDataFrame() shouldBe dataFrameOf("a", "b")(1, "a")
listOf(JavaPojo(1, "a")).toDataFrame() shouldBe dataFrameOf("a", "b")(1, "a")
// even though the names b, a, follow the constructor order
listOf(KotlinPojo("bb", 1)).toDataFrame() shouldBe dataFrameOf("b", "a")("bb", 1)

// cannot read java constructor parameter names with reflection, so sort lexigographically
listOf(JavaPojo("bb", 1)).toDataFrame() shouldBe dataFrameOf("a", "b")(1, "bb")

listOf(KotlinPojo(1, "a")).toDataFrame { properties(KotlinPojo::getA) } shouldBe dataFrameOf("a")(1)
listOf(KotlinPojo(1, "a")).toDataFrame { properties(KotlinPojo::getB) } shouldBe dataFrameOf("b")("a")
listOf(KotlinPojo("bb", 1)).toDataFrame { properties(KotlinPojo::getA) } shouldBe dataFrameOf("a")(1)
listOf(KotlinPojo("bb", 1)).toDataFrame { properties(KotlinPojo::getB) } shouldBe dataFrameOf("b")("bb")

listOf(JavaPojo(1, "a")).toDataFrame {
listOf(JavaPojo("bb", 1)).toDataFrame {
properties(JavaPojo::getA)
} shouldBe dataFrameOf("a")(1)

listOf(JavaPojo(1, "a")).toDataFrame {
listOf(JavaPojo("bb", 1)).toDataFrame {
properties(JavaPojo::getB)
} shouldBe dataFrameOf("b")("a")
} shouldBe dataFrameOf("b")("bb")
}
}

0 comments on commit b918f1b

Please sign in to comment.