Skip to content

Commit c27aa38

Browse files
committed
[#1984] Test queries on JSON fields
1 parent 7a34897 commit c27aa38

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.types;
7+
8+
import java.math.BigDecimal;
9+
import java.util.Collection;
10+
import java.util.List;
11+
import java.util.Objects;
12+
13+
import org.hibernate.reactive.BaseReactiveTest;
14+
import org.hibernate.reactive.annotations.EnabledFor;
15+
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.Test;
18+
19+
import io.vertx.core.json.JsonObject;
20+
import io.vertx.junit5.Timeout;
21+
import io.vertx.junit5.VertxTestContext;
22+
import jakarta.persistence.Column;
23+
import jakarta.persistence.Entity;
24+
import jakarta.persistence.Id;
25+
import jakarta.persistence.Table;
26+
27+
import static java.util.concurrent.TimeUnit.MINUTES;
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.COCKROACHDB;
30+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL;
31+
32+
/**
33+
* Test queries on JSON fields
34+
*/
35+
@Timeout(value = 10, timeUnit = MINUTES)
36+
@EnabledFor(POSTGRESQL)
37+
@EnabledFor(COCKROACHDB)
38+
public class JsonQueryTest extends BaseReactiveTest {
39+
40+
private final static BigDecimal PIE = BigDecimal.valueOf( 3.1416 );
41+
private final static BigDecimal TAO = BigDecimal.valueOf( 6.2832 );
42+
43+
final Book fakeHistory = new Book( 3, "Fake History", new JsonObject().put( "amount", PIE ) );
44+
final Book theBookOfM = new Book( 5, "The Book of M", new JsonObject().put( "amount", TAO ) );
45+
46+
@Override
47+
protected Collection<Class<?>> annotatedEntities() {
48+
return List.of( Book.class );
49+
}
50+
51+
@BeforeEach
52+
public void populateDb(VertxTestContext context) {
53+
test( context, getMutinySessionFactory().withTransaction( s -> s.persistAll( theBookOfM, fakeHistory ) ) );
54+
}
55+
56+
@Test
57+
public void nativeQuery(VertxTestContext context) {
58+
test( context, getMutinySessionFactory()
59+
.withTransaction( s -> s
60+
.createNativeQuery( "select id,title,price from BookWithJson b where (b.price ->> 'amount')::decimal between ?1 and ?2", Book.class )
61+
.setParameter( 1, BigDecimal.valueOf( 4.0 ) )
62+
.setParameter( 2, BigDecimal.valueOf( 100.0 ) )
63+
.getSingleResult()
64+
)
65+
.invoke( result -> assertThat( result ).isEqualTo( theBookOfM ) )
66+
);
67+
}
68+
69+
@Test
70+
public void hqlQuery(VertxTestContext context) {
71+
test( context, getMutinySessionFactory()
72+
.withTransaction( s -> s
73+
.createSelectionQuery( "from Book where sql('(price ->> ?)::decimal', 'amount') between ?1 and ?2", Book.class )
74+
.setParameter( 1, BigDecimal.valueOf( 4.0 ) )
75+
.setParameter( 2, BigDecimal.valueOf( 100.0 ) )
76+
.getSingleResult()
77+
)
78+
.invoke( result -> assertThat( result ).isEqualTo( theBookOfM ) )
79+
);
80+
}
81+
82+
@Entity(name = "Book")
83+
@Table(name = "BookWithJson")
84+
public static class Book {
85+
86+
@Id
87+
Integer id;
88+
89+
String title;
90+
91+
@Column(name = "price")
92+
JsonObject price;
93+
94+
public Book() {
95+
}
96+
97+
public Book(Integer id, String title, JsonObject price) {
98+
this.id = id;
99+
this.title = title;
100+
this.price = price;
101+
}
102+
103+
@Override
104+
public boolean equals(Object o) {
105+
if ( this == o ) {
106+
return true;
107+
}
108+
if ( o == null || getClass() != o.getClass() ) {
109+
return false;
110+
}
111+
Book book = (Book) o;
112+
return Objects.equals( id, book.id ) && Objects.equals(
113+
title,
114+
book.title
115+
) && Objects.equals( price, book.price );
116+
}
117+
118+
@Override
119+
public int hashCode() {
120+
return Objects.hash( id, title, price );
121+
}
122+
123+
@Override
124+
public String toString() {
125+
return id + ":" + title + ":" + price;
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)