-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
68737c4
commit fc10bf4
Showing
5 changed files
with
323 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
dorm/graphql/src/test/kotlin/org/sirius/dorm/graphql/AbstractTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package org.sirius.dorm.graphql | ||
/* | ||
* @COPYRIGHT (C) 2023 Andreas Ernst | ||
* | ||
* All rights reserved | ||
*/ | ||
|
||
import org.sirius.common.tracer.TraceLevel | ||
import org.sirius.common.tracer.Tracer | ||
import org.sirius.common.tracer.trace.ConsoleTrace | ||
|
||
import org.sirius.dorm.DORMConfiguration | ||
import org.sirius.dorm.ObjectManager | ||
|
||
import org.sirius.dorm.model.ObjectDescriptor | ||
import org.sirius.dorm.session.DummySessionContextProvider | ||
import org.sirius.dorm.session.SessionContext | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Import | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class DummySessionContext : SessionContext(DummySessionContextProvider("me")) {} | ||
|
||
@Configuration() | ||
@Import(DORMConfiguration::class) | ||
class GraphQLTestConfiguration { | ||
} | ||
|
||
@SpringBootTest(classes =[GraphQLTestConfiguration::class]) | ||
abstract class AbstractTest { | ||
@Autowired | ||
lateinit var objectManager : ObjectManager | ||
|
||
protected var personDescriptor : ObjectDescriptor? = null | ||
|
||
init { | ||
Tracer(ConsoleTrace(), "%t{yyyy-MM-dd HH:mm:ss,SSS} %l{-10s} [%p] %m") | ||
.setTraceLevel("", TraceLevel.OFF) | ||
.setTraceLevel("com", TraceLevel.LOW) | ||
.setTraceLevel("com.sirius.dorm", TraceLevel.HIGH) | ||
} | ||
|
||
protected fun createPerson(name: String, age: Int) : Long { | ||
objectManager.begin() | ||
try { | ||
val person = objectManager.create(personDescriptor!!) | ||
|
||
person["name"] = name | ||
person["age"] = age | ||
|
||
return person.id | ||
} | ||
finally { | ||
objectManager.commit() | ||
} | ||
} | ||
|
||
protected fun withTransaction(doIt: () -> Unit) { | ||
objectManager.begin() | ||
var committed = false | ||
try { | ||
doIt() | ||
|
||
committed = true | ||
objectManager.commit() | ||
} | ||
catch (throwable: Throwable) { | ||
if ( !committed ) | ||
objectManager.rollback() | ||
|
||
throw throwable | ||
} | ||
} | ||
|
||
protected fun measure(test: String, n: Int, doIt: () -> Unit) { | ||
println("> $test") | ||
val start = System.currentTimeMillis() | ||
|
||
var committed = false | ||
//objectManager.begin() | ||
try { | ||
doIt() | ||
|
||
committed = true | ||
//objectManager.commit() | ||
} | ||
catch (throwable: Throwable) { | ||
//if ( !committed ) | ||
// objectManager.rollback() | ||
|
||
throw throwable | ||
} | ||
finally { | ||
val ms = System.currentTimeMillis() - start | ||
val avg = ms.toFloat() / n | ||
|
||
println("< ${ms}ms, avg: ${avg}") | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
dorm/graphql/src/test/kotlin/org/sirius/dorm/graphql/GraphQLTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.sirius.dorm.graphql | ||
/* | ||
* @COPYRIGHT (C) 2023 Andreas Ernst | ||
* | ||
* All rights reserved | ||
*/ | ||
|
||
import graphql.GraphQL | ||
import jakarta.annotation.PostConstruct | ||
import org.junit.jupiter.api.RepeatedTest | ||
import org.sirius.dorm.DORMConfiguration | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Import | ||
import org.junit.jupiter.api.Test | ||
import org.sirius.dorm.ObjectManager | ||
import org.sirius.dorm.graphql.test.TestData | ||
|
||
@Configuration() | ||
@Import(DORMConfiguration::class) | ||
class TestConfiguration { | ||
@Autowired | ||
lateinit var objectManager : ObjectManager | ||
@PostConstruct | ||
fun createData() { | ||
TestData(objectManager) | ||
} | ||
} | ||
|
||
@SpringBootTest(classes =[TestConfiguration::class]) | ||
class GraphQLTest : AbstractTest() { | ||
@Autowired | ||
lateinit var graphQL : GraphQL | ||
|
||
@RepeatedTest(3) | ||
fun test() { | ||
measure("simple read", 2000) { | ||
for ( i in 0..2000) { | ||
val result = graphQL.execute( | ||
"query sampleQuery {\n" + | ||
" Human(filter: { id: { eq: 1 } }) {\n" + | ||
" versionCounter\n" + | ||
" name\n" + | ||
" father {\n" + | ||
" name\n" + | ||
" }\n" + | ||
" id\n" + | ||
" name\n" + | ||
" }\n" + | ||
"}" | ||
) | ||
} | ||
} | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
dorm/graphql/src/test/kotlin/org/sirius/dorm/graphql/TestData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.sirius.dorm.graphql.test | ||
/* | ||
* @COPYRIGHT (C) 2023 Andreas Ernst | ||
* | ||
* All rights reserved | ||
*/ | ||
|
||
import jakarta.annotation.PostConstruct | ||
import org.sirius.common.type.base.* | ||
import org.sirius.dorm.ObjectManager | ||
import org.sirius.dorm.model.Multiplicity | ||
import org.sirius.dorm.model.attribute | ||
import org.sirius.dorm.model.relation | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.stereotype.Component | ||
|
||
|
||
class TestData(val objectManager: ObjectManager) { | ||
init { | ||
setupData() | ||
} | ||
|
||
// private | ||
|
||
private fun <T> withTransaction(doIt: () -> T) : T { | ||
objectManager.begin() | ||
var committed = false | ||
|
||
try { | ||
val result = doIt() | ||
|
||
committed = true | ||
objectManager.commit() | ||
|
||
return result | ||
} | ||
catch (throwable: Throwable) { | ||
if ( !committed ) | ||
objectManager.rollback() | ||
|
||
throw throwable | ||
} | ||
} | ||
|
||
fun setupData() { | ||
withTransaction { | ||
// create type | ||
|
||
objectManager.type("Human") | ||
.add(attribute("firstName").type(string())) | ||
.add(attribute("name").type(string())) | ||
.add(attribute("age").type(int())) | ||
|
||
.add(attribute("short").type(short())) | ||
.add(attribute("long").type(long())) | ||
.add(attribute("float").type(float())) | ||
.add(attribute("double").type(double())) | ||
|
||
// relations | ||
|
||
.add(relation("father").target("Human").multiplicity(Multiplicity.ZERO_OR_ONE).inverse("children")) | ||
.add(relation("children").target("Human").multiplicity(Multiplicity.ZERO_OR_MANY).inverse("father").owner()) | ||
|
||
// done | ||
|
||
.register() | ||
|
||
// create data | ||
|
||
val personDescriptor = objectManager.getDescriptor("Human") | ||
|
||
val andi = objectManager.create(personDescriptor) | ||
|
||
andi["firstName"] = "Andi" | ||
andi["name"] = "Ernst" | ||
andi["age"] = 58 | ||
|
||
// child | ||
|
||
val child = objectManager.create(personDescriptor) | ||
|
||
child["firstName"] = "Nika" | ||
child["name"] = "Martinez" | ||
child["age"] = 14 | ||
|
||
// link | ||
|
||
child["father"] = andi | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
spring: | ||
application: | ||
name: dorm | ||
h2: | ||
console: | ||
enabled: true | ||
sql: | ||
init: | ||
mode: always | ||
datasource: | ||
url: jdbc:h2:mem:mydb | ||
username: sa | ||
password: password | ||
driverClassName: org.h2.Driver | ||
jpa: | ||
properties: | ||
hibernate: | ||
format_sql: false | ||
generate_statistics: false | ||
show-sql: false | ||
defer-datasource-initialization: true | ||
hibernate: | ||
ddl-auto: update | ||
naming: | ||
implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl | ||
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl | ||
strategy: org.hibernate.cfg.ImprovedNamingStrategy | ||
logging: | ||
level: | ||
org: | ||
xhibernate: | ||
sql: debug | ||
orm: | ||
jdbc: | ||
bind: TRACE | ||
type: | ||
descriptor: | ||
sql: trace |