Does anyone know if there is a recommended way to ...
# multiplatform
a
Does anyone know if there is a recommended way to unit test SQLDelight common code on Android? You need an android context for the sql driver. Should I use instrumented unit tests or Roboelectric? Is there some other better way?
j
This is article from last year by Sam Edwards that also covers unit testing SQLDelight code https://handstandsam.com/2019/08/23/sqldelight-1-x-quick-start-guide-for-android/
one key thing is that the unit tests are running on JVM and therefore use SQLDelight's JVM driver (JdbcSqliteDriver)
s
I found it easier to use an Android driver (in-memory) because it takes care of running table creation queries for me iirc.
p
I used the in memory JDBC driver to run unit tests
I found the documentation to be slightly out of date, hopefully this helps:
Copy code
commonTest {
    dependencies {
      implementation kotlin('test-common')
      implementation kotlin('test-annotations-common')

        implementation "com.squareup.sqldelight:sqlite-driver:$rootProject.sqlDelightVer"
        implementation "com.squareup.sqldelight:jdbc-driver:$rootProject.sqlDelightVer"
    }
}
and to create the db
Copy code
val driver: SqlDriver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
Database.Schema.create(driver)
val db = Database(driver)
242 Views