Skip to content

Commit

Permalink
test: Add corresponding test in UIntIdTableEntityTest and `ULongIdT…
Browse files Browse the repository at this point in the history
…ableEntityTest`
  • Loading branch information
joc-a committed Aug 19, 2024
1 parent df2065f commit cb7ec23
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import org.jetbrains.exposed.dao.UIntEntity
import org.jetbrains.exposed.dao.UIntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.UIntIdTable
import org.jetbrains.exposed.dao.with
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.exists
import org.jetbrains.exposed.sql.insertAndGetId
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.junit.Test
Expand Down Expand Up @@ -78,6 +81,35 @@ class UIntIdTableEntityTest : DatabaseTestsBase() {
assertEquals(false, allPeople.contains(Pair("Tanu Arora", "Pune")))
}
}

@Test
fun testForeignKeyBetweenUIntAndEntityIDColumns() {
withTables(UIntIdTables.Cities, UIntIdTables.Towns) {
val cId = UIntIdTables.Cities.insertAndGetId {
it[name] = "City A"
}
val tId = UIntIdTables.Towns.insertAndGetId {
it[cityId] = cId.value
}

// lazy loaded referencedOn
val town1 = UIntIdTables.Town.all().single()
assertEquals(cId, town1.city.id)

// eager loaded referencedOn
val town1WithCity = UIntIdTables.Town.all().with(UIntIdTables.Town::city).single()
assertEquals(cId, town1WithCity.city.id)

// lazy loaded referrersOn
val city1 = UIntIdTables.City.all().single()
val towns = city1.towns
assertEquals(cId, towns.first().city.id)

// eager loaded referrersOn
val city1WithTowns = UIntIdTables.City.all().with(UIntIdTables.City::towns).single()
assertEquals(tId, city1WithTowns.towns.first().id)
}
}
}

object UIntIdTables {
Expand All @@ -89,6 +121,7 @@ object UIntIdTables {
companion object : UIntEntityClass<City>(Cities)

var name by Cities.name
val towns by Town referrersOn Towns.cityId
}

object People : UIntIdTable() {
Expand All @@ -102,4 +135,14 @@ object UIntIdTables {
var name by People.name
var city by City referencedOn People.cityId
}

object Towns : UIntIdTable("towns") {
val cityId: Column<UInt> = uinteger("city_id").references(Cities.id)
}

class Town(id: EntityID<UInt>) : UIntEntity(id) {
companion object : UIntEntityClass<Town>(Towns)

var city by City referencedOn Towns.cityId
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import org.jetbrains.exposed.dao.ULongEntity
import org.jetbrains.exposed.dao.ULongEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.ULongIdTable
import org.jetbrains.exposed.dao.with
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.exists
import org.jetbrains.exposed.sql.insertAndGetId
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.junit.Test
Expand Down Expand Up @@ -78,6 +81,35 @@ class ULongIdTableEntityTest : DatabaseTestsBase() {
assertEquals(false, allPeople.contains(Pair("Tanu Arora", "Pune")))
}
}

@Test
fun testForeignKeyBetweenULongAndEntityIDColumns() {
withTables(ULongIdTables.Cities, ULongIdTables.Towns) {
val cId = ULongIdTables.Cities.insertAndGetId {
it[name] = "City A"
}
val tId = ULongIdTables.Towns.insertAndGetId {
it[cityId] = cId.value
}

// lazy loaded referencedOn
val town1 = ULongIdTables.Town.all().single()
assertEquals(cId, town1.city.id)

// eager loaded referencedOn
val town1WithCity = ULongIdTables.Town.all().with(ULongIdTables.Town::city).single()
assertEquals(cId, town1WithCity.city.id)

// lazy loaded referrersOn
val city1 = ULongIdTables.City.all().single()
val towns = city1.towns
assertEquals(cId, towns.first().city.id)

// eager loaded referrersOn
val city1WithTowns = ULongIdTables.City.all().with(ULongIdTables.City::towns).single()
assertEquals(tId, city1WithTowns.towns.first().id)
}
}
}

object ULongIdTables {
Expand All @@ -89,6 +121,7 @@ object ULongIdTables {
companion object : ULongEntityClass<City>(Cities)

var name by Cities.name
val towns by Town referrersOn Towns.cityId
}

object People : ULongIdTable() {
Expand All @@ -102,4 +135,14 @@ object ULongIdTables {
var name by People.name
var city by City referencedOn People.cityId
}

object Towns : ULongIdTable("towns") {
val cityId: Column<ULong> = ulong("city_id").references(Cities.id)
}

class Town(id: EntityID<ULong>) : ULongEntity(id) {
companion object : ULongEntityClass<Town>(Towns)

var city by City referencedOn Towns.cityId
}
}

0 comments on commit cb7ec23

Please sign in to comment.