-
-
Notifications
You must be signed in to change notification settings - Fork 70
refactor:Add min/max validation constraint to attribute #2200
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughValidation constraints were added to the Changes
Assessment against linked issues
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
⏰ Context from checks skipped due to timeout of 90000ms (11)
🔇 Additional comments (2)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2200 +/- ##
=========================================
Coverage 14.49% 14.49%
Complexity 403 403
=========================================
Files 243 243
Lines 6588 6588
Branches 757 757
=========================================
Hits 955 955
Misses 5582 5582
Partials 51 51 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
@coderabbitai Can you suggest a unit test for testing the code changes in this PR? |
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain@jo-elimu Here's a comprehensive unit test suggestion for testing the validation constraints on the package ai.elimu.entity.analytics;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
class LetterSoundAssessmentEventValidationTest {
private Validator validator;
private LetterSoundAssessmentEvent event;
@BeforeEach
void setUp() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
// Create a basic valid event for testing
event = new LetterSoundAssessmentEvent();
// Set other required fields if any...
}
@Test
@DisplayName("Valid mastery score values should pass validation")
void testValidMasteryScores() {
// Test boundary values and typical valid values
float[] validScores = {0.0f, 0.5f, 1.0f, 0.25f, 0.75f, 0.999f, 0.001f};
for (float score : validScores) {
event.setMasteryScore(score);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
assertTrue(violations.isEmpty(),
String.format("Mastery score %.3f should be valid but validation failed", score));
}
}
@Test
@DisplayName("Mastery score below minimum should fail validation")
void testMasteryScoreBelowMinimum() {
float[] invalidScores = {-0.1f, -1.0f, -0.001f, Float.NEGATIVE_INFINITY};
for (float score : invalidScores) {
event.setMasteryScore(score);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
assertFalse(violations.isEmpty(),
String.format("Mastery score %.3f should be invalid", score));
// Verify the specific violation message
boolean foundMinViolation = violations.stream()
.anyMatch(v -> v.getMessage().contains("Mastery score must be at least 0"));
assertTrue(foundMinViolation,
String.format("Expected minimum validation message for score %.3f", score));
}
}
@Test
@DisplayName("Mastery score above maximum should fail validation")
void testMasteryScoreAboveMaximum() {
float[] invalidScores = {1.1f, 2.0f, 1.001f, Float.POSITIVE_INFINITY};
for (float score : invalidScores) {
event.setMasteryScore(score);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
assertFalse(violations.isEmpty(),
String.format("Mastery score %.3f should be invalid", score));
// Verify the specific violation message
boolean foundMaxViolation = violations.stream()
.anyMatch(v -> v.getMessage().contains("Mastery score must not exceed 1"));
assertTrue(foundMaxViolation,
String.format("Expected maximum validation message for score %.3f", score));
}
}
@Test
@DisplayName("Null mastery score should pass validation")
void testNullMasteryScore() {
event.setMasteryScore(null);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
// @Min and @Max allow null values by default
assertTrue(violations.isEmpty(),
"Null mastery score should be valid as it might not be included");
}
@Test
@DisplayName("Boundary values should be properly validated")
void testBoundaryValues() {
// Test exact boundary values
event.setMasteryScore(0.0f);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
assertTrue(violations.isEmpty(), "Mastery score of 0.0 should be valid");
event.setMasteryScore(1.0f);
violations = validator.validate(event);
assertTrue(violations.isEmpty(), "Mastery score of 1.0 should be valid");
}
} This test suite covers:
The test uses Jakarta Bean Validation's 🏁 Script executed:
Length of output: 1312 🏁 Script executed:
Length of output: 731 🏁 Script executed:
Length of output: 99 🏁 Script executed:
Length of output: 80 Based on my analysis of the package ai.elimu.entity.analytics;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
class LetterSoundAssessmentEventValidationTest {
private Validator validator;
private LetterSoundAssessmentEvent event;
@BeforeEach
void setUp() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
// Create a basic valid event for testing
event = new LetterSoundAssessmentEvent();
event.setLetterSoundLetters("sh");
event.setLetterSoundSounds("ʃ");
// Set other fields as needed for a valid base object
}
@Test
@DisplayName("Valid mastery score values should pass validation")
void testValidMasteryScores() {
// Test boundary values and typical valid values
float[] validScores = {0.0f, 0.5f, 1.0f, 0.25f, 0.75f, 0.999f, 0.001f};
for (float score : validScores) {
event.setMasteryScore(score);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
// Filter only masteryScore violations for this test
long masteryScoreViolations = violations.stream()
.filter(v -> "masteryScore".equals(v.getPropertyPath().toString()))
.count();
assertEquals(0, masteryScoreViolations,
String.format("Mastery score %.3f should be valid but validation failed", score));
}
}
@Test
@DisplayName("Mastery score below minimum should fail validation")
void testMasteryScoreBelowMinimum() {
float[] invalidScores = {-0.1f, -1.0f, -0.001f};
for (float score : invalidScores) {
event.setMasteryScore(score);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
// Check for masteryScore violations specifically
boolean foundMinViolation = violations.stream()
.filter(v -> "masteryScore".equals(v.getPropertyPath().toString()))
.anyMatch(v -> v.getMessage().contains("Mastery score must be at least 0"));
assertTrue(foundMinViolation,
String.format("Expected minimum validation violation for score %.3f", score));
}
}
@Test
@DisplayName("Mastery score above maximum should fail validation")
void testMasteryScoreAboveMaximum() {
float[] invalidScores = {1.1f, 2.0f, 1.001f};
for (float score : invalidScores) {
event.setMasteryScore(score);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
// Check for masteryScore violations specifically
boolean foundMaxViolation = violations.stream()
.filter(v -> "masteryScore".equals(v.getPropertyPath().toString()))
.anyMatch(v -> v.getMessage().contains("Mastery score must not exceed 1"));
assertTrue(foundMaxViolation,
String.format("Expected maximum validation violation for score %.3f", score));
}
}
@Test
@DisplayName("Null mastery score should pass validation")
void testNullMasteryScore() {
event.setMasteryScore(null);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
// Check that no masteryScore violations exist
long masteryScoreViolations = violations.stream()
.filter(v -> "masteryScore".equals(v.getPropertyPath().toString()))
.count();
assertEquals(0, masteryScoreViolations,
"Null mastery score should be valid as the field documentation states it might not be included");
}
@Test
@DisplayName("Boundary values should be properly validated")
void testBoundaryValues() {
// Test exact boundary values
event.setMasteryScore(0.0f);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
long masteryScoreViolations = violations.stream()
.filter(v -> "masteryScore".equals(v.getPropertyPath().toString()))
.count();
assertEquals(0, masteryScoreViolations, "Mastery score of 0.0 should be valid");
event.setMasteryScore(1.0f);
violations = validator.validate(event);
masteryScoreViolations = violations.stream()
.filter(v -> "masteryScore".equals(v.getPropertyPath().toString()))
.count();
assertEquals(0, masteryScoreViolations, "Mastery score of 1.0 should be valid");
}
@Test
@DisplayName("Validation messages should be correct")
void testValidationMessages() {
// Test minimum validation message
event.setMasteryScore(-0.1f);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
ConstraintViolation<LetterSoundAssessmentEvent> minViolation = violations.stream()
.filter(v -> "masteryScore".equals(v.getPropertyPath().toString()))
.filter(v -> v.getMessage().contains("Mastery score must be at least 0"))
.findFirst()
.orElse(null);
assertNotNull(minViolation, "Should have minimum validation violation");
assertEquals("Mastery score must be at least 0", minViolation.getMessage());
// Test maximum validation message
event.setMasteryScore(1.1f);
violations = validator.validate(event);
ConstraintViolation<LetterSoundAssessmentEvent> maxViolation = violations.stream()
.filter(v -> "masteryScore".equals(v.getPropertyPath().toString()))
.filter(v -> v.getMessage().contains("Mastery score must not exceed 1"))
.findFirst()
.orElse(null);
assertNotNull(maxViolation, "Should have maximum validation violation");
assertEquals("Mastery score must not exceed 1", maxViolation.getMessage());
}
} Key Features of This Test Suite:
This test suite will ensure the validation constraints work correctly and provide clear feedback when the validation fails. 🏁 Script executed:
Length of output: 519 🏁 Script executed:
Length of output: 1709 🏁 Script executed:
Length of output: 893 🏁 Script executed:
Length of output: 1590 Based on my analysis of the existing test patterns in your codebase, here's an updated and more appropriate unit test suggestion for the package ai.elimu.entity.analytics;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import java.util.Calendar;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
class LetterSoundAssessmentEventValidationTest {
private Validator validator;
private LetterSoundAssessmentEvent event;
@BeforeEach
void setUp() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
// Create a valid event with required fields from parent AssessmentEvent
event = new LetterSoundAssessmentEvent();
event.setTimestamp(Calendar.getInstance());
event.setAndroidId("1234567890abcdef");
event.setPackageName("ai.elimu.soundcards");
event.setLetterSoundLetters("sh");
event.setLetterSoundSounds("ʃ");
}
@Test
@DisplayName("Valid mastery score values should pass validation")
void testValidMasteryScores() {
// Test boundary values and typical valid values
float[] validScores = {0.0f, 0.5f, 1.0f, 0.25f, 0.75f, 0.999f, 0.001f};
for (float score : validScores) {
event.setMasteryScore(score);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
// Filter only masteryScore violations for this test
long masteryScoreViolations = violations.stream()
.filter(v -> "masteryScore".equals(v.getPropertyPath().toString()))
.count();
assertEquals(0, masteryScoreViolations,
String.format("Mastery score %.3f should be valid but validation failed", score));
}
}
@Test
@DisplayName("Mastery score below minimum should fail validation")
void testMasteryScoreBelowMinimum() {
float[] invalidScores = {-0.1f, -1.0f, -0.001f};
for (float score : invalidScores) {
event.setMasteryScore(score);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
// Check for masteryScore violations specifically
boolean foundMinViolation = violations.stream()
.filter(v -> "masteryScore".equals(v.getPropertyPath().toString()))
.anyMatch(v -> v.getMessage().contains("Mastery score must be at least 0"));
assertTrue(foundMinViolation,
String.format("Expected minimum validation violation for score %.3f", score));
}
}
@Test
@DisplayName("Mastery score above maximum should fail validation")
void testMasteryScoreAboveMaximum() {
float[] invalidScores = {1.1f, 2.0f, 1.001f};
for (float score : invalidScores) {
event.setMasteryScore(score);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
// Check for masteryScore violations specifically
boolean foundMaxViolation = violations.stream()
.filter(v -> "masteryScore".equals(v.getPropertyPath().toString()))
.anyMatch(v -> v.getMessage().contains("Mastery score must not exceed 1"));
assertTrue(foundMaxViolation,
String.format("Expected maximum validation violation for score %.3f", score));
}
}
@Test
@DisplayName("Null mastery score should pass validation")
void testNullMasteryScore() {
event.setMasteryScore(null);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
// Check that no masteryScore violations exist
long masteryScoreViolations = violations.stream()
.filter(v -> "masteryScore".equals(v.getPropertyPath().toString()))
.count();
assertEquals(0, masteryScoreViolations,
"Null mastery score should be valid as the field documentation states it might not be included");
}
@Test
@DisplayName("Validation messages should be correct")
void testValidationMessages() {
// Test minimum validation message
event.setMasteryScore(-0.1f);
Set<ConstraintViolation<LetterSoundAssessmentEvent>> violations = validator.validate(event);
ConstraintViolation<LetterSoundAssessmentEvent> minViolation = violations.stream()
.filter(v -> "masteryScore".equals(v.getPropertyPath().toString()))
.filter(v -> v.getMessage().contains("Mastery score must be at least 0"))
.findFirst()
.orElse(null);
assertNotNull(minViolation, "Should have minimum validation violation");
assertEquals("Mastery score must be at least 0", minViolation.getMessage());
// Test maximum validation message
event.setMasteryScore(1.1f);
violations = validator.validate(event);
ConstraintViolation<LetterSoundAssessmentEvent> maxViolation = violations.stream()
.filter(v -> "masteryScore".equals(v.getPropertyPath().toString()))
.filter(v -> v.getMessage().contains("Mastery score must not exceed 1"))
.findFirst()
.orElse(null);
assertNotNull(maxViolation, "Should have maximum validation violation");
assertEquals("Mastery score must not exceed 1", maxViolation.getMessage());
}
} Key Updates Based on Your Codebase:
Test File Location:Place this test at: Required Dependencies:Make sure your test dependencies include: <dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.el</artifactId>
<scope>test</scope>
</dependency> This test suite validates that your |
@juancaCeb Can you add a unit test for your code changes as well? |
Issue Number
LetterSoundAssessmentEvent
#2198Purpose
Technical Details
Used Jakarta Bean Validation Annotation.