Skip to content

Unit Testing of Generation class #594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.springframework.ai.chat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.ai.chat.metadata.ChatGenerationMetadata;

import static org.junit.jupiter.api.Assertions.*;

public class GenerationTests {
@Mock
private ChatGenerationMetadata mockChatGenerationMetadata1;

@Mock
private ChatGenerationMetadata mockChatGenerationMetadata2;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void testGetOutput() {
String expectedText = "Test Assistant Message";
Generation generation = new Generation(expectedText);

assertEquals(expectedText, generation.getOutput().getContent());
}

@Test
void testWithGenerationMetadata(){
Generation generation = new Generation("Test Assistant Message");

assertEquals(generation, generation.withGenerationMetadata(mockChatGenerationMetadata1));
}

@Test
void testGetMetadata_Null() {
Generation generation = new Generation("Test Assistant Message");
ChatGenerationMetadata metadata = generation.getMetadata();

assertEquals(ChatGenerationMetadata.NULL, metadata);
}

@Test
void testGetMetadata_NotNull() {
Generation generation = new Generation("Test Assistant Message");
generation.withGenerationMetadata(mockChatGenerationMetadata1);
ChatGenerationMetadata metadata = generation.getMetadata();

assertEquals(mockChatGenerationMetadata1, metadata);
}

@Test
void testEquals_SameObjects() {
Generation generation1 = new Generation("Test Assistant Message");
Generation generation2 = generation1;

assertTrue(generation1.equals(generation2));
}

@Test
void testEquals_NotInstanceOfGeneration() {
Generation generation = new Generation("Test Assistant Message");
Object notGenerationObject = new Object();

assertFalse(generation.equals(notGenerationObject));
}

@Test
void testEquals_SameMetadata() {
Generation generation1 = new Generation("Test Assistant Message").withGenerationMetadata(mockChatGenerationMetadata1);
Generation generation2 = new Generation("Test Assistant Message").withGenerationMetadata(mockChatGenerationMetadata1);

assertTrue(generation1.equals(generation2));
}

@Test
void testEquals_DifferentMetadata() {
Generation generation1 = new Generation("Test Assistant Message").withGenerationMetadata(mockChatGenerationMetadata1);
Generation generation2 = new Generation("Test Assistant Message").withGenerationMetadata(mockChatGenerationMetadata2);

assertFalse(generation1.equals(generation2));
}
}