|
|
@ -29,16 +29,16 @@ import org.junit.Before
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.Test
|
|
|
|
|
|
|
|
|
|
|
|
class RepositoryTest : BaseUnitTest() {
|
|
|
|
class RepositoryTest : BaseUnitTest() {
|
|
|
|
private var repository: Repository<ThingRecord>? = null
|
|
|
|
private lateinit var repository: Repository<ThingRecord>
|
|
|
|
private var db: Database? = null
|
|
|
|
private lateinit var db: Database
|
|
|
|
@Before
|
|
|
|
@Before
|
|
|
|
@Throws(Exception::class)
|
|
|
|
@Throws(Exception::class)
|
|
|
|
override fun setUp() {
|
|
|
|
override fun setUp() {
|
|
|
|
super.setUp()
|
|
|
|
super.setUp()
|
|
|
|
db = buildMemoryDatabase()
|
|
|
|
db = buildMemoryDatabase()
|
|
|
|
repository = Repository(ThingRecord::class.java, db!!)
|
|
|
|
repository = Repository(ThingRecord::class.java, db)
|
|
|
|
db!!.execute("drop table if exists tests")
|
|
|
|
db.execute("drop table if exists tests")
|
|
|
|
db!!.execute(
|
|
|
|
db.execute(
|
|
|
|
"create table tests(" +
|
|
|
|
"create table tests(" +
|
|
|
|
"id integer not null primary key autoincrement, " +
|
|
|
|
"id integer not null primary key autoincrement, " +
|
|
|
|
"color_number integer not null, score float not null, " +
|
|
|
|
"color_number integer not null, score float not null, " +
|
|
|
@ -49,11 +49,11 @@ class RepositoryTest : BaseUnitTest() {
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testFind() {
|
|
|
|
fun testFind() {
|
|
|
|
db!!.execute(
|
|
|
|
db.execute(
|
|
|
|
"insert into tests(id, color_number, name, score) " +
|
|
|
|
"insert into tests(id, color_number, name, score) " +
|
|
|
|
"values (10, 20, 'hello', 8.0)"
|
|
|
|
"values (10, 20, 'hello', 8.0)"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
val record = repository!!.find(10L)
|
|
|
|
val record = repository.find(10L)
|
|
|
|
Assert.assertNotNull(record)
|
|
|
|
Assert.assertNotNull(record)
|
|
|
|
MatcherAssert.assertThat(record!!.id, IsEqual.equalTo(10L))
|
|
|
|
MatcherAssert.assertThat(record!!.id, IsEqual.equalTo(10L))
|
|
|
|
MatcherAssert.assertThat(record.color, IsEqual.equalTo(20))
|
|
|
|
MatcherAssert.assertThat(record.color, IsEqual.equalTo(20))
|
|
|
@ -64,38 +64,30 @@ class RepositoryTest : BaseUnitTest() {
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testSave_withId() {
|
|
|
|
fun testSave_withId() {
|
|
|
|
val record = ThingRecord()
|
|
|
|
val record = ThingRecord().apply {
|
|
|
|
record.id = 50L
|
|
|
|
id = 50L
|
|
|
|
record.color = 10
|
|
|
|
color = 10
|
|
|
|
record.name = "hello"
|
|
|
|
name = "hello"
|
|
|
|
record.score = 5.0
|
|
|
|
score = 5.0
|
|
|
|
repository!!.save(record)
|
|
|
|
}
|
|
|
|
MatcherAssert.assertThat(
|
|
|
|
repository.save(record)
|
|
|
|
record,
|
|
|
|
MatcherAssert.assertThat(record, IsEqual.equalTo(repository.find(50L)))
|
|
|
|
IsEqual.equalTo(
|
|
|
|
|
|
|
|
repository!!.find(50L)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
record.name = "world"
|
|
|
|
record.name = "world"
|
|
|
|
record.score = 128.0
|
|
|
|
record.score = 128.0
|
|
|
|
repository!!.save(record)
|
|
|
|
repository.save(record)
|
|
|
|
MatcherAssert.assertThat(
|
|
|
|
MatcherAssert.assertThat(record, IsEqual.equalTo(repository.find(50L)))
|
|
|
|
record,
|
|
|
|
|
|
|
|
IsEqual.equalTo(
|
|
|
|
|
|
|
|
repository!!.find(50L)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testSave_withNull() {
|
|
|
|
fun testSave_withNull() {
|
|
|
|
val record = ThingRecord()
|
|
|
|
val record = ThingRecord().apply {
|
|
|
|
record.color = 50
|
|
|
|
color = 50
|
|
|
|
record.name = null
|
|
|
|
name = null
|
|
|
|
record.score = 12.0
|
|
|
|
score = 12.0
|
|
|
|
repository!!.save(record)
|
|
|
|
}
|
|
|
|
val retrieved = repository!!.find(record.id!!)
|
|
|
|
repository.save(record)
|
|
|
|
|
|
|
|
val retrieved = repository.find(record.id!!)
|
|
|
|
Assert.assertNotNull(retrieved)
|
|
|
|
Assert.assertNotNull(retrieved)
|
|
|
|
Assert.assertNull(retrieved!!.name)
|
|
|
|
Assert.assertNull(retrieved!!.name)
|
|
|
|
MatcherAssert.assertThat(record, IsEqual.equalTo(retrieved))
|
|
|
|
MatcherAssert.assertThat(record, IsEqual.equalTo(retrieved))
|
|
|
@ -104,16 +96,18 @@ class RepositoryTest : BaseUnitTest() {
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testSave_withoutId() {
|
|
|
|
fun testSave_withoutId() {
|
|
|
|
val r1 = ThingRecord()
|
|
|
|
val r1 = ThingRecord().apply {
|
|
|
|
r1.color = 10
|
|
|
|
color = 10
|
|
|
|
r1.name = "hello"
|
|
|
|
name = "hello"
|
|
|
|
r1.score = 16.0
|
|
|
|
score = 16.0
|
|
|
|
repository!!.save(r1)
|
|
|
|
}
|
|
|
|
val r2 = ThingRecord()
|
|
|
|
repository.save(r1)
|
|
|
|
r2.color = 20
|
|
|
|
val r2 = ThingRecord().apply {
|
|
|
|
r2.name = "world"
|
|
|
|
color = 20
|
|
|
|
r2.score = 2.0
|
|
|
|
name = "world"
|
|
|
|
repository!!.save(r2)
|
|
|
|
score = 2.0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
repository.save(r2)
|
|
|
|
MatcherAssert.assertThat(r1.id, IsEqual.equalTo(1L))
|
|
|
|
MatcherAssert.assertThat(r1.id, IsEqual.equalTo(1L))
|
|
|
|
MatcherAssert.assertThat(r2.id, IsEqual.equalTo(2L))
|
|
|
|
MatcherAssert.assertThat(r2.id, IsEqual.equalTo(2L))
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -121,40 +115,27 @@ class RepositoryTest : BaseUnitTest() {
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
@Throws(Exception::class)
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun testRemove() {
|
|
|
|
fun testRemove() {
|
|
|
|
val rec1 = ThingRecord()
|
|
|
|
val rec1 = ThingRecord().apply {
|
|
|
|
rec1.color = 10
|
|
|
|
color = 10
|
|
|
|
rec1.name = "hello"
|
|
|
|
name = "hello"
|
|
|
|
rec1.score = 16.0
|
|
|
|
score = 16.0
|
|
|
|
repository!!.save(rec1)
|
|
|
|
}
|
|
|
|
val rec2 = ThingRecord()
|
|
|
|
repository.save(rec1)
|
|
|
|
rec2.color = 20
|
|
|
|
val rec2 = ThingRecord().apply {
|
|
|
|
rec2.name = "world"
|
|
|
|
color = 20
|
|
|
|
rec2.score = 32.0
|
|
|
|
name = "world"
|
|
|
|
repository!!.save(rec2)
|
|
|
|
score = 32.0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
repository.save(rec2)
|
|
|
|
val id = rec1.id!!
|
|
|
|
val id = rec1.id!!
|
|
|
|
MatcherAssert.assertThat(
|
|
|
|
MatcherAssert.assertThat(rec1, IsEqual.equalTo(repository.find(id)))
|
|
|
|
rec1,
|
|
|
|
MatcherAssert.assertThat(rec2, IsEqual.equalTo(repository.find(rec2.id!!)))
|
|
|
|
IsEqual.equalTo(
|
|
|
|
repository.remove(rec1)
|
|
|
|
repository!!.find(id)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
MatcherAssert.assertThat(
|
|
|
|
|
|
|
|
rec2,
|
|
|
|
|
|
|
|
IsEqual.equalTo(
|
|
|
|
|
|
|
|
repository!!.find(rec2.id!!)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
repository!!.remove(rec1)
|
|
|
|
|
|
|
|
MatcherAssert.assertThat(rec1.id, IsEqual.equalTo(null))
|
|
|
|
MatcherAssert.assertThat(rec1.id, IsEqual.equalTo(null))
|
|
|
|
Assert.assertNull(repository!!.find(id))
|
|
|
|
Assert.assertNull(repository.find(id))
|
|
|
|
MatcherAssert.assertThat(
|
|
|
|
MatcherAssert.assertThat(rec2, IsEqual.equalTo(repository.find(rec2.id!!)))
|
|
|
|
rec2,
|
|
|
|
repository.remove(rec1) // should have no effect
|
|
|
|
IsEqual.equalTo(
|
|
|
|
Assert.assertNull(repository.find(id))
|
|
|
|
repository!!.find(rec2.id!!)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
repository!!.remove(rec1) // should have no effect
|
|
|
|
|
|
|
|
Assert.assertNull(repository!!.find(id))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Table(name = "tests")
|
|
|
|
@Table(name = "tests")
|
|
|
|