Skip to content

Commit 880aab7

Browse files
committed
Don't zip QIF exports when it results in only one exported file
Fixes #759
1 parent 131c534 commit 880aab7

3 files changed

Lines changed: 36 additions & 25 deletions

File tree

app/src/main/java/org/gnucash/android/export/qif/QifExporter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,14 @@ public List<String> generateExport() throws ExporterException {
242242

243243
/// export successful
244244
PreferencesHelper.setLastExportTime(TimestampHelper.getTimestampFromNow());
245+
245246
List<String> exportedFiles = splitQIF(file);
246247
if (exportedFiles.isEmpty())
247248
return Collections.emptyList();
248-
else
249+
else if (exportedFiles.size() > 1)
249250
return zipQifs(exportedFiles);
251+
else
252+
return exportedFiles;
250253
} catch (IOException e) {
251254
throw new ExporterException(mExportParams, e);
252255
}

app/src/test/java/org/gnucash/android/test/unit/export/QifExporterTest.java

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.gnucash.android.test.unit.export;
1717

1818
import android.database.sqlite.SQLiteDatabase;
19+
import android.support.annotation.NonNull;
1920

2021
import org.gnucash.android.app.GnuCashApplication;
2122
import org.gnucash.android.db.BookDbHelper;
@@ -45,6 +46,7 @@
4546
import java.io.FileReader;
4647
import java.io.IOException;
4748
import java.util.List;
49+
import java.util.zip.ZipFile;
4850

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

@@ -83,7 +85,7 @@ public void testWithNoTransactionsToExport_shouldNotCreateAnyFile(){
8385
/**
8486
* Test that QIF files are generated
8587
*/
86-
//// FIXME: 20.04.2017 Test failing with NPE
88+
@Test
8789
public void testGenerateQIFExport(){
8890
AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(mDb);
8991

@@ -109,10 +111,11 @@ public void testGenerateQIFExport(){
109111
}
110112

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

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

142-
assertThat(exportedFiles).hasSize(2);
145+
assertThat(exportedFiles).hasSize(1);
143146
File file = new File(exportedFiles.get(0));
144-
assertThat(file).exists().hasExtension("qif");
145-
assertThat(file.length()).isGreaterThan(0L);
147+
assertThat(file).exists().hasExtension("zip");
148+
assertThat(new ZipFile(file).size()).isEqualTo(2);
146149
}
147150

148-
//@Test
149-
public void description_and_memo_field_test() {
150-
// arrange
151-
151+
/**
152+
* Test that the memo and description fields of transactions are exported.
153+
*/
154+
@Test
155+
public void memoAndDescription_shouldBeExported() throws IOException {
152156
String expectedDescription = "my description";
153157
String expectedMemo = "my memo";
154158

155159
AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(mDb);
160+
156161
Account account = new Account("Basic Account");
157162
Transaction transaction = new Transaction("One transaction");
163+
transaction.addSplit(new Split(Money.createZeroInstance("EUR"), account.getUID()));
158164
transaction.setDescription(expectedDescription);
159165
transaction.setNote(expectedMemo);
160166
account.addTransaction(transaction);
167+
161168
accountsDbAdapter.addRecord(account);
162169

163170
ExportParams exportParameters = new ExportParams(ExportFormat.QIF);
164171
exportParameters.setExportStartTime(TimestampHelper.getTimestampFromEpochZero());
165172
exportParameters.setExportTarget(ExportParams.ExportTarget.SD_CARD);
166173
exportParameters.setDeleteTransactionsAfterExport(false);
167174

168-
// act
169-
170175
QifExporter qifExporter = new QifExporter(exportParameters, mDb);
171176
List<String> exportedFiles = qifExporter.generateExport();
172177

173-
// assert
174-
175178
assertThat(exportedFiles).hasSize(1);
176179
File file = new File(exportedFiles.get(0));
180+
String fileContent = readFileContent(file);
177181
assertThat(file).exists().hasExtension("qif");
178-
StringBuilder fileContentsBuilder = new StringBuilder();
179-
try {
180-
BufferedReader reader = new BufferedReader(new FileReader(file));
181-
fileContentsBuilder.append(reader.readLine());
182-
} catch (IOException e) {
183-
e.printStackTrace();
184-
}
185-
// todo: check the description & memo fields.
186-
String fileContent = fileContentsBuilder.toString();
187182
assertThat(fileContent.contains(expectedDescription));
188183
assertThat(fileContent.contains(expectedMemo));
189184
}
185+
186+
@NonNull
187+
public String readFileContent(File file) throws IOException {
188+
StringBuilder fileContentsBuilder = new StringBuilder();
189+
BufferedReader reader = new BufferedReader(new FileReader(file));
190+
String line;
191+
while ((line = reader.readLine()) != null) {
192+
fileContentsBuilder.append(line).append('\n');
193+
}
194+
195+
return fileContentsBuilder.toString();
196+
}
190197
}

app/src/test/java/org/gnucash/android/test/unit/service/ScheduledActionServiceTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ public void scheduledBackups_shouldIncludeTransactionsAfterTheLastRun() {
481481
assertThat(scheduledBackup.getExecutionCount()).isEqualTo(2);
482482
assertThat(scheduledBackup.getLastRunTime()).isGreaterThan(previousLastRun);
483483
assertThat(backupFolder.listFiles()).hasSize(1);
484+
assertThat(backupFolder.listFiles()[0].getName()).endsWith(".qif");
484485
}
485486

486487
@After

0 commit comments

Comments
 (0)