-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/enhancement/63-transactionarchiv…
…etestjava' merge manually
- Loading branch information
Showing
1 changed file
with
130 additions
and
3 deletions.
There are no files selected for viewing
133 changes: 130 additions & 3 deletions
133
src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/TransactionArchiveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,130 @@ | ||
| public class TransactionArchiveTest { | ||
|
|
||
| } | ||
| package edu.ntnu.idi.idatt2003.g40.mappe; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class TransactionArchiveTest { | ||
|
|
||
| private final TransactionCalculator calculator = new TransactionCalculator() { | ||
| @Override | ||
| public BigDecimal calculateGross() { | ||
| return BigDecimal.ZERO; | ||
| } | ||
|
|
||
| @Override | ||
| public BigDecimal calculateCommission() { | ||
| return BigDecimal.ZERO; | ||
| } | ||
|
|
||
| @Override | ||
| public BigDecimal calculateTax() { | ||
| return BigDecimal.ZERO; | ||
| } | ||
|
|
||
| @Override | ||
| public BigDecimal calculateTotal() { | ||
| return BigDecimal.ZERO; | ||
| } | ||
| }; | ||
|
|
||
| @Test | ||
| void newArchiveIsEmpty() { | ||
| TransactionArchive archive = new TransactionArchive(); | ||
|
|
||
| assertTrue(archive.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void addMakesArchiveNonEmpty() { | ||
| TransactionArchive archive = new TransactionArchive(); | ||
| Transaction transaction = createPurchase("AAPL", "Apple", 1); | ||
|
|
||
| boolean result = archive.add(transaction); | ||
|
|
||
| assertTrue(result); | ||
| assertFalse(archive.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void getTransactionsReturnsOnlyTransactionsFromGivenWeek() { | ||
| TransactionArchive archive = new TransactionArchive(); | ||
|
|
||
| Transaction transaction1 = createPurchase("AAPL", "Apple", 1); | ||
| Transaction transaction2 = createSale("TSLA", "Tesla", 2); | ||
| Transaction transaction3 = createPurchase("NVDA", "Nvidia", 1); | ||
|
|
||
| archive.add(transaction1); | ||
| archive.add(transaction2); | ||
| archive.add(transaction3); | ||
|
|
||
| List<Transaction> result = archive.getTransactions(1); | ||
|
|
||
| assertEquals(2, result.size()); | ||
| assertTrue(result.contains(transaction1)); | ||
| assertTrue(result.contains(transaction3)); | ||
| } | ||
|
|
||
| @Test | ||
| void getPurchasesReturnsOnlyPurchasesFromGivenWeek() { | ||
| TransactionArchive archive = new TransactionArchive(); | ||
|
|
||
| Purchase purchase1 = createPurchase("AAPL", "Apple", 1); | ||
| Purchase purchase2 = createPurchase("NVDA", "Nvidia", 2); | ||
| Sale sale = createSale("TSLA", "Tesla", 1); | ||
|
|
||
| archive.add(purchase1); | ||
| archive.add(purchase2); | ||
| archive.add(sale); | ||
|
|
||
| List<Purchase> result = archive.getPurchases(1); | ||
|
|
||
| assertEquals(1, result.size()); | ||
| assertTrue(result.contains(purchase1)); | ||
| } | ||
|
|
||
| @Test | ||
| void getSalesReturnsOnlySalesFromGivenWeek() { | ||
| TransactionArchive archive = new TransactionArchive(); | ||
|
|
||
| Sale sale1 = createSale("TSLA", "Tesla", 1); | ||
| Sale sale2 = createSale("NVDA", "Nvidia", 2); | ||
| Purchase purchase = createPurchase("AAPL", "Apple", 1); | ||
|
|
||
| archive.add(sale1); | ||
| archive.add(sale2); | ||
| archive.add(purchase); | ||
|
|
||
| List<Sale> result = archive.getSales(1); | ||
|
|
||
| assertEquals(1, result.size()); | ||
| assertTrue(result.contains(sale1)); | ||
| } | ||
|
|
||
| @Test | ||
| void countDistinctWeeksCountsUniqueWeeksOnly() { | ||
| TransactionArchive archive = new TransactionArchive(); | ||
|
|
||
| archive.add(createPurchase("AAPL", "Apple", 1)); | ||
| archive.add(createSale("TSLA", "Tesla", 1)); | ||
| archive.add(createPurchase("NVDA", "Nvidia", 2)); | ||
| archive.add(createSale("META", "Meta", 3)); | ||
|
|
||
| assertEquals(3, archive.countDistinctWeeks()); | ||
| } | ||
|
|
||
| private Purchase createPurchase(String symbol, String company, int week) { | ||
| Stock stock = new Stock(symbol, company, new BigDecimal("100")); | ||
| Share share = new Share(stock, BigDecimal.ONE, new BigDecimal("100")); | ||
| return new Purchase(share, week, calculator); | ||
| } | ||
|
|
||
| private Sale createSale(String symbol, String company, int week) { | ||
| Stock stock = new Stock(symbol, company, new BigDecimal("100")); | ||
| Share share = new Share(stock, BigDecimal.ONE, new BigDecimal("100")); | ||
| return new Sale(share, week, calculator); | ||
| } | ||
| } |