@@ -633,6 +633,58 @@ class CommonBasicTest(private val path: DatabasePath) {
633633 }
634634 }
635635
636+ @OptIn(AdvancedInsertAPI ::class )
637+ fun testInsertOrReplace () {
638+ Database (getNewAPIDBConfig()).databaseAutoClose { database ->
639+ // Insert an initial entity with a known ID
640+ val original = PersonWithId (id = 100L , name = " Eve" , age = 28 )
641+ database {
642+ PersonWithIdTable { table ->
643+ table INSERT_WITH_ID original
644+ }
645+ }
646+
647+ lateinit var selectStatement: SelectStatement <PersonWithId >
648+ database {
649+ selectStatement = PersonWithIdTable SELECT X
650+ }
651+ assertEquals(1 , selectStatement.getResults().size)
652+ assertEquals(100L , selectStatement.getResults().first().id)
653+ assertEquals(" Eve" , selectStatement.getResults().first().name)
654+
655+ // INSERT_OR_REPLACE with the same PK — should replace the existing row
656+ val replacement = PersonWithId (id = 100L , name = " Eve Updated" , age = 29 )
657+ database {
658+ PersonWithIdTable { table ->
659+ table INSERT_OR_REPLACE replacement
660+ }
661+ }
662+
663+ database {
664+ selectStatement = PersonWithIdTable SELECT X
665+ }
666+ val resultsAfterReplace = selectStatement.getResults()
667+ assertEquals(1 , resultsAfterReplace.size)
668+ assertEquals(100L , resultsAfterReplace.first().id)
669+ assertEquals(" Eve Updated" , resultsAfterReplace.first().name)
670+ assertEquals(29 , resultsAfterReplace.first().age)
671+
672+ // INSERT_OR_REPLACE with a new entity (null ID) — should insert without conflict
673+ val newEntity = PersonWithId (id = null , name = " Frank" , age = 35 )
674+ database {
675+ PersonWithIdTable { table ->
676+ table INSERT_OR_REPLACE newEntity
677+ }
678+ }
679+
680+ database {
681+ selectStatement = PersonWithIdTable SELECT X
682+ }
683+ assertEquals(2 , selectStatement.getResults().size)
684+ assertEquals(true , selectStatement.getResults().any { it.name == " Frank" })
685+ }
686+ }
687+
636688 fun testCreateInDatabaseScope () {
637689 Database (getNewAPIDBConfig()).databaseAutoClose { database ->
638690 val person = PersonWithId (id = null , name = " Grace" , age = 40 )
0 commit comments