Skip to content

Commit

Permalink
Response tests. (langchain4j#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
crutcher authored Jan 15, 2024
1 parent d078d2f commit b1cd592
Showing 1 changed file with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package dev.langchain4j.model.output;

import org.assertj.core.api.WithAssertions;
import org.junit.jupiter.api.Test;

class ResponseTest implements WithAssertions {
@Test
public void test_methods() {
{
Response<String> response = new Response<>("content");
assertThat(response.content()).isEqualTo("content");
assertThat(response.tokenUsage()).isNull();
assertThat(response.finishReason()).isNull();
assertThat(response).hasToString("Response { content = content, tokenUsage = null, finishReason = null }");
}
{
TokenUsage tokenUsage = new TokenUsage(1, 2, 3);
Response<String> response = new Response<>("content", tokenUsage, null);
assertThat(response.content()).isEqualTo("content");
assertThat(response.tokenUsage()).isEqualTo(tokenUsage);
assertThat(response.finishReason()).isNull();
assertThat(response)
.hasToString(
"Response { " +
"content = content, tokenUsage = TokenUsage { " +
"inputTokenCount = 1, outputTokenCount = 2, totalTokenCount = 3 }, " +
"finishReason = null }");
}
{
TokenUsage tokenUsage = new TokenUsage(1, 2, 3);
Response<String> response = new Response<>("content", tokenUsage, FinishReason.LENGTH);
assertThat(response.content()).isEqualTo("content");
assertThat(response.tokenUsage()).isEqualTo(tokenUsage);
assertThat(response.finishReason()).isEqualTo(FinishReason.LENGTH);
assertThat(response)
.hasToString(
"Response { " +
"content = content, tokenUsage = TokenUsage { " +
"inputTokenCount = 1, outputTokenCount = 2, totalTokenCount = 3 }, " +
"finishReason = LENGTH }");
}
}

@Test
public void test_equals_hashCode() {
String content1 = "content";
String content2 = "changed";
TokenUsage tokenUsage1 = new TokenUsage(1, 2, 3);
TokenUsage tokenUsage2 = new TokenUsage(10, 2, 3);

assertThat(new Response<>(content1, null, null))
.isEqualTo(new Response<>(content1, null, null))
.hasSameHashCodeAs(new Response<>(content1, null, null))
.isNotEqualTo(null)
.isNotEqualTo(new Object())
.isNotEqualTo(new Response<>(content2, null, null))
.doesNotHaveSameHashCodeAs(new Response<>(content2, null, null))
.isNotEqualTo(new Response<>(content1, tokenUsage1, null))
.doesNotHaveSameHashCodeAs(new Response<>(content1, tokenUsage1, null))
.isNotEqualTo(new Response<>(content1, null, FinishReason.LENGTH))
.doesNotHaveSameHashCodeAs(new Response<>(content1, null, FinishReason.LENGTH))
.isNotEqualTo(new Response<>(content1, tokenUsage1, FinishReason.LENGTH))
.doesNotHaveSameHashCodeAs(new Response<>(content1, tokenUsage1, FinishReason.LENGTH));

assertThat(new Response<>(content1, tokenUsage1, FinishReason.LENGTH))
.isEqualTo(new Response<>(content1, tokenUsage1, FinishReason.LENGTH))
.hasSameHashCodeAs(new Response<>(content1, tokenUsage1, FinishReason.LENGTH))
.isNotEqualTo(new Response<>(content2, tokenUsage1, FinishReason.LENGTH))
.isNotEqualTo(new Response<>(content1, tokenUsage2, FinishReason.LENGTH))
.isNotEqualTo(new Response<>(content1, tokenUsage1, FinishReason.STOP));
}

@Test
public void test_builders() {
assertThat(new Response<>("content"))
.isEqualTo(Response.from("content"));

TokenUsage tokenUsage = new TokenUsage(1, 2, 3);

assertThat(new Response<>("content", tokenUsage, null))
.isEqualTo(Response.from("content", tokenUsage))
.isEqualTo(Response.from("content", tokenUsage, null));

assertThat(new Response<>("content", tokenUsage, FinishReason.LENGTH))
.isEqualTo(Response.from("content", tokenUsage, FinishReason.LENGTH));

}
}

0 comments on commit b1cd592

Please sign in to comment.