|
| 1 | +/* |
| 2 | + * Copyright (C) 2009-2019 Slava Semushin <slava.semushin@gmail.com> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | + */ |
| 18 | + |
| 19 | +package ru.mystamps.web.feature.category; |
| 20 | + |
| 21 | +import org.assertj.core.api.WithAssertions; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; |
| 26 | +import org.springframework.test.context.ContextConfiguration; |
| 27 | +import org.springframework.test.context.TestPropertySource; |
| 28 | +import org.springframework.test.context.jdbc.Sql; |
| 29 | +import org.springframework.test.context.junit4.SpringRunner; |
| 30 | +import ru.mystamps.web.tests.Random; |
| 31 | + |
| 32 | +import java.util.Map; |
| 33 | + |
| 34 | +@JdbcTest |
| 35 | +// @todo #1143 Introduce a dedicated config for DAO classes |
| 36 | +@ContextConfiguration(classes = CategoryConfig.Services.class) |
| 37 | +@TestPropertySource( |
| 38 | + properties = { |
| 39 | + // don't load test data, start with an empty database |
| 40 | + "liquibase.contexts=scheme,init-data", |
| 41 | + // overrides settings from application-test.properties to keep the console clean, |
| 42 | + // comment this out when you need to debug tests. See also logback-test.xml |
| 43 | + "logging.level.=WARN", "logging.level.ru.mystamps=WARN" |
| 44 | + }, |
| 45 | + // @todo #1143 Improve a way of importing properties files in the tests |
| 46 | + locations = "/sql/category_dao_queries.properties") |
| 47 | +@RunWith(SpringRunner.class) |
| 48 | +public class JdbcCategoryDaoTest implements WithAssertions { |
| 49 | + |
| 50 | + @Autowired |
| 51 | + private CategoryDao categoryDao; |
| 52 | + |
| 53 | + // |
| 54 | + // Tests for getStatisticsOf() |
| 55 | + // |
| 56 | + |
| 57 | + @Test |
| 58 | + public void getStatisticsOfWithEmptyCollection() { |
| 59 | + // given |
| 60 | + // when |
| 61 | + Map<String, Integer> statistics = categoryDao.getStatisticsOf(Random.id(), Random.lang()); |
| 62 | + // then |
| 63 | + assertThat(statistics).isEmpty(); |
| 64 | + } |
| 65 | + |
| 66 | + // LATER: extract all "scripts" to a class level. Requires @SqlMergeMode from Spring 5.2 |
| 67 | + @Test |
| 68 | + @Sql( |
| 69 | + scripts = { |
| 70 | + "/db/users-coder.sql", |
| 71 | + "/db/collections-coder.sql", |
| 72 | + "/db/categories-sport.sql", |
| 73 | + "/db/categories-fauna.sql", |
| 74 | + "/db/series-1-fauna-qty5.sql", |
| 75 | + "/db/series-2-sport-qty3.sql", |
| 76 | + "/db/series-3-sport-qty7.sql" |
| 77 | + }, |
| 78 | + statements = { |
| 79 | + "INSERT INTO collections_series(collection_id, series_id, number_of_stamps) " |
| 80 | + + "VALUES (1, 1, 5), (1, 2, 3), (1, 3, 7)" |
| 81 | + } |
| 82 | + ) |
| 83 | + public void getStatisticsOfWithSeriesWithAllStamps() { |
| 84 | + // given |
| 85 | + final Integer expectedStampsInFaunaCategory = 5; |
| 86 | + final Integer expectedStampsInSportCategory = 10; |
| 87 | + // when |
| 88 | + Map<String, Integer> statistics = categoryDao.getStatisticsOf(1, "en"); |
| 89 | + // then |
| 90 | + assertThat(statistics) |
| 91 | + .containsEntry("Fauna", expectedStampsInFaunaCategory) |
| 92 | + .containsEntry("Sport", expectedStampsInSportCategory) |
| 93 | + .hasSize(2); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + @Sql( |
| 98 | + scripts = { |
| 99 | + "/db/users-coder.sql", |
| 100 | + "/db/collections-coder.sql", |
| 101 | + "/db/categories-fauna.sql", |
| 102 | + "/db/series-1-fauna-qty5.sql" |
| 103 | + }, |
| 104 | + statements = { |
| 105 | + "INSERT INTO collections_series(collection_id, series_id, number_of_stamps) " |
| 106 | + + "VALUES (1, 1, 5)" |
| 107 | + } |
| 108 | + ) |
| 109 | + public void getStatisticsOfInRussian() { |
| 110 | + // given |
| 111 | + // when |
| 112 | + Map<String, Integer> statisticsInRussian = categoryDao.getStatisticsOf(1, "ru"); |
| 113 | + // then |
| 114 | + assertThat(statisticsInRussian).containsKeys("Фауна"); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + @Sql( |
| 119 | + scripts = { |
| 120 | + "/db/users-coder.sql", |
| 121 | + "/db/collections-coder.sql", |
| 122 | + "/db/categories-sport.sql", |
| 123 | + "/db/series-2-sport-qty3.sql", |
| 124 | + }, |
| 125 | + statements = { |
| 126 | + "INSERT INTO collections_series(collection_id, series_id, number_of_stamps) " |
| 127 | + + "VALUES (1, 2, 3)" |
| 128 | + } |
| 129 | + ) |
| 130 | + public void getStatisticsOfInRussianWithFallbackToEnglish() { |
| 131 | + // given |
| 132 | + // when |
| 133 | + Map<String, Integer> statistics = categoryDao.getStatisticsOf(1, "ru"); |
| 134 | + // then |
| 135 | + assertThat(statistics).containsKey("Sport"); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + @Sql( |
| 140 | + scripts = { |
| 141 | + "/db/users-coder.sql", |
| 142 | + "/db/collections-coder.sql", |
| 143 | + "/db/categories-fauna.sql", |
| 144 | + "/db/series-1-fauna-qty5.sql", |
| 145 | + }, |
| 146 | + statements = { |
| 147 | + "INSERT INTO collections_series(collection_id, series_id, number_of_stamps) " |
| 148 | + + "VALUES (1, 1, 5)" |
| 149 | + } |
| 150 | + ) |
| 151 | + public void getStatisticsOfInUnsupportedLanguageWithFallbackToEnglish() { |
| 152 | + // given |
| 153 | + // when |
| 154 | + Map<String, Integer> statistics = categoryDao.getStatisticsOf(1, "fr"); |
| 155 | + // then |
| 156 | + assertThat(statistics).containsKey("Fauna"); |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments