@Database(entities = [Note::class], version = 1)
@TypeConverters(com.misaka.workflow.data.local.TypeConverter::class)
abstract class SQLiteDataBase : RoomDatabase() {
abstract fun getNoteDAO(): NoteDAO
companion object {
@Volatile
private var INSTANCE: SQLiteDataBase? = null
fun getInstance (context: Context): SQLiteDataBase {
return INSTANCE ?: synchronized(this) {
INSTANCE ?: Room.databaseBuilder(
context.applicationContext,
SQLiteDataBase::class.java,
"WorkFlow"
).build().also { INSTANCE = it }
}
}
}
}
@Dao
interface NoteDAO {
@Query("select * from note")
fun getAllNote(): List
@Delete
fun deleteNotes(vararg notes: Note): Int
@Insert
fun insertNewNotes(vararg node: Note)
@Update(onConflict = OnConflictStrategy.REPLACE)
fun updateNote(note: Note): Int
@Query("select * from note")
fun getAllNoteInFlow(): Flow
@Query("delete from note where id = :id")
fun test(id: Int)
@Query("update note set title = :title where id = :id")
fun test2(id: Int, title: String)
}
@Entity(
tableName = "note"
)
data class Note(
@PrimaryKey(autoGenerate = true)
val id: Int = 0,
@ColumnInfo(name = "title")
val title: String = "",
@ColumnInfo(name = "noteContent")
val content: String = "",
@ColumnInfo(name = "modifyTime")
val modifyTime: LocalDateTime = LocalDateTime.now()
)
class NoteUnitTest {
private lateinit var repository: NoteRepository
private lateinit var database: SQLiteDataBase
private lateinit var dao: NoteDAO
@Before
fun setup() {
database = Room.inMemoryDatabaseBuilder(
ApplicationProvider.getApplicationContext(),
SQLiteDataBase::class.java
).build()
dao = database.getNoteDAO()
repository = NoteRepository(dao)
}
@After
fun close() {
database.close()
}
@Test
fun testDAO() = runTest {
try {
val noteFlow = dao.getAllNoteInFlow()
val note = Note(title = "test", content = "testContent", modifyTime = LocalDateTime.now())
dao.insertNewNotes(note)
launch {
noteFlow.collect { value ->
println(value)
}
}
val newNote = note.copy(title = "test1", id = note.id)
dao.test2(newNote.id, newNote.title)
dao.updateNote(newNote)
dao.deleteNotes(newNote)
} catch (e: Exception) {
println(e.stackTrace)
}
}
}
После запуска теста я получаю только одно значение из потока — объект, который я вставил вначале. Я не понимаю, в чем проблема. Буду очень признателен, если кто-нибудь сможет мне помочь.
Я учусь использовать структуру комнаты, но не могу обновить или удалить объект в androidTest. Вот мой код: [code]@Database(entities = [Note::class], version = 1) @TypeConverters(com.misaka.workflow.data.local.TypeConverter::class) abstract class SQLiteDataBase : RoomDatabase() {
abstract fun getNoteDAO(): NoteDAO
companion object { @Volatile private var INSTANCE: SQLiteDataBase? = null
launch { noteFlow.collect { value -> println(value) } }
val newNote = note.copy(title = "test1", id = note.id) dao.test2(newNote.id, newNote.title) dao.updateNote(newNote)
dao.deleteNotes(newNote) } catch (e: Exception) { println(e.stackTrace) } } } [/code] После запуска теста я получаю только одно значение из потока — объект, который я вставил вначале. Я не понимаю, в чем проблема. Буду очень признателен, если кто-нибудь сможет мне помочь.