Skip to content

Commit 451c743

Browse files
committed
add test-containers tests
1 parent fd83b04 commit 451c743

File tree

9 files changed

+164
-32
lines changed

9 files changed

+164
-32
lines changed

WeatherCastApp/pom.xml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
2626
<postgresql.version>42.7.5</postgresql.version>
2727
<spring-cloud.version>2024.0.1</spring-cloud.version>
28-
<junit.version>5.10.2</junit.version>
2928
<feign-jackson.version>12.3</feign-jackson.version>
3029
<mockito.version>5.11.0</mockito.version>
31-
<test-containers.version>1.19.8</test-containers.version>
30+
<test-containers.version>1.21.0</test-containers.version>
31+
<jakarta-validation.version>3.1.1</jakarta-validation.version>
3232
</properties>
3333

3434
<dependencyManagement>
@@ -66,7 +66,7 @@
6666
<dependency>
6767
<groupId>jakarta.validation</groupId>
6868
<artifactId>jakarta.validation-api</artifactId>
69-
<version>3.1.1</version>
69+
<version>${jakarta-validation.version}</version>
7070
</dependency>
7171

7272
<dependency>
@@ -99,7 +99,6 @@
9999
<dependency>
100100
<groupId>org.junit.jupiter</groupId>
101101
<artifactId>junit-jupiter</artifactId>
102-
<version>${junit.version}</version>
103102
<scope>test</scope>
104103
</dependency>
105104

@@ -130,6 +129,19 @@
130129
<scope>provided</scope>
131130
</dependency>
132131

132+
<dependency>
133+
<groupId>org.testcontainers</groupId>
134+
<artifactId>junit-jupiter</artifactId>
135+
<version>${test-containers.version}</version>
136+
<scope>test</scope>
137+
</dependency>
138+
139+
<dependency>
140+
<groupId>org.testcontainers</groupId>
141+
<artifactId>postgresql</artifactId>
142+
<scope>test</scope>
143+
</dependency>
144+
133145
<dependency>
134146
<groupId>org.mapstruct</groupId>
135147
<artifactId>mapstruct</artifactId>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package tb.wca;
2+
3+
import org.springframework.test.context.DynamicPropertyRegistry;
4+
import org.springframework.test.context.DynamicPropertySource;
5+
import org.testcontainers.containers.GenericContainer;
6+
import org.testcontainers.utility.DockerImageName;
7+
8+
public class AbstractContainerBaseTest {
9+
10+
private static final String DB_NAME = "testdb";
11+
private static final String DB_USER = "testuser";
12+
private static final String DB_PASSWORD = "testpass";
13+
14+
static final GenericContainer<?> postgres = new GenericContainer<>(
15+
DockerImageName.parse("postgres:16-alpine"))
16+
.withEnv("POSTGRES_DB", DB_NAME)
17+
.withEnv("POSTGRES_USER", DB_USER)
18+
.withEnv("POSTGRES_PASSWORD", DB_PASSWORD)
19+
.withExposedPorts(5432);
20+
21+
static {
22+
postgres.start();
23+
}
24+
25+
@DynamicPropertySource
26+
static void registerPgProperties(DynamicPropertyRegistry registry) {
27+
String jdbcUrl = String.format(
28+
"jdbc:postgresql://%s:%d/%s",
29+
postgres.getHost(),
30+
postgres.getMappedPort(5432),
31+
DB_NAME
32+
);
33+
34+
registry.add("spring.datasource.url", () -> jdbcUrl);
35+
registry.add("spring.datasource.username", () -> DB_USER);
36+
registry.add("spring.datasource.password", () -> DB_PASSWORD);
37+
38+
registry.add("spring.flyway.url", () -> jdbcUrl);
39+
registry.add("spring.flyway.user", () -> DB_USER);
40+
registry.add("spring.flyway.password", () -> DB_PASSWORD);
41+
registry.add("spring.flyway.locations", () -> "classpath:db/migration");
42+
}
43+
44+
}

WeatherCastApp/src/test/java/tb/wca/client/WeatherApiClientTest.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

WeatherCastApp/src/test/java/tb/wca/client/YandexGeocodeClientTest.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

WeatherCastApp/src/test/java/tb/wca/client/mapper/WeatherProjectEolMapperTest.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

WeatherCastApp/src/test/java/tb/wca/client/mapper/YandexGeoMapperTest.java

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package tb.wca.repository;
2+
3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
7+
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
8+
import tb.wca.AbstractContainerBaseTest;
9+
import tb.wca.entity.CityWeatherEntity;
10+
11+
import java.time.LocalDate;
12+
import java.time.LocalTime;
13+
import java.util.List;
14+
import java.util.Optional;
15+
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertTrue;
18+
19+
@DataJpaTest
20+
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
21+
class CityWeatherRepositoryTest extends AbstractContainerBaseTest {
22+
23+
@Autowired
24+
private CityWeatherRepository cityWeatherRepository;
25+
26+
@AfterEach
27+
void shutDown() {
28+
cityWeatherRepository.deleteAll();
29+
}
30+
31+
@Test
32+
void findByCityNameAndDate_shouldReturnRecords() {
33+
List<CityWeatherEntity> results = cityWeatherRepository.findByCity_NameAndDate(
34+
"Обнинск", LocalDate.of(2025, 12, 1));
35+
36+
assertEquals(2, results.size());
37+
}
38+
39+
@Test
40+
void findByCityNameAndDateAndTime_shouldReturnSingleRecord() {
41+
Optional<CityWeatherEntity> result = cityWeatherRepository.findByCity_NameAndDateAndTime(
42+
"Обнинск",
43+
LocalDate.of(2025, 12, 1),
44+
LocalTime.of(6, 0)
45+
);
46+
assertTrue(result.isPresent());
47+
assertEquals(LocalDate.of(2025, 12, 1), result.get().getDate());
48+
assertEquals(LocalTime.of(6, 0), result.get().getTime());
49+
assertEquals("Обнинск", result.get().getCity().getName());
50+
}
51+
52+
@Test
53+
void findByCityNameAndDateBetween_shouldReturnRange() {
54+
List<CityWeatherEntity> results = cityWeatherRepository.findByCity_NameAndDateBetween(
55+
"Саратов",
56+
LocalDate.of(2025, 5, 1),
57+
LocalDate.of(2025, 5, 31)
58+
);
59+
60+
assertEquals(2, results.size());
61+
}
62+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.jpa.hibernate.ddl-auto=none
2+
spring.flyway.enabled=true
3+
spring.flyway.locations=classpath:db/migration
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
TRUNCATE public.city_weather RESTART IDENTITY CASCADE;
2+
TRUNCATE public.weather RESTART IDENTITY CASCADE;
3+
TRUNCATE public.city RESTART IDENTITY CASCADE;
4+
5+
INSERT INTO public.city (id, name, longitude, latitude) VALUES
6+
(1, 'Обнинск', 55.55, 55.55),
7+
(2, 'Петушки', 55.55, 55.55),
8+
(3, 'Саратов', 55.55, 55.55);
9+
10+
INSERT INTO public.weather (
11+
id, temperature, humidity, wind_speed, uv_index,
12+
wind_direction, feels_like, pressure, created_at
13+
) VALUES
14+
(1, -5.0, 85, 3.5, 0.20, 180, -9.0, 1015, '2025-06-06 06:00:00+00'),
15+
(2, -3.0, 80, 4.0, 0.30, 190, -6.0, 1010, '2025-06-06 06:00:00+00'),
16+
(3, 10.0, 60, 5.0, 3.20, 170, 8.0, 1005, '2025-06-06 06:00:00+00'),
17+
(4, 12.0, 55, 4.2, 4.10, 160, 11.0, 1003, '2025-06-06 06:00:00+00'),
18+
(5, 15.0, 50, 2.8, 5.50, 200, 14.0, 1008, '2025-06-06 06:00:00+00'),
19+
(6, 17.0, 45, 3.0, 6.10, 210, 16.0, 1009, '2025-06-06 06:00:00+00'),
20+
(7, -10.0, 90, 6.0, 0.10, 140, -15.0, 1020, '2025-06-06 06:00:00+00'),
21+
(8, -8.0, 88, 5.5, 0.15, 130, -12.0, 1018, '2025-06-06 06:00:00+00'),
22+
(9, 20.0, 40, 2.0, 7.00, 220, 19.0, 1002, '2025-06-06 06:00:00+00'),
23+
(10, 22.0, 38, 1.8, 7.50, 225, 21.0, 1001, '2025-06-06 06:00:00+00');
24+
25+
INSERT INTO public.city_weather (
26+
id, date, time, weather_id, city_id
27+
) VALUES
28+
(1, '2025-12-01', '06:00:00', 1, 1),
29+
(2, '2025-12-01', '09:00:00', 2, 1),
30+
(3, '2025-06-02', '14:00:00', 9, 1),
31+
(4, '2025-06-02', '15:00:00', 9, 1),
32+
33+
(5, '2025-04-15', '12:00:00', 3, 2),
34+
(6, '2025-04-15', '15:00:00', 4, 2),
35+
(7, '2025-01-20', '12:00:00', 8, 2),
36+
37+
(8, '2025-05-10', '12:00:00', 5, 3),
38+
(9, '2025-05-10', '18:00:00', 6, 3),
39+
(10, '2025-01-20', '06:00:00', 7, 3);

0 commit comments

Comments
 (0)