use of com.ichi2.libanki.importer.AnkiPackageImporter in project AnkiChinaAndroid by ankichinateam.
the class ImportTest method testDupeIgnore.
/**
* Custom tests for AnkiDroid.
*/
@Test
public void testDupeIgnore() throws IOException, ImportExportException {
// create a new empty deck
String tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "update1.apkg");
AnkiPackageImporter imp = new AnkiPackageImporter(testCol, tmp);
imp.run();
tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "update3.apkg");
imp = new AnkiPackageImporter(testCol, tmp);
imp.run();
// there is a dupe, but it was ignored
assertEquals(1, imp.getDupes());
assertEquals(0, imp.getAdded());
assertEquals(0, imp.getUpdated());
}
use of com.ichi2.libanki.importer.AnkiPackageImporter in project AnkiChinaAndroid by ankichinateam.
the class ImportTest method testAnki2Updates.
@Test
public void testAnki2Updates() throws IOException, ImportExportException {
// create a new empty deck
String tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "update1.apkg");
AnkiPackageImporter imp = new AnkiPackageImporter(testCol, tmp);
imp.run();
assertEquals(0, imp.getDupes());
assertEquals(1, imp.getAdded());
assertEquals(0, imp.getUpdated());
// importing again should be idempotent
imp = new AnkiPackageImporter(testCol, tmp);
imp.run();
assertEquals(1, imp.getDupes());
assertEquals(0, imp.getAdded());
assertEquals(0, imp.getUpdated());
// importing a newer note should update
assertEquals(1, testCol.noteCount());
assertTrue(testCol.getDb().queryString("select flds from notes").startsWith("hello"));
tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "update2.apkg");
imp = new AnkiPackageImporter(testCol, tmp);
imp.run();
assertEquals(1, imp.getDupes());
assertEquals(0, imp.getAdded());
assertEquals(1, imp.getUpdated());
assertTrue(testCol.getDb().queryString("select flds from notes").startsWith("goodbye"));
}
use of com.ichi2.libanki.importer.AnkiPackageImporter in project AnkiChinaAndroid by ankichinateam.
the class CollectionTask method doInBackgroundImportAdd.
private TaskData doInBackgroundImportAdd(TaskData param) {
Timber.d("doInBackgroundImportAdd");
Resources res = AnkiDroidApp.getInstance().getBaseContext().getResources();
Collection col = getCol();
String path = param.getString();
AnkiPackageImporter imp = new AnkiPackageImporter(col, path);
imp.setProgressCallback(new ProgressCallback(this, res));
try {
imp.run();
} catch (ImportExportException e) {
return new TaskData(e.getMessage(), true);
}
return new TaskData(new Object[] { imp });
}
use of com.ichi2.libanki.importer.AnkiPackageImporter in project Anki-Android by ankidroid.
the class ImportTest method testApkg.
@Test
public void testApkg() throws IOException, ImportExportException {
String apkg = Shared.getTestFilePath(getTestContext(), "media.apkg");
Importer imp = new AnkiPackageImporter(mTestCol, apkg);
List<String> expected = Collections.emptyList();
List<String> actual = Arrays.asList(new File(mTestCol.getMedia().dir()).list());
actual.retainAll(expected);
assertEquals(actual.size(), expected.size());
imp.run();
expected = Collections.singletonList("foo.wav");
actual = Arrays.asList(new File(mTestCol.getMedia().dir()).list());
actual.retainAll(expected);
assertEquals(expected.size(), actual.size());
// import again should be idempotent in terms of media
mTestCol.remCards(mTestCol.getDb().queryLongList("select id from cards"));
imp = new AnkiPackageImporter(mTestCol, apkg);
imp.run();
expected = Collections.singletonList("foo.wav");
actual = Arrays.asList(new File(mTestCol.getMedia().dir()).list());
actual.retainAll(expected);
assertEquals(expected.size(), actual.size());
// but if the local file has different data, it will rename
mTestCol.remCards(mTestCol.getDb().queryLongList("select id from cards"));
FileOutputStream os = new FileOutputStream(new File(mTestCol.getMedia().dir(), "foo.wav"), false);
os.write("xyz".getBytes());
os.close();
imp = new AnkiPackageImporter(mTestCol, apkg);
imp.run();
assertEquals(2, new File(mTestCol.getMedia().dir()).list().length);
}
use of com.ichi2.libanki.importer.AnkiPackageImporter in project AnkiChinaAndroid by ankichinateam.
the class ImportTest method testAnki2DiffmodelTemplates.
@Test
public void testAnki2DiffmodelTemplates() throws IOException, JSONException, ImportExportException {
// different from the above as this one tests only the template text being
// changed, not the number of cards/fields
// import the first version of the model
String tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "diffmodeltemplates-1.apkg");
AnkiPackageImporter imp = new AnkiPackageImporter(testCol, tmp);
imp.setDupeOnSchemaChange(true);
imp.run();
// then the version with updated template
tmp = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "diffmodeltemplates-2.apkg");
imp = new AnkiPackageImporter(testCol, tmp);
imp.setDupeOnSchemaChange(true);
imp.run();
// collection should contain the note we imported
assertEquals(1, testCol.noteCount());
// the front template should contain the text added in the 2nd package
Long tcid = testCol.findCards("").get(0);
Note tnote = testCol.getCard(tcid).note();
assertTrue(testCol.findTemplates(tnote).get(0).getString("qfmt").contains("Changed Front Template"));
}
Aggregations