From 2e368d5ad3be2b083d20cd38b0e63e6226dd53aa Mon Sep 17 00:00:00 2001 From: hudescottes Date: Wed, 17 Jan 2024 22:48:14 +0100 Subject: [PATCH] Add repository tests with embedded db --- app-api/build.gradle.kts | 4 ++ .../stock/StockPostgresRepositoryTest.java | 41 +++++++++++++ .../stock/StockRepositoryTest.java | 59 +++++++++++++++++++ app-api/src/test/resources/init-zonky.sql | 7 +++ 4 files changed, 111 insertions(+) create mode 100644 app-api/src/test/java/com/project/reactdashboard/infrastructure/stock/StockPostgresRepositoryTest.java create mode 100644 app-api/src/test/java/com/project/reactdashboard/infrastructure/stock/StockRepositoryTest.java create mode 100644 app-api/src/test/resources/init-zonky.sql diff --git a/app-api/build.gradle.kts b/app-api/build.gradle.kts index ce09ee0..60340c6 100644 --- a/app-api/build.gradle.kts +++ b/app-api/build.gradle.kts @@ -3,6 +3,8 @@ val postgresVersion by extra { "42.7.1" } val jsr310Version by extra { "2.16.1" } val liquibaseVersion by extra { "4.25.1" } val apacheLang3Version by extra { "3.14.0" } +val zonkyEmbeddedDbVersion by extra { "2.5.0" } +val zonkyPostgresVersion by extra { "2.0.6" } buildscript { repositories { @@ -50,4 +52,6 @@ dependencies { runtimeOnly("org.postgresql:postgresql:${postgresVersion}") testImplementation("org.springframework.boot:spring-boot-starter-test:$springBootVersion") testImplementation("org.apache.commons:commons-lang3:$apacheLang3Version") + testImplementation("io.zonky.test:embedded-database-spring-test:$zonkyEmbeddedDbVersion") + testImplementation("io.zonky.test:embedded-postgres:$zonkyPostgresVersion") } diff --git a/app-api/src/test/java/com/project/reactdashboard/infrastructure/stock/StockPostgresRepositoryTest.java b/app-api/src/test/java/com/project/reactdashboard/infrastructure/stock/StockPostgresRepositoryTest.java new file mode 100644 index 0000000..0e4ebf2 --- /dev/null +++ b/app-api/src/test/java/com/project/reactdashboard/infrastructure/stock/StockPostgresRepositoryTest.java @@ -0,0 +1,41 @@ +package com.project.reactdashboard.infrastructure.stock; + +import com.project.reactdashboard.infrastructure.stock.model.Stock; +import io.zonky.test.db.AutoConfigureEmbeddedDatabase; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.List; + +import static com.project.reactdashboard.ObjectRandomizer.randomStock; +import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.ZONKY; +import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.RefreshMode.BEFORE_EACH_TEST_METHOD; +import static org.junit.jupiter.api.Assertions.assertEquals; + +@SpringBootTest +@RunWith(SpringRunner.class) +@AutoConfigureEmbeddedDatabase(provider = ZONKY, refresh = BEFORE_EACH_TEST_METHOD) +public class StockPostgresRepositoryTest { + + @Autowired + private StockRepository stockRepository; + + @Autowired + private StockPostgresRepository repository; + + @Test + void should_upsert_stock() { + Stock stock = randomStock(); + stock.setSymbol("ML.XPAR"); + + repository.upsert(stock); + List dbStocks = stockRepository.findAll(); + + assertEquals(1, dbStocks.size()); + assertEquals(stock.getSymbol(), dbStocks.get(0).getSymbol()); + assertEquals(stock.getHigh(), dbStocks.get(0).getHigh()); + } +} diff --git a/app-api/src/test/java/com/project/reactdashboard/infrastructure/stock/StockRepositoryTest.java b/app-api/src/test/java/com/project/reactdashboard/infrastructure/stock/StockRepositoryTest.java new file mode 100644 index 0000000..d9e7b30 --- /dev/null +++ b/app-api/src/test/java/com/project/reactdashboard/infrastructure/stock/StockRepositoryTest.java @@ -0,0 +1,59 @@ +package com.project.reactdashboard.infrastructure.stock; + +import com.project.reactdashboard.infrastructure.stock.model.Stock; +import io.zonky.test.db.AutoConfigureEmbeddedDatabase; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.junit4.SpringRunner; + +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.util.List; + +import static io.zonky.test.db.AutoConfigureEmbeddedDatabase.DatabaseProvider.ZONKY; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@RunWith(SpringRunner.class) +@DataJpaTest +@Sql({"/init-zonky.sql"}) +@AutoConfigureEmbeddedDatabase(provider = ZONKY) +public class StockRepositoryTest { + + @Autowired + private StockRepository repository; + + @Test + void should_find_stock_by_symbol() { + OffsetDateTime date = OffsetDateTime.parse("2023-01-01T00:00:00" + "+00:00"); + + List stocks = repository.findBySymbol("MC.XPAR", date); + + assertEquals(3, stocks.size()); + assertTrue(stocks.stream().anyMatch(element -> "MC.XPAR".equals(element.getSymbol()))); + } + + @Test + void should_find_latest_stocks() { + List stocks = repository.findAllLatest(); + + assertEquals(2, stocks.size()); + assertEquals("POM.XPAR", stocks.get(0).getSymbol()); + assertTrue(stocks.get(0).getDate().toString().contains("2023-12-30")); + assertEquals("MC.XPAR", stocks.get(1).getSymbol()); + assertTrue(stocks.get(1).getDate().toString().contains("2023-12-29")); + } + + @Test + void should_find_last_working_day_stock() { + OffsetDateTime date = OffsetDateTime.parse("2023-12-28T00:00:00" + "+00:00") + .withOffsetSameLocal(ZoneOffset.UTC); + + Stock stock = repository.findLastWorkingDayBySymbol("MC.XPAR", date); + + assertTrue(stock.getDate().toString().contains("2023-12-28")); + } +} diff --git a/app-api/src/test/resources/init-zonky.sql b/app-api/src/test/resources/init-zonky.sql new file mode 100644 index 0000000..7e6d23a --- /dev/null +++ b/app-api/src/test/resources/init-zonky.sql @@ -0,0 +1,7 @@ +INSERT INTO stock (symbol, date, open, close, high, low, volume) +VALUES + ('POM.XPAR', '2023-12-27 00:00:00+00', 15.00, 14.50, 15.20, 14.20, 10000), + ('POM.XPAR', '2023-12-30 00:00:00+00', 15.50, 14.00, 16.20, 14.20, 15000), + ('MC.XPAR', '2023-12-27 00:00:00+00', 600.00, 602.50, 605.20, 599.80, 50000), + ('MC.XPAR', '2023-12-28 00:00:00+00', 602.60, 604.20, 606.00, 601.50, 55000), + ('MC.XPAR', '2023-12-29 00:00:00+00', 604.50, 606.00, 607.80, 603.20, 48000); \ No newline at end of file