SQLDelight in Android: Getting Started

Aug 3 2021 · Kotlin 1.4, Android 11, Android Studio 4.1

Part 1: Preparation & Setup

11. Validate & Test Database Code

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 10. Create Views for Reusable Sub-Queries Next episode: 12. Integrate with RxJava

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

When it comes to database logic, validation and integrity play an important role - arguably even moreso than other application code. As developers, we have to make sure that the schema of the database is always consistent and correct, especially when migrations come into play that transform the schema over time.

CREATE TABLE bug (
    bugId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    description TEXT,
    imageUrl TEXT,
+    foo INTEGER
);
CREATE TABLE bug (
    bugId INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    description TEXT,
    imageUrl TEXT,
-    foo INTEGER
);
dependencies {
    testImplementation "junit:junit:4.13.2"
}
dependencies {
    testImplementation "com.squareup.sqldelight:sqlite-driver:$sqldelightVersion"
}
@Test
fun `test creating a new collection`() {
}
@Test
fun `test creating a new collection`() {
+    val repository = DatabaseRepository(...)
+
+    repository.addCollection("New Collection")
+
+    val collections = repository.listCollections().executeAsList()
+    assertEquals(1, collections.size)
+    assertEquals("New Collection", collections.first().name)
}
+private val driver = JdbcSqliteDriver(IN_MEMORY).also(Database.Schema::create)

@Test
fun `test creating a new collection`() {
+    val repository = DatabaseRepository(driver)

    repository.addCollection("New Collection")

    val collections = repository.listCollections().executeAsList()
    assertEquals(1, collections.size)
    assertEquals("New Collection", collections.first().name)
}
-private val driver = JdbcSqliteDriver(IN_MEMORY)
+private val driver = JdbcSqliteDriver(IN_MEMORY).also(Database.Schema::create)