Skip to content

Commit

Permalink
Add repository tests with embedded db
Browse files Browse the repository at this point in the history
  • Loading branch information
hdescottes committed Jan 17, 2024
1 parent 0c4a7d3 commit 2e368d5
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
@@ -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<Stock> dbStocks = stockRepository.findAll();

assertEquals(1, dbStocks.size());
assertEquals(stock.getSymbol(), dbStocks.get(0).getSymbol());
assertEquals(stock.getHigh(), dbStocks.get(0).getHigh());
}
}
Original file line number Diff line number Diff line change
@@ -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<Stock> 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<Stock> 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"));
}
}
7 changes: 7 additions & 0 deletions app-api/src/test/resources/init-zonky.sql
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 2e368d5

Please sign in to comment.