-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat : adds test case for embeddingClientConversion #136
Changes from 2 commits
972bd2c
60f1954
23fd193
9a176f5
bcb1671
6f7b1b4
2777a25
01c8726
88dc14a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.example.ai.controller; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
import static org.hamcrest.Matchers.hasSize; | ||
|
@@ -9,6 +10,7 @@ | |
import com.example.ai.model.request.AIChatRequest; | ||
import io.restassured.RestAssured; | ||
import io.restassured.http.ContentType; | ||
import java.util.Arrays; | ||
import org.apache.http.HttpStatus; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
@@ -76,6 +78,26 @@ void sentimentAnalyzer() { | |
.body("answer", is("SARCASTIC")); | ||
} | ||
|
||
@Test | ||
void embeddingClientConversion() { | ||
String response = given().contentType(ContentType.JSON) | ||
.body(new AIChatRequest("Why did the Python programmer go broke? Because he couldn't C#")) | ||
.when() | ||
.post("/api/ai/embedding-client-conversion") | ||
.then() | ||
.statusCode(HttpStatus.SC_OK) | ||
.contentType(ContentType.JSON) | ||
.extract() | ||
.jsonPath() | ||
.get("answer"); | ||
|
||
double[] doubles = Arrays.stream(response.replaceAll("[\\[\\]]", "").split(",")) | ||
.mapToDouble(Double::parseDouble) | ||
.toArray(); | ||
|
||
assertThat(doubles.length).isEqualTo(1536).as("Dimensions for openai model is 1536"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai, there is no method called allSatisfy in assertj library, please fix below suggestion as provided in nitpick assertThat(doubles).allSatisfy(value ->
assertThat(value).isBetween(-1.0, 1.0)
).as("All embedding values should be between -1 and 1"); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rajadilipkolli, thanks for pointing that out! Since for (double value : doubles) {
assertThat(value).isBetween(-1.0, 1.0);
} Alternatively, with streams: assertThat(Arrays.stream(doubles).allMatch(value -> value >= -1.0 && value <= 1.0))
.isTrue()
.as("All embedding values should be between -1.0 and 1.0"); |
||
} | ||
|
||
@Test | ||
void outputParser() { | ||
given().param("actor", "Jr NTR") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai, please review all test cases in this class and provide if there are any suggestions or improvements
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rajadilipkolli, here are my suggestions and observations after reviewing the test cases in
ChatControllerTest
:Balance of Positive and Negative Tests
Additional Assertions
testChat
,chatWithPrompt
,chatWithSystemPrompt
, orsentimentAnalyzer
, you might add more assertions checking the structure of the JSON body. For instance, verifying each top-level field in the response to ensure your controller includes all expected properties.outputParser
, besides checking the size ofmovies
, you might verify each item meets certain criteria if relevant (such as ensuring the type is a string and non-empty).Edge / Corner Cases
embeddingClientConversion
, you test the expected size of the double array. Consider verifying whether the values are valid embeddings (e.g., checking if any non-numeric values appear, or if each value is finite), though this depends on how much your application needs to validate them.ragWithSimpleStore
, you are checking a general text snippet in the answer. If the application logic is expected to do specific manipulations, consider verifying that the entire returned structure is well-formed.Descriptive Test Names
shouldReturnSarcasticSentiment()
ortestRagWithSimpleStoreProvidesValidResponse()
. This especially helps when reading test reports or debugging.Parameterization
chatWithPrompt
andchatWithSystemPrompt
, if you plan to test multiple variations of user prompts, JUnit’s parameterized tests could reduce duplication.Maintainability
ChatController
grows more complex, consider refactoring common RestAssured setup steps into a utility function or a custom test extension. This way, you keep each test more “readable” while reducing duplicate code.Overall, the tests are straightforward and effectively use RestAssured’s fluent approach. Implementing additional negative tests and refining validations will help ensure broader coverage and correctness of the API responses. If you have any further questions or would like more suggestions, let me know!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coderabbitai, can you please help me with writing code to implement all above suggestions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rajadilipkolli, here's some example code illustrating how you could incorporate the suggestions from my earlier review into your test suite. Please feel free to adjust any details to suit your project’s requirements.
1. Negative Test Cases
Below is an example test checking how the endpoint responds to a malformed request (e.g., an empty JSON). You can add more variations of malformed or invalid data as needed:
2. Additional Assertions
For more thorough checks on JSON structure, you can use RestAssured’s fluent approach to verify multiple response fields. Here’s an example updated from the
testChat
method, checking top-level fields:3. Edge / Corner Cases
In
embeddingClientConversion
, if you want to ensure the returned values are valid embeddings (for instance, checking if they are finite numbers), you can add these checks:4. Descriptive Test Names
Renaming existing tests helps clarify their expected behavior. For example, instead of
sentimentAnalyzer
, you could rename it toshouldAnalyzeSentimentAsSarcastic()
. Below is an illustrative renaming (you can rename others too):5. Parameterization
If you plan to test multiple input prompts for
chatWithPrompt
, you can use JUnit parameterized tests. Below is a minimal example of how you might set this up:Notice how the
containsStringIgnoringCase
check ensures the response includes your provided prompt in some manner.6. Maintainability
If your test class grows in complexity, you can reduce duplication of common setup steps by extracting them into utility methods or a separate class. For example, if you find yourself constructing
AIChatRequest
with the same defaults repeatedly, you might do something like:I hope these examples help you integrate the suggestions effectively. Let me know if you have any questions, or if there are other specific adjustments you’d like!