-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
246 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 14 additions & 1 deletion
15
ychat/src/commonMain/kotlin/co/yml/ychat/domain/model/ImageGenerationsParams.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
ychat/src/commonTest/kotlin/co/yml/ychat/domain/mapper/ChatCompletionsMapperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package co.yml.ychat.domain.mapper | ||
|
||
import co.yml.ychat.data.dto.ChatCompletionsChoiceDto | ||
import co.yml.ychat.data.dto.ChatCompletionsDto | ||
import co.yml.ychat.data.dto.ChatMessageDto | ||
import co.yml.ychat.data.dto.UsageDto | ||
import co.yml.ychat.domain.model.ChatCompletionsParams | ||
import co.yml.ychat.domain.model.ChatMessage | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class ChatCompletionsMapperTest { | ||
|
||
@Test | ||
fun `on convert ChatCompletionsDto to ChatMessages`() { | ||
val listOfChatMessages = listOf(ChatMessage("user", "message 1"), ChatMessage("user", "message 2")) | ||
val chatCompletionsDto = ChatCompletionsDto( | ||
choices = listOf( | ||
ChatCompletionsChoiceDto(1, ChatMessageDto("user", "message 1"), null), | ||
ChatCompletionsChoiceDto(1, ChatMessageDto("user", "message 2"), null) | ||
), | ||
id = "1", | ||
model = "", | ||
usage = UsageDto(1, 1, 1) | ||
) | ||
assertEquals(listOfChatMessages, chatCompletionsDto.toChatMessages()) | ||
} | ||
|
||
|
||
@Test | ||
fun `on convert ChatCompletionsParams to ChatCompletionParamsDto`() { | ||
val messages = arrayListOf(ChatMessage("user", "message 1"), ChatMessage("user", "message 2")) | ||
val chatCompletionsParams = ChatCompletionsParams(messages) | ||
assertEquals(messages.map { ChatMessageDto(it.role, it.content) }, chatCompletionsParams.toChatCompletionParamsDto().messages, "") | ||
assertEquals("gpt-3.5-turbo", chatCompletionsParams.toChatCompletionParamsDto().model) | ||
assertEquals(1, chatCompletionsParams.toChatCompletionParamsDto().maxResults) | ||
assertEquals(4096, chatCompletionsParams.toChatCompletionParamsDto().maxTokens) | ||
assertEquals(1.0, chatCompletionsParams.toChatCompletionParamsDto().temperature) | ||
assertEquals(1.0, chatCompletionsParams.toChatCompletionParamsDto().topP) | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
ychat/src/commonTest/kotlin/co/yml/ychat/domain/mapper/ImageGenerationsMapperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package co.yml.ychat.domain.mapper | ||
|
||
import co.yml.ychat.data.dto.ImageGenerationsDto | ||
import co.yml.ychat.domain.model.ImageGenerated | ||
import co.yml.ychat.domain.model.ImageGenerationsParams | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class ImageGenerationsMapperTest { | ||
|
||
@Test | ||
fun `on convert ImageGenerationsDto to ImageGenerated`() { | ||
val listOfImageGenerated = listOf(ImageGenerated("http://url1.test"), ImageGenerated("http://url2.test")) | ||
val imageGenerationsDto = ImageGenerationsDto( | ||
created = 12345, | ||
data = listOfImageGenerated | ||
) | ||
assertEquals(listOfImageGenerated, imageGenerationsDto.toImageGenerated()) | ||
} | ||
|
||
@Test | ||
fun `on convert ImageGenerationsParams to ImageGenerationsDto`() { | ||
val imageGenerationsParams = ImageGenerationsParams(prompt = "/image test") | ||
assertEquals("/image test", imageGenerationsParams.toImageGenerationsParamsDto().prompt) | ||
assertEquals("url", imageGenerationsParams.toImageGenerationsParamsDto().responseFormat) | ||
assertEquals("256x256", imageGenerationsParams.toImageGenerationsParamsDto().size) | ||
assertEquals("", imageGenerationsParams.toImageGenerationsParamsDto().user) | ||
assertEquals(1, imageGenerationsParams.toImageGenerationsParamsDto().results) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
ychat/src/commonTest/kotlin/co/yml/ychat/domain/model/ImageGenerationsParamsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package co.yml.ychat.domain.model | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class ImageGenerationsParamsTest { | ||
|
||
@Test | ||
fun `on ChatCompletionsParams verify default values`() { | ||
// arrange | ||
val params = ImageGenerationsParams() | ||
|
||
// assert | ||
assertEquals(true, params.prompt.isEmpty()) | ||
assertEquals(1, params.results) | ||
assertEquals("256x256", params.size) | ||
assertEquals("url", params.responseFormat) | ||
assertEquals("", params.user) | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
ychat/src/commonTest/kotlin/co/yml/ychat/domain/usecases/ImageGenerationsUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package co.yml.ychat.domain.usecases | ||
|
||
import co.yml.ychat.data.api.ChatGptApi | ||
import co.yml.ychat.data.dto.ImageGenerationsDto | ||
import co.yml.ychat.data.exception.ChatGptException | ||
import co.yml.ychat.data.infrastructure.ApiResult | ||
import co.yml.ychat.domain.model.ImageGenerated | ||
import co.yml.ychat.domain.model.ImageGenerationsParams | ||
import io.mockk.coEvery | ||
import io.mockk.mockk | ||
import kotlinx.coroutines.runBlocking | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class ImageGenerationsUseCaseTest { | ||
|
||
private lateinit var imageGenerationsUseCase: ImageGenerationsUseCase | ||
|
||
private val chatGptApiMock = mockk<ChatGptApi>() | ||
|
||
@BeforeTest | ||
fun setup() { | ||
imageGenerationsUseCase = ImageGenerationsUseCase(chatGptApiMock) | ||
} | ||
|
||
@Test | ||
fun `on requestImageGenerations when request succeed then should return formatted result`() { | ||
// arrange | ||
val prompt = "/image test" | ||
val imageGenerationsDto = buildImageGenerationsDto("https://image-generated.test") | ||
val params = ImageGenerationsParams(prompt = prompt) | ||
val apiResult = ApiResult(body = imageGenerationsDto) | ||
coEvery { chatGptApiMock.imageGenerations(any()) } returns apiResult | ||
|
||
// act | ||
val result = runBlocking { imageGenerationsUseCase.requestImageGenerations(params) } | ||
|
||
// assert | ||
assertEquals("https://image-generated.test", result.last().url) | ||
} | ||
|
||
@Test | ||
fun `on requestChatCompletions when not request succeed then should throw an exception`() { | ||
// arrange | ||
val prompt = "/image test" | ||
val params = ImageGenerationsParams(prompt = prompt) | ||
val apiResult = ApiResult<ImageGenerationsDto>(exception = ChatGptException()) | ||
coEvery { chatGptApiMock.imageGenerations(any()) } returns apiResult | ||
|
||
// act | ||
val result = | ||
runCatching { runBlocking { imageGenerationsUseCase.requestImageGenerations(params) } } | ||
|
||
// assert | ||
assertEquals(true, result.exceptionOrNull() is ChatGptException) | ||
} | ||
|
||
private fun buildImageGenerationsDto(url: String): ImageGenerationsDto { | ||
return ImageGenerationsDto( | ||
created = 12345, | ||
data = listOf(ImageGenerated(url)) | ||
) | ||
} | ||
} |