Skip to content

Commit

Permalink
HSEARCH-3958 Use as much shared code as possible to test the nested p…
Browse files Browse the repository at this point in the history
…redicate
  • Loading branch information
yrodiere committed Jul 1, 2020
1 parent 88b3067 commit 40ec0de
Showing 1 changed file with 25 additions and 46 deletions.
Expand Up @@ -7,26 +7,25 @@
package org.hibernate.search.integrationtest.backend.tck.search.predicate;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThat;
import static org.hibernate.search.util.impl.integrationtest.common.assertion.SearchResultAssert.assertThatQuery;

import org.hibernate.search.engine.backend.document.DocumentElement;
import org.hibernate.search.engine.backend.document.IndexFieldReference;
import org.hibernate.search.engine.backend.document.IndexObjectFieldReference;
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaElement;
import org.hibernate.search.engine.backend.document.model.dsl.IndexSchemaObjectField;
import org.hibernate.search.engine.backend.types.ObjectStructure;
import org.hibernate.search.engine.search.predicate.SearchPredicate;
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper;
import org.hibernate.search.util.common.SearchException;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.SimpleMappedIndex;
import org.hibernate.search.util.impl.integrationtest.mapper.stub.StubMappingScope;
import org.hibernate.search.integrationtest.backend.tck.testsupport.util.rule.SearchSetupHelper;
import org.hibernate.search.engine.backend.common.DocumentReference;
import org.hibernate.search.engine.search.predicate.SearchPredicate;
import org.hibernate.search.engine.search.query.SearchQuery;
import org.junit.Before;
import org.junit.Rule;

import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;

public class NestedSearchPredicateIT {
public class NestedPredicateSpecificsIT {

private static final String DOCUMENT_1 = "nestedQueryShouldMatchId";
private static final String DOCUMENT_2 = "nonNestedQueryShouldMatchId";
Expand All @@ -43,23 +42,21 @@ public class NestedSearchPredicateIT {
private static final String NON_MATCHING_SECOND_LEVEL_CONDITION2_FIELD1 = "secondNonMatchingWord";
private static final String NON_MATCHING_SECOND_LEVEL_CONDITION2_FIELD2 = "secondNonMatchingWord";

@Rule
public final SearchSetupHelper setupHelper = new SearchSetupHelper();
@ClassRule
public static final SearchSetupHelper setupHelper = new SearchSetupHelper();

private final SimpleMappedIndex<IndexBinding> index = SimpleMappedIndex.of( IndexBinding::new );
private static final SimpleMappedIndex<IndexBinding> index = SimpleMappedIndex.of( IndexBinding::new );

@Before
public void setup() {
@BeforeClass
public static void setup() {
setupHelper.start().withIndex( index ).setup();

initData();
}

@Test
public void search_nestedOnTwoLevels() {
StubMappingScope scope = index.createScope();

SearchQuery<DocumentReference> query = scope.query()
assertThatQuery( index.query()
.where( f -> f.nested().objectField( "nestedObject" )
.nest( f.bool()
// This is referred to as "condition 1" in the data initialization method
Expand Down Expand Up @@ -89,18 +86,14 @@ public void search_nestedOnTwoLevels() {
)
)
)
)
.toQuery();
assertThat( query )
) )
.hasDocRefHitsAnyOrder( index.typeName(), DOCUMENT_1 )
.hasTotalHitCount( 1 );
}

@Test
public void search_nestedOnTwoLevels_onlySecondLevel() {
StubMappingScope scope = index.createScope();

SearchQuery<DocumentReference> query = scope.query()
assertThatQuery( index.query()
.where( f -> f.bool()
// This is referred to as "condition 1" in the data initialization method
.must( f.nested().objectField( "nestedObject.nestedObject" )
Expand Down Expand Up @@ -128,18 +121,14 @@ public void search_nestedOnTwoLevels_onlySecondLevel() {
)
)
)
)
.toQuery();
assertThat( query )
) )
.hasDocRefHitsAnyOrder( index.typeName(), DOCUMENT_1, DOCUMENT_2 )
.hasTotalHitCount( 2 );
}

@Test
public void search_nestedOnTwoLevels_conditionOnFirstLevel() {
StubMappingScope scope = index.createScope();

SearchQuery<DocumentReference> query = scope.query()
assertThatQuery( index.query()
.where( f -> f.nested().objectField( "nestedObject" )
.nest( f.bool()
.must( f.match()
Expand All @@ -160,9 +149,7 @@ public void search_nestedOnTwoLevels_conditionOnFirstLevel() {
)
)
)
)
.toQuery();
assertThat( query )
) )
.hasDocRefHitsAnyOrder( index.typeName(), DOCUMENT_2 )
.hasTotalHitCount( 1 );
}
Expand Down Expand Up @@ -195,29 +182,25 @@ public void search_nestedOnTwoLevels_separatePredicates() {
)
.toPredicate();

SearchQuery<DocumentReference> query = scope.query()
assertThatQuery( scope.query()
.where( f -> f.nested().objectField( "nestedObject" )
.nest( f.bool()
// This is referred to as "condition 1" in the data initialization method
.must( predicate1 )
// This is referred to as "condition 2" in the data initialization method
.must( predicate2 )
)
)
.toQuery();
assertThat( query )
) )
.hasDocRefHitsAnyOrder( index.typeName(), DOCUMENT_1 )
.hasTotalHitCount( 1 );
}

@Test
public void invalidNestedPath_parent() {
StubMappingScope scope = index.createScope();

String objectFieldPath = "nestedObject";
String fieldInParentPath = "string";

assertThatThrownBy( () -> scope.query()
assertThatThrownBy( () -> index.query()
.where( f -> f.nested().objectField( objectFieldPath )
.nest( f.bool()
.must( f.match()
Expand All @@ -229,8 +212,7 @@ public void invalidNestedPath_parent() {
.matching( "irrelevant_because_this_will_fail" )
)
)
)
)
) )
.isInstanceOf( SearchException.class )
.hasMessageContainingAll(
"Predicate targets unexpected fields [" + fieldInParentPath + "]",
Expand All @@ -241,12 +223,10 @@ public void invalidNestedPath_parent() {

@Test
public void invalidNestedPath_sibling() {
StubMappingScope scope = index.createScope();

String objectFieldPath = "nestedObject";
String fieldInSiblingPath = "nestedObject2.string";

assertThatThrownBy( () -> scope.query()
assertThatThrownBy( () -> index.query()
.where( f -> f.nested().objectField( objectFieldPath )
.nest( f.bool()
.must( f.match()
Expand All @@ -258,8 +238,7 @@ public void invalidNestedPath_sibling() {
.matching( "irrelevant_because_this_will_fail" )
)
)
)
)
) )
.isInstanceOf( SearchException.class )
.hasMessageContainingAll(
"Predicate targets unexpected fields [" + fieldInSiblingPath + "]",
Expand All @@ -268,7 +247,7 @@ public void invalidNestedPath_sibling() {
);
}

private void initData() {
private static void initData() {
index.bulkIndexer()
.add( DOCUMENT_1, document -> {
ObjectMapping level1;
Expand Down

0 comments on commit 40ec0de

Please sign in to comment.