From 6acbabf6f2da3c9cf9b3845c5492e0508458de61 Mon Sep 17 00:00:00 2001 From: Kashey Date: Thu, 29 Oct 2026 17:30:57 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B8=D0=BD=D1=82=D0=B5=D0=B3=D1=80=D0=B0=D1=86=D0=B8=D0=BE?= =?UTF-8?q?=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20JDB?= =?UTF-8?q?C-=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0=20?= =?UTF-8?q?=D1=81=D1=87=D0=B5=D1=82=D0=BE=D0=B2=20=D1=81=20PostgreSQL=20Te?= =?UTF-8?q?stcontainers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 18 +++++ .../persistence/AccountRepositoryTest.java | 75 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/test/java/net/sytes/kashey/easymoney/persistence/AccountRepositoryTest.java 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