Skip to content

Commit a8b6af6

Browse files
committed
test: add metamodel generation tests for @NumericIndexed annotation (#613)
Add comprehensive tests to verify that @NumericIndexed annotation correctly generates NumericField metamodel fields. Tests cover: - Basic numeric types (Double, Integer, Float) - Primitive numeric types - BigDecimal and BigInteger - Alias functionality
1 parent a9a9a4f commit a8b6af6

File tree

4 files changed

+238
-0
lines changed

4 files changed

+238
-0
lines changed

tests/src/test/java/com/redis/om/spring/metamodel/MetamodelGeneratorTest.java

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,120 @@ private List<String> getWarningStrings(Results results) {
443443
private List<String> getErrorStrings(Results results) {
444444
return results.find().errors().list().stream().map(w -> w.getMessage(Locale.US)).collect(Collectors.toList());
445445
}
446+
447+
@Test
448+
@Classpath(
449+
"data.metamodel.ValidDocumentNumericIndexedComplex"
450+
)
451+
void testValidDocumentNumericIndexedComplex(Results results) throws IOException {
452+
List<String> warnings = getWarningStrings(results);
453+
assertThat(warnings).isEmpty();
454+
455+
List<String> errors = getErrorStrings(results);
456+
assertThat(errors).isEmpty();
457+
458+
assertThat(results.generated).hasSize(1);
459+
JavaFileObject metamodel = results.generated.get(0);
460+
assertThat(metamodel.getName()).isEqualTo("/SOURCE_OUTPUT/valid/ValidDocumentNumericIndexedComplex$.java");
461+
462+
var fileContents = metamodel.getCharContent(true);
463+
464+
assertAll( //
465+
// Test the exact case from the GitHub issue
466+
() -> assertThat(fileContents).contains(
467+
"public static NumericField<ValidDocumentNumericIndexedComplex, Double> ISSUE_REPORTED_FIELD;"),
468+
() -> assertThat(fileContents).contains(
469+
"public static NumericField<ValidDocumentNumericIndexedComplex, Double> INDEXED_FIELD;"),
470+
471+
// Test all numeric types work with @NumericIndexed
472+
() -> assertThat(fileContents).contains(
473+
"public static NumericField<ValidDocumentNumericIndexedComplex, Integer> INTEGER_FIELD;"),
474+
() -> assertThat(fileContents).contains(
475+
"public static NumericField<ValidDocumentNumericIndexedComplex, Long> LONG_FIELD;"),
476+
() -> assertThat(fileContents).contains(
477+
"public static NumericField<ValidDocumentNumericIndexedComplex, Float> FLOAT_FIELD;"),
478+
() -> assertThat(fileContents).contains(
479+
"public static NumericField<ValidDocumentNumericIndexedComplex, BigDecimal> BIG_DECIMAL_FIELD;"),
480+
() -> assertThat(fileContents).contains(
481+
"public static NumericField<ValidDocumentNumericIndexedComplex, BigInteger> BIG_INTEGER_FIELD;"),
482+
483+
// Test primitive types
484+
() -> assertThat(fileContents).contains(
485+
"public static NumericField<ValidDocumentNumericIndexedComplex, Integer> PRIMITIVE_INT;"),
486+
() -> assertThat(fileContents).contains(
487+
"public static NumericField<ValidDocumentNumericIndexedComplex, Long> PRIMITIVE_LONG;"),
488+
() -> assertThat(fileContents).contains(
489+
"public static NumericField<ValidDocumentNumericIndexedComplex, Double> PRIMITIVE_DOUBLE;"),
490+
() -> assertThat(fileContents).contains(
491+
"public static NumericField<ValidDocumentNumericIndexedComplex, Float> PRIMITIVE_FLOAT;")
492+
);
493+
}
494+
495+
@Test
496+
@Classpath(
497+
"data.metamodel.ValidDocumentNumericIndexed"
498+
)
499+
void testValidDocumentNumericIndexed(Results results) throws IOException {
500+
List<String> warnings = getWarningStrings(results);
501+
assertThat(warnings).isEmpty();
502+
503+
List<String> errors = getErrorStrings(results);
504+
assertThat(errors).isEmpty();
505+
506+
assertThat(results.generated).hasSize(1);
507+
JavaFileObject metamodel = results.generated.get(0);
508+
assertThat(metamodel.getName()).isEqualTo("/SOURCE_OUTPUT/valid/ValidDocumentNumericIndexed$.java");
509+
510+
var fileContents = metamodel.getCharContent(true);
511+
512+
assertAll( //
513+
514+
// test package matches source package
515+
() -> assertThat(fileContents).contains("package valid;"), //
516+
517+
// test Fields generation
518+
() -> assertThat(fileContents).contains("public static Field id;"), //
519+
() -> assertThat(fileContents).contains("public static Field price;"), //
520+
() -> assertThat(fileContents).contains("public static Field quantity;"), //
521+
() -> assertThat(fileContents).contains("public static Field rating;"), //
522+
523+
// test fields initialization
524+
() -> assertThat(fileContents).contains(
525+
"id = com.redis.om.spring.util.ObjectUtils.getDeclaredFieldTransitively(ValidDocumentNumericIndexed.class, \"id\");"),
526+
//
527+
() -> assertThat(fileContents).contains(
528+
"price = com.redis.om.spring.util.ObjectUtils.getDeclaredFieldTransitively(ValidDocumentNumericIndexed.class, \"price\");"),
529+
//
530+
() -> assertThat(fileContents).contains(
531+
"quantity = com.redis.om.spring.util.ObjectUtils.getDeclaredFieldTransitively(ValidDocumentNumericIndexed.class, \"quantity\");"),
532+
//
533+
() -> assertThat(fileContents).contains(
534+
"rating = com.redis.om.spring.util.ObjectUtils.getDeclaredFieldTransitively(ValidDocumentNumericIndexed.class, \"rating\");"),
535+
//
536+
537+
// test Metamodel Field generation
538+
() -> assertThat(fileContents).contains("public static TextTagField<ValidDocumentNumericIndexed, String> ID;"), //
539+
() -> assertThat(fileContents).contains(
540+
"public static NumericField<ValidDocumentNumericIndexed, Double> PRICE;"), //
541+
() -> assertThat(fileContents).contains(
542+
"public static NumericField<ValidDocumentNumericIndexed, Integer> QUANTITY;"), //
543+
() -> assertThat(fileContents).contains(
544+
"public static NumericField<ValidDocumentNumericIndexed, Float> RATING;"), //
545+
546+
// test Metamodel Field initialization - verify aliases are used
547+
() -> assertThat(fileContents).contains(
548+
"ID = new TextTagField<ValidDocumentNumericIndexed, String>(new SearchFieldAccessor(\"id\", \"$.id\", id),true);"),
549+
//
550+
() -> assertThat(fileContents).contains(
551+
"PRICE = new NumericField<ValidDocumentNumericIndexed, Double>(new SearchFieldAccessor(\"price\", \"$.price\", price),true);"),
552+
//
553+
() -> assertThat(fileContents).contains(
554+
"QUANTITY = new NumericField<ValidDocumentNumericIndexed, Integer>(new SearchFieldAccessor(\"qty\", \"$.quantity\", quantity),true);"),
555+
//
556+
() -> assertThat(fileContents).contains(
557+
"RATING = new NumericField<ValidDocumentNumericIndexed, Float>(new SearchFieldAccessor(\"rating\", \"$.rating\", rating),true);"),
558+
//
559+
() -> assertThat(fileContents).contains(
560+
"_KEY = new MetamodelField<ValidDocumentNumericIndexed, String>(\"__key\", String.class, true);"));
561+
}
446562
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package valid;
2+
3+
import com.redis.om.spring.annotations.Document;
4+
import com.redis.om.spring.annotations.NumericIndexed;
5+
import com.redis.om.spring.annotations.SchemaFieldType;
6+
import lombok.*;
7+
import org.springframework.data.annotation.Id;
8+
9+
@Data
10+
@RequiredArgsConstructor(staticName = "of")
11+
@AllArgsConstructor(access = AccessLevel.PROTECTED)
12+
@NoArgsConstructor
13+
@Document
14+
public class InvalidDocumentNumericIndexedWithSchemaFieldType {
15+
@Id
16+
private String id;
17+
18+
// This is INVALID - @NumericIndexed does not have a schemaFieldType parameter
19+
// This will cause a compilation error
20+
/*
21+
@NumericIndexed(
22+
schemaFieldType = SchemaFieldType.NUMERIC,
23+
sortable = true
24+
)
25+
private Double invalidField;
26+
*/
27+
28+
// This is the correct way to use @NumericIndexed
29+
@NumericIndexed(sortable = true)
30+
private Double validField;
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package valid;
2+
3+
import com.redis.om.spring.annotations.Document;
4+
import com.redis.om.spring.annotations.NumericIndexed;
5+
import lombok.*;
6+
import org.springframework.data.annotation.Id;
7+
8+
@Data
9+
@RequiredArgsConstructor(staticName = "of")
10+
@AllArgsConstructor(access = AccessLevel.PROTECTED)
11+
@NoArgsConstructor
12+
@Document
13+
public class ValidDocumentNumericIndexed {
14+
@Id
15+
private String id;
16+
17+
@NonNull
18+
@NumericIndexed(sortable = true)
19+
private Double price;
20+
21+
@NonNull
22+
@NumericIndexed(alias = "qty")
23+
private Integer quantity;
24+
25+
@NonNull
26+
@NumericIndexed(sortable = true, alias = "rating")
27+
private Float rating;
28+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package valid;
2+
3+
import com.redis.om.spring.annotations.Document;
4+
import com.redis.om.spring.annotations.Indexed;
5+
import com.redis.om.spring.annotations.NumericIndexed;
6+
import com.redis.om.spring.annotations.SchemaFieldType;
7+
import lombok.*;
8+
import org.springframework.data.annotation.Id;
9+
10+
import java.math.BigDecimal;
11+
import java.math.BigInteger;
12+
13+
@Data
14+
@RequiredArgsConstructor(staticName = "of")
15+
@AllArgsConstructor(access = AccessLevel.PROTECTED)
16+
@NoArgsConstructor
17+
@Document
18+
public class ValidDocumentNumericIndexedComplex {
19+
@Id
20+
private String id;
21+
22+
// Test case from the GitHub issue - but corrected (schemaFieldType is not valid for @NumericIndexed)
23+
@NumericIndexed(
24+
sortable = true
25+
)
26+
private Double issueReportedField;
27+
28+
// Compare with @Indexed annotation
29+
@Indexed(
30+
schemaFieldType = SchemaFieldType.NUMERIC,
31+
sortable = true
32+
)
33+
private Double indexedField;
34+
35+
// Test various numeric types with @NumericIndexed
36+
@NumericIndexed
37+
private Integer integerField;
38+
39+
@NumericIndexed
40+
private Long longField;
41+
42+
@NumericIndexed
43+
private Float floatField;
44+
45+
@NumericIndexed
46+
private BigDecimal bigDecimalField;
47+
48+
@NumericIndexed
49+
private BigInteger bigIntegerField;
50+
51+
// Test primitive types
52+
@NumericIndexed
53+
private int primitiveInt;
54+
55+
@NumericIndexed
56+
private long primitiveLong;
57+
58+
@NumericIndexed
59+
private double primitiveDouble;
60+
61+
@NumericIndexed
62+
private float primitiveFloat;
63+
}

0 commit comments

Comments
 (0)