Skip to content

Commit

Permalink
Don't zip QIF exports when it results in only one exported file
Browse files Browse the repository at this point in the history
  • Loading branch information
rivaldi8 committed Feb 27, 2018
1 parent 131c534 commit 880aab7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,14 @@ public List<String> generateExport() throws ExporterException {

/// export successful
PreferencesHelper.setLastExportTime(TimestampHelper.getTimestampFromNow());

List<String> exportedFiles = splitQIF(file);
if (exportedFiles.isEmpty())
return Collections.emptyList();
else
else if (exportedFiles.size() > 1)
return zipQifs(exportedFiles);
else
return exportedFiles;
} catch (IOException e) {
throw new ExporterException(mExportParams, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.gnucash.android.test.unit.export;

import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull;

import org.gnucash.android.app.GnuCashApplication;
import org.gnucash.android.db.BookDbHelper;
Expand Down Expand Up @@ -45,6 +46,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipFile;

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

Expand Down Expand Up @@ -83,7 +85,7 @@ public void testWithNoTransactionsToExport_shouldNotCreateAnyFile(){
/**
* Test that QIF files are generated
*/
//// FIXME: 20.04.2017 Test failing with NPE
@Test
public void testGenerateQIFExport(){
AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(mDb);

Expand All @@ -109,10 +111,11 @@ public void testGenerateQIFExport(){
}

/**
* Test that when more than one currency is in use, multiple QIF files will be generated
* Test that when more than one currency is in use, a zip with multiple QIF files
* will be generated
*/
//// FIXME: 20.04.2017 test failing with NPE
public void multiCurrencyTransactions_shouldResultInMultipleQifFiles(){
// @Test Fails randomly. Sometimes it doesn't split the QIF.
public void multiCurrencyTransactions_shouldResultInMultipleZippedQifFiles() throws IOException {
AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(mDb);

Account account = new Account("Basic Account", Commodity.getInstance("EUR"));
Expand All @@ -139,52 +142,56 @@ public void multiCurrencyTransactions_shouldResultInMultipleQifFiles(){
QifExporter qifExporter = new QifExporter(exportParameters, mDb);
List<String> exportedFiles = qifExporter.generateExport();

assertThat(exportedFiles).hasSize(2);
assertThat(exportedFiles).hasSize(1);
File file = new File(exportedFiles.get(0));
assertThat(file).exists().hasExtension("qif");
assertThat(file.length()).isGreaterThan(0L);
assertThat(file).exists().hasExtension("zip");
assertThat(new ZipFile(file).size()).isEqualTo(2);
}

//@Test
public void description_and_memo_field_test() {
// arrange

/**
* Test that the memo and description fields of transactions are exported.
*/
@Test
public void memoAndDescription_shouldBeExported() throws IOException {
String expectedDescription = "my description";
String expectedMemo = "my memo";

AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(mDb);

Account account = new Account("Basic Account");
Transaction transaction = new Transaction("One transaction");
transaction.addSplit(new Split(Money.createZeroInstance("EUR"), account.getUID()));
transaction.setDescription(expectedDescription);
transaction.setNote(expectedMemo);
account.addTransaction(transaction);

accountsDbAdapter.addRecord(account);

ExportParams exportParameters = new ExportParams(ExportFormat.QIF);
exportParameters.setExportStartTime(TimestampHelper.getTimestampFromEpochZero());
exportParameters.setExportTarget(ExportParams.ExportTarget.SD_CARD);
exportParameters.setDeleteTransactionsAfterExport(false);

// act

QifExporter qifExporter = new QifExporter(exportParameters, mDb);
List<String> exportedFiles = qifExporter.generateExport();

// assert

assertThat(exportedFiles).hasSize(1);
File file = new File(exportedFiles.get(0));
String fileContent = readFileContent(file);
assertThat(file).exists().hasExtension("qif");
StringBuilder fileContentsBuilder = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
fileContentsBuilder.append(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
// todo: check the description & memo fields.
String fileContent = fileContentsBuilder.toString();
assertThat(fileContent.contains(expectedDescription));
assertThat(fileContent.contains(expectedMemo));
}

@NonNull
public String readFileContent(File file) throws IOException {
StringBuilder fileContentsBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
fileContentsBuilder.append(line).append('\n');
}

return fileContentsBuilder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ public void scheduledBackups_shouldIncludeTransactionsAfterTheLastRun() {
assertThat(scheduledBackup.getExecutionCount()).isEqualTo(2);
assertThat(scheduledBackup.getLastRunTime()).isGreaterThan(previousLastRun);
assertThat(backupFolder.listFiles()).hasSize(1);
assertThat(backupFolder.listFiles()[0].getName()).endsWith(".qif");
}

@After
Expand Down

0 comments on commit 880aab7

Please sign in to comment.