Skip to content

Commit

Permalink
#71 - Added tests case
Browse files Browse the repository at this point in the history
  • Loading branch information
starnowski committed Dec 20, 2023
1 parent 9b06d25 commit cd5be12
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.tree.expression.SqmExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
Expand All @@ -40,7 +38,7 @@ public List<Tweet> findBySinglePlainQueryInDescriptionForConfiguration(String ph
CriteriaQuery<Tweet> query = cb.createQuery(Tweet.class);
Root<Tweet> root = query.from(Tweet.class);
query.select(root);
query.where(new TextOperatorFunction((NodeBuilder) cb, new TSVectorFunction(root.get("shortContent"), configuration, (NodeBuilder) cb) ,new PlainToTSQueryFunction((NodeBuilder) cb, configuration, phrase), hibernateContext));
query.where(new TextOperatorFunction((NodeBuilder) cb, new TSVectorFunction(root.get("shortContent"), configuration, (NodeBuilder) cb), new PlainToTSQueryFunction((NodeBuilder) cb, configuration, phrase), hibernateContext));
return entityManager.createQuery(query).getResultList();
}

Expand All @@ -52,4 +50,13 @@ public List<Tweet> findBySinglePhraseInDescriptionForDefaultConfiguration(String
query.where(new TextOperatorFunction((NodeBuilder) cb, new TSVectorFunction(root.get("shortContent"), (NodeBuilder) cb), new PhraseToTSQueryFunction((NodeBuilder) cb, null, phrase), hibernateContext));
return entityManager.createQuery(query).getResultList();
}

public List<Tweet> findBySinglePhraseInDescriptionForConfiguration(String phrase, String configuration) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Tweet> query = cb.createQuery(Tweet.class);
Root<Tweet> root = query.from(Tweet.class);
query.select(root);
query.where(new TextOperatorFunction((NodeBuilder) cb, new TSVectorFunction(root.get("shortContent"), configuration, (NodeBuilder) cb), new PhraseToTSQueryFunction((NodeBuilder) cb, configuration, phrase), hibernateContext));
return entityManager.createQuery(query).getResultList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,29 @@ public void shouldFindCorrectTweetsBySinglePhraseInDescriptionForDefaultConfigur
assertThat(results).hasSize(expectedIds.size());
assertThat(results.stream().map(Tweet::getId).collect(toSet())).containsAll(expectedIds);
}

private static Stream<Arguments> provideShouldFindCorrectTweetsBySinglePhraseInDescription() {
return Stream.of(
Arguments.of("rat and cats", asList(3L)),
Arguments.of("rat and cat", asList(3L)),
Arguments.of("rat cat", new ArrayList<>()),
Arguments.of("cat Rats", new ArrayList<>())
);
}

@Sql(value = {CLEAR_DATABASE_SCRIPT_PATH, TWEETS_SCRIPT_PATH},
config = @SqlConfig(transactionMode = ISOLATED),
executionPhase = BEFORE_TEST_METHOD)
@DisplayName("should return all ids {0} when searching by query '{1}' for english configuration' for phraseto_tsquery function")
@ParameterizedTest
@MethodSource("provideShouldFindCorrectTweetsBySinglePhraseInDescription")
public void shouldFindCorrectTweetsBySinglePhraseInDescription(String phrase, List<Long> expectedIds) {

// when
List<Tweet> results = tested.findBySinglePhraseInDescriptionForConfiguration(phrase, ENGLISH_CONFIGURATION);

// then
assertThat(results).hasSize(expectedIds.size());
assertThat(results.stream().map(Tweet::getId).collect(toSet())).containsAll(expectedIds);
}
}

0 comments on commit cd5be12

Please sign in to comment.