HHH-14738 Add test cases for functions with parameter arguments

Fix rebase errors
This commit is contained in:
Karel Maesen 2021-08-26 23:45:36 +02:00
parent 60d3914769
commit f72d6045b1
5 changed files with 144 additions and 61 deletions

View File

@ -21,11 +21,8 @@ dependencies {
testCompile(project(':hibernate-testing'))
<<<<<<< HEAD
testCompile( project( path: ':hibernate-core', configuration: 'tests' ) )
=======
testCompile( project( path: ':hibernate-core', configuration: 'tests' ) )
>>>>>>> f70b7d7c50 (HHH-14738 Clean up obsolete test support code)
testCompile(libraries.validation)
testCompile(libraries.jandex)
testCompile(libraries.classmate)
@ -47,10 +44,9 @@ sourceSets.test.resources {
setSrcDirs(['src/test/java', 'src/test/resources'])
}
tasks.test {
enabled = project.db == 'pgsql'
enabled = ['pgsql', 'cockroachdb'].contains( project.db )
}
//tasks.test.include '**/*'
tasks.test.include '**/*'

View File

@ -17,7 +17,7 @@ import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
* Registers all available spatial functions for a <code>Dialect</code>
* <p>
* Created by Karel Maesen, Geovise BVBA on 29/10/16.
* @Deprecated Use
* @Deprecated Use KeyedSqmFunctionDescriptors
*/
public abstract class SpatialFunctionsRegistry implements Iterable<Map.Entry<String, SqmFunctionDescriptor>>, Serializable {
protected final Map<String, SqmFunctionDescriptor> functionMap = new HashMap<String, SqmFunctionDescriptor>();

View File

@ -16,24 +16,14 @@ package org.hibernate.spatial.integration.functions;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Stream;
import org.hibernate.spatial.integration.SpatialTestDataProvider;
import org.hibernate.spatial.testing.IsSupportedBySpatial;
import org.hibernate.spatial.testing.SpatialTestBase;
import org.hibernate.spatial.testing.datareader.TestSupport;
import org.hibernate.spatial.testing.domain.GeomEntity;
import org.hibernate.spatial.testing.domain.JtsGeomEntity;
import org.hibernate.spatial.testing.domain.SpatialDomainModel;
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.testing.orm.junit.SessionFactoryScopeAware;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.function.Executable;
@ -53,56 +43,28 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* </p>
*/
@SuppressWarnings("ALL")
@DomainModel(modelDescriptorClasses = SpatialDomainModel.class)
@RequiresDialectFeature( feature = IsSupportedBySpatial.class)
@RequiresDialectFeature(feature = IsSupportedBySpatial.class)
@SessionFactory
public class CommonFunctionTests extends SpatialTestDataProvider
implements SessionFactoryScopeAware {
public class CommonFunctionTests extends SpatialTestBase {
public final static TestSupport.TestDataPurpose PURPOSE = TestSupport.TestDataPurpose.SpatialFunctionsData;
private SessionFactoryScope scope;
private Set<String> supportedFunctions;
List received;
List expected;
@Override
public void injectSessionFactoryScope(SessionFactoryScope scope) {
this.scope = scope;
//scope is set to null during test cleanup
if ( scope != null ) {
this.supportedFunctions = scope.getSessionFactory()
.getQueryEngine()
.getSqmFunctionRegistry()
.getFunctions()
.keySet();
}
public TestSupport.TestDataPurpose purpose() {
return PURPOSE;
}
@BeforeEach
public void beforeEach() {
scope.inTransaction( session -> super.entities(
JtsGeomEntity.class,
TestSupport.TestDataPurpose.SpatialFunctionsData
)
.forEach( session::save ) );
scope.inTransaction( session -> super.entities(
GeomEntity.class,
TestSupport.TestDataPurpose.SpatialFunctionsData
).forEach( session::save ) );
}
@AfterEach
public void cleanup() {
scope.inTransaction( session -> session.createQuery( "delete from GeomEntity" ).executeUpdate() );
scope.inTransaction( session -> session.createQuery( "delete from JtsGeomEntity" ).executeUpdate() );
}
@TestFactory
public Stream<DynamicTest> testFunction() {
return
TestTemplates.all( templates, hqlOverrides )
.filter( this::isSupported )
.filter( f -> isSupported( f.function ) )
.flatMap( t -> Stream.of(
t.build( Model.JTSMODEL, codec ),
t.build( Model.GLMODEL, codec )
@ -111,9 +73,6 @@ public class CommonFunctionTests extends SpatialTestDataProvider
}
private boolean isSupported(FunctionTestTemplate.Builder builder) {
return supportedFunctions.contains( builder.function.name() );
}
protected Stream<DynamicTest> buildTests(FunctionTestTemplate template) {
return Stream.of(

View File

@ -0,0 +1,109 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.spatial.integration.functions;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import org.hibernate.spatial.CommonSpatialFunction;
import org.hibernate.spatial.testing.IsSupportedBySpatial;
import org.hibernate.spatial.testing.SpatialTestBase;
import org.hibernate.spatial.testing.datareader.TestSupport;
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.junit.jupiter.api.function.Executable;
import org.geolatte.geom.G2D;
import org.geolatte.geom.Geometry;
import static org.geolatte.geom.builder.DSL.g;
import static org.geolatte.geom.builder.DSL.polygon;
import static org.geolatte.geom.builder.DSL.ring;
import static org.geolatte.geom.crs.CoordinateReferenceSystems.WGS84;
@RequiresDialectFeature(feature = IsSupportedBySpatial.class)
@SessionFactory
public class TestGeometryConstructionWithParameter extends SpatialTestBase {
final private Map<CommonSpatialFunction, String> templates = new HashMap<>();
final private Geometry<G2D> geometry = polygon(
WGS84,
ring(
g( 0, 0 ),
g( 10, 0 ),
g( 10, 10 ),
g( 0, 10 ),
g( 0, 0 )
)
);
TestGeometryConstructionWithParameter() {
templates.put(
CommonSpatialFunction.ST_ENVELOPE,
"select g from GeomEntity g where st_intersects(g.geom, st_envelope(:poly) ) = true"
);
templates.put(
CommonSpatialFunction.ST_BOUNDARY,
"select g from GeomEntity g where st_intersects(g.geom, st_boundary(:poly) ) = true"
);
templates.put(
CommonSpatialFunction.ST_BUFFER,
"select g from GeomEntity g where st_intersects(g.geom, st_buffer(:poly, 1.0) ) = true"
);
templates.put(
CommonSpatialFunction.ST_CONVEXHULL,
//TODO -- this is a degenerate case of convexhull
"select g from GeomEntity g where st_intersects(g.geom, st_convexhull(:poly) ) = true"
);
}
@Override
public TestSupport.TestDataPurpose purpose() {
return TestSupport.TestDataPurpose.SpatialFunctionsData;
}
@TestFactory
public Stream<DynamicTest> testFunctions() {
return Arrays.stream( CommonSpatialFunction.values() )
.filter( f ->
f.getType() == CommonSpatialFunction.Type.CONSTRUCTION &&
isSupported( f ) &&
templateAvailable( f ) )
.map( this::buildTestFunction );
}
private DynamicTest buildTestFunction(CommonSpatialFunction func) {
return DynamicTest.dynamicTest( buildName( func ), buildExec( func ) );
}
private Executable buildExec(final CommonSpatialFunction func) {
return () -> {
scope.inSession( session -> {
String hql = templates.get( func );
session.createQuery( hql )
.setParameter( "poly", geometry )
.getResultList();
//we just check that this parses for now.
} );
};
}
private String buildName(CommonSpatialFunction func) {
return func.getKey().getName();
}
private boolean templateAvailable(CommonSpatialFunction f) {
return templates.keySet().contains( f );
}
}

View File

@ -7,6 +7,9 @@
package org.hibernate.spatial.testing;
import java.util.Set;
import org.hibernate.spatial.CommonSpatialFunction;
import org.hibernate.spatial.integration.SpatialTestDataProvider;
import org.hibernate.spatial.testing.datareader.TestSupport;
import org.hibernate.spatial.testing.domain.GeomEntity;
@ -20,26 +23,38 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@DomainModel(modelDescriptorClasses = SpatialDomainModel.class)
abstract public class SpatialTestFactoryBase
abstract public class SpatialTestBase
extends SpatialTestDataProvider implements SessionFactoryScopeAware {
protected SessionFactoryScope scope;
protected Set<String> supportedFunctions;
public abstract TestSupport.TestDataPurpose purpose();
@Override
public void injectSessionFactoryScope(SessionFactoryScope scope) {
this.scope = scope;
//scope is set to null during test cleanup
if ( scope != null ) {
this.supportedFunctions = scope.getSessionFactory()
.getQueryEngine()
.getSqmFunctionRegistry()
.getFunctions()
.keySet();
}
}
@BeforeEach
public void beforeEach() {
scope.inTransaction( session -> super.entities(
JtsGeomEntity.class,
TestSupport.TestDataPurpose.SpatialFunctionsData
purpose()
)
.forEach( session::save ) );
scope.inTransaction( session -> super.entities(
GeomEntity.class,
TestSupport.TestDataPurpose.SpatialFunctionsData
purpose()
).forEach( session::save ) );
}
@ -49,4 +64,8 @@ abstract public class SpatialTestFactoryBase
scope.inTransaction( session -> session.createQuery( "delete from JtsGeomEntity" ).executeUpdate() );
}
public boolean isSupported(CommonSpatialFunction function) {
return supportedFunctions.contains( function.name() );
}
}