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

2
.idea/gradle.xml generated
View File

@@ -5,7 +5,7 @@
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>
<option name="delegatedBuild" value="true" /> <option name="delegatedBuild" value="true" />
<option name="testRunner" value="GRADLE" /> <option name="testRunner" value="PLATFORM" />
<option name="distributionType" value="DEFAULT_WRAPPED" /> <option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="11" /> <option name="gradleJvm" value="11" />

View File

@@ -4,14 +4,15 @@ import java.util.*
class Article( class Article(
id: UUID?, id: UUID = UUID.randomUUID(),
var versionId: UUID?, var versionId: UUID = UUID.randomUUID(),
var versionNumber: Int?, var versionNumber: Int? = null,
var title: String?, var title: String?,
var annonymous: Boolean?, var annonymous: Boolean? = true,
var content: String?, var content: String?,
var description: String?, var description: String?,
var tags: List<String> var tags: List<String> = emptyList(),
override var createdBy: Citizen?
): ):
UuidEntity(id), UuidEntity(id),
EntityCreatedAt by EntityCreatedAtImp(), EntityCreatedAt by EntityCreatedAtImp(),

View File

@@ -0,0 +1,10 @@
package fr.dcproject.utils
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
internal class LoggerDelegate<in R: Any>: ReadOnlyProperty<R, Logger> {
override fun getValue(thisRef: R, property: KProperty<*>) = LoggerFactory.getLogger(thisRef.javaClass.packageName)
}

View File

@@ -2,14 +2,16 @@ do
$$ $$
declare declare
_password text := crypt('azerty', gen_salt('bf', 8)); _password text := crypt('azerty', gen_salt('bf', 8));
multiple int = coalesce(current_setting('fixture.quantity.multiple', true), '50')::int;
begin begin
delete from "user"; delete from "user";
insert into "user" (username, password, blocked_at) insert into "user" (id, username, password, blocked_at)
select select
'username' || s, uuid_in(md5('user'||rn::text)::cstring),
'username' || rn,
_password, _password,
case when s % 10 = 0 then now() else null end case when rn % 10 = 0 then now() else null end
from generate_series(1, 1000) s; from generate_series(1, multiple) rn;
raise notice 'user fixtures done'; raise notice 'user fixtures done';
end; end;

View File

@@ -2,8 +2,9 @@ do
$$ $$
begin begin
delete from citizen; delete from citizen;
insert into citizen (name, birthday, user_id, vote_annonymous, follow_annonymous) insert into citizen (id, name, birthday, user_id, vote_annonymous, follow_annonymous)
select select
uuid_in(md5('citizen'||row_number() over ()::text)::cstring),
jsonb_build_object( jsonb_build_object(
'first_name', 'first name' || row_number() over (), 'first_name', 'first name' || row_number() over (),
'last_name', 'LAST NAME' || row_number() over (), 'last_name', 'LAST NAME' || row_number() over (),

View File

@@ -1,11 +1,14 @@
do do
$$ $$
declare
citizen_count int = (select count(*) from citizen);
begin begin
delete from citizen_in_workgroup; delete from citizen_in_workgroup;
delete from workgroup; delete from workgroup;
insert into workgroup (created_by_id, name, description, annonymous, owner_id) insert into workgroup (id, created_by_id, name, description, annonymous, owner_id)
select select
uuid_in(md5('workgroup'||rn::text)::cstring),
z.id, z.id,
'name' || rn, 'name' || rn,
'description' || rn, 'description' || rn,
@@ -17,7 +20,7 @@ begin
select select
z.id, z.id,
w.id w.id
from (select *, row_number() over ()+5 % 1000 rn from citizen) z from (select *, row_number() over ()+5 % citizen_count rn from citizen) z
join (select *, row_number() over () rn from workgroup) w using (rn); join (select *, row_number() over () rn from workgroup) w using (rn);
raise notice 'workgroup fixtures done'; raise notice 'workgroup fixtures done';

View File

@@ -12,9 +12,10 @@ begin
delete from article_relations; delete from article_relations;
delete from article; delete from article;
insert into article (version_id, created_by_id, title, annonymous, content, description, tags) insert into article (id, version_id, created_by_id, title, annonymous, content, description, tags)
select select
uuid_generate_v4(), uuid_in(md5('article'||row_number() over ())::cstring),
uuid_in(md5('article_v'||row_number() over ())::cstring),
z.id, z.id,
'title' || row_number() over (), 'title' || row_number() over (),
row_number() over () % 3 = 0, row_number() over () % 3 = 0,

View File

@@ -1,5 +1,7 @@
do do
$$ $$
declare
article_count int = (select count(*) from article);
begin begin
delete from article_in_title; delete from article_in_title;
delete from title; delete from title;
@@ -29,7 +31,7 @@ begin
ti.id, ti.id,
a.id, a.id,
ti.constitution_id ti.constitution_id
from (select *, (row_number() over () % 1005) rn from title, lateral generate_series(1, 3) g) ti from (select *, (row_number() over () % (article_count+7)) rn from title, lateral generate_series(1, 3) g) ti
join (select *, row_number() over () rn from article) a using (rn); join (select *, row_number() over () rn from article) a using (rn);
raise notice 'constitution fixtures done'; raise notice 'constitution fixtures done';

View File

@@ -1,27 +1,32 @@
do do
$$ $$
declare
article_count int = (select count(*) from article);
begin begin
delete from follow; delete from follow;
insert into follow_article (citizen_id, target_id) insert into follow_article (id, citizen_id, target_id)
select select
uuid_in(md5('follow_article'||row_number() over ())::cstring),
z.id, z.id,
a.id a.id
from (select *, row_number() over () % 995 rn from citizen, lateral generate_series(1, 5)) z from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5)) z
join (select *, row_number() over () rn from article) a using (rn); join (select *, row_number() over () rn from article) a using (rn);
insert into follow_constitution (citizen_id, target_id) insert into follow_constitution (id, citizen_id, target_id)
select select
uuid_in(md5('follow_constitution'||row_number() over ())::cstring),
z.id, z.id,
a.id a.id
from (select *, row_number() over () % 995 rn from citizen, lateral generate_series(1, 5)) z from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5)) z
join (select *, row_number() over () rn from constitution) a using (rn); join (select *, row_number() over () rn from constitution) a using (rn);
insert into follow_citizen (citizen_id, target_id) insert into follow_citizen (id, citizen_id, target_id)
select select
uuid_in(md5('follow_citizen'||row_number() over ())::cstring),
z.id, z.id,
a.id a.id
from (select *, row_number() over () % 995 rn from citizen, lateral generate_series(1, 5)) z from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5)) z
join (select *, row_number() over () rn from citizen) a using (rn); join (select *, row_number() over () rn from citizen) a using (rn);
raise notice 'follow fixtures done'; raise notice 'follow fixtures done';

View File

@@ -1,40 +1,46 @@
do do
$$ $$
declare
article_count int = (select count(*) from article);
begin begin
delete from comment; delete from comment;
insert into comment_on_article (citizen_id, target_id, content) insert into comment_on_article (id, citizen_id, target_id, content)
select select
uuid_in(md5('comment_on_article'||row_number() over ())::cstring),
z.id, z.id,
a.id, a.id,
'content' || (row_number() over () * g) 'content' || (row_number() over () * g)
from (select *, row_number() over () % 995 rn from citizen, lateral generate_series(1, 5) g) z from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z
join (select *, row_number() over () rn from article) a using (rn); join (select *, row_number() over () rn from article) a using (rn);
insert into comment_on_article (citizen_id, target_id, content, parent_id) insert into comment_on_article (id, citizen_id, target_id, content, parent_id)
select select
uuid_in(md5('comment_on_article_2'||row_number() over ())::cstring),
z.id, z.id,
a.target_id, a.target_id,
'content' || row_number() over () * g, 'content' || row_number() over () * g,
a.id a.id
from (select *, row_number() over () % 995 rn from citizen, lateral generate_series(1, 5) g) z from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z
join (select *, row_number() over () rn from comment_on_article) a using (rn); join (select *, row_number() over () rn from comment_on_article) a using (rn);
insert into comment_on_article (citizen_id, target_id, content, parent_id) insert into comment_on_article (id, citizen_id, target_id, content, parent_id)
select select
uuid_in(md5('comment_on_article_3'||row_number() over ())::cstring),
z.id, z.id,
a.target_id, a.target_id,
'content' || row_number() over () * g, 'content' || row_number() over () * g,
a.id a.id
from (select *, row_number() over () % 995 rn from citizen, lateral generate_series(1, 5) g) z from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z
join (select *, row_number() over () rn from comment_on_article where parent_id is not null) a using (rn); join (select *, row_number() over () rn from comment_on_article where parent_id is not null) a using (rn);
insert into comment_on_constitution (citizen_id, target_id, content) insert into comment_on_constitution (id, citizen_id, target_id, content)
select select
uuid_in(md5('comment_on_constitution'||row_number() over ())::cstring),
z.id, z.id,
a.id, a.id,
'content' || row_number() over () * g 'content' || row_number() over () * g
from (select *, row_number() over () % 995 rn from citizen, lateral generate_series(1, 5) g) z from (select *, row_number() over () % (article_count+7) rn from citizen, lateral generate_series(1, 5) g) z
join (select *, row_number() over () rn from constitution) a using (rn); join (select *, row_number() over () rn from constitution) a using (rn);
raise notice 'comment fixtures done'; raise notice 'comment fixtures done';

View File

@@ -1,45 +1,51 @@
do do
$$ $$
declare
article_count int = (select count(*) from article);
begin begin
delete from vote_for_article; delete from vote_for_article;
delete from vote_for_constitution; delete from vote_for_constitution;
delete from vote_for_comment_on_article; delete from vote_for_comment_on_article;
delete from vote_for_comment_on_constitution; delete from vote_for_comment_on_constitution;
raise notice '%', article_count;
insert into vote_for_article (citizen_id, target_id, note, anonymous) insert into vote_for_article (id, citizen_id, target_id, note, anonymous)
select select
uuid_in(md5('vote_for_article'||row_number() over ())::cstring),
z.id, z.id,
a.id, a.id,
(row_number() over () % 3) -1, (row_number() over () % 3) -1,
(row_number() over () % 3 = 1) (row_number() over () % 3 = 1)
from (select *, row_number() over () % 995 rn, g from citizen, lateral generate_series(1, 10) g) z from (select *, row_number() over ()+g % (article_count+7) rn, g from citizen, lateral generate_series(1, 5) g) z
join (select *, row_number() over () rn from article) a using (rn); join (select *, row_number() over () rn from article) a using (rn);
insert into vote_for_constitution (citizen_id, target_id, note, anonymous) insert into vote_for_constitution (id, citizen_id, target_id, note, anonymous)
select select
uuid_in(md5('vote_for_constitution'||row_number() over ())::cstring),
z.id, z.id,
a.id, a.id,
(row_number() over () % 3) -1, (row_number() over () % 3) -1,
(row_number() over () % 3 = 1) (row_number() over () % 3 = 1)
from (select *, row_number() over () % 995 rn, g from citizen, lateral generate_series(1, 5) g) z from (select *, row_number() over () % (article_count+7) rn, g from citizen, lateral generate_series(1, 5) g) z
join (select *, row_number() over () rn from constitution) a using (rn); join (select *, row_number() over () rn from constitution) a using (rn);
insert into vote_for_comment_on_article (citizen_id, target_id, note, anonymous) insert into vote_for_comment_on_article (id, citizen_id, target_id, note, anonymous)
select select
uuid_in(md5('vote_for_comment_on_article'||row_number() over ())::cstring),
z.id, z.id,
a.id, a.id,
(row_number() over () % 3) -1, (row_number() over () % 3) -1,
(row_number() over () % 3 = 1) (row_number() over () % 3 = 1)
from (select *, row_number() over () % 995 rn, g from citizen, lateral generate_series(1, 3) g) z from (select *, row_number() over () % (article_count+7) rn, g from citizen, lateral generate_series(1, 3) g) z
join (select *, row_number() over () rn from comment_on_article) a using (rn); join (select *, row_number() over () rn from comment_on_article) a using (rn);
insert into vote_for_comment_on_constitution (citizen_id, target_id, note, anonymous) insert into vote_for_comment_on_constitution (id, citizen_id, target_id, note, anonymous)
select select
uuid_in(md5('vote_for_comment_on_constitution'||row_number() over ())::cstring),
z.id, z.id,
a.id, a.id,
(row_number() over () % 3) -1, (row_number() over () % 3) -1,
(row_number() over () % 3 = 1) (row_number() over () % 3 = 1)
from (select *, row_number() over () % 995 rn, g from citizen, lateral generate_series(1, 2) g) z from (select *, row_number() over () % (article_count+7) rn, g from citizen, lateral generate_series(1, 2) g) z
join (select *, row_number() over () rn from comment_on_constitution) a using (rn); join (select *, row_number() over () rn from comment_on_constitution) a using (rn);
raise notice 'vote fixtures done'; raise notice 'vote fixtures done';

View File

@@ -39,4 +39,3 @@ drop table if exists citizen_in_workgroup;
drop table if exists workgroup; drop table if exists workgroup;
drop table if exists citizen; drop table if exists citizen;
drop table if exists "user"; drop table if exists "user";
drop type if exists public."name";

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 cucumber.api.junit.Cucumber
import feature.Context import feature.Context
import fr.dcproject.config 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 fr.postgresjson.migration.Migrations
import io.ktor.locations.KtorExperimentalLocationsAPI
import io.ktor.server.testing.TestApplicationEngine import io.ktor.server.testing.TestApplicationEngine
import io.ktor.server.testing.createTestEnvironment import io.ktor.server.testing.createTestEnvironment
import io.ktor.util.KtorExperimentalAPI
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.koin.test.KoinTest import org.koin.test.KoinTest
import org.koin.test.inject import org.koin.test.inject
import org.slf4j.Logger
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import feature.Context.Companion.current as contextCurrent import feature.Context.Companion.current as contextCurrent
var unitialized: Boolean = false
@KtorExperimentalAPI
@KtorExperimentalLocationsAPI
@RunWith(Cucumber::class) @RunWith(Cucumber::class)
@CucumberOptions(plugin = ["pretty"]) @CucumberOptions(plugin = ["pretty"])
class RunCucumberTest: En, KoinTest { 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 { init {
Before(-1) { scenario: Scenario -> Before(-1) { scenario: Scenario ->
config.database = "test" config.database = "test"
@@ -24,12 +36,38 @@ class RunCucumberTest: En, KoinTest {
config.password = "test" config.password = "test"
contextCurrent = Context(TestApplicationEngine(createTestEnvironment()) {}, scenario) 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 -> After { _: Scenario ->
migrations.forceAllDown()
contextCurrent.engine.stop(0L, 0L, TimeUnit.MILLISECONDS) 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" When I send a "GET" request to "/articles"
Then the response status code should be 200 Then the response status code should be 200
# Scenario: The route for get article must response a 200 Scenario: The route for get article must response a 200
# When I send a "GET" request to "/articles/55a24426-139b-4ee7-b1e2-a3d016d66cc2" When I send a "GET" request to "/articles/9226c1a3-8091-c3fa-7d0d-c2e98c9bee7b"
# Then the response status code should be 200 Then the response status code should be 200
Scenario: The route for get article must response a 200 Scenario: The route for get article must response a 200
Given I have citizen: Given I have citizen: