forked from hibernate/hibernate-reactive
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[hibernate#1984] Test queries on JSON fields
- Loading branch information
Showing
1 changed file
with
128 additions
and
0 deletions.
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
hibernate-reactive-core/src/test/java/org/hibernate/reactive/types/JsonQueryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright: Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.reactive.types; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import org.hibernate.reactive.BaseReactiveTest; | ||
import org.hibernate.reactive.annotations.EnabledFor; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.junit5.Timeout; | ||
import io.vertx.junit5.VertxTestContext; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
|
||
import static java.util.concurrent.TimeUnit.MINUTES; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.COCKROACHDB; | ||
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL; | ||
|
||
/** | ||
* Test queries on JSON fields | ||
*/ | ||
@Timeout(value = 10, timeUnit = MINUTES) | ||
@EnabledFor(POSTGRESQL) | ||
@EnabledFor(COCKROACHDB) | ||
public class JsonQueryTest extends BaseReactiveTest { | ||
|
||
private final static BigDecimal PIE = BigDecimal.valueOf( 3.1416 ); | ||
private final static BigDecimal TAO = BigDecimal.valueOf( 6.2832 ); | ||
|
||
final Book fakeHistory = new Book( 3, "Fake History", new JsonObject().put( "amount", PIE ) ); | ||
final Book theBookOfM = new Book( 5, "The Book of M", new JsonObject().put( "amount", TAO ) ); | ||
|
||
@Override | ||
protected Collection<Class<?>> annotatedEntities() { | ||
return List.of( Book.class ); | ||
} | ||
|
||
@BeforeEach | ||
public void populateDb(VertxTestContext context) { | ||
test( context, getMutinySessionFactory().withTransaction( s -> s.persistAll( theBookOfM, fakeHistory ) ) ); | ||
} | ||
|
||
@Test | ||
public void nativeQuery(VertxTestContext context) { | ||
test( context, getMutinySessionFactory() | ||
.withTransaction( s -> s | ||
.createNativeQuery( "select id,title,price from BookWithJson b where (b.price ->> 'amount')::decimal between ?1 and ?2", Book.class ) | ||
.setParameter( 1, BigDecimal.valueOf( 4.0 ) ) | ||
.setParameter( 2, BigDecimal.valueOf( 100.0 ) ) | ||
.getSingleResult() | ||
) | ||
.invoke( result -> assertThat( result ).isEqualTo( theBookOfM ) ) | ||
); | ||
} | ||
|
||
@Test | ||
public void hqlQuery(VertxTestContext context) { | ||
test( context, getMutinySessionFactory() | ||
.withTransaction( s -> s | ||
.createSelectionQuery( "from Book where sql('(price ->> ?)::decimal', 'amount') between ?1 and ?2", Book.class ) | ||
.setParameter( 1, BigDecimal.valueOf( 4.0 ) ) | ||
.setParameter( 2, BigDecimal.valueOf( 100.0 ) ) | ||
.getSingleResult() | ||
) | ||
.invoke( result -> assertThat( result ).isEqualTo( theBookOfM ) ) | ||
); | ||
} | ||
|
||
@Entity(name = "Book") | ||
@Table(name = "BookWithJson") | ||
public static class Book { | ||
|
||
@Id | ||
Integer id; | ||
|
||
String title; | ||
|
||
@Column(name = "price") | ||
JsonObject price; | ||
|
||
public Book() { | ||
} | ||
|
||
public Book(Integer id, String title, JsonObject price) { | ||
this.id = id; | ||
this.title = title; | ||
this.price = price; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if ( this == o ) { | ||
return true; | ||
} | ||
if ( o == null || getClass() != o.getClass() ) { | ||
return false; | ||
} | ||
Book book = (Book) o; | ||
return Objects.equals( id, book.id ) && Objects.equals( | ||
title, | ||
book.title | ||
) && Objects.equals( price, book.price ); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash( id, title, price ); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return id + ":" + title + ":" + price; | ||
} | ||
} | ||
} |