Добавил тесты проведения, повторного проведения и изменения сумм операций
This commit is contained in:
1 parent
d264ed6583
commit
8af851a49a
18 files changed
+302
-34
No files matched your search
@@ -28,3 +28,7 @@ bin/
|
||||
|
||||
### Mac OS ###
|
||||
.DS_Store
|
||||
|
||||
/target/
|
||||
.idea/
|
||||
*.iml
|
||||
Generated
-3
@@ -1,3 +0,0 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
Generated
-6
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
-8
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/EasyMoney.iml" filepath="$PROJECT_DIR$/EasyMoney.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
-6
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -1,11 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>net.sytes.kashey</groupId>
|
||||
<artifactId>easy-money</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.release>21</maven.compiler.release>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.11.4</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.5.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,262 @@
|
||||
package service;
|
||||
|
||||
import model.Account;
|
||||
import model.Category;
|
||||
import model.Operation;
|
||||
import model.OperationLine;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class LedgerTest {
|
||||
|
||||
@Test
|
||||
void openingBalanceSetsAccountBalance() {
|
||||
// Given
|
||||
Ledger ledger = new Ledger();
|
||||
Account card = new Account(1L, "Карта");
|
||||
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||
|
||||
Operation operation = Operation.openingBalance(date, 101L, card, new BigDecimal(15000));
|
||||
|
||||
// When
|
||||
ledger.post(operation);
|
||||
|
||||
// Then
|
||||
BigDecimal actual = ledger.balance(card, date);
|
||||
|
||||
assertEquals(0, new BigDecimal(15000).compareTo(actual),
|
||||
"Остаток счёта должен совпадать с начальным остатком");
|
||||
}
|
||||
|
||||
@Test
|
||||
void incomeIncreasesAccountBalance() {
|
||||
// Given
|
||||
Ledger ledger = new Ledger();
|
||||
Account card = new Account(1L, "Карта");
|
||||
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||
|
||||
ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000)));
|
||||
|
||||
Operation income = Operation.income(date, 102L, card);
|
||||
OperationLine operationLine1 = new OperationLine(new Category(1L, "Salary"), new BigDecimal(2000));
|
||||
income.addLine(operationLine1);
|
||||
OperationLine operationLine2 = new OperationLine(new Category(2L, "Present"), new BigDecimal(500));
|
||||
income.addLine(operationLine2);
|
||||
|
||||
// When
|
||||
ledger.post(income);
|
||||
|
||||
// Then
|
||||
BigDecimal actual = ledger.balance(card, date);
|
||||
|
||||
assertEquals(0, new BigDecimal(17500).compareTo(actual),
|
||||
"Остаток счёта должен увеличиться на сумму документа прихода");
|
||||
}
|
||||
|
||||
@Test
|
||||
void expenseDecreasesAccountBalance() {
|
||||
// Given
|
||||
Ledger ledger = new Ledger();
|
||||
Account card = new Account(1L, "Карта");
|
||||
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||
|
||||
ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000)));
|
||||
|
||||
Operation expense = Operation.expense(date, 102L, card);
|
||||
OperationLine operationLine1 = new OperationLine(new Category(1L, "Mineral water"), new BigDecimal(100));
|
||||
expense.addLine(operationLine1);
|
||||
OperationLine operationLine2 = new OperationLine(new Category(2L, "Pizza"), new BigDecimal(200));
|
||||
expense.addLine(operationLine2);
|
||||
|
||||
// When
|
||||
ledger.post(expense);
|
||||
|
||||
// Then
|
||||
BigDecimal actual = ledger.balance(card, date);
|
||||
|
||||
assertEquals(0, new BigDecimal(14700).compareTo(actual),
|
||||
"Остаток счёта должен уменьшиться на сумму документа расхода");
|
||||
}
|
||||
|
||||
@Test
|
||||
void transferMovesAmountBetweenAccounts() {
|
||||
// Given
|
||||
Ledger ledger = new Ledger();
|
||||
Account card = new Account(1L, "Карта");
|
||||
Account cash = new Account(2L, "Наличные");
|
||||
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||
|
||||
ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000)));
|
||||
ledger.post(Operation.openingBalance(date, 102L, cash, new BigDecimal(5000)));
|
||||
|
||||
Operation transfer = Operation.transfer(date, 103L, card, cash, new BigDecimal(3000));
|
||||
|
||||
// When
|
||||
ledger.post(transfer);
|
||||
|
||||
// Then
|
||||
BigDecimal actualCash = ledger.balance(cash, date);
|
||||
BigDecimal actualCard = ledger.balance(card, date);
|
||||
|
||||
assertEquals(0, new BigDecimal(8000).compareTo(actualCash),
|
||||
"Остаток наличных должен увеличиться на сумму документа перемещения");
|
||||
assertEquals(0, new BigDecimal(12000).compareTo(actualCard),
|
||||
"Остаток на карте должен уменьшиться на сумму документа перемещения");
|
||||
}
|
||||
|
||||
@Test
|
||||
void postingSameOpeningBalanceDoesNotChangeBalanceAgain() {
|
||||
// Given
|
||||
Ledger ledger = new Ledger();
|
||||
Account card = new Account(1L, "Карта");
|
||||
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||
|
||||
Operation openingBalance = Operation.openingBalance(date, 101L, card, new BigDecimal(15000));
|
||||
|
||||
// When
|
||||
ledger.post(openingBalance);
|
||||
ledger.post(openingBalance);
|
||||
|
||||
// Then
|
||||
BigDecimal actual = ledger.balance(card, date);
|
||||
|
||||
assertEquals(0, new BigDecimal(15000).compareTo(actual),
|
||||
"Остаток счёта должен совпадать с начальным остатком независимо от количества проведений документа");
|
||||
}
|
||||
|
||||
@Test
|
||||
void postingSameIncomeTwiceDoesNotChangeBalanceAgain() {
|
||||
// Given
|
||||
Ledger ledger = new Ledger();
|
||||
Account card = new Account(1L, "Карта");
|
||||
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||
|
||||
ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000)));
|
||||
|
||||
Operation income = Operation.income(date, 102L, card);
|
||||
OperationLine operationLine1 = new OperationLine(new Category(1L, "Salary"), new BigDecimal(2000));
|
||||
income.addLine(operationLine1);
|
||||
OperationLine operationLine2 = new OperationLine(new Category(2L, "Present"), new BigDecimal(500));
|
||||
income.addLine(operationLine2);
|
||||
|
||||
// When
|
||||
ledger.post(income);
|
||||
ledger.post(income);
|
||||
|
||||
// Then
|
||||
BigDecimal actual = ledger.balance(card, date);
|
||||
|
||||
assertEquals(0, new BigDecimal(17500).compareTo(actual),
|
||||
"Остаток счёта должен увеличиться на сумму документа прихода единожды");
|
||||
}
|
||||
|
||||
@Test
|
||||
void postingSameExpenseTwiceDoesNotChangeBalanceAgain() {
|
||||
// Given
|
||||
Ledger ledger = new Ledger();
|
||||
Account card = new Account(1L, "Карта");
|
||||
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||
|
||||
ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000)));
|
||||
|
||||
Operation expense = Operation.expense(date, 102L, card);
|
||||
OperationLine operationLine1 = new OperationLine(new Category(1L, "Mineral water"), new BigDecimal(100));
|
||||
expense.addLine(operationLine1);
|
||||
OperationLine operationLine2 = new OperationLine(new Category(2L, "Pizza"), new BigDecimal(200));
|
||||
expense.addLine(operationLine2);
|
||||
|
||||
// When
|
||||
ledger.post(expense);
|
||||
ledger.post(expense);
|
||||
|
||||
// Then
|
||||
BigDecimal actual = ledger.balance(card, date);
|
||||
|
||||
assertEquals(0, new BigDecimal(14700).compareTo(actual),
|
||||
"Остаток счёта должен уменьшиться на сумму документа расхода единожды");
|
||||
}
|
||||
|
||||
@Test
|
||||
void postingSameTransferTwiceDoesNotChangeBalanceAgain() {
|
||||
// Given
|
||||
Ledger ledger = new Ledger();
|
||||
Account card = new Account(1L, "Карта");
|
||||
Account cash = new Account(2L, "Наличные");
|
||||
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||
|
||||
ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000)));
|
||||
ledger.post(Operation.openingBalance(date, 102L, cash, new BigDecimal(5000)));
|
||||
|
||||
Operation transfer = Operation.transfer(date, 103L, card, cash, new BigDecimal(3000));
|
||||
|
||||
// When
|
||||
ledger.post(transfer);
|
||||
ledger.post(transfer);
|
||||
|
||||
// Then
|
||||
BigDecimal actualCash = ledger.balance(cash, date);
|
||||
BigDecimal actualCard = ledger.balance(card, date);
|
||||
|
||||
assertEquals(0, new BigDecimal(8000).compareTo(actualCash),
|
||||
"Остаток наличных должен увеличиться на сумму документа перемещения единожды");
|
||||
assertEquals(0, new BigDecimal(12000).compareTo(actualCard),
|
||||
"Остаток на карте должен уменьшиться на сумму документа перемещения единожды");
|
||||
}
|
||||
|
||||
@Test
|
||||
void changingLineAmountDoesNotAffectBalanceBeforeReposting() {
|
||||
// Given
|
||||
Ledger ledger = new Ledger();
|
||||
Account card = new Account(1L, "Карта");
|
||||
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||
|
||||
ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000)));
|
||||
|
||||
Operation expense = Operation.expense(date, 102L, card);
|
||||
OperationLine operationLine1 = new OperationLine(new Category(1L, "Mineral water"), new BigDecimal(100));
|
||||
expense.addLine(operationLine1);
|
||||
OperationLine operationLine2 = new OperationLine(new Category(2L, "Pizza"), new BigDecimal(200));
|
||||
expense.addLine(operationLine2);
|
||||
ledger.post(expense);
|
||||
|
||||
// When
|
||||
expense.replaceLine(1, new OperationLine(new Category(2L, "Pizza"), new BigDecimal(250)));
|
||||
|
||||
// Then
|
||||
BigDecimal actual = ledger.balance(card, date);
|
||||
|
||||
assertEquals(0, new BigDecimal(14700).compareTo(actual),
|
||||
"Остаток счёта не меняется если изменённый документ не проведён");
|
||||
}
|
||||
|
||||
@Test
|
||||
void repostingOperationAfterLineAmountChangeUpdatesBalance() {
|
||||
// Given
|
||||
Ledger ledger = new Ledger();
|
||||
Account card = new Account(1L, "Карта");
|
||||
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||
|
||||
ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000)));
|
||||
|
||||
Operation expense = Operation.expense(date, 102L, card);
|
||||
OperationLine operationLine1 = new OperationLine(new Category(1L, "Mineral water"), new BigDecimal(100));
|
||||
expense.addLine(operationLine1);
|
||||
OperationLine operationLine2 = new OperationLine(new Category(2L, "Pizza"), new BigDecimal(200));
|
||||
expense.addLine(operationLine2);
|
||||
ledger.post(expense);
|
||||
expense.replaceLine(1, new OperationLine(new Category(2L, "Pizza"), new BigDecimal(250)));
|
||||
|
||||
// When
|
||||
ledger.post(expense);
|
||||
|
||||
// Then
|
||||
BigDecimal actual = ledger.balance(card, date);
|
||||
|
||||
assertEquals(0, new BigDecimal(14650).compareTo(actual),
|
||||
"Остаток счёта меняется в соответствии с новыми значениями в изменённом и проведённом документе");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user