Search in sources :

Example 11 with TextImporter

use of com.ichi2.libanki.importer.TextImporter in project Anki-Android by ankidroid.

the class ImportTest method csvManualLineBreakExample.

@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
public void csvManualLineBreakExample() throws IOException {
    String file = Shared.getTestFilePath(getTestContext(), "text-anki-manual-csv-multi-line.txt");
    TextImporter i = new TextImporter(mTestCol, file);
    i.setAllowHtml(true);
    i.initMapping();
    i.run();
    Note n = mTestCol.getNote(mTestCol.getDb().queryLongScalar("select id from notes"));
    assertThat(Arrays.asList(n.getFields()), contains("hello", "this is\na two line answer"));
}
Also used : Note(com.ichi2.libanki.Note) TextImporter(com.ichi2.libanki.importer.TextImporter) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest) SdkSuppress(androidx.test.filters.SdkSuppress)

Example 12 with TextImporter

use of com.ichi2.libanki.importer.TextImporter in project Anki-Android by ankidroid.

the class ImportTest method testCsv.

@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
public void testCsv() throws IOException {
    String file = Shared.getTestFilePath(getTestContext(), "text-2fields.txt");
    TextImporter i = new TextImporter(mTestCol, file);
    i.initMapping();
    i.run();
    if (TestEnvironment.isDisplayingDefaultEnglishStrings()) {
        assertThat(i.getLog(), contains("‘多すぎる too many fields’ had 3 fields, expected 2", "‘not, enough, fields’ had 1 fields, expected 2", "Appeared twice in file: 飲む", "Empty first field:  to play", "5 notes added, 0 notes updated, 0 notes unchanged."));
    } else {
        assertThat(i.getLog(), hasSize(5));
    }
    assertEquals(5, i.getTotal());
    // if we run the import again, it should update instead
    i.run();
    if (TestEnvironment.isDisplayingDefaultEnglishStrings()) {
        assertThat(i.getLog(), contains("‘多すぎる too many fields’ had 3 fields, expected 2", "‘not, enough, fields’ had 1 fields, expected 2", "Appeared twice in file: 飲む", "Empty first field:  to play", "0 notes added, 0 notes updated, 5 notes unchanged.", "First field matched: 食べる", "First field matched: 飲む", "First field matched: テスト", "First field matched: to eat", "First field matched: 遊ぶ"));
    } else {
        assertThat(i.getLog(), hasSize(10));
    }
    assertEquals(5, i.getTotal());
    // but importing should not clobber tags if they're unmapped
    Note n = mTestCol.getNote(mTestCol.getDb().queryLongScalar("select id from notes"));
    n.addTag("test");
    n.flush();
    i.run();
    n.load();
    assertThat(n.getTags(), contains("test"));
    assertThat(n.getTags(), hasSize(1));
    // if add-only mode, count will be 0
    i.setImportMode(NoteImporter.ImportMode.IGNORE_MODE);
    i.run();
    assertEquals(0, i.getTotal());
    // and if dupes mode, will reimport everything
    assertEquals(5, mTestCol.cardCount());
    i.setImportMode(NoteImporter.ImportMode.ADD_MODE);
    i.run();
    // includes repeated field
    assertEquals(6, i.getTotal());
    assertEquals(11, mTestCol.cardCount());
}
Also used : Note(com.ichi2.libanki.Note) TextImporter(com.ichi2.libanki.importer.TextImporter) Test(org.junit.Test) InstrumentedTest(com.ichi2.anki.tests.InstrumentedTest) SdkSuppress(androidx.test.filters.SdkSuppress)

Aggregations

SdkSuppress (androidx.test.filters.SdkSuppress)12 Note (com.ichi2.libanki.Note)12 TextImporter (com.ichi2.libanki.importer.TextImporter)12 Test (org.junit.Test)12 InstrumentedTest (com.ichi2.anki.tests.InstrumentedTest)6 Model (com.ichi2.libanki.Model)2 JSONObject (com.ichi2.utils.JSONObject)2 Ignore (org.junit.Ignore)2 ModelManager (com.ichi2.libanki.ModelManager)1 Models (com.ichi2.libanki.Models)1