Skip to content

Commit 0371330

Browse files
committed
Add tests for KotlinNotbookPluginUtils
1 parent 7ddeab1 commit 0371330

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/jupyter/RenderingTests.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.jetbrains.kotlinx.dataframe.jupyter
22

3+
import com.beust.klaxon.*
34
import io.kotest.matchers.shouldBe
45
import io.kotest.matchers.string.shouldContain
56
import io.kotest.matchers.string.shouldNotContain
67
import org.intellij.lang.annotations.Language
8+
import org.jetbrains.kotlinx.jupyter.api.MimeTypedResult
79
import org.jetbrains.kotlinx.jupyter.testkit.JupyterReplTestCase
810
import org.junit.Test
911

@@ -74,4 +76,53 @@ class RenderingTests : JupyterReplTestCase() {
7476
htmlLight shouldNotContain darkClassAttribute
7577
htmlDark shouldContain darkClassAttribute
7678
}
79+
80+
@Test
81+
fun `test kotlin notebook plugin utils rows subset`() {
82+
@Language("kts")
83+
val result = exec<MimeTypedResult>(
84+
"""
85+
data class Row(val id: Int)
86+
val df = (1..100).map { Row(it) }.toDataFrame()
87+
KotlinNotebookPluginUtils.getRowsSubsetForRendering(df, 20 , 50)
88+
""".trimIndent()
89+
)
90+
91+
val json = parseDataframeJson(result)
92+
93+
json.int("nrow") shouldBe 30
94+
json.int("ncol") shouldBe 1
95+
96+
val rows = json.array<JsonArray<*>>("kotlin_dataframe")!!
97+
rows.getObj(0).int("id") shouldBe 21
98+
rows.getObj(rows.lastIndex).int("id") shouldBe 50
99+
}
100+
101+
private fun parseDataframeJson(result: MimeTypedResult): JsonObject {
102+
val parser = Parser.default()
103+
return parser.parse(StringBuilder(result["application/kotlindataframe+json"]!!)) as JsonObject
104+
}
105+
106+
private fun JsonArray<*>.getObj(index: Int) = this.get(index) as JsonObject
107+
108+
@Test
109+
fun `test kotlin notebook plugin utils groupby`() {
110+
@Language("kts")
111+
val result = exec<MimeTypedResult>(
112+
"""
113+
data class Row(val id: Int, val group: Int)
114+
val df = (1..100).map { Row(it, if (it <= 50) 1 else 2) }.toDataFrame()
115+
KotlinNotebookPluginUtils.getRowsSubsetForRendering(df.groupBy("group"), 0, 10)
116+
""".trimIndent()
117+
)
118+
119+
val json = parseDataframeJson(result)
120+
121+
json.int("nrow") shouldBe 2
122+
json.int("ncol") shouldBe 2
123+
124+
val rows = json.array<JsonArray<*>>("kotlin_dataframe")!!
125+
rows.getObj(0).array<JsonObject>("group1")!!.size shouldBe 50
126+
rows.getObj(1).array<JsonObject>("group1")!!.size shouldBe 50
127+
}
77128
}

0 commit comments

Comments
 (0)