Skip to content

Commit

Permalink
New test case
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideD committed Sep 20, 2024
1 parent c45355d commit 4cd00b7
Showing 1 changed file with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* 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 java.util.concurrent.CompletionStage;

import org.hibernate.reactive.BaseReactiveTest;
import org.hibernate.reactive.util.impl.CompletionStages;

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 org.assertj.core.api.Assertions;

import static java.util.concurrent.TimeUnit.MINUTES;

/**
* Test types that we expect to work only on selected DBs.
*/
@Timeout(value = 10, timeUnit = MINUTES)

public class JsonWithBigDecimalTypeTest extends BaseReactiveTest {

private final static BigDecimal TAO = BigDecimal.valueOf( 6.2832 );

@Override
protected Collection<Class<?>> annotatedEntities() {
return List.of( Book.class );
}


@Override
protected CompletionStage<Void> cleanDb() {
return CompletionStages.voidFuture();
}

@Test
public void testJsonType(VertxTestContext context) {
JsonObject jsonPrice = new JsonObject().put( "amount", TAO );
final Book theBookOfM = new Book( 5, "The Book of M", jsonPrice );
test( context, getMutinySessionFactory()
.withTransaction( session -> session.persist( theBookOfM ) )
.chain( () -> getMutinySessionFactory().withTransaction( s -> {
return s.createNativeQuery( "select id,title,price from BookWithJson b where (b.price ->> 'amount')::decimal between ?1 and ?2" )
.setParameter( 1, BigDecimal.valueOf( 4.0 ) )
.setParameter( 2, BigDecimal.valueOf( 100.0 ) )
.getSingleResult();
} ) )
.invoke( result -> Assertions.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( title, book.title );
}

@Override
public int hashCode() {
return Objects.hashCode( title );
}
}
}

0 comments on commit 4cd00b7

Please sign in to comment.