Skip to content

Commit 8ff090b

Browse files
committed
adding pojo tests
1 parent 18dbbbf commit 8ff090b

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.jetbrains.kotlinx.dataframe.api;
2+
3+
import java.util.Objects;
4+
5+
public class JavaPojo {
6+
7+
private int a;
8+
private String b;
9+
10+
public JavaPojo() {}
11+
12+
public JavaPojo(int a, String b) {
13+
this.a = a;
14+
this.b = b;
15+
}
16+
17+
public int getA() {
18+
return a;
19+
}
20+
21+
public void setA(int a) {
22+
this.a = a;
23+
}
24+
25+
public String getB() {
26+
return b;
27+
}
28+
29+
public void setB(String b) {
30+
this.b = b;
31+
}
32+
33+
@Override
34+
public boolean equals(Object o) {
35+
if (this == o) return true;
36+
if (o == null || getClass() != o.getClass()) return false;
37+
38+
JavaPojo testPojo = (JavaPojo) o;
39+
40+
if (a != testPojo.a) return false;
41+
return Objects.equals(b, testPojo.b);
42+
}
43+
44+
@Override
45+
public int hashCode() {
46+
int result = a;
47+
result = 31 * result + (b != null ? b.hashCode() : 0);
48+
return result;
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return "TestPojo{" +
54+
"a=" + a +
55+
", b='" + b + '\'' +
56+
'}';
57+
}
58+
}

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/toDataFrame.kt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,64 @@ class CreateDataFrameTests {
312312
fun `convert private class with public members`() {
313313
listOf(PrivateClass(1)).toDataFrame() shouldBe dataFrameOf("a")(1)
314314
}
315+
316+
class KotlinPojo {
317+
318+
private var a: Int = 0
319+
private var b: String = ""
320+
321+
constructor()
322+
323+
constructor(a: Int, b: String) {
324+
this.a = a
325+
this.b = b
326+
}
327+
328+
fun getA(): Int = a
329+
fun setA(a: Int) {
330+
this.a = a
331+
}
332+
333+
fun getB(): String = b
334+
fun setB(b: String) {
335+
this.b = b
336+
}
337+
338+
override fun equals(other: Any?): Boolean {
339+
if (this === other) return true
340+
if (other !is KotlinPojo) return false
341+
342+
if (a != other.a) return false
343+
if (b != other.b) return false
344+
345+
return true
346+
}
347+
348+
override fun hashCode(): Int {
349+
var result = a
350+
result = 31 * result + b.hashCode()
351+
return result
352+
}
353+
354+
override fun toString(): String {
355+
return "FakePojo(a=$a, b='$b')"
356+
}
357+
}
358+
359+
@Test
360+
fun `convert POJO to DF`() {
361+
listOf(KotlinPojo(1, "a")).toDataFrame() shouldBe dataFrameOf("a", "b")(1, "a")
362+
listOf(JavaPojo(1, "a")).toDataFrame() shouldBe dataFrameOf("a", "b")(1, "a")
363+
364+
listOf(KotlinPojo(1, "a")).toDataFrame { properties(KotlinPojo::getA) } shouldBe dataFrameOf("a")(1)
365+
listOf(KotlinPojo(1, "a")).toDataFrame { properties(KotlinPojo::getB) } shouldBe dataFrameOf("b")("a")
366+
367+
listOf(JavaPojo(1, "a")).toDataFrame {
368+
properties(JavaPojo::getA)
369+
} shouldBe dataFrameOf("a")(1)
370+
371+
listOf(JavaPojo(1, "a")).toDataFrame {
372+
properties(JavaPojo::getB)
373+
} shouldBe dataFrameOf("b")("a")
374+
}
315375
}

0 commit comments

Comments
 (0)