Add user service tests
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
import be.vandewalleh.services.ChapterDTO
|
||||
import be.vandewalleh.services.FullNoteCreateDTO
|
||||
import be.vandewalleh.services.NotesService
|
||||
import com.github.javafaker.Faker
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.kodein.di.generic.instance
|
||||
|
||||
class FakeDataTest {
|
||||
val notesService by kodein.instance<NotesService>()
|
||||
|
||||
@Test
|
||||
fun addNotes() {
|
||||
val faker = Faker()
|
||||
val title = faker.hobbit().quote()
|
||||
|
||||
val tags = listOf(
|
||||
faker.beer().name(),
|
||||
faker.beer().yeast()
|
||||
)
|
||||
|
||||
val chapters = listOf(
|
||||
ChapterDTO(
|
||||
faker.animal().name(),
|
||||
faker.lorem().paragraph()
|
||||
),
|
||||
ChapterDTO(
|
||||
faker.animal().name(),
|
||||
faker.lorem().paragraph()
|
||||
)
|
||||
)
|
||||
|
||||
val note = FullNoteCreateDTO(title, tags, chapters)
|
||||
|
||||
notesService.createNote(1, note)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
import be.vandewalleh.services.NotesService
|
||||
import be.vandewalleh.services.UserService
|
||||
import com.hekeki.huckleberry.Benchmark
|
||||
import com.hekeki.huckleberry.BenchmarkRunner
|
||||
import com.hekeki.huckleberry.BenchmarkTest
|
||||
import com.hekeki.huckleberry.TimeUnit
|
||||
import com.zaxxer.hikari.HikariConfig
|
||||
import com.zaxxer.hikari.HikariDataSource
|
||||
import me.liuwj.ktorm.database.*
|
||||
import org.junit.jupiter.api.Assertions.*
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.generic.bind
|
||||
import org.kodein.di.generic.instance
|
||||
import org.kodein.di.generic.singleton
|
||||
|
||||
|
||||
val hikariConfig = HikariConfig().apply {
|
||||
jdbcUrl = "jdbc:mariadb://localhost:3306/notes"
|
||||
username = "notes"
|
||||
password = "notes"
|
||||
}
|
||||
|
||||
val dataSource = HikariDataSource(hikariConfig)
|
||||
|
||||
val db = Database.Companion.connect(dataSource)
|
||||
|
||||
val kodein = Kodein {
|
||||
bind<Database>() with singleton { db }
|
||||
bind<UserService>() with singleton { UserService(this.kodein) }
|
||||
bind<NotesService>() with singleton { NotesService(this.kodein) }
|
||||
}
|
||||
|
||||
val notesService by kodein.instance<NotesService>()
|
||||
|
||||
@Benchmark(threads = 1, iterations = 30, warmup = true, warmupIterations = 1000, timeUnit = TimeUnit.MILLIS)
|
||||
class RetrieveNotesBenchmarkTest : BenchmarkTest {
|
||||
|
||||
override fun execute() {
|
||||
notesService.getNotes(15)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun compute() {
|
||||
val benchmarkResult = BenchmarkRunner(RetrieveNotesBenchmarkTest::class.java).run()
|
||||
assertTrue(benchmarkResult.median(2, TimeUnit.MILLIS))
|
||||
assertTrue(benchmarkResult.maxTime(4, TimeUnit.MILLIS))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package services
|
||||
|
||||
import be.vandewalleh.migrations.Migration
|
||||
import be.vandewalleh.services.NotesService
|
||||
import be.vandewalleh.services.UserService
|
||||
import com.zaxxer.hikari.HikariConfig
|
||||
import com.zaxxer.hikari.HikariDataSource
|
||||
import me.liuwj.ktorm.database.*
|
||||
import org.amshove.kluent.*
|
||||
import org.junit.jupiter.api.*
|
||||
import org.kodein.di.Kodein
|
||||
import org.kodein.di.generic.bind
|
||||
import org.kodein.di.generic.instance
|
||||
import org.kodein.di.generic.singleton
|
||||
import org.testcontainers.containers.MariaDBContainer
|
||||
import javax.sql.DataSource
|
||||
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
|
||||
class UserServiceTest {
|
||||
|
||||
class KMariadbContainer : MariaDBContainer<KMariadbContainer>()
|
||||
|
||||
private val mariadb = KMariadbContainer().apply {
|
||||
this.start()
|
||||
}
|
||||
|
||||
private val hikariConfig = HikariConfig().apply {
|
||||
jdbcUrl = mariadb.jdbcUrl
|
||||
username = mariadb.username
|
||||
password = mariadb.password
|
||||
}
|
||||
|
||||
private val dataSource = HikariDataSource(hikariConfig)
|
||||
|
||||
private val db = Database.Companion.connect(dataSource)
|
||||
|
||||
private val kodein = Kodein {
|
||||
bind<DataSource>() with singleton { dataSource }
|
||||
bind<Database>() with singleton { db }
|
||||
bind<Migration>() with singleton { Migration(this.kodein) }
|
||||
bind<UserService>() with singleton { UserService(this.kodein) }
|
||||
bind<NotesService>() with singleton { NotesService(this.kodein) }
|
||||
}
|
||||
|
||||
private val migration by kodein.instance<Migration>()
|
||||
|
||||
init {
|
||||
migration.migrate()
|
||||
}
|
||||
|
||||
private val userService by kodein.instance<UserService>()
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
fun `test create user`() {
|
||||
val username = "hubert"
|
||||
val email = "a@a"
|
||||
val password = "password"
|
||||
println(userService.createUser(username, email, password))
|
||||
|
||||
val id = userService.getUserId(email)
|
||||
id `should not be` null
|
||||
|
||||
userService.getUserInfo(id!!)!!.let {
|
||||
it.username `should be equal to` username
|
||||
it.email `should be equal to` email
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
fun `test create same user`() {
|
||||
userService.createUser(username = "hubert", hashedPassword = "password", email = "a@a") `should be` null
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
fun `test delete user`() {
|
||||
val email = "a@a"
|
||||
val id = userService.getUserId(email)!!
|
||||
userService.deleteUser(id)
|
||||
|
||||
userService.getUserId(email) `should be` null
|
||||
userService.getUserInfo(id) `should be` null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user