|
1 | 1 | package org.jetbrains.kotlinx.dataframe.jupyter
|
2 | 2 |
|
| 3 | +import com.beust.klaxon.* |
3 | 4 | import io.kotest.matchers.shouldBe
|
4 | 5 | import io.kotest.matchers.string.shouldContain
|
5 | 6 | import io.kotest.matchers.string.shouldNotContain
|
6 | 7 | import org.intellij.lang.annotations.Language
|
| 8 | +import org.jetbrains.kotlinx.jupyter.api.MimeTypedResult |
7 | 9 | import org.jetbrains.kotlinx.jupyter.testkit.JupyterReplTestCase
|
8 | 10 | import org.junit.Test
|
9 | 11 |
|
@@ -74,4 +76,53 @@ class RenderingTests : JupyterReplTestCase() {
|
74 | 76 | htmlLight shouldNotContain darkClassAttribute
|
75 | 77 | htmlDark shouldContain darkClassAttribute
|
76 | 78 | }
|
| 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 | + } |
77 | 128 | }
|
0 commit comments