Improve query findFollowsByTarget & add tests

This commit is contained in:
2021-04-27 18:52:36 +02:00
parent 76e4033a22
commit 371483ccde
8 changed files with 228 additions and 303 deletions

View File

@@ -66,7 +66,7 @@ class NotificationConsumerTest {
@KtorExperimentalAPI
@ExperimentalCoroutinesApi
@Test
fun `can be send notification`() = runBlocking {
fun `can be receive article update notification when follow article`() = runBlocking {
val config: Configuration = Configuration("application-test.conf")
/* Create mocks and spy's */
val emailSender = mockk<NotificationEmailSender>() {

View File

@@ -3,6 +3,7 @@ package integration
import fr.dcproject.component.citizen.database.CitizenI.Name
import fr.dcproject.component.notification.ArticleUpdateNotificationMessage
import fr.dcproject.component.notification.NotificationMessage
import integration.steps.given.`And follow citizen`
import integration.steps.given.`Given I have article update notification`
import integration.steps.given.`Given I have article`
import integration.steps.given.`Given I have citizen`
@@ -50,4 +51,38 @@ class `Notification routes` : BaseTest() {
}
}
}
@Test
fun `I can receive article update notification when follow the creator`() {
withIntegrationApplication {
`Given I have citizen`("Thomas", "Pesquet", id = "1a34191a-9cde-45ba-8ac1-230138a102d3")
`Given I have article`(id = "a06cbfb7-3094-4d64-aaa1-7486c0c292f4", createdBy = Name(firstName = "Thomas", lastName = "Pesquet"))
`Given I have citizen`("Alan", "Bean") {
`And follow citizen`(Name("Thomas", "Pesquet"))
}
`Given I have article update notification`("a06cbfb7-3094-4d64-aaa1-7486c0c292f4")
Thread.sleep(1000)
handleWebSocketConversation(
"/notifications",
{
`authenticated in url as`("Alan", "Bean")
}
) { incoming, outgoing ->
incoming.receive().let {
when (it) {
is Frame.Text -> NotificationMessage.fromString<ArticleUpdateNotificationMessage>(it.readText()).let { notif ->
assertEquals(
"a06cbfb7-3094-4d64-aaa1-7486c0c292f4",
notif.target.id.toString()
)
outgoing.send(it)
}
else -> error(it.toString())
}
}
}
}
}
}

View File

@@ -30,6 +30,13 @@ fun Citizen.`And follow citizen`(
) {
createFollow(this, CitizenRef(citizen.toUUID()))
}
fun Citizen.`And follow citizen`(
name: CitizenI.Name,
) {
val citizenRepository: CitizenRepository by lazy { GlobalContext.get().get() }
val citizen = citizenRepository.findByName(name) ?: error("Citizen not exist")
createFollow(this, CitizenRef(citizen.id))
}
fun TestApplicationEngine.`Given I have follow on article`(
firstName: String,
@@ -41,6 +48,17 @@ fun TestApplicationEngine.`Given I have follow on article`(
createFollow(citizen, ArticleRef(article.toUUID()))
}
fun TestApplicationEngine.`Given I have follow on citizen`(
firstName: String,
lastName: String,
target: CitizenI.Name,
) {
val citizenRepository: CitizenRepository by lazy { GlobalContext.get().get() }
val citizen = citizenRepository.findByName(CitizenI.Name(firstName, lastName)) ?: error("Citizen not exist")
val targetCitizen = citizenRepository.findByName(target) ?: error("Citizen not exist")
createFollow(citizen, CitizenRef(targetCitizen.id))
}
fun TestApplicationEngine.`Given I have follow on constitution`(
firstName: String,
lastName: String,

View File

@@ -7,17 +7,19 @@ declare
_version_id1 uuid = uuid_generate_v4();
first_article_id uuid := fixture_article(_citizen_id := _citizen_id, _version_id := _version_id1);
first_article_updated_id uuid;
_follow_count int = 0;
begin
perform follow('citizen'::regclass, _citizen_id, _citizen_id2);
assert (select count(*) = 1 from follow), 'follow must be inserted';
assert (select following = true from find_follow(_citizen_id, _citizen_id2, 'citizen')), 'find_follow must return the following';
perform follow('citizen'::regclass, _citizen_id, _citizen_id2);
assert (select count(*) = 1 from follow), 'follow must be inserted';
assert (select count(*) = 1 from follow), 're follow must be do nothing';
perform unfollow('citizen'::regclass, _citizen_id, _citizen_id2);
assert (select count(*) = 0 from follow), 'follow must be deleted after unfollow';
perform follow('article'::regclass, first_article_id, _citizen_id);
assert (select following = true from find_follow(first_article_id, _citizen_id, 'article')), 'find_follow must return the following';
assert (select following = false from find_follow(first_article_id, _citizen_id2, 'article')), 'find_follow must not return the following if not followinf';
@@ -29,11 +31,17 @@ begin
assert (select following = true from find_follow(first_article_id, _citizen_id, 'article')), '(v1) find_follow must return the following';
assert (select following = true from find_follow(first_article_updated_id, _citizen_id, 'article')), '(v2) find_follow must return the following';
assert (select f.total = 1 from find_follows_article_by_target(first_article_id) as f), 'find_follows_article_by_target must return 1 follow';
assert (select (f.resource#>>'{0, created_by, id}')::uuid = _citizen_id from find_follows_article_by_target(first_article_id) as f), 'find_follows_article_by_target must return follows with creator';
assert (select count(*) = 1 from follow), 'must be only 1 follow';
perform follow('citizen'::regclass, _citizen_id, _citizen_id2);
assert (select count(*) = 2 from follow), 'follow citizen must be inserted';
assert (select json_array_length(f.resource) = 2 from find_follows_article_by_target(first_article_id) as f), 'find_follows_article_by_target must return 2 follows';
assert (select (f.resource#>>'{0, created_by, id}')::uuid = _citizen_id from find_follows_article_by_target(first_article_id) as f), 'find_follows_article_by_target must return follows with creator';
assert (select (f.resource#>>'{1, created_by, id}')::uuid = _citizen_id2 from find_follows_article_by_target(first_article_id) as f), 'find_follows_article_by_target must return follows with creator';
_follow_count = (select count(*) from follow);
perform unfollow('article'::regclass, first_article_id, _citizen_id);
assert (select count(*) = 0 from follow), 'follow must be deleted after unfollow, event if article is on other version';
assert (select count(*) = _follow_count-1 from follow), 'follow must be deleted after unfollow, event if article is on other version';
rollback;
raise notice 'follow test pass';