-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add repository tests with embedded db
- Loading branch information
1 parent
0c4a7d3
commit 2e368d5
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...est/java/com/project/reactdashboard/infrastructure/stock/StockPostgresRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...pi/src/test/java/com/project/reactdashboard/infrastructure/stock/StockRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |