|
| 1 | +package org.jetbrains.kotlinx.dataframe |
| 2 | + |
| 3 | +import io.kotest.matchers.shouldBe |
| 4 | +import org.jetbrains.kotlinx.dataframe.api.dataFrameOf |
| 5 | +import org.jetbrains.kotlinx.dataframe.api.toColumn |
| 6 | +import org.jetbrains.kotlinx.dataframe.jupyter.KotlinNotebookPluginUtils |
| 7 | +import org.junit.Test |
| 8 | +import kotlin.random.Random |
| 9 | + |
| 10 | +/** |
| 11 | + * Other tests are located in Jupyter module: |
| 12 | + * org.jetbrains.kotlinx.dataframe.jupyter.RenderingTests |
| 13 | + */ |
| 14 | +class KotlinNotebookPluginUtilsTests { |
| 15 | + @Test |
| 16 | + fun `sort lists by size descending`() { |
| 17 | + val random = Random(123) |
| 18 | + val lists = List(20) { List(random.nextInt(1, 100)) { it } } + null |
| 19 | + val df = dataFrameOf("listColumn" to lists) |
| 20 | + |
| 21 | + val res = KotlinNotebookPluginUtils.sortByColumns(df, listOf(listOf("listColumn")), desc = listOf(true)) |
| 22 | + |
| 23 | + res["listColumn"].values() shouldBe lists.sortedByDescending { it?.size ?: 0 } |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + fun `sort lists by size ascending`() { |
| 28 | + val lists = listOf(listOf(1, 2, 3), listOf(1), listOf(1, 2), null) |
| 29 | + val df = dataFrameOf("listColumn" to lists) |
| 30 | + |
| 31 | + val res = KotlinNotebookPluginUtils.sortByColumns(df, listOf(listOf("listColumn")), desc = listOf(false)) |
| 32 | + |
| 33 | + res["listColumn"].values() shouldBe listOf(null, listOf(1), listOf(1, 2), listOf(1, 2, 3)) |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + fun `sort empty lists`() { |
| 38 | + val lists = listOf(listOf(1, 2), emptyList(), listOf(1), emptyList()) |
| 39 | + val df = dataFrameOf("listColumn" to lists) |
| 40 | + |
| 41 | + val res = KotlinNotebookPluginUtils.sortByColumns(df, listOf(listOf("listColumn")), desc = listOf(true)) |
| 42 | + |
| 43 | + res["listColumn"].values() shouldBe listOf(listOf(1, 2), listOf(1), emptyList(), emptyList()) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `sort lists with equal sizes preserves stability`() { |
| 48 | + val lists = listOf(listOf("a"), listOf("b"), listOf("c")) |
| 49 | + val df = dataFrameOf("listColumn" to lists) |
| 50 | + |
| 51 | + val res = KotlinNotebookPluginUtils.sortByColumns(df, listOf(listOf("listColumn")), desc = listOf(true)) |
| 52 | + |
| 53 | + res["listColumn"].values() shouldBe lists |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun `sort frame column by row count descending`() { |
| 58 | + val frames = listOf( |
| 59 | + dataFrameOf("x" to listOf(1)), |
| 60 | + dataFrameOf("x" to listOf(1, 2, 3)), |
| 61 | + dataFrameOf("x" to listOf(1, 2)), |
| 62 | + DataFrame.empty(), |
| 63 | + ) |
| 64 | + val df = dataFrameOf("nested" to frames.toColumn()) |
| 65 | + |
| 66 | + val res = KotlinNotebookPluginUtils.sortByColumns(df, listOf(listOf("nested")), desc = listOf(true)) |
| 67 | + |
| 68 | + res["nested"].values().map { (it as DataFrame<*>).rowsCount() } shouldBe listOf(3, 2, 1, 0) |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + fun `sort frame column by row count ascending`() { |
| 73 | + val frames = listOf( |
| 74 | + dataFrameOf("x" to listOf(1, 2, 3)), |
| 75 | + dataFrameOf("x" to listOf(1)), |
| 76 | + DataFrame.empty(), |
| 77 | + ) |
| 78 | + val df = dataFrameOf("nested" to frames.toColumn()) |
| 79 | + |
| 80 | + val res = KotlinNotebookPluginUtils.sortByColumns(df, listOf(listOf("nested")), desc = listOf(false)) |
| 81 | + |
| 82 | + res["nested"].values().map { (it as DataFrame<*>).rowsCount() } shouldBe listOf(0, 1, 3) |
| 83 | + } |
| 84 | +} |
0 commit comments