Skip to content

Commit e9bac15

Browse files
committed
Put tests related to readJsonStr in one file
1 parent 949a8d7 commit e9bac15

File tree

3 files changed

+78
-88
lines changed
  • core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/io
  • tests/src/test/kotlin/org/jetbrains/kotlinx/dataframe

3 files changed

+78
-88
lines changed

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/io/JsonTests.kt

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/parse.kt

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import kotlinx.datetime.Instant
55
import kotlinx.datetime.LocalDate
66
import kotlinx.datetime.LocalDateTime
77
import org.jetbrains.kotlinx.dataframe.DataFrame
8-
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
9-
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
10-
import org.jetbrains.kotlinx.dataframe.io.readJsonStr
118
import org.jetbrains.kotlinx.dataframe.type
129
import org.junit.Test
1310
import java.time.LocalTime
@@ -19,70 +16,6 @@ import kotlin.time.Duration.Companion.minutes
1916
import kotlin.time.Duration.Companion.seconds
2017

2118
class ParseTests {
22-
23-
@Test
24-
fun parseJson1() {
25-
val json = """[
26-
{"a":1, "b":"text"},
27-
{"a":2, "b":5, "c":4.5}
28-
]
29-
""".trimIndent()
30-
val df = DataFrame.readJsonStr(json)
31-
df.columnsCount() shouldBe 3
32-
df.rowsCount() shouldBe 2
33-
df["a"].type() shouldBe typeOf<Int>()
34-
df["b"].type() shouldBe typeOf<Comparable<*>>()
35-
df["c"].type() shouldBe typeOf<Double?>()
36-
}
37-
38-
@Test
39-
fun parseJson2() {
40-
val json = """[
41-
{"a":"text"},
42-
{"a":{"b":2}},
43-
{"a":[6,7,8]}
44-
]
45-
""".trimIndent()
46-
val df = DataFrame.readJsonStr(json)
47-
println(df)
48-
df.columnsCount() shouldBe 1
49-
df.rowsCount() shouldBe 3
50-
val group = df["a"] as ColumnGroup<*>
51-
group.columnsCount() shouldBe 3
52-
group["b"].type() shouldBe typeOf<Int?>()
53-
group["value"].type() shouldBe typeOf<String?>()
54-
group["array"].type() shouldBe typeOf<List<Int>>()
55-
}
56-
57-
@Test
58-
fun parseJson3() {
59-
val json = """[
60-
{"a":[3, 5]},
61-
{},
62-
{"a":[3.4, 5.6]}
63-
]
64-
""".trimIndent()
65-
val df = DataFrame.readJsonStr(json)
66-
df.columnsCount() shouldBe 1
67-
df.rowsCount() shouldBe 3
68-
df["a"].type() shouldBe typeOf<List<Number>>()
69-
df[1]["a"] shouldBe emptyList<Int>()
70-
}
71-
72-
@Test
73-
fun parseJson4() {
74-
val json = """[
75-
{"a":[ {"b":2}, {"c":3} ]},
76-
{"a":[ {"b":4}, {"d":5} ]}
77-
]
78-
""".trimIndent()
79-
val df = DataFrame.readJsonStr(json)
80-
df.columnsCount() shouldBe 1
81-
df.rowsCount() shouldBe 2
82-
println(df)
83-
val group = df["a"] as FrameColumn<*>
84-
}
85-
8619
@Test
8720
fun parseDate() {
8821
val date by columnOf("January 1, 2020")

tests/src/test/kotlin/org/jetbrains/kotlinx/dataframe/io/json.kt

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,89 @@ import org.jetbrains.kotlinx.dataframe.DataFrame
55
import org.jetbrains.kotlinx.dataframe.api.convert
66
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
77
import org.jetbrains.kotlinx.dataframe.api.toDouble
8+
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
9+
import org.jetbrains.kotlinx.dataframe.columns.FrameColumn
810
import org.junit.Test
911
import kotlin.reflect.*
1012

1113
class JsonTests {
1214

15+
@Test
16+
fun parseJson1() {
17+
val json = """[
18+
{"a":1, "b":"text"},
19+
{"a":2, "b":5, "c":4.5}
20+
]
21+
""".trimIndent()
22+
val df = DataFrame.readJsonStr(json)
23+
df.columnsCount() shouldBe 3
24+
df.rowsCount() shouldBe 2
25+
df["a"].type() shouldBe typeOf<Int>()
26+
df["b"].type() shouldBe typeOf<Comparable<*>>()
27+
df["c"].type() shouldBe typeOf<Double?>()
28+
}
29+
30+
@Test
31+
fun parseJson2() {
32+
val json = """[
33+
{"a":"text"},
34+
{"a":{"b":2}},
35+
{"a":[6,7,8]}
36+
]
37+
""".trimIndent()
38+
val df = DataFrame.readJsonStr(json)
39+
println(df)
40+
df.columnsCount() shouldBe 1
41+
df.rowsCount() shouldBe 3
42+
val group = df["a"] as ColumnGroup<*>
43+
group.columnsCount() shouldBe 3
44+
group["b"].type() shouldBe typeOf<Int?>()
45+
group["value"].type() shouldBe typeOf<String?>()
46+
group["array"].type() shouldBe typeOf<List<Int>>()
47+
}
48+
49+
@Test
50+
fun parseJson3() {
51+
val json = """[
52+
{"a":[3, 5]},
53+
{},
54+
{"a":[3.4, 5.6]}
55+
]
56+
""".trimIndent()
57+
val df = DataFrame.readJsonStr(json)
58+
df.columnsCount() shouldBe 1
59+
df.rowsCount() shouldBe 3
60+
df["a"].type() shouldBe typeOf<List<Number>>()
61+
df[1]["a"] shouldBe emptyList<Int>()
62+
}
63+
64+
@Test
65+
fun parseJson4() {
66+
val json = """[
67+
{"a":[ {"b":2}, {"c":3} ]},
68+
{"a":[ {"b":4}, {"d":5} ]}
69+
]
70+
""".trimIndent()
71+
val df = DataFrame.readJsonStr(json)
72+
df.columnsCount() shouldBe 1
73+
df.rowsCount() shouldBe 2
74+
println(df)
75+
df
76+
val group = df["a"] as FrameColumn<*>
77+
}
78+
79+
@Test
80+
fun `write df with primitive types`() {
81+
val df = dataFrameOf("colInt", "colDouble?", "colBoolean?")(
82+
1, 1.0, true,
83+
2, null, false,
84+
3, 3.0, null
85+
)
86+
87+
val res = DataFrame.readJsonStr(df.toJson())
88+
res shouldBe df
89+
}
90+
1391
@Test
1492
fun `NaN double serialization`() {
1593
val df = dataFrameOf("v")(1.1, Double.NaN)

0 commit comments

Comments
 (0)