From 7543f7f4856ca463ccda8a52d79973ea07073147 Mon Sep 17 00:00:00 2001 From: Szymon Tarnowski <33316705+starnowski@users.noreply.github.com> Date: Sun, 31 Dec 2023 12:18:07 +0100 Subject: [PATCH] #84 - Added tests --- .../text/hibernate6/Application.java | 2 ++ .../text/hibernate6/dao/TextIndexTest.java | 31 +++++++++++++++---- .../hibernate6/dao/TweetWithLocalDao.java | 7 +++++ .../text/hibernate6/dao/clean-database.sql | 3 +- .../hibernate6/dao/tweets-with-locale.sql | 2 +- 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/text/hibernate6-text-tests/src/test/java/com/github/starnowski/posjsonhelper/text/hibernate6/Application.java b/text/hibernate6-text-tests/src/test/java/com/github/starnowski/posjsonhelper/text/hibernate6/Application.java index 3f94a5f4..0706fd5e 100644 --- a/text/hibernate6-text-tests/src/test/java/com/github/starnowski/posjsonhelper/text/hibernate6/Application.java +++ b/text/hibernate6-text-tests/src/test/java/com/github/starnowski/posjsonhelper/text/hibernate6/Application.java @@ -10,8 +10,10 @@ public class Application { private final Logger log = LoggerFactory.getLogger(this.getClass()); public static final String CLEAR_DATABASE_SCRIPT_PATH = "clean-database.sql"; public static final String TWEETS_SCRIPT_PATH = "tweets.sql"; + public static final String TWEETS_WITH_LOCALE_SCRIPT_PATH = "tweets-with-locale.sql"; public static final String TEXT_INDEX_SCRIPT_PATH = "text-index.sql"; public static final String ENGLISH_CONFIGURATION = "english"; + public static final String POLISH_CONFIGURATION = "pl_ispell"; public static void main(String[] args) { SpringApplication.run(Application.class, args); diff --git a/text/hibernate6-text-tests/src/test/java/com/github/starnowski/posjsonhelper/text/hibernate6/dao/TextIndexTest.java b/text/hibernate6-text-tests/src/test/java/com/github/starnowski/posjsonhelper/text/hibernate6/dao/TextIndexTest.java index b992d608..cc92d1b8 100644 --- a/text/hibernate6-text-tests/src/test/java/com/github/starnowski/posjsonhelper/text/hibernate6/dao/TextIndexTest.java +++ b/text/hibernate6-text-tests/src/test/java/com/github/starnowski/posjsonhelper/text/hibernate6/dao/TextIndexTest.java @@ -1,6 +1,7 @@ package com.github.starnowski.posjsonhelper.text.hibernate6.dao; import com.github.starnowski.posjsonhelper.text.hibernate6.model.Tweet; +import com.github.starnowski.posjsonhelper.text.hibernate6.model.TweetWithLocale; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIfSystemProperty; @@ -26,22 +27,40 @@ import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TEST_METHOD; import static org.springframework.test.context.jdbc.SqlConfig.TransactionMode.ISOLATED; -@Sql(value = {CLEAR_DATABASE_SCRIPT_PATH, TEXT_INDEX_SCRIPT_PATH}, +@Sql(value = {CLEAR_DATABASE_SCRIPT_PATH, TEXT_INDEX_SCRIPT_PATH, TWEETS_WITH_LOCALE_SCRIPT_PATH}, config = @SqlConfig(transactionMode = ISOLATED), executionPhase = BEFORE_TEST_METHOD) @Sql(value = CLEAR_DATABASE_SCRIPT_PATH, config = @SqlConfig(transactionMode = ISOLATED), executionPhase = AFTER_TEST_METHOD) +@EnabledIfSystemProperty(named = "run.custom.directory.test", matches = "true") public class TextIndexTest extends AbstractItTest { @Autowired - private TweetDao tested; + private TweetWithLocalDao tested; - @Test + private static Stream provideShouldFindCorrectTweetsBySinglePlainQueryInDescription() { + return Stream.of( + Arguments.of("EV future", ENGLISH_CONFIGURATION, asList(1L)), + Arguments.of("Hydrogen", ENGLISH_CONFIGURATION, asList(2L, 3L)), + Arguments.of("Hydrogen", POLISH_CONFIGURATION, asList(4L)), + Arguments.of("Zmywarka 61 kWh", POLISH_CONFIGURATION, asList(5L, 6L, 8L)) + ); + } + + @DisplayName("should return all ids when searching by query for english configuration' for plainto_tsquery function") + @ParameterizedTest + @MethodSource("provideShouldFindCorrectTweetsBySinglePlainQueryInDescription") @EnabledIfSystemProperty(named = "run.custom.directory.test", matches = "true") - public void doSomeTest() - { - //TODO + public void shouldFindCorrectTweetsBySinglePlainQueryInDescription(String phrase, String configuration, List expectedIds) { + + // when + List results = tested.findBySinglePlainQueryInDescriptionForConfiguration(phrase, configuration); + + // then + assertThat(results).hasSize(expectedIds.size()); + assertThat(results.stream().map(TweetWithLocale::getId).collect(toSet())).containsAll(expectedIds); } + } diff --git a/text/hibernate6-text-tests/src/test/java/com/github/starnowski/posjsonhelper/text/hibernate6/dao/TweetWithLocalDao.java b/text/hibernate6-text-tests/src/test/java/com/github/starnowski/posjsonhelper/text/hibernate6/dao/TweetWithLocalDao.java index bf67fbdd..e83c1f21 100644 --- a/text/hibernate6-text-tests/src/test/java/com/github/starnowski/posjsonhelper/text/hibernate6/dao/TweetWithLocalDao.java +++ b/text/hibernate6-text-tests/src/test/java/com/github/starnowski/posjsonhelper/text/hibernate6/dao/TweetWithLocalDao.java @@ -1,10 +1,13 @@ package com.github.starnowski.posjsonhelper.text.hibernate6.dao; import com.github.starnowski.posjsonhelper.core.HibernateContext; +import com.github.starnowski.posjsonhelper.text.hibernate6.model.TweetWithLocale; import jakarta.persistence.EntityManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public class TweetWithLocalDao { @@ -12,4 +15,8 @@ public class TweetWithLocalDao { private EntityManager entityManager; @Autowired private HibernateContext hibernateContext; + + public List findBySinglePlainQueryInDescriptionForConfiguration(String phrase, String configuration) { + return null; + } } diff --git a/text/hibernate6-text-tests/src/test/resources/com/github/starnowski/posjsonhelper/text/hibernate6/dao/clean-database.sql b/text/hibernate6-text-tests/src/test/resources/com/github/starnowski/posjsonhelper/text/hibernate6/dao/clean-database.sql index 6f6e08c3..0e5e2582 100644 --- a/text/hibernate6-text-tests/src/test/resources/com/github/starnowski/posjsonhelper/text/hibernate6/dao/clean-database.sql +++ b/text/hibernate6-text-tests/src/test/resources/com/github/starnowski/posjsonhelper/text/hibernate6/dao/clean-database.sql @@ -1 +1,2 @@ -TRUNCATE tweet CASCADE; \ No newline at end of file +TRUNCATE tweet CASCADE; +TRUNCATE tweet_with_locale CASCADE; \ No newline at end of file diff --git a/text/hibernate6-text-tests/src/test/resources/com/github/starnowski/posjsonhelper/text/hibernate6/dao/tweets-with-locale.sql b/text/hibernate6-text-tests/src/test/resources/com/github/starnowski/posjsonhelper/text/hibernate6/dao/tweets-with-locale.sql index 0b68fab4..aaddad55 100644 --- a/text/hibernate6-text-tests/src/test/resources/com/github/starnowski/posjsonhelper/text/hibernate6/dao/tweets-with-locale.sql +++ b/text/hibernate6-text-tests/src/test/resources/com/github/starnowski/posjsonhelper/text/hibernate6/dao/tweets-with-locale.sql @@ -1,5 +1,5 @@ -INSERT INTO tweet_with_locale (id, title, short_content, locale) VALUES (1, 'EV cars', 'EV cars are the future', 'english'); +INSERT INTO tweet_with_locale (id, title, short_content, locale) VALUES (1, 'EV cars', 'Thier are the future', 'english'); INSERT INTO tweet_with_locale (id, title, short_content, locale) VALUES (2, 'Hydrogen Cars', 'Those cars do not have luck for infrastructure', 'english'); INSERT INTO tweet_with_locale (id, title, short_content, locale) VALUES (3, 'Hydrogen batteries', 'It is great idea for storing electicity power', 'english');