Compare commits

..
2 Commits
8 changed files with 135 additions and 15 deletions

No files matched your search

+18
View File
@@ -59,6 +59,24 @@
<groupId>org.flywaydb</groupId> <groupId>org.flywaydb</groupId>
<artifactId>flyway-database-postgresql</artifactId> <artifactId>flyway-database-postgresql</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-postgresql</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
-4
View File
@@ -50,10 +50,6 @@ public class Main {
printAccountBalance(card, ledger); printAccountBalance(card, ledger);
printAccountBalance(cash, ledger); printAccountBalance(cash, ledger);
// operation5.removeLine(0);
// operationService.post(operation5);
// System.out.println("Удаление строк");
Operation operation6 = Operation.income(LocalDate.now(), 504L, card); Operation operation6 = Operation.income(LocalDate.now(), 504L, card);
operationLine = new OperationLine(new Category(4L, "Salary"), new BigDecimal(20000)); operationLine = new OperationLine(new Category(4L, "Salary"), new BigDecimal(20000));
operation6.addLine(operationLine); operation6.addLine(operationLine);
@@ -20,18 +20,10 @@ public class AccountingConfig {
} }
@Bean @Bean
public OperationService operationService( public OperationService operationService(OperationJournal journal, Ledger ledger) {
OperationJournal journal,
Ledger ledger
) {
return new OperationService(journal, ledger); return new OperationService(journal, ledger);
} }
@Bean
public AccountStore accountStore() {
return new InMemoryAccountStore();
}
@Bean @Bean
public AccountService accountService(AccountStore accountStore) { public AccountService accountService(AccountStore accountStore) {
return new AccountService(accountStore); return new AccountService(accountStore);
@@ -0,0 +1,16 @@
package net.sytes.kashey.easymoney.config;
import net.sytes.kashey.easymoney.persistence.AccountRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.simple.JdbcClient;
import service.AccountStore;
@Configuration
public class PersistenceConfig {
@Bean
public AccountStore accountStore(JdbcClient jdbcClient) {
return new AccountRepository(jdbcClient);
}
}
@@ -3,6 +3,7 @@ package net.sytes.kashey.easymoney.api;
import model.Account; import model.Account;
import model.Operation; import model.Operation;
import net.sytes.kashey.easymoney.config.AccountingConfig; import net.sytes.kashey.easymoney.config.AccountingConfig;
import net.sytes.kashey.easymoney.config.InMemoryStorageTestConfig;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource; import org.junit.jupiter.params.provider.NullAndEmptySource;
@@ -29,7 +30,10 @@ import java.math.BigDecimal;
import java.time.LocalDate; import java.time.LocalDate;
@WebMvcTest(AccountController.class) @WebMvcTest(AccountController.class)
@Import(AccountingConfig.class) @Import({
AccountingConfig.class,
InMemoryStorageTestConfig.class
})
class AccountControllerTest { class AccountControllerTest {
@Autowired @Autowired
@@ -3,6 +3,7 @@ package net.sytes.kashey.easymoney.api;
import model.Account; import model.Account;
import model.Operation; import model.Operation;
import model.OperationType; import model.OperationType;
import net.sytes.kashey.easymoney.config.InMemoryStorageTestConfig;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest; import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
@@ -27,7 +28,10 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest(OperationController.class) @WebMvcTest(OperationController.class)
@Import(AccountingConfig.class) @Import({
AccountingConfig.class,
InMemoryStorageTestConfig.class
})
class OperationControllerTest { class OperationControllerTest {
@Autowired @Autowired
@@ -0,0 +1,15 @@
package net.sytes.kashey.easymoney.config;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import service.AccountStore;
import service.InMemoryAccountStore;
@TestConfiguration
public class InMemoryStorageTestConfig {
@Bean
public AccountStore accountStore() {
return new InMemoryAccountStore();
}
}
@@ -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 созданных счетов должны различаться");
}
}