Skip to content

Commit

Permalink
test window frame
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentlauvlwj committed Jan 14, 2023
1 parent e007f54 commit ff9855b
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions ktorm-core/src/test/kotlin/org/ktorm/dsl/WindowFunctionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package org.ktorm.dsl

import org.junit.Test
import org.ktorm.BaseTest
import org.ktorm.dsl.WindowFrames.currentRow
import org.ktorm.dsl.WindowFrames.following
import org.ktorm.dsl.WindowFrames.preceding
import org.ktorm.dsl.WindowFrames.unboundedFollowing
import org.ktorm.dsl.WindowFrames.unboundedPreceding
import kotlin.test.assertEquals

/**
Expand Down Expand Up @@ -378,4 +383,88 @@ class WindowFunctionTest : BaseTest() {

assertEquals(setOf("vince:1", "marry:2", "tom:3", "penny:4"), results.toSet())
}

@Test
fun testMultiPartitionBy() {
val results = database
.from(Employees)
.select(Employees.name, max(Employees.salary).over { partitionBy(Employees.id, Employees.name) })
.map { row ->
"${row.getString(1)}:${row.getLong(2)}"
}

assertEquals(setOf("vince:100", "marry:50", "tom:200", "penny:100"), results.toSet())
}

@Test
fun testMultiOrderBy() {
val results = database
.from(Employees)
.select(Employees.name, sum(Employees.salary).over { orderBy(Employees.id.asc(), Employees.name.desc()) })
.map { row ->
"${row.getString(1)}:${row.getLong(2)}"
}

assertEquals(setOf("vince:100", "marry:150", "tom:350", "penny:450"), results.toSet())
}

@Test
fun testRows() {
val results = database
.from(Employees)
.select(
Employees.name,
sum(Employees.salary).over { orderBy(Employees.id.asc()).rows(preceding(1)) }
)
.map { row ->
"${row.getString(1)}:${row.getLong(2)}"
}

assertEquals(setOf("vince:100", "marry:150", "tom:250", "penny:300"), results.toSet())
}

@Test
fun testRowsBetween() {
val results = database
.from(Employees)
.select(
Employees.name,
sum(Employees.salary).over { orderBy(Employees.id.asc()).rowsBetween(currentRow(), following(1)) }
)
.map { row ->
"${row.getString(1)}:${row.getLong(2)}"
}

assertEquals(setOf("vince:150", "marry:250", "tom:300", "penny:100"), results.toSet())
}

@Test
fun testRange() {
val results = database
.from(Employees)
.select(
Employees.name,
sum(Employees.salary).over { orderBy(Employees.salary.asc()).range(preceding(100)) }
)
.map { row ->
"${row.getString(1)}:${row.getLong(2)}"
}

assertEquals(setOf("marry:50", "vince:250", "penny:250", "tom:400"), results.toSet())
}

@Test
fun testRangeBetween() {
val results = database
.from(Employees)
.select(
Employees.name,
sum(Employees.salary).over { orderBy(Employees.id.asc()).rangeBetween(unboundedPreceding(), unboundedFollowing()) }
)
.map { row ->
"${row.getString(1)}:${row.getLong(2)}"
}

assertEquals(setOf("vince:450", "marry:450", "tom:450", "penny:450"), results.toSet())
}
}

0 comments on commit ff9855b

Please sign in to comment.