https://kotlinlang.org logo
#exposed
Title
# exposed
l

Leonid Yavorskyi

03/14/2022, 3:17 PM
Hi there! I’m trying to cover the logic based on Exposed with unit tests and faced to a problem - how to create an entity manually to mock repository calls. Found the solution that works for “simple” entity fields but how to set manually entity relations?
Copy code
class MyEntity(id: EntityID<UUID>) : UUIDEntity(id) {
    companion object : UUIDEntityClass<MyEntity>(MyEntities)
    var name by Migs.name
    var unit by Migs.unit
    val relations by MyRelationEntity referrersOn MyRelations.id
}

val myFieldIndex: Map<Expression<*>, Int> = mapOf(
        MyEntities.id to 0,
        MyEntities.name to 1,
        MyEntities.unit to 2,
)

val myResultRowData = arrayOfNulls<Any?>(3)
myResultRowData[0]=1
myResultRowData[1]="Name1"
myResultRowData[2]="Unit1"

val e = MyEntity(EntityID(UUID.randomUUID(), Migs))
e._readValues = ResultRow(fieldIndex = myFieldIndex, data = myResultRowData)

// e.setRelations < --- how to set relations ???
h

hfhbd

03/14/2022, 3:30 PM
I would use a new layer: repositories with domain classes.
l

Leonid Yavorskyi

03/14/2022, 3:34 PM
@hfhbd you mean repository should return domain objects rather than entities?
h

hfhbd

03/14/2022, 3:35 PM
yes
👍 1
l

Leonid Yavorskyi

03/14/2022, 3:35 PM
Seems it will be the best solution, thanks!
9 Views