Skip to content

Commit 0fba728

Browse files
authored
Add support for IN/NIN, BETWEEN/NOT_BETWEEN predicates for floating point numbers (#305)
1 parent 499798d commit 0fba728

File tree

4 files changed

+135
-3
lines changed

4 files changed

+135
-3
lines changed

graphql-jpa-query-schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/JpaPredicateBuilder.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,10 @@ protected Predicate mayBeArrayValuePredicate(Path<?> path, PredicateFilter filte
313313
}
314314

315315
protected Predicate getFloatingPointPredicate(Path<? extends Number> root, PredicateFilter filter) {
316-
if (filter.getValue() != null && filter.getValue() instanceof Number) {
316+
317+
Predicate arrayValuePredicate = mayBeArrayValuePredicate(root, filter);
318+
319+
if (arrayValuePredicate == null && filter.getValue() != null && filter.getValue() instanceof Number) {
317320
if (filter.getCriterias().contains(PredicateFilter.Criteria.LT)) {
318321
return cb.lt(root, (Number) filter.getValue());
319322
}
@@ -332,7 +335,7 @@ protected Predicate getFloatingPointPredicate(Path<? extends Number> root, Predi
332335
// LE or default
333336
return cb.le(root, (Number) filter.getValue());
334337
}
335-
return null;
338+
return arrayValuePredicate;
336339
}
337340

338341
protected Predicate getDatePredicate(Path<? extends Date> root, PredicateFilter filter) {

graphql-jpa-query-schema/src/test/java/com/introproventures/graphql/jpa/query/schema/GraphQLExecutorTests.java

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2547,6 +2547,110 @@ public void queryWithOperationName() {
25472547

25482548
// then
25492549
assertThat(result.toString()).isEqualTo(expected);
2550-
}
2550+
}
2551+
2552+
@Test
2553+
public void queryForFloatingThingViaWhereIn() {
2554+
//given:
2555+
String query = "{"
2556+
+ " FloatingThings("
2557+
+ " where: {"
2558+
+ " AND: {"
2559+
+ " floatValue: {IN: [4.55, 5]}"
2560+
+ " doubleValue: {IN: [4.55, 5]}"
2561+
+ " bigDecimalValue: {IN: [4.55, 5]}"
2562+
+ " }"
2563+
+ " }"
2564+
+ " ) {"
2565+
+ " select {"
2566+
+ " id"
2567+
+ " }}"
2568+
+ "}";
2569+
String expected = "{FloatingThings={select=[{id=1}]}}";
2570+
2571+
//when:
2572+
Object result = executor.execute(query).getData();
2573+
2574+
//then:
2575+
assertThat(result.toString()).isEqualTo(expected);
2576+
}
2577+
2578+
@Test
2579+
public void queryForFloatingThingViaWhereNin() {
2580+
//given:
2581+
String query = "{"
2582+
+ " FloatingThings("
2583+
+ " where: {"
2584+
+ " AND: {"
2585+
+ " floatValue: {NIN: [4.55, 5]}"
2586+
+ " doubleValue: {NIN: [4.55, 5]}"
2587+
+ " bigDecimalValue: {NIN: [4.55, 5]}"
2588+
+ " }"
2589+
+ " }"
2590+
+ " ) {"
2591+
+ " select {"
2592+
+ " id"
2593+
+ " }}"
2594+
+ "}";
2595+
String expected = "{FloatingThings={select=[{id=2}]}}";
2596+
2597+
//when:
2598+
Object result = executor.execute(query).getData();
2599+
2600+
//then:
2601+
assertThat(result.toString()).isEqualTo(expected);
2602+
}
2603+
2604+
@Test
2605+
public void queryForFloatingThingViaWhereBetween() {
2606+
//given:
2607+
String query = "{"
2608+
+ " FloatingThings("
2609+
+ " where: {"
2610+
+ " AND: {"
2611+
+ " floatValue: {BETWEEN: [-1, 0]}"
2612+
+ " doubleValue: {BETWEEN: [-1, 0]}"
2613+
+ " bigDecimalValue: {BETWEEN: [-1, 0]}"
2614+
+ " }"
2615+
+ " }"
2616+
+ " ) {"
2617+
+ " select {"
2618+
+ " id"
2619+
+ " }}"
2620+
+ "}";
2621+
String expected = "{FloatingThings={select=[{id=2}]}}";
2622+
2623+
//when:
2624+
Object result = executor.execute(query).getData();
2625+
2626+
//then:
2627+
assertThat(result.toString()).isEqualTo(expected);
2628+
}
2629+
2630+
@Test
2631+
public void queryForFloatingThingViaWhereNotBetween() {
2632+
//given:
2633+
String query = "{"
2634+
+ " FloatingThings("
2635+
+ " where: {"
2636+
+ " AND: {"
2637+
+ " floatValue: {NOT_BETWEEN: [-1, 0]}"
2638+
+ " doubleValue: {NOT_BETWEEN: [-1, 0]}"
2639+
+ " bigDecimalValue: {NOT_BETWEEN: [-1, 0]}"
2640+
+ " }"
2641+
+ " }"
2642+
+ " ) {"
2643+
+ " select {"
2644+
+ " id"
2645+
+ " }}"
2646+
+ "}";
2647+
String expected = "{FloatingThings={select=[{id=1}]}}";
2648+
2649+
//when:
2650+
Object result = executor.execute(query).getData();
2651+
2652+
//then:
2653+
assertThat(result.toString()).isEqualTo(expected);
2654+
}
25512655

25522656
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.introproventures.graphql.jpa.query.schema.model.floating;
2+
3+
import java.math.BigDecimal;
4+
import javax.persistence.Entity;
5+
import javax.persistence.Id;
6+
import lombok.Data;
7+
8+
@Data
9+
@Entity
10+
public class FloatingThing {
11+
12+
@Id
13+
private Long id;
14+
15+
float floatValue;
16+
17+
double doubleValue;
18+
19+
BigDecimal bigDecimalValue;
20+
}

graphql-jpa-query-schema/src/test/resources/data.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,8 @@ insert into Boat (id, country, identification) values
159159
insert into calculated_entity (id, title, info) values
160160
(1, 'title 1', 'inf 1'),
161161
(2, 'title 2', 'inf 2');
162+
163+
-- FloatingThing
164+
insert into floating_thing (id, float_value, double_value, big_decimal_value) values
165+
(1, 4.55, 4.55, 4.55),
166+
(2, -0.44, -0.44, -0.44)

0 commit comments

Comments
 (0)