Initial commit
This commit is contained in:
152
src/test/kotlin/nl/astraeus/persistence/TestPersistence.kt
Normal file
152
src/test/kotlin/nl/astraeus/persistence/TestPersistence.kt
Normal file
@@ -0,0 +1,152 @@
|
||||
package nl.astraeus.persistence
|
||||
|
||||
import nl.astraeus.nl.astraeus.persistence.Index
|
||||
import nl.astraeus.nl.astraeus.persistence.Persistable
|
||||
import nl.astraeus.nl.astraeus.persistence.Persistent
|
||||
import nl.astraeus.nl.astraeus.persistence.Reference
|
||||
import java.io.File
|
||||
import kotlin.test.Test
|
||||
|
||||
class TestPersistence {
|
||||
|
||||
class Company(
|
||||
override var id: Long = 0,
|
||||
override var version: Long = 0,
|
||||
val name: String
|
||||
) : Persistable, Cloneable {
|
||||
//var persons: MutableList<Person> by ListReference<Person, Company>(Person::class.java)
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID: Long = 1L
|
||||
}
|
||||
}
|
||||
|
||||
class Person(
|
||||
override var id: Long = 0,
|
||||
override var version: Long = 0,
|
||||
val name: String,
|
||||
val age: Int,
|
||||
) : Persistable, Cloneable {
|
||||
var company: Company by Reference<Company, Person>(Company::class.java)
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID: Long = 1L
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPersistence() {
|
||||
println("Test persistence")
|
||||
|
||||
val pst = Persistent(
|
||||
directory = File("data"),
|
||||
arrayOf(
|
||||
Index(Person::class, "name") { p -> (p as? Person)?.name ?: "" },
|
||||
Index(Person::class, "age") { p -> (p as? Person)?.age ?: -1 },
|
||||
Index(Person::class, "ageGt20") { p -> ((p as? Person)?.age ?: 0) > 20 },
|
||||
Index(Person::class, "ageGt23") { p -> ((p as? Person)?.age ?: 0) > 23 },
|
||||
Index(Person::class, "ageOnlyGt20") { p ->
|
||||
if (((p as? Person)?.age ?: 0) > 20) {
|
||||
true
|
||||
} else {
|
||||
null
|
||||
}
|
||||
},
|
||||
Index(Company::class, "name") { p -> (p as? Company)?.name ?: "" },
|
||||
)
|
||||
)
|
||||
|
||||
pst.transaction {
|
||||
val person = find(Person::class, 1L) ?: Person(
|
||||
id = 1L,
|
||||
name = "John Doe",
|
||||
age = 25
|
||||
)
|
||||
|
||||
val company = find(Company::class, 1L) ?: Company(
|
||||
id = 1L,
|
||||
name = "ACME"
|
||||
)
|
||||
|
||||
person.company = company
|
||||
//company.persons.add(person)
|
||||
|
||||
store(person)
|
||||
store(Person(
|
||||
id = 2L,
|
||||
name = "John Doe",
|
||||
age = 23
|
||||
))
|
||||
store(Person(
|
||||
id = 3L,
|
||||
name = "John Doe",
|
||||
age = 18
|
||||
))
|
||||
|
||||
findByIndex(Person::class, "name", "John Doe").forEach { p ->
|
||||
println("Found person by name: ${p.name} - ${p.age}")
|
||||
}
|
||||
|
||||
findByIndex(Person::class, "age", 23).forEach { p ->
|
||||
println("Found person by age: ${p.name} - ${p.age}")
|
||||
}
|
||||
|
||||
findByIndex(Person::class, "ageGt20", true).forEach { p ->
|
||||
println("Found person by age > 20: ${p.name} - ${p.age}")
|
||||
}
|
||||
|
||||
findByIndex(Person::class, "ageGt23", true).forEach { p ->
|
||||
println("Found person by age > 23: ${p.name} - ${p.age}")
|
||||
}
|
||||
|
||||
findByIndex(Person::class, "ageGt20", false).forEach { p ->
|
||||
println("Found person by age <= 20: ${p.name} - ${p.age}")
|
||||
}
|
||||
|
||||
val p2 = find(Person::class, 1L)
|
||||
|
||||
assert(p2 != null)
|
||||
|
||||
val c2 = find(Company::class, 1L)
|
||||
|
||||
assert(c2 != null)
|
||||
}
|
||||
|
||||
pst.transaction {
|
||||
val person = find(Person::class, 1L)
|
||||
|
||||
delete(person!!)
|
||||
|
||||
val p2 = find(Person::class, 1L)
|
||||
|
||||
assert(p2 == null)
|
||||
}
|
||||
|
||||
pst.transaction {
|
||||
val persons = search(Person::class) { p -> p.name == "John Doe" }
|
||||
|
||||
if (persons.isNotEmpty()) {
|
||||
delete(persons[0])
|
||||
}
|
||||
}
|
||||
|
||||
pst.snapshot()
|
||||
|
||||
pst.transaction {
|
||||
store(
|
||||
Person(
|
||||
id = 10L,
|
||||
name = "Pipo",
|
||||
age = 23
|
||||
)
|
||||
)
|
||||
store(
|
||||
Person(
|
||||
id = 11L,
|
||||
name = "Clown",
|
||||
age = 18
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user