Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#42: implemented createCsvHeader; extracted CSV_FIELD_DELIMITER #5

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/src/androidTest/assets/test-export.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
account;category;currency;amount;ref_currency_amount;type;payment_type;payment_type_local;note;date;gps_latitude;gps_longitude;gps_accuracy_in_meters;warranty_in_month;transfer;payee;labels;envelope_id;custom_category
Cash;Gifts;EUR;190.00;190.00;Income;CASH;Cash;Yay, such a big gift;2023-01-30 09:38:17;;;;0;false;;;10010;false
Cash;Life events;EUR;-15.00;-15.00;Expenses;TRANSFER;Bank transfer;Ein Beispiel;2023-01-30 09:37:05;;;;0;false;;;6004;false
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ License, or (at your option) any later version.

package org.secuso.privacyfriendlyfinance.csv;

import androidx.annotation.NonNull;

import org.joda.time.LocalDate;
import org.secuso.privacyfriendlyfinance.domain.model.Transaction;
import org.secuso.privacyfriendlyfinance.domain.model.common.Id2Name;
import org.secuso.privacyfriendlyfinance.domain.model.common.NameWithIdDto;

import java.util.Arrays;

/**
* converts (list of) {@link Transaction} items to csv format used for export.
*/
public class CsvExporter {
public static final String CSV_FIELD_DELIMITER = ";";
private final Id2Name<?> id2Category;
private final Id2Name<?> id2Account;

Expand All @@ -38,6 +39,17 @@ public CsvExporter(Id2Name<?> id2Category, Id2Name<?> id2Account) {

public String toCsv(Transaction transaction) {

return transaction.getDate() + "," + transaction.getAmount() + "," + transaction.getName() + "," + id2Category.get(transaction.getCategoryId()) + "," + id2Account.get(transaction.getAccountId());
return toCsv(transaction.getDate(), transaction.getAmount(), transaction.getName(),
id2Category.get(transaction.getCategoryId()), id2Account.get(transaction.getAccountId()));
}

public String createCsvHeader() {
return toCsv("date","amount","note","category","account");
}

@NonNull
private String toCsv(Object date, Object amount, Object comment, Object categoryName, Object accountName) {
return date + CSV_FIELD_DELIMITER + amount + CSV_FIELD_DELIMITER + comment
+ CSV_FIELD_DELIMITER + categoryName + CSV_FIELD_DELIMITER + accountName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public void tearDown() throws Exception {

@Test
public void toCsv() {
// date,amount,transaction-name,category-name,account-name
String expected = "1999-12-31,10000,My Test Transaction,my test category,my test account";
// date,amount,note,category,account
String expected = "1999-12-31;10000;My Test Transaction;my test category;my test account";
Id2Name<NameWithIdDto> id2Account = new Id2Name<>(Arrays.asList(new NameWithIdDto("my test account", 12345L)));
Id2Name<NameWithIdDto> id2Category = new Id2Name<>(Arrays.asList(new NameWithIdDto("my test category", 54321L)));

Expand All @@ -56,7 +56,18 @@ public void toCsv() {
"My Test Transaction",10000, LocalDate.parse("1999-12-31"),
12345L, 54321L);

String csvLine = exporter.toCsv(transaction).toString();
String csvLine = exporter.toCsv(transaction);

assertEquals(expected, csvLine);
}

@Test
public void createCsvHeader() {
String expected = "date;amount;note;category;account";

CsvExporter exporter = new CsvExporter(null, null);

String csvLine = exporter.createCsvHeader();

assertEquals(expected, csvLine);
}
Expand Down