Search in sources :

Example 1 with AnkiPackageImporter

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());
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) Test(org.junit.Test)

Example 2 with AnkiPackageImporter

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"));
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) Test(org.junit.Test)

Example 3 with AnkiPackageImporter

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 });
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) ImportExportException(com.ichi2.anki.exception.ImportExportException) Collection(com.ichi2.libanki.Collection) Resources(android.content.res.Resources)

Example 4 with AnkiPackageImporter

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);
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Anki2Importer(com.ichi2.libanki.importer.Anki2Importer) Importer(com.ichi2.libanki.importer.Importer) AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) NoteImporter(com.ichi2.libanki.importer.NoteImporter) TextImporter(com.ichi2.libanki.importer.TextImporter) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest)

Example 5 with AnkiPackageImporter

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"));
}
Also used : AnkiPackageImporter(com.ichi2.libanki.importer.AnkiPackageImporter) Note(com.ichi2.libanki.Note) Test(org.junit.Test)

Aggregations

AnkiPackageImporter (com.ichi2.libanki.importer.AnkiPackageImporter)9 Test (org.junit.Test)8 InstrumentedTest (com.ichi2.anki.tests.InstrumentedTest)4 File (java.io.File)3 Collection (com.ichi2.libanki.Collection)2 Note (com.ichi2.libanki.Note)2 Anki2Importer (com.ichi2.libanki.importer.Anki2Importer)2 Importer (com.ichi2.libanki.importer.Importer)2 NoteImporter (com.ichi2.libanki.importer.NoteImporter)2 TextImporter (com.ichi2.libanki.importer.TextImporter)2 FileOutputStream (java.io.FileOutputStream)2 Resources (android.content.res.Resources)1 JsonReader (com.google.gson.stream.JsonReader)1 ImportExportException (com.ichi2.anki.exception.ImportExportException)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)1