Skip to content

Commit 9683990

Browse files
committed
test(JdbcCountryDaoTest): add tests for getStatisticsOf() method.
Prerequisite for #1143
1 parent c5441da commit 9683990

File tree

6 files changed

+227
-0
lines changed

6 files changed

+227
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Copyright (C) 2009-2019 Slava Semushin <[email protected]>
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.country;
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+
// LATER: use a single application context with DAOs (see #1150)
36+
@ContextConfiguration(classes = CountryConfig.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+
// LATER: find a better way for importing properties files (see #1151)
46+
locations = "/sql/country_dao_queries.properties")
47+
@RunWith(SpringRunner.class)
48+
public class JdbcCountryDaoTest implements WithAssertions {
49+
50+
@Autowired
51+
private CountryDao countryDao;
52+
53+
//
54+
// Tests for getStatisticsOf()
55+
//
56+
57+
@Test
58+
public void getStatisticsOfWithEmptyCollection() {
59+
// given
60+
// when
61+
Map<String, Integer> statistics = countryDao.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/countries-italy.sql",
74+
"/db/countries-france.sql",
75+
"/db/series-4-italy-qty5.sql",
76+
"/db/series-5-france-qty4.sql",
77+
"/db/series-6-france-qty6.sql"
78+
},
79+
statements = {
80+
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
81+
+ "VALUES (1, 4, 5), (1, 5, 4), (1, 6, 6)"
82+
}
83+
)
84+
public void getStatisticsOfWithSeriesWithAllStamps() {
85+
// given
86+
final Integer expectedStampsFromItaly = 5;
87+
final Integer expectedStampsFromFrance = 10;
88+
// when
89+
Map<String, Integer> statistics = countryDao.getStatisticsOf(1, "en");
90+
// then
91+
assertThat(statistics)
92+
.containsEntry("Italy", expectedStampsFromItaly)
93+
.containsEntry("France", expectedStampsFromFrance)
94+
.hasSize(2);
95+
}
96+
97+
@Test
98+
@Sql(
99+
scripts = {
100+
"/db/users-coder.sql",
101+
"/db/collections-coder.sql",
102+
"/db/categories-fauna.sql",
103+
"/db/series-1-fauna-qty5.sql"
104+
},
105+
statements = {
106+
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
107+
+ "VALUES (1, 1, 5)"
108+
}
109+
)
110+
public void getStatisticsOfWithSeriesWithoutCountry() {
111+
// given
112+
// when
113+
Map<String, Integer> statisticsInEnglish = countryDao.getStatisticsOf(1, "en");
114+
Map<String, Integer> statisticsInRussian = countryDao.getStatisticsOf(1, "ru");
115+
// then
116+
assertThat(statisticsInEnglish).containsKeys("Unknown");
117+
assertThat(statisticsInRussian).containsKeys("Unknown");
118+
}
119+
120+
@Test
121+
@Sql(
122+
scripts = {
123+
"/db/users-coder.sql",
124+
"/db/collections-coder.sql",
125+
"/db/categories-sport.sql",
126+
"/db/countries-italy.sql",
127+
"/db/series-4-italy-qty5.sql"
128+
},
129+
statements = {
130+
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
131+
+ "VALUES (1, 4, 5)"
132+
}
133+
)
134+
public void getStatisticsOfInRussian() {
135+
// given
136+
// when
137+
Map<String, Integer> statisticsInRussian = countryDao.getStatisticsOf(1, "ru");
138+
// then
139+
assertThat(statisticsInRussian).containsKeys("Италия");
140+
}
141+
142+
@Test
143+
@Sql(
144+
scripts = {
145+
"/db/users-coder.sql",
146+
"/db/collections-coder.sql",
147+
"/db/categories-sport.sql",
148+
"/db/countries-france.sql",
149+
"/db/series-5-france-qty4.sql"
150+
},
151+
statements = {
152+
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
153+
+ "VALUES (1, 5, 4)"
154+
}
155+
)
156+
public void getStatisticsOfInRussianWithFallbackToEnglish() {
157+
// given
158+
// when
159+
Map<String, Integer> statistics = countryDao.getStatisticsOf(1, "ru");
160+
// then
161+
assertThat(statistics).containsKey("France");
162+
}
163+
164+
@Test
165+
@Sql(
166+
scripts = {
167+
"/db/users-coder.sql",
168+
"/db/collections-coder.sql",
169+
"/db/categories-sport.sql",
170+
"/db/countries-france.sql",
171+
"/db/series-5-france-qty4.sql"
172+
},
173+
statements = {
174+
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
175+
+ "VALUES (1, 5, 4)"
176+
}
177+
)
178+
public void getStatisticsOfInUnsupportedLanguageWithFallbackToEnglish() {
179+
// given
180+
// when
181+
Map<String, Integer> statistics = countryDao.getStatisticsOf(1, "fr");
182+
// then
183+
assertThat(statistics).containsKey("France");
184+
}
185+
186+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--
2+
-- creates country "France" with id=2
3+
--
4+
-- depends on: users-coder.sql
5+
--
6+
INSERT INTO countries(id, name, slug, created_at, created_by, updated_at, updated_by)
7+
SELECT 2, 'France', 'france', CURRENT_TIMESTAMP(), id, CURRENT_TIMESTAMP(), id FROM users WHERE login = 'coder';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--
2+
-- creates country "Italy" with id=1 and name in Russian
3+
--
4+
-- depends on: users-coder.sql
5+
--
6+
INSERT INTO countries(id, name, name_ru, slug, created_at, created_by, updated_at, updated_by)
7+
SELECT 1, 'Italy', 'Италия', 'italy', CURRENT_TIMESTAMP(), id, CURRENT_TIMESTAMP(), id FROM users WHERE login = 'coder';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--
2+
-- creates a series with id=4, issued in Italy and having 5 stamps
3+
--
4+
-- depends on: users-coder.sql
5+
-- depends on: categories-sport.sql
6+
-- depends on: countries-italy.sql
7+
--
8+
INSERT INTO series(id, quantity, perforated, category_id, country_id, created_at, created_by, updated_at, updated_by)
9+
SELECT 4, 5, TRUE, (SELECT id FROM categories WHERE slug = 'sport'), (SELECT id FROM countries WHERE slug = 'italy'), CURRENT_TIMESTAMP(), id, CURRENT_TIMESTAMP(), id FROM users WHERE login = 'coder';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--
2+
-- creates a series with id=5, issued in France and having 4 stamps
3+
--
4+
-- depends on: users-coder.sql
5+
-- depends on: categories-sport.sql
6+
-- depends on: countries-france.sql
7+
--
8+
INSERT INTO series(id, quantity, perforated, category_id, country_id, created_at, created_by, updated_at, updated_by)
9+
SELECT 5, 4, TRUE, (SELECT id FROM categories WHERE slug = 'sport'), (SELECT id FROM countries WHERE slug = 'france'), CURRENT_TIMESTAMP(), id, CURRENT_TIMESTAMP(), id FROM users WHERE login = 'coder';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
--
2+
-- creates a series with id=6, issued in France and having 6 stamps
3+
--
4+
-- depends on: users-coder.sql
5+
-- depends on: categories-sport.sql
6+
-- depends on: countries-france.sql
7+
--
8+
INSERT INTO series(id, quantity, perforated, category_id, country_id, created_at, created_by, updated_at, updated_by)
9+
SELECT 6, 6, TRUE, (SELECT id FROM categories WHERE slug = 'sport'), (SELECT id FROM countries WHERE slug = 'france'), CURRENT_TIMESTAMP(), id, CURRENT_TIMESTAMP(), id FROM users WHERE login = 'coder';

0 commit comments

Comments
 (0)