Search in sources :

Example 41 with Models

use of com.ichi2.libanki.Models in project AnkiChinaAndroid by ankichinateam.

the class ContentProviderTest method testInsertTemplate.

/**
 * Check that inserting and removing a note into default deck works as expected
 */
@Test
public void testInsertTemplate() throws Exception {
    // Get required objects for test
    final ContentResolver cr = InstrumentationRegistry.getInstrumentation().getTargetContext().getContentResolver();
    Collection col = getCol();
    // Add a new basic model that we use for testing purposes (existing models could potentially be corrupted)
    Model model = StdModels.basicModel.add(col, BASIC_MODEL_NAME);
    long modelId = model.getLong("id");
    // Add the note
    Uri modelUri = ContentUris.withAppendedId(FlashCardsContract.Model.CONTENT_URI, modelId);
    // choose the last one because not the same as the basic model template
    int testIndex = TEST_MODEL_CARDS.length - 1;
    int expectedOrd = model.getJSONArray("tmpls").length();
    ContentValues cv = new ContentValues();
    cv.put(FlashCardsContract.CardTemplate.NAME, TEST_MODEL_CARDS[testIndex]);
    cv.put(FlashCardsContract.CardTemplate.QUESTION_FORMAT, TEST_MODEL_QFMT[testIndex]);
    cv.put(FlashCardsContract.CardTemplate.ANSWER_FORMAT, TEST_MODEL_AFMT[testIndex]);
    cv.put(FlashCardsContract.CardTemplate.BROWSER_QUESTION_FORMAT, TEST_MODEL_QFMT[testIndex]);
    cv.put(FlashCardsContract.CardTemplate.BROWSER_ANSWER_FORMAT, TEST_MODEL_AFMT[testIndex]);
    Uri templatesUri = Uri.withAppendedPath(modelUri, "templates");
    Uri templateUri = cr.insert(templatesUri, cv);
    // test that the changes are physically saved to the DB
    col = reopenCol();
    assertNotNull("Check template uri", templateUri);
    assertEquals("Check template uri ord", expectedOrd, ContentUris.parseId(templateUri));
    model = col.getModels().get(modelId);
    assertNotNull("Check model", model);
    JSONObject template = model.getJSONArray("tmpls").getJSONObject(expectedOrd);
    assertEquals("Check template JSONObject ord", expectedOrd, template.getInt("ord"));
    assertEquals("Check template name", TEST_MODEL_CARDS[testIndex], template.getString("name"));
    assertEquals("Check qfmt", TEST_MODEL_QFMT[testIndex], template.getString("qfmt"));
    assertEquals("Check afmt", TEST_MODEL_AFMT[testIndex], template.getString("afmt"));
    assertEquals("Check bqfmt", TEST_MODEL_QFMT[testIndex], template.getString("bqfmt"));
    assertEquals("Check bafmt", TEST_MODEL_AFMT[testIndex], template.getString("bafmt"));
    col.getModels().rem(model);
}
Also used : ContentValues(android.content.ContentValues) JSONObject(com.ichi2.utils.JSONObject) Model(com.ichi2.libanki.Model) Collection(com.ichi2.libanki.Collection) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Example 42 with Models

use of com.ichi2.libanki.Models in project AnkiChinaAndroid by ankichinateam.

the class ContentProviderTest method testProviderProvidesDefaultForEmptyModelDeck.

/**
 * Test that a null did will not crash the provider (#6378)
 */
@Test
public void testProviderProvidesDefaultForEmptyModelDeck() {
    assumeTrue("This causes mild data corruption - should not be run on a collection you care about", isEmulator());
    Collection col = getCol();
    col.getModels().all().get(0).put("did", JSONObject.NULL);
    col.save();
    final ContentResolver cr = InstrumentationRegistry.getInstrumentation().getTargetContext().getContentResolver();
    // Query all available models
    final Cursor allModels = cr.query(FlashCardsContract.Model.CONTENT_URI, null, null, null, null);
    assertNotNull(allModels);
}
Also used : Collection(com.ichi2.libanki.Collection) Cursor(android.database.Cursor) ContentResolver(android.content.ContentResolver) Test(org.junit.Test)

Example 43 with Models

use of com.ichi2.libanki.Models in project AnkiChinaAndroid by ankichinateam.

the class ImportTest method testCsv2.

@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
public void testCsv2() throws IOException, ConfirmModSchemaException {
    Models mm = testCol.getModels();
    Model m = mm.current();
    JSONObject f = mm.newField("Three");
    mm.addField(m, f);
    mm.save(m);
    Note n = testCol.newNote();
    n.setField(0, "1");
    n.setField(1, "2");
    n.setField(2, "3");
    testCol.addNote(n);
    // an update with unmapped fields should not clobber those fields
    String file = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "text-update.txt");
    TextImporter i = new TextImporter(testCol, file);
    i.initMapping();
    i.run();
    n.load();
    List<String> fields = Arrays.asList(n.getFields());
    assertThat(fields, contains("1", "x", "3"));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Note(com.ichi2.libanki.Note) Model(com.ichi2.libanki.Model) Models(com.ichi2.libanki.Models) TextImporter(com.ichi2.libanki.importer.TextImporter) Test(org.junit.Test) SdkSuppress(androidx.test.filters.SdkSuppress)

Example 44 with Models

use of com.ichi2.libanki.Models in project AnkiChinaAndroid by ankichinateam.

the class ImportTest method addFieldToCurrentModel.

private void addFieldToCurrentModel(String fieldName) throws ConfirmModSchemaException {
    Models mm = testCol.getModels();
    Model m = mm.current();
    JSONObject f = mm.newField(fieldName);
    mm.addField(m, f);
    mm.save(m);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Model(com.ichi2.libanki.Model) Models(com.ichi2.libanki.Models)

Example 45 with Models

use of com.ichi2.libanki.Models in project AnkiChinaAndroid by ankichinateam.

the class ModelTest method bigQuery.

@Test
public void bigQuery() {
    assumeTrue("This test is flaky on API29, ignoring", Build.VERSION.SDK_INT != 29);
    Models models = testCol.getModels();
    Model model = models.all().get(0);
    final String testString = "test";
    final int size = testString.length() * 1024 * 1024;
    StringBuilder buf = new StringBuilder((int) (size * 1.01));
    // * 1.01 for padding
    for (int i = 0; i < 1024 * 1024; ++i) {
        buf.append(testString);
    }
    model.put(testString, buf.toString());
    // Buf should be more than 4MB, so at least two chunks from database.
    models.flush();
    // Reload models
    testCol.load();
    Model newModel = models.all().get(0);
    assertEquals(newModel, model);
}
Also used : Model(com.ichi2.libanki.Model) Models(com.ichi2.libanki.Models) Test(org.junit.Test)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)48 Model (com.ichi2.libanki.Model)28 JSONArray (com.ichi2.utils.JSONArray)26 Test (org.junit.Test)26 Collection (com.ichi2.libanki.Collection)25 RobolectricTest (com.ichi2.anki.RobolectricTest)16 Note (com.ichi2.libanki.Note)16 JSONException (com.ichi2.utils.JSONException)13 ArrayList (java.util.ArrayList)13 Card (com.ichi2.libanki.Card)11 Models (com.ichi2.libanki.Models)11 SuppressLint (android.annotation.SuppressLint)10 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)7 ModelManager (com.ichi2.libanki.ModelManager)7 File (java.io.File)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Map (java.util.Map)6 ContentValues (android.content.ContentValues)5 NonNull (androidx.annotation.NonNull)5