Skip to content

Commit

Permalink
add test base
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-wls committed Nov 20, 2024
1 parent e53fa9d commit cb4a308
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package de.tum.informatics.www1.artemis.native_app.feature.metis.conversation.service.storage.impl

import androidx.test.platform.app.InstrumentationRegistry
import de.tum.informatics.www1.artemis.native_app.core.model.account.User
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.content.MetisContext
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.content.dto.AnswerPost
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.content.dto.StandalonePost
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.content.dto.UserRole
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.content.dto.conversation.OneToOneChat
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.db.pojo.AnswerPostPojo
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.db.pojo.PostPojo
import de.tum.informatics.www1.artemis.native_app.feature.metistest.MetisDatabaseProviderMock
import kotlinx.datetime.Clock

abstract class MetisStorageBaseTest {

private val databaseProviderMock = MetisDatabaseProviderMock(InstrumentationRegistry.getInstrumentation().context)
internal val sut = MetisStorageServiceImpl(databaseProviderMock)

internal val host = "host"

private val author = User(id = 20, name = "AuthorName")
private val parentClientPostId = "parent-client-id-0"
internal val answerClientPostId = "answer-client-id-0"

private val course: MetisContext.Course = MetisContext.Course(courseId = 1)
internal val conversation = OneToOneChat(id = 2)
internal val metisContext = MetisContext.Conversation(course.courseId, conversation.id)

internal val localAnswerPojo = AnswerPostPojo(
parentPostId = parentClientPostId,
postId = answerClientPostId,
resolvesPost = false,
basePostingCache = AnswerPostPojo.BasePostingCache(
serverPostId = 0,
authorId = author.id,
creationDate = Clock.System.now(),
updatedDate = null,
content = "Answer post content 0",
authorRole = UserRole.USER,
authorName = author.name!!
),
reactions = emptyList(),
serverPostIdCache = AnswerPostPojo.ServerPostIdCache(
serverPostId = null // Only local answer post, no server id
)
)

internal val basePostPojo = PostPojo(
clientPostId = parentClientPostId,
serverPostId = 0,
content = "Base post content",
resolved = false,
updatedDate = null,
creationDate = Clock.System.now(),
authorId = author.id,
title = null,
authorName = author.name!!,
authorRole = UserRole.USER,
courseWideContext = null,
tags = emptyList(),
answers = emptyList(),
reactions = emptyList()
)

internal val basePost = StandalonePost(basePostPojo, conversation)
internal val localAnswer = AnswerPost(localAnswerPojo, basePost)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@ import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingSource
import androidx.paging.testing.asSnapshot
import androidx.test.platform.app.InstrumentationRegistry
import de.tum.informatics.www1.artemis.native_app.core.common.test.UnitTest
import de.tum.informatics.www1.artemis.native_app.core.model.account.User
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.content.MetisContext
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.content.dto.AnswerPost
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.content.dto.StandalonePost
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.content.dto.UserRole
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.content.dto.conversation.OneToOneChat
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.db.pojo.AnswerPostPojo
import de.tum.informatics.www1.artemis.native_app.feature.metis.shared.db.pojo.PostPojo
import de.tum.informatics.www1.artemis.native_app.feature.metistest.MetisDatabaseProviderMock
import kotlinx.coroutines.test.runTest
import kotlinx.datetime.Clock
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.experimental.categories.Category
Expand All @@ -25,59 +16,7 @@ import org.robolectric.RobolectricTestRunner

@Category(UnitTest::class)
@RunWith(RobolectricTestRunner::class)
class MetisStorageServiceImplUpgradeLocalAnswerPostTest {

private val databaseProviderMock = MetisDatabaseProviderMock(InstrumentationRegistry.getInstrumentation().context)
private val sut = MetisStorageServiceImpl(databaseProviderMock)

private val host = "host"

private val author = User(id = 20, name = "AuthorName")
private val parentClientPostId = "parent-client-id-0"
private val answerClientPostId = "answer-client-id-0"

private val course: MetisContext.Course = MetisContext.Course(courseId = 1)
private val conversation = OneToOneChat(id = 2)
private val metisContext = MetisContext.Conversation(course.courseId, conversation.id)

private val localAnswerPojo = AnswerPostPojo(
parentPostId = parentClientPostId,
postId = answerClientPostId,
resolvesPost = false,
basePostingCache = AnswerPostPojo.BasePostingCache(
serverPostId = 0,
authorId = author.id,
creationDate = Clock.System.now(),
updatedDate = null,
content = "Answer post content 0",
authorRole = UserRole.USER,
authorName = author.name!!
),
reactions = emptyList(),
serverPostIdCache = AnswerPostPojo.ServerPostIdCache(
serverPostId = null // Only local answer post, no server id
)
)

private val basePostPojo = PostPojo(
clientPostId = parentClientPostId,
serverPostId = 0,
content = "Base post content",
resolved = false,
updatedDate = null,
creationDate = Clock.System.now(),
authorId = author.id,
title = null,
authorName = author.name!!,
authorRole = UserRole.USER,
courseWideContext = null,
tags = emptyList(),
answers = emptyList(),
reactions = emptyList()
)

private val basePost = StandalonePost(basePostPojo, conversation)
private val localAnswer = AnswerPost(localAnswerPojo, basePost)
class MetisStorageServiceImplUpgradeLocalAnswerPostTest : MetisStorageBaseTest() {

private lateinit var basePostUpdated: StandalonePost
private lateinit var answerUpdated: AnswerPost
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package de.tum.informatics.www1.artemis.native_app.feature.metis.conversation.service.storage.impl

import de.tum.informatics.www1.artemis.native_app.core.common.test.UnitTest
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.experimental.categories.Category
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@Category(UnitTest::class)
@RunWith(RobolectricTestRunner::class)
class MetisStorageServiceTestLiveCreation : MetisStorageBaseTest() {

@Test
fun testInsertLiveCreatedPostWithExistingPost() = runTest {
// GIVEN: A base post already exists
sut.insertOrUpdatePosts(
host = host,
metisContext = metisContext,
posts = listOf(basePost),
)

// WHEN: Inserting a live-created post
sut.insertLiveCreatedPost(
host = host,
metisContext = metisContext,
post = basePost
)

// THEN: No post should be inserted and the existing posts should not be modified
assertStoredContentIsNotModified()
}

@Test
fun testInsertLiveCreatedPost() = runTest {
// GIVEN: An empty chat

// WHEN: Inserting a live-created post
sut.insertLiveCreatedPost(
host = host,
metisContext = metisContext,
post = basePost
)

// THEN: The post should be inserted and matched to the correct conversation
assertPostIsCreated()
assertPostIsMatchedToConversation()
}

private fun assertPostIsCreated() {
//TODO
}

private fun assertStoredContentIsNotModified() {
//TODO
}

private fun assertPostIsMatchedToConversation() {
//TODO
}
}

0 comments on commit cb4a308

Please sign in to comment.