Skip to content

Commit

Permalink
Enforced single call convention for parts of "MappingSupportSQLiteQuery"
Browse files Browse the repository at this point in the history
Also added workaround for tests

*This commit is related to issue #529 [1]*

[1] #529
  • Loading branch information
JaniruTEC committed Apr 22, 2024
1 parent 77fa76f commit cfde324
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.sqlite.db.SupportSQLiteOpenHelper
import androidx.sqlite.db.SupportSQLiteProgram
import androidx.sqlite.db.SupportSQLiteQuery
import androidx.sqlite.db.SupportSQLiteStatement
import timber.log.Timber

internal class MappingSupportSQLiteDatabase(
private val delegate: SupportSQLiteDatabase,
Expand Down Expand Up @@ -166,8 +165,8 @@ internal class MappingSupportSQLiteDatabase(
) : SupportSQLiteQuery by delegateQuery {

private val _sql = map(delegateQuery.sql)
private val sqlDelegate = OneOffDelegate { Timber.tag("MappingSupportSQLiteQuery").e("SQL queried twice") }
private val bindToDelegate = OneOffDelegate { Timber.tag("MappingSupportSQLiteQuery").e("bindTo called twice") }
private val sqlDelegate = OneOffDelegate { throw IllegalStateException("SQL queried twice") }
private val bindToDelegate = OneOffDelegate { throw IllegalStateException("bindTo called twice") }

override val sql: String
get() = sqlDelegate.call { _sql }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class MappingSupportSQLiteDatabaseTest {
identityMapping.query(SimpleSQLiteQuery("SELECT `col` FROM `id_test`"))
commentMapping.query(SimpleSQLiteQuery("SELECT `col` FROM `comment_test`"))

val supportSQLiteQueryProperties = newCachedSupportSQLiteQueryProperties()
verify(delegateMock).query(
anyPseudoEquals(SimpleSQLiteQuery("SELECT `col` FROM `id_test`"), supportSQLiteQueryProperties)
)
Expand All @@ -134,6 +135,7 @@ class MappingSupportSQLiteDatabaseTest {
identityMapping.query(SimpleSQLiteQuery("SELECT `col` FROM `id_test` WHERE `col` = ?", arrayOf("test1")))
commentMapping.query(SimpleSQLiteQuery("SELECT `col` FROM `comment_test` WHERE `col` = ?", arrayOf("test2")))

val supportSQLiteQueryProperties = newCachedSupportSQLiteQueryProperties()
verify(delegateMock).query(
anyPseudoEquals(SimpleSQLiteQuery("SELECT `col` FROM `id_test` WHERE `col` = ?", arrayOf("test1")), supportSQLiteQueryProperties)
)
Expand All @@ -151,6 +153,7 @@ class MappingSupportSQLiteDatabaseTest {
identityMapping.query(queries.idCall, signals.idCall)
commentMapping.query(queries.commentCall, signals.commentCall)

val supportSQLiteQueryProperties = newCachedSupportSQLiteQueryProperties()
verify(delegateMock).query(
anyPseudoEquals(queries.idExpected, supportSQLiteQueryProperties),
anyPseudoEqualsUnlessNull(signals.idExpected, setOf<ValueExtractor<CancellationSignal>>(CancellationSignal::isCanceled))
Expand Down Expand Up @@ -352,6 +355,15 @@ private class PseudoEqualsMatcher<T : Any>(

private typealias ValueExtractor<T> = (T) -> Any?

private data class CacheEntry(val value: Any?) //Allows correct handling of nulls

private fun <T : Any> ValueExtractor<T>.asCached(): ValueExtractor<T> {
val cache = mutableMapOf<T, CacheEntry>()
return {
cache.computeIfAbsent(it) { key -> CacheEntry(this@asCached(key)) }.value
}
}

private inline fun <T> OngoingStubbing<T>.thenDo(crossinline action: (invocation: InvocationOnMock) -> Unit): OngoingStubbing<T> = thenAnswer { action(it) }

private class NullHandlingMatcher<T>(
Expand All @@ -367,10 +379,13 @@ private class NullHandlingMatcher<T>(
}
}

private val supportSQLiteQueryProperties: Set<ValueExtractor<SupportSQLiteQuery>>
get() = setOf(SupportSQLiteQuery::sql, SupportSQLiteQuery::argCount, { query: SupportSQLiteQuery ->
private fun newCachedSupportSQLiteQueryProperties(): Set<ValueExtractor<SupportSQLiteQuery>> = setOf(
SupportSQLiteQuery::sql.asCached(),
SupportSQLiteQuery::argCount,
{ query: SupportSQLiteQuery ->
CachingSupportSQLiteProgram().also { query.bindTo(it) }.bindings
})
}.asCached()
)

private class CachingSupportSQLiteProgram : SupportSQLiteProgram {

Expand Down

0 comments on commit cfde324

Please sign in to comment.