add fixture to tests

This commit is contained in:
2019-08-03 17:14:24 +02:00
parent 7c3028eca2
commit e31282e24e
16 changed files with 199 additions and 143 deletions

View File

@@ -1,96 +0,0 @@
package fr.dcproject
import fr.postgresjson.migration.Migrations
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.locations.KtorExperimentalLocationsAPI
import io.ktor.server.testing.handleRequest
import io.ktor.server.testing.setBody
import io.ktor.server.testing.withTestApplication
import io.ktor.util.KtorExperimentalAPI
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.test.KoinTest
import org.koin.test.inject
import kotlin.test.assertEquals
import kotlin.test.assertTrue
@KtorExperimentalLocationsAPI
@KtorExperimentalAPI
@TestInstance(PER_CLASS)
class ArticleRouteTest: KoinTest {
private val migrations: Migrations by inject()
@BeforeEach
fun beforeAll() {
startKoin {
modules(Module)
config.database = "test"
config.username = "test"
config.password = "test"
}
migrations.run()
}
@AfterEach
fun afterAll() {
migrations.forceAllDown()
stopKoin()
}
private val article: String = """{
"id" : "8e8dd0aa-2b2b-41e1-bff5-ea613c988774",
"version_id" : "e3ec9ea8-87ac-46ac-8321-8f2bc8c687bc",
"version_number" : 1,
"title" : "title13",
"annonymous" : false,
"content" : "content13",
"description" : "description13",
"tags" : [ "sky", "nuclear" ],
"created_at" : "2019-07-30T14:08:51.420Z",
"created_by" : {
"id" : "d821a211-10d6-4d65-b0db-e0bd33d21761",
"name" : {
"civility" : "m",
"last_name" : "LAST NAME13",
"first_name" : "first name13"
},
"birthday" : "1994-07-30",
"user_id" : "127b9979-1474-4da1-8453-1e10462ae593",
"vote_annonymous" : false,
"follow_annonymous" : false,
"user" : null,
"created_at" : "2019-07-30T14:08:49.742Z"
}
}"""
@Test
fun testRoute() {
withTestApplication({ module() }) {
handleRequest(HttpMethod.Get, "/articles").apply {
assertEquals(HttpStatusCode.OK, response.status())
}
handleRequest(HttpMethod.Post, "/articles") {
this.setBody(article)
this.addHeader(HttpHeaders.ContentType, ContentType.Application.Json.toString())
}.apply {
assertEquals(HttpStatusCode.OK, response.status())
}
handleRequest(HttpMethod.Get, "/articles/8e8dd0aa-2b2b-41e1-bff5-ea613c988774").apply {
assertEquals(HttpStatusCode.OK, response.status())
assertTrue(response.content!!.contains("8e8dd0aa-2b2b-41e1-bff5-ea613c988774"))
}
}
}
}

View File

@@ -0,0 +1,78 @@
import fr.dcproject.entity.Article
import fr.dcproject.entity.Citizen
import fr.dcproject.entity.User
import fr.postgresjson.serializer.deserialize
import fr.postgresjson.serializer.serialize
import io.ktor.locations.KtorExperimentalLocationsAPI
import io.ktor.util.KtorExperimentalAPI
import org.amshove.kluent.`should equal`
import org.amshove.kluent.shouldBe
import org.intellij.lang.annotations.Language
import org.joda.time.DateTime
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
@KtorExperimentalLocationsAPI
@KtorExperimentalAPI
@TestInstance(PER_CLASS)
class ArticleTest {
@Language("JSON")
private val articleJson: String = """
{
"id" : "83b0b60a-5ab3-44f2-b243-1dc469a7564f",
"version_id" : "fff2311c-07cc-43a6-bab1-aec6b649a903",
"version_number" : null,
"title" : "Hello world!",
"annonymous" : true,
"content" : "bla bla bla",
"description" : "this is the changement !",
"tags" : [ ],
"created_by" : {
"id" : "3fff09e4-5ff2-46ee-9fd2-3803a1ffb600",
"name" : {
"first_name" : "Jaque",
"last_name" : "Bono",
"civility" : null
},
"birthday" : "2019-08-03T13:43:13.765Z",
"user_id" : null,
"vote_annonymous" : null,
"follow_annonymous" : null,
"user" : {
"id" : "151ec430-3aad-4792-9a14-e394b2be491b",
"username" : "jaque",
"blocked_at" : null,
"plain_password" : "azerty",
"created_at" : null,
"updated_at" : null
},
"created_at" : null
},
"created_at" : null
}
""".trimIndent()
@Test
fun `test Article serialize`() {
val user = User(username = "jaque", plainPassword = "azerty")
val citizen = Citizen(
name = Citizen.Name("Jaque", "Bono"),
birthday = DateTime.now(),
user = user
)
val article = Article(
title = "Hello world!",
content = "bla bla bla",
description = "this is the changement !",
createdBy = citizen
)
article.serialize().contains("""Hello world!""") shouldBe true
}
@Test
fun `test Article Deserialize`() {
val article2: Article = articleJson.deserialize()!!
article2.id.toString() `should equal` "83b0b60a-5ab3-44f2-b243-1dc469a7564f"
}
}

View File

@@ -4,19 +4,31 @@ import cucumber.api.java8.En
import cucumber.api.junit.Cucumber
import feature.Context
import fr.dcproject.config
import fr.dcproject.utils.LoggerDelegate
import fr.postgresjson.connexion.Connection
import fr.postgresjson.connexion.Requester
import fr.postgresjson.migration.Migrations
import io.ktor.locations.KtorExperimentalLocationsAPI
import io.ktor.server.testing.TestApplicationEngine
import io.ktor.server.testing.createTestEnvironment
import io.ktor.util.KtorExperimentalAPI
import org.junit.runner.RunWith
import org.koin.test.KoinTest
import org.koin.test.inject
import org.slf4j.Logger
import java.util.concurrent.TimeUnit
import feature.Context.Companion.current as contextCurrent
var unitialized: Boolean = false
@KtorExperimentalAPI
@KtorExperimentalLocationsAPI
@RunWith(Cucumber::class)
@CucumberOptions(plugin = ["pretty"])
class RunCucumberTest: En, KoinTest {
private val migrations: Migrations by inject()
private val migrations: Migrations by inject()
private val connection = Connection("test", "test", "test")
private val logger: Logger? by LoggerDelegate()
init {
Before(-1) { scenario: Scenario ->
config.database = "test"
@@ -24,12 +36,38 @@ class RunCucumberTest: En, KoinTest {
config.password = "test"
contextCurrent = Context(TestApplicationEngine(createTestEnvironment()) {}, scenario)
migrations.run()
beforeAll()
logger?.info("Fixtures Begin")
//language=PostgreSQL
connection.sendQuery("""truncate table "user" cascade;""")
//language=PostgreSQL
connection.sendQuery("""SET fixture.quantity.multiple = '50';""")
getFixturesRequester()
.getQueries()
.sortedBy { it.name }
.forEach { it.exec() }
logger?.info("Fixtures Done")
}
After { scenario: Scenario ->
migrations.forceAllDown()
After { _: Scenario ->
contextCurrent.engine.stop(0L, 0L, TimeUnit.MILLISECONDS)
}
}
private fun beforeAll()
{
if (!unitialized) {
migrations.forceAllDown()
migrations.run()
unitialized = true
}
}
private fun getFixturesRequester(): Requester {
return Requester.RequesterFactory(
connection = connection,
queriesDirectory = config.sqlFiles.resolve("fixtures")
).createRequester()
}
}

View File

@@ -4,9 +4,9 @@ Feature: articles routes
When I send a "GET" request to "/articles"
Then the response status code should be 200
# Scenario: The route for get article must response a 200
# When I send a "GET" request to "/articles/55a24426-139b-4ee7-b1e2-a3d016d66cc2"
# Then the response status code should be 200
Scenario: The route for get article must response a 200
When I send a "GET" request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b"
Then the response status code should be 200
Scenario: The route for get article must response a 200
Given I have citizen: