Добавил тесты хранения операций, отбора по датам и сортировки журнала
This commit is contained in:
1 parent
8af851a49a
commit
3a81f79e7b
2 files changed
+276
No files matched your search
@@ -0,0 +1,159 @@
|
|||||||
|
package model;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class OperationJournalTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void savedOperationCanBeFoundById() {
|
||||||
|
// Given
|
||||||
|
OperationJournal operationJournal = new OperationJournal();
|
||||||
|
|
||||||
|
Account card = new Account(1L, "Карта");
|
||||||
|
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||||
|
Operation operation = Operation.openingBalance(date, 101L, card, new BigDecimal(15000));
|
||||||
|
operationJournal.saveOperation(operation);
|
||||||
|
|
||||||
|
// When
|
||||||
|
Operation actualOperation = operationJournal.getOperationById(101L);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertSame(operation, actualOperation, "Журнал должен возвращать сохранённую операцию");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void savingOperationWithSameIdReplacesPreviousVersion() {
|
||||||
|
// Given
|
||||||
|
OperationJournal operationJournal = new OperationJournal();
|
||||||
|
|
||||||
|
Account card = new Account(1L, "Карта");
|
||||||
|
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||||
|
Operation operation1 = Operation.openingBalance(date, 101L, card, new BigDecimal(15000));
|
||||||
|
operationJournal.saveOperation(operation1);
|
||||||
|
Operation updatedOperation = Operation.openingBalance(date, 101L, card, new BigDecimal(25000));
|
||||||
|
|
||||||
|
// When
|
||||||
|
operationJournal.saveOperation(updatedOperation);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertSame(updatedOperation, operationJournal.getOperationById(101L), "Операция с одинаковым идентификатором должна заменять существующую");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findingUnknownOperationReturnsNull() {
|
||||||
|
// Given
|
||||||
|
OperationJournal operationJournal = new OperationJournal();
|
||||||
|
|
||||||
|
Account card = new Account(1L, "Карта");
|
||||||
|
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||||
|
Operation operation1 = Operation.openingBalance(date, 101L, card, new BigDecimal(15000));
|
||||||
|
operationJournal.saveOperation(operation1);
|
||||||
|
|
||||||
|
// When
|
||||||
|
Operation unknownOperation = operationJournal.getOperationById(999L);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertNull(unknownOperation,
|
||||||
|
"Поиск операции с несуществующим идентификатором должен возвращать null");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findingOperationsBetweenDatesIncludesBothBoundaries() {
|
||||||
|
// Given
|
||||||
|
OperationJournal operationJournal = new OperationJournal();
|
||||||
|
|
||||||
|
Account card = new Account(1L, "Карта");
|
||||||
|
Account cash = new Account(2L, "Наличные");
|
||||||
|
LocalDate dateBefore = LocalDate.of(2026, 10, 16);
|
||||||
|
LocalDate dateFrom = LocalDate.of(2026, 10, 17);
|
||||||
|
LocalDate dateTo = LocalDate.of(2026, 10, 18);
|
||||||
|
LocalDate dateAfter = LocalDate.of(2026, 10, 19);
|
||||||
|
|
||||||
|
Operation openingBalance = Operation.openingBalance(dateBefore, 101L, card, new BigDecimal(15000));
|
||||||
|
operationJournal.saveOperation(openingBalance);
|
||||||
|
|
||||||
|
Operation transfer = Operation.transfer(dateFrom, 102L, card, cash, new BigDecimal(15000));
|
||||||
|
operationJournal.saveOperation(transfer);
|
||||||
|
|
||||||
|
Operation expense1 = Operation.expense(dateTo, 103L, card);
|
||||||
|
operationJournal.saveOperation(expense1);
|
||||||
|
|
||||||
|
Operation expense2 = Operation.expense(dateAfter, 104L, card);
|
||||||
|
operationJournal.saveOperation(expense2);
|
||||||
|
|
||||||
|
// When
|
||||||
|
List<Operation> operationsBetweenDates = operationJournal.getOperationsBetweenDates(dateFrom, dateTo);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertIterableEquals(List.of(transfer, expense1), operationsBetweenDates,
|
||||||
|
"Список должен содержать операции за выбранный период");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void operationsBetweenDatesAreSortedByDateThenId() {
|
||||||
|
// Given
|
||||||
|
OperationJournal operationJournal = new OperationJournal();
|
||||||
|
|
||||||
|
Account card = new Account(1L, "Карта");
|
||||||
|
Account cash = new Account(2L, "Наличные");
|
||||||
|
LocalDate dateBefore = LocalDate.of(2026, 10, 16);
|
||||||
|
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||||
|
|
||||||
|
Operation openingBalance = Operation.openingBalance(date, 103L, card, new BigDecimal(15000));
|
||||||
|
operationJournal.saveOperation(openingBalance);
|
||||||
|
|
||||||
|
Operation transfer = Operation.transfer(date, 101L, card, cash, new BigDecimal(15000));
|
||||||
|
operationJournal.saveOperation(transfer);
|
||||||
|
|
||||||
|
Operation expense = Operation.expense(dateBefore, 102L, card);
|
||||||
|
operationJournal.saveOperation(expense);
|
||||||
|
|
||||||
|
// When
|
||||||
|
List<Operation> operationsBetweenDates = operationJournal.getOperationsBetweenDates(dateBefore, date);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertIterableEquals(List.of(expense, transfer, openingBalance), operationsBetweenDates,
|
||||||
|
"Операции должны сортироваться по дате, затем по ID");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findingOperationsWithReversedDateRangeThrowsException() {
|
||||||
|
// Given
|
||||||
|
OperationJournal operationJournal = new OperationJournal();
|
||||||
|
|
||||||
|
LocalDate dateBefore = LocalDate.of(2026, 10, 16);
|
||||||
|
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||||
|
|
||||||
|
// When / Then
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> operationJournal.getOperationsBetweenDates(date, dateBefore),
|
||||||
|
"В указанном периоде дата начала не может быть позже даты окончания");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findingOperationsWithNullStartDateThrowsException() {
|
||||||
|
// Given
|
||||||
|
OperationJournal operationJournal = new OperationJournal();
|
||||||
|
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||||
|
|
||||||
|
// When / Then
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> operationJournal.getOperationsBetweenDates(null, date),
|
||||||
|
"В указанном периоде дата начала не должна быть null");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void findingOperationsWithNullEndDateThrowsException() {
|
||||||
|
// Given
|
||||||
|
OperationJournal operationJournal = new OperationJournal();
|
||||||
|
LocalDate date = LocalDate.of(2026, 10, 16);
|
||||||
|
|
||||||
|
// When / Then
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> operationJournal.getOperationsBetweenDates(date, null),
|
||||||
|
"В указанном периоде дата окончания не должна быть null");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -259,4 +259,121 @@ class LedgerTest {
|
|||||||
assertEquals(0, new BigDecimal(14650).compareTo(actual),
|
assertEquals(0, new BigDecimal(14650).compareTo(actual),
|
||||||
"Остаток счёта меняется в соответствии с новыми значениями в изменённом и проведённом документе");
|
"Остаток счёта меняется в соответствии с новыми значениями в изменённом и проведённом документе");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void failedRepostingPreservesPreviousMovements() {
|
||||||
|
// 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.removeLine(1);
|
||||||
|
expense.removeLine(0);
|
||||||
|
|
||||||
|
// When / Then
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> ledger.post(expense));
|
||||||
|
|
||||||
|
BigDecimal actual = ledger.balance(card, date);
|
||||||
|
|
||||||
|
assertEquals(0, new BigDecimal(14700).compareTo(actual),
|
||||||
|
"При ошибке перепроведения прежний остаток должен сохраняться");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void unpostingTransferRestoresBothAccountBalances() {
|
||||||
|
// 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));
|
||||||
|
|
||||||
|
ledger.post(transfer);
|
||||||
|
|
||||||
|
// When
|
||||||
|
ledger.unpost(transfer.getId());
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertFalse(ledger.isPosted(transfer.getId()),
|
||||||
|
"После отмены проведения у перемещения не должно быть движений");
|
||||||
|
|
||||||
|
BigDecimal actualCash = ledger.balance(cash, date);
|
||||||
|
BigDecimal actualCard = ledger.balance(card, date);
|
||||||
|
|
||||||
|
assertEquals(0, new BigDecimal(5000).compareTo(actualCash),
|
||||||
|
"Остаток наличных должен восстановиться до начального");
|
||||||
|
assertEquals(0, new BigDecimal(15000).compareTo(actualCard),
|
||||||
|
"Остаток на карте должен восстановиться до начального");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void balanceIncludesMovementsOnDateAndExcludesLaterOnes() {
|
||||||
|
// Given
|
||||||
|
Ledger ledger = new Ledger();
|
||||||
|
Account card = new Account(1L, "Карта");
|
||||||
|
LocalDate date = LocalDate.of(2026, 10, 18);
|
||||||
|
LocalDate dateBefore = LocalDate.of(2026, 10, 17);
|
||||||
|
LocalDate dateAfter = LocalDate.of(2026, 10, 19);
|
||||||
|
|
||||||
|
ledger.post(Operation.openingBalance(dateBefore, 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);
|
||||||
|
|
||||||
|
Operation income = Operation.income(dateAfter, 103L, card);
|
||||||
|
operationLine1 = new OperationLine(new Category(3L, "Salary"), new BigDecimal(20000));
|
||||||
|
income.addLine(operationLine1);
|
||||||
|
ledger.post(income);
|
||||||
|
|
||||||
|
// When
|
||||||
|
BigDecimal actual = ledger.balance(card, date);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
assertEquals(0, new BigDecimal(14700).compareTo(actual),
|
||||||
|
"Остаток на дату должен включать движения до указанной даты включительно и не включать движения" +
|
||||||
|
" после указанной даты");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void movementsOnAnotherAccountDoNotAffectBalance() {
|
||||||
|
// 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(3000)));
|
||||||
|
|
||||||
|
Operation expense = Operation.expense(date, 103L, 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(cash, date);
|
||||||
|
|
||||||
|
assertEquals(0, new BigDecimal(3000).compareTo(actual),
|
||||||
|
"Движения по счёту карты не влияют на баланс наличных");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user