Skip to content

Commit

Permalink
#724 - added tests for CsvParser
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlocker8 committed Jan 21, 2023
1 parent 868f339 commit aeb8745
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.deadlocker8.budgetmaster.transactions.csvimport;

import java.util.List;
import java.util.Objects;

public class CsvRow
{
Expand All @@ -21,6 +22,21 @@ public List<String> getColumns()
return columns;
}

@Override
public boolean equals(Object o)
{
if(this == o) return true;
if(o == null || getClass() != o.getClass()) return false;
CsvRow csvRow = (CsvRow) o;
return Objects.equals(columns, csvRow.columns);
}

@Override
public int hashCode()
{
return Objects.hash(columns);
}

@Override
public String toString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,9 @@ void test_createCsvTransactionFromCsvRow() throws CsvTransactionParseException
@Test
void test_createCsvTransactionFromCsvRow_dateParseException()
{
final LocalDate date = LocalDate.of(2023, 1, 21);

final CsvRow csvRow = new CsvRow("21.01.2023", "Groceries", "-12.00", "dolor sit amet");
final CsvColumnSettings csvColumnSettings = new CsvColumnSettings(1, "dd.MM.", 2, 3, 4);

final CsvTransaction expectedCsvTransaction = new CsvTransaction(date, "Groceries", -1200, "dolor sit amet", CsvTransactionStatus.PENDING, CATEGORY_NONE);

final Settings settings = new Settings();
settings.setLanguage(LanguageType.ENGLISH);
Mockito.when(settingsService.getSettings()).thenReturn(settings);
Expand All @@ -148,13 +144,9 @@ void test_createCsvTransactionFromCsvRow_dateParseException()
@Test
void test_createCsvTransactionFromCsvRow_amountParseException()
{
final LocalDate date = LocalDate.of(2023, 1, 21);

final CsvRow csvRow = new CsvRow("21.01.2023", "Groceries", "non_amount", "dolor sit amet");
final CsvColumnSettings csvColumnSettings = new CsvColumnSettings(1, "dd.MM.yyyy", 2, 3, 4);

final CsvTransaction expectedCsvTransaction = new CsvTransaction(date, "Groceries", -1200, "dolor sit amet", CsvTransactionStatus.PENDING, CATEGORY_NONE);

final Settings settings = new Settings();
settings.setLanguage(LanguageType.ENGLISH);
Mockito.when(settingsService.getSettings()).thenReturn(settings);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package de.deadlocker8.budgetmaster.unit.transaction.csvimport;

import com.opencsv.exceptions.CsvValidationException;
import de.deadlocker8.budgetmaster.transactions.csvimport.CsvParser;
import de.deadlocker8.budgetmaster.transactions.csvimport.CsvRow;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;

class CsvParserTest
{
private static final String VALID_CSV = "Date;Title;Amount\n" +
"03.01.2023;Lorem;50.00\n" +
"05.01.2023;Ipsum;-8.37\n" +
"08.01.2023;dolor sit amet;-12.00";

@Test
void test_parseCsv_emptyFile() throws CsvValidationException, IOException
{
assertThat(CsvParser.parseCsv("", ';', 0))
.isEmpty();
}

@Test
void test_parseCsv_separatorNotPresent() throws CsvValidationException, IOException
{
assertThat(CsvParser.parseCsv("abc,17", ';', 0))
.containsExactly(new CsvRow("abc,17"));
}

@Test
void test_parseCsv() throws CsvValidationException, IOException
{
assertThat(CsvParser.parseCsv(VALID_CSV, ';', 0)).
containsExactly(new CsvRow("Date", "Title", "Amount"),
new CsvRow("03.01.2023", "Lorem", "50.00"),
new CsvRow("05.01.2023", "Ipsum", "-8.37"),
new CsvRow("08.01.2023", "dolor sit amet", "-12.00"));
}

@Test
void test_parseCsv_skipLines() throws CsvValidationException, IOException
{
assertThat(CsvParser.parseCsv(VALID_CSV, ';', 2)).
containsExactly(new CsvRow("05.01.2023", "Ipsum", "-8.37"),
new CsvRow("08.01.2023", "dolor sit amet", "-12.00"));
}

@Test
void test_parseCsv_skipLines_moreThanExisting() throws CsvValidationException, IOException
{
assertThat(CsvParser.parseCsv(VALID_CSV, ';', 999))
.isEmpty();
}
}

0 comments on commit aeb8745

Please sign in to comment.