Search in sources :

Example 1 with CSVImporter

use of org.liberty.android.fantastischmemo.converter.CSVImporter in project AnyMemo by helloworld1.

the class ImportMergingTest method testMergeCsvIntoDb.

@SmallTest
@Test
public void testMergeCsvIntoDb() throws Exception {
    srcFilePath = AMEnv.DEFAULT_ROOT_PATH + "/" + "csv-test.csv";
    destFilePath = AMEnv.DEFAULT_ROOT_PATH + "/" + "csv-test.db";
    new File(srcFilePath).delete();
    new File(destFilePath).delete();
    AnyMemoDBOpenHelper helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), destFilePath);
    try {
        // Create an new db with some contents
        helper.getCardDao().createCards(newDbCardList);
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
    // Now convert the csv-test.csv into csv-test.db which will be merged
    // into existing csv-test.db
    amFileUtil.copyFileFromAsset("csv-test.csv", new File(srcFilePath));
    Converter converter = new CSVImporter(amFileUtil);
    converter.convert(srcFilePath, destFilePath);
    // verify the content of csv-test has merged cards
    helper = AnyMemoDBOpenHelperManager.getHelper(getContext(), destFilePath);
    try {
        List<Card> cards = helper.getCardDao().getAllCards(null);
        assertEquals(6, cards.size());
        assertEquals(1, (int) cards.get(0).getId());
        assertEquals(1, (int) cards.get(0).getOrdinal());
        assertEquals("old question 1", cards.get(0).getQuestion());
        assertEquals("old answer 1", cards.get(0).getAnswer());
        assertEquals(2, (int) cards.get(1).getId());
        assertEquals(2, (int) cards.get(1).getOrdinal());
        assertEquals("old question 2", cards.get(1).getQuestion());
        assertEquals("old answer 2", cards.get(1).getAnswer());
        assertEquals(3, (int) cards.get(2).getId());
        assertEquals(3, (int) cards.get(2).getOrdinal());
        assertEquals("Question1", cards.get(2).getQuestion());
        assertEquals("Answer1", cards.get(2).getAnswer());
        assertEquals("Category1", cards.get(2).getCategory().getName());
        assertEquals("Note1", cards.get(2).getNote());
        assertEquals(4, (int) cards.get(3).getId());
        assertEquals(4, (int) cards.get(3).getOrdinal());
        assertEquals("Question2", cards.get(3).getQuestion());
        assertEquals("Answer2", cards.get(3).getAnswer());
        assertEquals("Category1", cards.get(3).getCategory().getName());
        assertEquals("Note2", cards.get(3).getNote());
    } finally {
        AnyMemoDBOpenHelperManager.releaseHelper(helper);
    }
}
Also used : AnyMemoDBOpenHelper(org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper) Converter(org.liberty.android.fantastischmemo.converter.Converter) File(java.io.File) CSVImporter(org.liberty.android.fantastischmemo.converter.CSVImporter) Card(org.liberty.android.fantastischmemo.entity.Card) Test(org.junit.Test) SmallTest(android.support.test.filters.SmallTest) BaseTest(org.liberty.android.fantastischmemo.test.BaseTest) SmallTest(android.support.test.filters.SmallTest)

Aggregations

SmallTest (android.support.test.filters.SmallTest)1 File (java.io.File)1 Test (org.junit.Test)1 AnyMemoDBOpenHelper (org.liberty.android.fantastischmemo.common.AnyMemoDBOpenHelper)1 CSVImporter (org.liberty.android.fantastischmemo.converter.CSVImporter)1 Converter (org.liberty.android.fantastischmemo.converter.Converter)1 Card (org.liberty.android.fantastischmemo.entity.Card)1 BaseTest (org.liberty.android.fantastischmemo.test.BaseTest)1