Skip to content

Commit 0165c43

Browse files
committed
Fix tests code according to quality check
1 parent 8c09cf4 commit 0165c43

File tree

5 files changed

+149
-91
lines changed
  • ktorm-global/src/test/kotlin/org/ktorm/global
  • ktorm-support-mysql/src/test/kotlin/org/ktorm/support/mysql
  • ktorm-support-oracle/src/test/kotlin/org/ktorm/support/oracle
  • ktorm-support-sqlite/src/test/kotlin/org/ktorm/support/sqlite
  • ktorm-support-sqlserver/src/test/kotlin/org/ktorm/support/sqlserver

5 files changed

+149
-91
lines changed

ktorm-global/src/test/kotlin/org/ktorm/global/GlobalQueryTest.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import org.ktorm.expression.ScalarExpression
99
* Created by vince at Apr 05, 2020.
1010
*/
1111
class GlobalQueryTest : BaseGlobalTest() {
12+
companion object {
13+
const val TWO = 2
14+
}
1215

1316
@Test
1417
fun testSelect() {
@@ -164,14 +167,20 @@ class GlobalQueryTest : BaseGlobalTest() {
164167
}
165168
}
166169

170+
/**
171+
* An exception is thrown because pagination should be provided by dialects.
172+
*/
167173
@Test(expected = DialectFeatureNotSupportedException::class)
168174
fun testLimitWithoutOffset() {
169-
Employees.select().orderBy(Employees.id.desc()).limit(2).iterator()
175+
Employees.select().orderBy(Employees.id.desc()).limit(TWO).iterator()
170176
}
171177

178+
/**
179+
* An exception is thrown because pagination should be provided by dialects.
180+
*/
172181
@Test(expected = DialectFeatureNotSupportedException::class)
173182
fun testOffsetWithoutLimit() {
174-
Employees.select().orderBy(Employees.id.desc()).offset(2).iterator()
183+
Employees.select().orderBy(Employees.id.desc()).offset(TWO).iterator()
175184
}
176185

177186
@Test

ktorm-support-mysql/src/test/kotlin/org/ktorm/support/mysql/MySqlTest.kt

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ import java.util.concurrent.TimeoutException
2626
class MySqlTest : BaseTest() {
2727

2828
companion object {
29+
const val TOTAL_RECORDS = 4
30+
const val MINUS_ONE = -1
31+
const val ZERO = 0
32+
const val ONE = 1
33+
const val TWO = 2
34+
const val ID_1 = 1
35+
const val ID_2 = 2
36+
const val ID_3 = 3
37+
const val ID_4 = 4
38+
2939
class KMySqlContainer : MySQLContainer<KMySqlContainer>()
3040

3141
@ClassRule
@@ -84,50 +94,52 @@ class MySqlTest : BaseTest() {
8494
assert(ids[1] == 3)
8595
}
8696

97+
/**
98+
* Verifies that invalid pagination parameters are ignored.
99+
*/
87100
@Test
88101
fun testBothLimitAndOffsetAreNotPositive() {
89-
val totalRecords = 4
90-
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(0, -1)
91-
assert(query.totalRecords == totalRecords)
102+
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(ZERO, MINUS_ONE)
103+
assert(query.totalRecords == TOTAL_RECORDS)
92104

93105
val ids = query.map { it[Employees.id] }
94-
assert(ids.size == totalRecords)
95-
assert(ids[0] == 4)
96-
assert(ids[1] == 3)
97-
assert(ids[2] == 2)
98-
assert(ids[3] == 1)
106+
assert(ids == listOf(ID_4, ID_3, ID_2, ID_1))
99107
}
100108

109+
/**
110+
* Verifies that limit parameter works as expected.
111+
*/
101112
@Test
102113
fun testLimitWithoutOffset() {
103-
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(2)
104-
assert(query.totalRecords == 4)
114+
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(TWO)
115+
assert(query.totalRecords == TOTAL_RECORDS)
105116

106117
val ids = query.map { it[Employees.id] }
107-
assert(ids.size == 2)
108-
assert(ids[0] == 4)
109-
assert(ids[1] == 3)
118+
assert(ids == listOf(ID_4, ID_3))
110119
}
111120

121+
/**
122+
* Verifies that offset parameter works as expected.
123+
*/
112124
@Test
113125
fun testOffsetWithoutLimit() {
114-
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(2)
115-
assert(query.totalRecords == 4)
126+
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(TWO)
127+
assert(query.totalRecords == TOTAL_RECORDS)
116128

117129
val ids = query.map { it[Employees.id] }
118-
assert(ids.size == 2)
119-
assert(ids[0] == 2)
120-
assert(ids[1] == 1)
130+
assert(ids == listOf(ID_2, ID_1))
121131
}
122132

133+
/**
134+
* Verifies that limit and offset parameters work together as expected.
135+
*/
123136
@Test
124137
fun testOffsetWithLimit() {
125-
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(2).limit(1)
126-
assert(query.totalRecords == 4)
138+
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(TWO).limit(ONE)
139+
assert(query.totalRecords == TOTAL_RECORDS)
127140

128141
val ids = query.map { it[Employees.id] }
129-
assert(ids.size == 1)
130-
assert(ids[0] == 2)
142+
assert(ids == listOf(ID_2))
131143
}
132144

133145
@Test

ktorm-support-oracle/src/test/kotlin/org/ktorm/support/oracle/OracleTest.kt

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ import java.util.concurrent.TimeoutException
2525
class OracleTest : BaseTest() {
2626

2727
companion object {
28+
const val TOTAL_RECORDS = 4
29+
const val MINUS_ONE = -1
30+
const val ZERO = 0
31+
const val ONE = 1
32+
const val TWO = 2
33+
const val ID_1 = 1
34+
const val ID_2 = 2
35+
const val ID_3 = 3
36+
const val ID_4 = 4
37+
2838
@ClassRule
2939
@JvmField
3040
val oracle: OracleContainer = OracleContainer("zerda/oracle-database:11.2.0.2-xe")
@@ -78,55 +88,56 @@ class OracleTest : BaseTest() {
7888
assert(query.totalRecords == 4)
7989

8090
val ids = query.map { it[Employees.id] }
81-
assert(ids.size == 2)
8291
assert(ids[0] == 4)
8392
assert(ids[1] == 3)
8493
}
8594

95+
/**
96+
* Verifies that invalid pagination parameters are ignored.
97+
*/
8698
@Test
8799
fun testBothLimitAndOffsetAreNotPositive() {
88-
val totalRecords = 4
89-
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(0, -1)
90-
assert(query.totalRecords == totalRecords)
100+
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(ZERO, MINUS_ONE)
101+
assert(query.totalRecords == TOTAL_RECORDS)
91102

92103
val ids = query.map { it[Employees.id] }
93-
assert(ids.size == totalRecords)
94-
assert(ids[0] == 4)
95-
assert(ids[1] == 3)
96-
assert(ids[2] == 2)
97-
assert(ids[3] == 1)
104+
assert(ids == listOf(ID_4, ID_3, ID_2, ID_1))
98105
}
99106

107+
/**
108+
* Verifies that limit parameter works as expected.
109+
*/
100110
@Test
101111
fun testLimitWithoutOffset() {
102-
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(2)
103-
assert(query.totalRecords == 4)
112+
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(TWO)
113+
assert(query.totalRecords == TOTAL_RECORDS)
104114

105115
val ids = query.map { it[Employees.id] }
106-
assert(ids.size == 2)
107-
assert(ids[0] == 4)
108-
assert(ids[1] == 3)
116+
assert(ids == listOf(ID_4, ID_3))
109117
}
110118

119+
/**
120+
* Verifies that offset parameter works as expected.
121+
*/
111122
@Test
112123
fun testOffsetWithoutLimit() {
113-
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(2)
114-
assert(query.totalRecords == 4)
124+
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(TWO)
125+
assert(query.totalRecords == TOTAL_RECORDS)
115126

116127
val ids = query.map { it[Employees.id] }
117-
assert(ids.size == 2)
118-
assert(ids[0] == 2)
119-
assert(ids[1] == 1)
128+
assert(ids == listOf(ID_2, ID_1))
120129
}
121130

131+
/**
132+
* Verifies that limit and offset parameters work together as expected.
133+
*/
122134
@Test
123135
fun testOffsetWithLimit() {
124-
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(2).limit(1)
125-
assert(query.totalRecords == 4)
136+
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(TWO).limit(ONE)
137+
assert(query.totalRecords == TOTAL_RECORDS)
126138

127139
val ids = query.map { it[Employees.id] }
128-
assert(ids.size == 1)
129-
assert(ids[0] == 2)
140+
assert(ids == listOf(ID_2))
130141
}
131142

132143
@Test

ktorm-support-sqlite/src/test/kotlin/org/ktorm/support/sqlite/SQLiteTest.kt

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ import java.time.LocalDate
2121
*/
2222
class SQLiteTest : BaseTest() {
2323

24+
companion object {
25+
const val TOTAL_RECORDS = 4
26+
const val MINUS_ONE = -1
27+
const val ZERO = 0
28+
const val ONE = 1
29+
const val TWO = 2
30+
const val ID_1 = 1
31+
const val ID_2 = 2
32+
const val ID_3 = 3
33+
const val ID_4 = 4
34+
}
35+
2436
lateinit var connection: Connection
2537

2638
override fun init() {
@@ -77,50 +89,52 @@ class SQLiteTest : BaseTest() {
7789
assert(ids[1] == 3)
7890
}
7991

92+
/**
93+
* Verifies that invalid pagination parameters are ignored.
94+
*/
8095
@Test
8196
fun testBothLimitAndOffsetAreNotPositive() {
82-
val totalRecords = 4
83-
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(0, -1)
84-
assert(query.totalRecords == totalRecords)
97+
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(ZERO, MINUS_ONE)
98+
assert(query.totalRecords == TOTAL_RECORDS)
8599

86100
val ids = query.map { it[Employees.id] }
87-
assert(ids.size == totalRecords)
88-
assert(ids[0] == 4)
89-
assert(ids[1] == 3)
90-
assert(ids[2] == 2)
91-
assert(ids[3] == 1)
101+
assert(ids == listOf(ID_4, ID_3, ID_2, ID_1))
92102
}
93103

104+
/**
105+
* Verifies that limit parameter works as expected.
106+
*/
94107
@Test
95108
fun testLimitWithoutOffset() {
96-
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(2)
97-
assert(query.totalRecords == 4)
109+
val query = database.from(Employees).select().orderBy(Employees.id.desc()).limit(TWO)
110+
assert(query.totalRecords == TOTAL_RECORDS)
98111

99112
val ids = query.map { it[Employees.id] }
100-
assert(ids.size == 2)
101-
assert(ids[0] == 4)
102-
assert(ids[1] == 3)
113+
assert(ids == listOf(ID_4, ID_3))
103114
}
104115

116+
/**
117+
* Verifies that offset parameter works as expected.
118+
*/
105119
@Test
106120
fun testOffsetWithoutLimit() {
107-
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(2)
108-
assert(query.totalRecords == 4)
121+
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(TWO)
122+
assert(query.totalRecords == TOTAL_RECORDS)
109123

110124
val ids = query.map { it[Employees.id] }
111-
assert(ids.size == 2)
112-
assert(ids[0] == 2)
113-
assert(ids[1] == 1)
125+
assert(ids == listOf(ID_2, ID_1))
114126
}
115127

128+
/**
129+
* Verifies that limit and offset parameters work together as expected.
130+
*/
116131
@Test
117132
fun testOffsetWithLimit() {
118-
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(2).limit(1)
119-
assert(query.totalRecords == 4)
133+
val query = database.from(Employees).select().orderBy(Employees.id.desc()).offset(TWO).limit(ONE)
134+
assert(query.totalRecords == TOTAL_RECORDS)
120135

121136
val ids = query.map { it[Employees.id] }
122-
assert(ids.size == 1)
123-
assert(ids[0] == 2)
137+
assert(ids == listOf(ID_2))
124138
}
125139

126140
@Test

0 commit comments

Comments
 (0)