Skip to content

Instantly share code, notes, and snippets.

@oharaandrew314
Last active January 24, 2022 03:27
Show Gist options
  • Save oharaandrew314/e4de318c332e55b2014f446c81093552 to your computer and use it in GitHub Desktop.
Save oharaandrew314/e4de318c332e55b2014f446c81093552 to your computer and use it in GitHub Desktop.
import org.h2.jdbcx.JdbcDataSource
fun PetsDao.Companion.mock(): PetsDao {
val source = JdbcDataSource().apply {
setURL("jdbc:h2:mem:${UUID.randomUUID()};MODE=MySQL;DB_CLOSE_DELAY=-1")
this.user = "sa"
this.password = ""
connection.use { conn ->
conn.prepareStatement("CREATE TABLE pets (id INT(64) PRIMARY KEY NOT NULL AUTO_INCREMENT, name TEXT NOT NULL)").executeUpdate()
conn.prepareStatement("CREATE TABLE photos (id INT(64) PRIMARY KEY NOT NULL AUTO_INCREMENT, pet_id INT(64), url TEXT NOT NULL)").executeUpdate()
}
}
return PetsDao(source)
}
class PetsDaoTest {
private val testObj = PetsDao.mock()
@Test
fun `get missing`() {
testObj[123] shouldBe null
}
@Test
fun `create and get`() {
val id = testObj.create("Tigger")
testObj[id] shouldBe Pet(
id = id,
name = "Tigger",
photoUrls = emptyList()
)
}
@Test
fun `add photo urls and get`() {
val id = testObj.create("Tigger")
testObj.addImage(id, "http://foo.jpg")
testObj.addImage(id, "http://bar.jpg")
testObj[id] shouldBe Pet(
id = id,
name = "Tigger",
photoUrls = listOf("http://foo.jpg", "http://bar.jpg")
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment