Skip to content

Commit

Permalink
More tests (#40)
Browse files Browse the repository at this point in the history
- **Renamed and enhanced DocumentParserExtensions test:**
- Test class renamed to `DocumentParserExtensionsKtTest` to adhere to
Kotlin naming conventions.
- Added a test case to validate the `parseAsync` method when no source
metadata is available, ensuring proper document parsing.

- **Added unit tests for ChatLanguageModel extensions:**
- Implemented tests for `chatAsync` and `generateAsync` methods in
`ChatLanguageModel`.
- Updated README with a Codecov badge for enhanced code coverage
visibility.
    - Ensures asynchronous chat processing functions correctly.

---------

Co-authored-by: Konstantin Pavlov <{ID}+{username}@users.noreply.github.com>
  • Loading branch information
kpavlov and Konstantin Pavlov authored Dec 2, 2024
1 parent 993b463 commit 223c83e
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![Kotlin CI with Maven](https://github.com/kpavlov/langchain4j-kotlin/actions/workflows/maven.yml/badge.svg?branch=main)](https://github.com/kpavlov/langchain4j-kotlin/actions/workflows/maven.yml)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/644f664ad05a4a009b299bc24c8be4b8)](https://app.codacy.com/gh/kpavlov/langchain4j-kotlin/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
[![Codacy Coverage](https://app.codacy.com/project/badge/Coverage/644f664ad05a4a009b299bc24c8be4b8)](https://app.codacy.com/gh/kpavlov/langchain4j-kotlin/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
[![codecov](https://codecov.io/gh/kpavlov/langchain4j-kotlin/graph/badge.svg?token=VYIJ92CYHD)](https://codecov.io/gh/kpavlov/langchain4j-kotlin)
[![Maintainability](https://api.codeclimate.com/v1/badges/176ba2c4e657d3e7981a/maintainability)](https://codeclimate.com/github/kpavlov/langchain4j-kotlin/maintainability)
[![Api Docs](https://img.shields.io/badge/api-docs-blue)](https://kpavlov.github.io/langchain4j-kotlin/api/)
Kotlin enhancements for [LangChain4j](https://github.com/langchain4j/langchain4j), providing coroutine support and Flow-based streaming capabilities for chat language models.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package me.kpavlov.langchain4j.kotlin.data.document
import assertk.assertThat
import assertk.assertions.containsOnly
import assertk.assertions.isEqualTo
import assertk.assertions.isSameInstanceAs
import dev.langchain4j.data.document.Document
import dev.langchain4j.data.document.Document.ABSOLUTE_DIRECTORY_PATH
import dev.langchain4j.data.document.Document.FILE_NAME
Expand All @@ -18,7 +19,7 @@ import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.whenever

@ExtendWith(MockitoExtension::class)
internal class DocumentParserExtensionsTest {
internal class DocumentParserExtensionsKtTest {
@Mock
private lateinit var documentParser: DocumentParser

Expand Down Expand Up @@ -55,4 +56,23 @@ internal class DocumentParserExtensionsTest {
)
}
}

@Test
fun `parseAsync should return parser document when no source metadata is present`() {
val documentContent = "parsed document content"
runTest {
val inputStream = documentContent.byteInputStream()
val documentMetadata = Metadata.from(mapOf<String, Any?>("title" to "Bar"))
val fileMetadata = Metadata.from(mapOf<String, Any?>())
val document = Document.from(documentContent, documentMetadata)

whenever(documentSource.inputStream()).thenReturn(inputStream)
whenever(documentParser.parse(inputStream)).thenReturn(document)
whenever(documentSource.metadata()).thenReturn(fileMetadata)

val result = documentParser.parseAsync(documentSource, Dispatchers.IO)

assertThat(result).isSameInstanceAs(document)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package me.kpavlov.langchain4j.kotlin.model.chat

import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isSameInstanceAs
import dev.langchain4j.data.message.AiMessage
import dev.langchain4j.data.message.ChatMessage
import dev.langchain4j.model.chat.ChatLanguageModel
import dev.langchain4j.model.chat.request.ChatRequest
import dev.langchain4j.model.chat.response.ChatResponse
import dev.langchain4j.model.output.Response
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

@ExtendWith(MockitoExtension::class)
internal class ChatLanguageModelExtensionsKtTest {
@Mock
private lateinit var mockModel: ChatLanguageModel

@Mock
private lateinit var request: ChatRequest

@Mock
private lateinit var requestBuilder: ChatRequest.Builder

@Mock
private lateinit var expectedResponse: ChatResponse

/**
* This class tests the `chatAsync` extension function of `ChatLanguageModel`.
* The function takes a `ChatRequest` or a `ChatRequest.Builder` as input,
* performs asynchronous processing, and returns a `ChatResponse`.
*/

@Test
fun `chatAsync should return expected ChatResponse when using ChatRequest`() =
runTest {
whenever(mockModel.chat(request)).thenReturn(expectedResponse)

val actualResponse = mockModel.chatAsync(request)

assertThat(actualResponse).isEqualTo(expectedResponse)
verify(mockModel).chat(request)
}

@Test
fun `generateAsync should return expected AiMessage when using valid ChatMessages`() =
runTest {
val messages = mock<List<ChatMessage>>()
val expectedAiMessage = mock<Response<AiMessage>>()

whenever(mockModel.generate(messages)).thenReturn(expectedAiMessage)

val actualAiMessage = mockModel.generateAsync(messages)

assertThat(actualAiMessage).isSameInstanceAs(expectedAiMessage)
}

@Test
fun `chatAsync should return expected ChatResponse when using ChatRequest Builder`() =
runTest {
whenever(requestBuilder.build()).thenReturn(request)
whenever(mockModel.chat(request)).thenReturn(expectedResponse)

val actualResponse = mockModel.chatAsync(requestBuilder)

assertThat(actualResponse).isEqualTo(expectedResponse)
verify(mockModel).chat(request)
}

@Test
fun `chat should return expected ChatResponse when using ChatRequest Builder`() =
runTest {
whenever(requestBuilder.build()).thenReturn(request)
whenever(mockModel.chat(request)).thenReturn(expectedResponse)

val actualResponse = mockModel.chat(requestBuilder)

assertThat(actualResponse).isEqualTo(expectedResponse)
verify(mockModel).chat(request)
}
}

0 comments on commit 223c83e

Please sign in to comment.