diff --git a/pom.xml b/pom.xml index 1f9218b..1e7b624 100644 --- a/pom.xml +++ b/pom.xml @@ -59,6 +59,24 @@ org.flywaydb flyway-database-postgresql + + + org.springframework.boot + spring-boot-testcontainers + test + + + + org.testcontainers + testcontainers-junit-jupiter + test + + + + org.testcontainers + testcontainers-postgresql + test + diff --git a/src/test/java/net/sytes/kashey/easymoney/persistence/AccountRepositoryTest.java b/src/test/java/net/sytes/kashey/easymoney/persistence/AccountRepositoryTest.java new file mode 100644 index 0000000..7bbf998 --- /dev/null +++ b/src/test/java/net/sytes/kashey/easymoney/persistence/AccountRepositoryTest.java @@ -0,0 +1,75 @@ +package net.sytes.kashey.easymoney.persistence; + +import model.Account; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.transaction.annotation.Transactional; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.postgresql.PostgreSQLContainer; +import service.AccountStore; + +import static org.junit.jupiter.api.Assertions.*; + +@Testcontainers +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) +@Transactional +class AccountRepositoryTest { + + @Container + @ServiceConnection + static PostgreSQLContainer postgres = + new PostgreSQLContainer("postgres:17"); + + @Autowired + private AccountStore accountStore; + + @Test + void creatingAccountPersistsNameAndGeneratesId() { + // When + Account created = accountStore.create("Карта"); + + // Then + assertNotNull(created.getId(), "База должна назначить ID"); + assertEquals("Карта", created.getName()); + + Account loaded = accountStore.findById(created.getId()); + + assertNotNull(loaded, "Созданный счёт должен находиться в БД"); + assertEquals(created.getId(), loaded.getId()); + assertEquals("Карта", loaded.getName()); + } + + @Test + void findingUnknownAccountReturnsNull() { + // When + Account account = accountStore.findById(-1L); + + // Then + assertNull(account, "Для отсутствующего ID должен возвращаться null"); + } + + @Test + void creatingTwoAccountsPersistsNamesAndGeneratesDifferentIds() { + // When + Account card = accountStore.create("Карта"); + Account cash = accountStore.create("Наличные"); + + // Then + Account loadedCard = accountStore.findById(card.getId()); + Account loadedCash = accountStore.findById(cash.getId()); + + assertNotNull(loadedCard); + assertNotNull(loadedCash); + + assertEquals("Карта", loadedCard.getName()); + assertEquals("Наличные", loadedCash.getName()); + + assertEquals(card, loadedCard); + assertEquals(cash, loadedCash); + + assertNotEquals(card.getId(), cash.getId(), "Id созданных счетов должны различаться"); + } +} \ No newline at end of file