Skip to content

Commit 5f04641

Browse files
committed
Add the null-values unit tests
1 parent 5fbf014 commit 5f04641

File tree

6 files changed

+53
-22
lines changed

6 files changed

+53
-22
lines changed

sqllin-dsl/src/androidInstrumentedTest/kotlin/com/ctrip/sqllin/dsl/AndroidTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class AndroidTest {
7474
@Test
7575
fun testPrimitiveTypeForKSP() = commonTest.testPrimitiveTypeForKSP()
7676

77+
@Test
78+
fun testNullInsertAndSelect() = commonTest.testNullInsertAndSelect()
79+
7780
@Before
7881
fun setUp() {
7982
val context = InstrumentationRegistry.getInstrumentation().targetContext

sqllin-dsl/src/commonMain/kotlin/com/ctrip/sqllin/dsl/sql/compiler/AbstractValuesEncoder.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.ctrip.sqllin.dsl.sql.compiler
1818

1919
import kotlinx.serialization.ExperimentalSerializationApi
20-
import kotlinx.serialization.SerializationStrategy
2120
import kotlinx.serialization.descriptors.SerialDescriptor
2221
import kotlinx.serialization.encoding.AbstractEncoder
2322
import kotlinx.serialization.modules.EmptySerializersModule
@@ -83,18 +82,8 @@ internal abstract class AbstractValuesEncoder : AbstractEncoder() {
8382
sqlStrBuilder.append(value).appendTail()
8483
}
8584

86-
override fun <T : Any> encodeNullableSerializableElement(
87-
descriptor: SerialDescriptor,
88-
index: Int,
89-
serializer: SerializationStrategy<T>,
90-
value: T?
91-
) {
92-
if (value == null) {
93-
elementsCount = descriptor.elementsCount
94-
elementsIndex = index
95-
sqlStrBuilder.append("NULL").appendTail()
96-
} else
97-
super.encodeNullableSerializableElement(descriptor, index, serializer, value)
85+
override fun encodeNull() {
86+
sqlStrBuilder.append("NULL").appendTail()
9887
}
9988

10089
override fun encodeEnum(enumDescriptor: SerialDescriptor, index: Int) = encodeInt(index)

sqllin-dsl/src/commonTest/kotlin/com/ctrip/sqllin/dsl/CommonBasicTest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class CommonBasicTest(private val path: DatabasePath) {
4040
const val DATABASE_NAME = "BookStore.db"
4141
const val SQL_CREATE_BOOK = "create table book (id integer primary key autoincrement, name text, author text, pages integer, price real)"
4242
const val SQL_CREATE_CATEGORY = "create table category (id integer primary key autoincrement, name text, code integer)"
43+
const val SQL_CREATE_NULL_TESTER = "create table NullTester (id integer primary key autoincrement, paramInt integer, paramString text, paramDouble real)"
4344
}
4445

4546
private inline fun Database.databaseAutoClose(block: (Database) -> Unit) = try {
@@ -427,6 +428,30 @@ class CommonBasicTest(private val path: DatabasePath) {
427428
}
428429
}
429430

431+
fun testNullInsertAndSelect() {
432+
val config = DatabaseConfiguration(
433+
name = DATABASE_NAME,
434+
path = path,
435+
version = 1,
436+
create = {
437+
it.execSQL(SQL_CREATE_NULL_TESTER)
438+
}
439+
)
440+
Database(config, true).databaseAutoClose { database ->
441+
lateinit var selectStatement: SelectStatement<NullTester>
442+
database {
443+
NullTesterTable { table ->
444+
table INSERT listOf(
445+
NullTester(null, null, null),
446+
NullTester(8, "888", 8.8),
447+
)
448+
selectStatement = table SELECT X
449+
}
450+
}
451+
assertEquals(2, selectStatement.getResults().size)
452+
}
453+
}
454+
430455
private fun getDefaultDBConfig(): DatabaseConfiguration =
431456
DatabaseConfiguration(
432457
name = DATABASE_NAME,

sqllin-dsl/src/commonTest/kotlin/com/ctrip/sqllin/dsl/Entities.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,25 @@ data class Category(
4242

4343
@Serializable
4444
data class Joiner(
45-
val name: String,
46-
val author: String,
47-
val price: Double,
48-
val pages: Int,
49-
val code: Int,
45+
val name: String?,
46+
val author: String?,
47+
val price: Double?,
48+
val pages: Int?,
49+
val code: Int?,
5050
)
5151

5252
@Serializable
5353
data class CrossJoiner(
54-
val author: String,
55-
val price: Double,
56-
val pages: Int,
57-
val code: Int,
54+
val author: String?,
55+
val price: Double?,
56+
val pages: Int?,
57+
val code: Int?,
58+
)
59+
60+
@DBRow("NullTester")
61+
@Serializable
62+
data class NullTester(
63+
val paramInt: Int?,
64+
val paramString: String?,
65+
val paramDouble: Double?,
5866
)

sqllin-dsl/src/jvmTest/kotlin/com/ctrip/sqllin/dsl/JvmTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class JvmTest {
6565
@Test
6666
fun testPrimitiveTypeForKSP() = commonTest.testPrimitiveTypeForKSP()
6767

68+
@Test
69+
fun testNullInsertAndSelect() = commonTest.testNullInsertAndSelect()
70+
6871
@BeforeTest
6972
fun setUp() {
7073
deleteDatabase(path, CommonBasicTest.DATABASE_NAME)

sqllin-dsl/src/nativeTest/kotlin/com/ctrip/sqllin/dsl/NativeTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class NativeTest {
6868
@Test
6969
fun testPrimitiveTypeForKSP() = commonTest.testPrimitiveTypeForKSP()
7070

71+
@Test
72+
fun testNullInsertAndSelect() = commonTest.testNullInsertAndSelect()
73+
7174
@BeforeTest
7275
fun setUp() {
7376
deleteDatabase(path, CommonBasicTest.DATABASE_NAME)

0 commit comments

Comments
 (0)