Skip to content

Commit acfbdcf

Browse files
committed
Add tests for the count function
1 parent a5a069f commit acfbdcf

File tree

1 file changed

+175
-0
lines changed
  • core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api

1 file changed

+175
-0
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package org.jetbrains.kotlinx.dataframe.api
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.jetbrains.kotlinx.dataframe.nrow
5+
import org.junit.Test
6+
7+
class CountTests {
8+
9+
// Test data
10+
11+
val df = dataFrameOf(
12+
"name" to columnOf("Alice", "Bob", "Charlie"),
13+
"age" to columnOf(15, 20, 25),
14+
"group" to columnOf(1, 1, 2)
15+
)
16+
val age = df["age"].cast<Int>()
17+
val name = df["name"].cast<String>()
18+
val grouped = df.groupBy("group")
19+
val pivot = df.pivot("group")
20+
21+
val emptyDf = df.drop(df.nrow)
22+
23+
val dfWithNulls = df.append("Martin", null, null)
24+
val ageWithNulls = dfWithNulls["age"].cast<Int?>()
25+
val groupedWithNulls = dfWithNulls.groupBy("group")
26+
val pivotWithNulls = dfWithNulls.pivot("group")
27+
28+
// DataColumn
29+
30+
@Test
31+
fun `count on column`() {
32+
age.count() shouldBe 3
33+
age.count { it > 18 } shouldBe 2
34+
name.count { it.startsWith("A") } shouldBe 1
35+
}
36+
37+
@Test
38+
fun `count on empty column`() {
39+
emptyDf["name"].count() shouldBe 0
40+
emptyDf["name"].count { it == "Alice" } shouldBe 0
41+
}
42+
43+
@Test
44+
fun `count on column with nulls`() {
45+
ageWithNulls.count() shouldBe 4
46+
ageWithNulls.count { it == null } shouldBe 1
47+
}
48+
49+
// DataRow
50+
51+
@Test
52+
fun `count on row`() {
53+
val row = df[0]
54+
row.count() shouldBe 3
55+
(row.count { it is Number }) shouldBe 2
56+
}
57+
58+
@Test
59+
fun `count on row with nulls`() {
60+
val row = dfWithNulls[3]
61+
row.count() shouldBe 3
62+
row.count { it == null } shouldBe 2
63+
}
64+
65+
// DataFrame
66+
67+
@Test
68+
fun `count on dataframe`() {
69+
df.count() shouldBe 3
70+
df.count { age > 18 } shouldBe 2
71+
df.count { it["name"] == "Alice" } shouldBe 1
72+
}
73+
74+
@Test
75+
fun `count on empty dataframe`() {
76+
emptyDf.count() shouldBe 0
77+
}
78+
79+
@Test
80+
fun `count on dataframe with nulls`() {
81+
dfWithNulls.count() shouldBe 4
82+
dfWithNulls.count { it["age"] != null } shouldBe 3
83+
}
84+
85+
// GroupBy
86+
87+
@Test
88+
fun `count on grouped dataframe`() {
89+
val groupedCount = grouped.count()
90+
val expected = dataFrameOf(
91+
"group" to columnOf(1, 2),
92+
"count" to columnOf(2, 1)
93+
)
94+
groupedCount shouldBe expected
95+
}
96+
97+
@Test
98+
fun `count on grouped dataframe with predicate`() {
99+
val groupedCount = grouped.count { "age"<Int>() > 18 }
100+
val expected = dataFrameOf(
101+
"group" to columnOf(1, 2),
102+
"count" to columnOf(1, 1)
103+
)
104+
groupedCount shouldBe expected
105+
}
106+
107+
@Test
108+
fun `count on empty grouped dataframe`() {
109+
emptyDf.groupBy("group").count().count() shouldBe 0
110+
}
111+
112+
@Test
113+
fun `count on grouped dataframe with nulls`() {
114+
val groupedWithNullsCount = groupedWithNulls.count()
115+
val expected = dataFrameOf(
116+
"group" to columnOf(1, 2, null),
117+
"count" to columnOf(2, 1, 1)
118+
)
119+
groupedWithNullsCount shouldBe expected
120+
}
121+
122+
@Test
123+
fun `count on grouped dataframe with nulls and predicate`() {
124+
val groupedWithNullsCount = groupedWithNulls.count { it["age"] != null }
125+
val expected = dataFrameOf(
126+
"group" to columnOf(1, 2, null),
127+
"count" to columnOf(2, 1, 0)
128+
)
129+
groupedWithNullsCount shouldBe expected
130+
}
131+
132+
// Pivot
133+
134+
@Test
135+
fun `count on pivot`() {
136+
val counted = pivot.count()
137+
val expected = dataFrameOf(
138+
"1" to columnOf(2),
139+
"2" to columnOf(1)
140+
)[0]
141+
counted shouldBe expected
142+
}
143+
144+
@Test
145+
fun `count on pivot with predicate`() {
146+
val counted = pivot.count { "group"<Int>() != 1 }
147+
val expected = dataFrameOf(
148+
"1" to columnOf(0),
149+
"2" to columnOf(1)
150+
)[0]
151+
counted shouldBe expected
152+
}
153+
154+
@Test
155+
fun `count on pivot with nulls`() {
156+
val counted = pivotWithNulls.count()
157+
val expected = dataFrameOf(
158+
"1" to columnOf(2),
159+
"2" to columnOf(1),
160+
"null" to columnOf(1)
161+
)[0]
162+
counted shouldBe expected
163+
}
164+
165+
@Test
166+
fun `count on pivot with nulls and predicate`() {
167+
val counted = pivotWithNulls.count { it["age"] != null }
168+
val expected = dataFrameOf(
169+
"1" to columnOf(2),
170+
"2" to columnOf(1),
171+
"null" to columnOf(0)
172+
)[0]
173+
counted shouldBe expected
174+
}
175+
}

0 commit comments

Comments
 (0)