|
1 | 1 | package org.jetbrains.kotlinx.dataframe.api
|
2 | 2 |
|
3 | 3 | import io.kotest.matchers.shouldBe
|
4 |
| -import org.jetbrains.kotlinx.dataframe.impl.columns.singleImpl |
| 4 | +import org.jetbrains.kotlinx.dataframe.DataFrame |
| 5 | +import org.jetbrains.kotlinx.dataframe.DataRow |
| 6 | +import org.jetbrains.kotlinx.dataframe.annotations.DataSchema |
| 7 | +import org.jetbrains.kotlinx.dataframe.impl.columns.asValueColumn |
5 | 8 | import org.jetbrains.kotlinx.dataframe.samples.api.TestBase
|
6 | 9 | import org.jetbrains.kotlinx.dataframe.samples.api.age
|
7 | 10 | import org.jetbrains.kotlinx.dataframe.samples.api.firstName
|
8 | 11 | import org.jetbrains.kotlinx.dataframe.samples.api.isHappy
|
| 12 | +import org.jetbrains.kotlinx.dataframe.samples.api.lastName |
9 | 13 | import org.jetbrains.kotlinx.dataframe.samples.api.name
|
10 | 14 | import org.junit.Test
|
11 | 15 |
|
@@ -110,4 +114,95 @@ class ColumnsSelectionDslTests : TestBase() {
|
110 | 114 | }
|
111 | 115 | } shouldBe df.select { name.firstName }
|
112 | 116 | }
|
| 117 | + |
| 118 | + @Test |
| 119 | + fun col() { |
| 120 | + df.select { col("age") } shouldBe df.select { age } |
| 121 | + df.select { col<Int>("age") } shouldBe df.select { age } |
| 122 | + df.select { col(pathOf("age")) } shouldBe df.select { age } |
| 123 | + df.select { col<Int>(pathOf("age")) } shouldBe df.select { age } |
| 124 | + df.select { col(Person::age) } shouldBe df.select { age } |
| 125 | + |
| 126 | + df.select { colGroup("name").col("firstName") } shouldBe df.select { name.firstName } |
| 127 | + df.select { colGroup("name").col<String>("firstName") } shouldBe df.select { name.firstName } |
| 128 | + df.select { colGroup("name").col(pathOf("firstName")) } shouldBe df.select { name.firstName } |
| 129 | + df.select { colGroup("name").col<String>(pathOf("firstName")) } shouldBe df.select { name.firstName } |
| 130 | + df.select { colGroup("name").col(Name::firstName) } shouldBe df.select { name.firstName } |
| 131 | + } |
| 132 | + |
| 133 | + @DataSchema |
| 134 | + interface FirstNames { |
| 135 | + val firstName: String |
| 136 | + val secondName: String? |
| 137 | + val thirdName: String? |
| 138 | + } |
| 139 | + |
| 140 | + @DataSchema |
| 141 | + interface MyName : Name { |
| 142 | + val firstNames: FirstNames |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + fun colGroup() { |
| 147 | + val firstNames by columnGroup<FirstNames>() |
| 148 | + val dfGroup = df.convert { name.firstName }.to { |
| 149 | + val firstName by it |
| 150 | + val secondName by it.map<_, String?> { null }.asValueColumn() |
| 151 | + val thirdName by it.map<_, String?> { null }.asValueColumn() |
| 152 | + |
| 153 | + dataFrameOf(firstName, secondName, thirdName) |
| 154 | + .cast<FirstNames>(verify = true) |
| 155 | + .asColumnGroup(firstNames) |
| 156 | + } |
| 157 | + |
| 158 | + dfGroup.print(columnTypes = true, title = true) |
| 159 | + |
| 160 | + dfGroup.select { colGroup("name") } shouldBe dfGroup.select { name } |
| 161 | + dfGroup.select { colGroup<String>("name") } shouldBe dfGroup.select { name } |
| 162 | + dfGroup.select { colGroup(pathOf("name")) } shouldBe dfGroup.select { name } |
| 163 | + dfGroup.select { colGroup<String>(pathOf("name")) } shouldBe dfGroup.select { name } |
| 164 | + dfGroup.select { colGroup(Person::name) } shouldBe dfGroup.select { name } |
| 165 | + |
| 166 | + dfGroup.select { colGroup("name").colGroup("firstNames") } shouldBe dfGroup.select { name[firstNames] } |
| 167 | + dfGroup.select { colGroup("name").colGroup<String>("firstNames") } shouldBe dfGroup.select { name[firstNames] } |
| 168 | + dfGroup.select { colGroup("name").colGroup(pathOf("firstNames")) } shouldBe dfGroup.select { name[firstNames] } |
| 169 | + dfGroup.select { colGroup("name").colGroup<String>(pathOf("firstNames")) } shouldBe dfGroup.select { name[firstNames] } |
| 170 | + dfGroup.select { colGroup("name").colGroup(MyName::firstNames) } shouldBe dfGroup.select { name[firstNames] } |
| 171 | + } |
| 172 | + |
| 173 | + @DataSchema |
| 174 | + interface PersonWithFrame : Person { |
| 175 | + val frameCol: DataFrame<Person> |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + fun frameCol() { |
| 180 | + val frameCol by frameColumn<Person>() |
| 181 | + |
| 182 | + val dfWithFrames = df |
| 183 | + .add { |
| 184 | + expr { df } into frameCol |
| 185 | + } |
| 186 | + .convert { name }.to { |
| 187 | + val firstName by it.asColumnGroup().firstName |
| 188 | + val lastName by it.asColumnGroup().lastName |
| 189 | + |
| 190 | + @Suppress("NAME_SHADOWING") |
| 191 | + val frameCol by it.map { df }.asFrameColumn() |
| 192 | + |
| 193 | + dataFrameOf(firstName, lastName, frameCol).asColumnGroup("name") |
| 194 | + } |
| 195 | + |
| 196 | + dfWithFrames.select { frameCol("frameCol") } shouldBe dfWithFrames.select { frameCol } |
| 197 | + dfWithFrames.select { frameCol<Person>("frameCol") } shouldBe dfWithFrames.select { frameCol } |
| 198 | + dfWithFrames.select { frameCol(pathOf("frameCol")) } shouldBe dfWithFrames.select { frameCol } |
| 199 | + dfWithFrames.select { frameCol<Person>(pathOf("frameCol")) } shouldBe dfWithFrames.select { frameCol } |
| 200 | + dfWithFrames.select { frameCol(PersonWithFrame::frameCol) } shouldBe dfWithFrames.select { frameCol } |
| 201 | + |
| 202 | + dfWithFrames.select { colGroup("name").frameCol("frameCol") } shouldBe dfWithFrames.select { name[frameCol] } |
| 203 | + dfWithFrames.select { colGroup("name").frameCol<Person>("frameCol") } shouldBe dfWithFrames.select { name[frameCol] } |
| 204 | + dfWithFrames.select { colGroup("name").frameCol(pathOf("frameCol")) } shouldBe dfWithFrames.select { name[frameCol] } |
| 205 | + dfWithFrames.select { colGroup("name").frameCol<Person>(pathOf("frameCol")) } shouldBe dfWithFrames.select { name[frameCol] } |
| 206 | + dfWithFrames.select { colGroup("name").frameCol(PersonWithFrame::frameCol) } shouldBe dfWithFrames.select { name[frameCol] } |
| 207 | + } |
113 | 208 | }
|
0 commit comments