Search in sources :

Example 66 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project Anki-Android by ankidroid.

the class ImportTest method addFieldToCurrentModel.

private void addFieldToCurrentModel(String fieldName) throws ConfirmModSchemaException {
    ModelManager mm = mTestCol.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) ModelManager(com.ichi2.libanki.ModelManager)

Example 67 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project Anki-Android by ankidroid.

the class ReviewerTest method addNoteWithThreeCards.

private void addNoteWithThreeCards() throws ConfirmModSchemaException {
    ModelManager models = getCol().getModels();
    Model m = models.copy(models.current());
    m.put("name", "Three");
    models.add(m);
    m = models.byName("Three");
    models.flush();
    cloneTemplate(models, m);
    cloneTemplate(models, m);
    @NonNull Note newNote = getCol().newNote();
    newNote.setField(0, "Hello");
    assertThat(newNote.model().get("name"), is("Three"));
    assertThat(getCol().addNote(newNote), is(3));
}
Also used : NonNull(androidx.annotation.NonNull) Note(com.ichi2.libanki.Note) Model(com.ichi2.libanki.Model) ModelManager(com.ichi2.libanki.ModelManager)

Example 68 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project Anki-Android by ankidroid.

the class ModelTest method test_templates.

@Test
public void test_templates() throws ConfirmModSchemaException {
    Collection col = getCol();
    Model m = col.getModels().current();
    ModelManager mm = col.getModels();
    JSONObject t = Models.newTemplate("Reverse");
    t.put("qfmt", "{{Back}}");
    t.put("afmt", "{{Front}}");
    mm.addTemplateModChanged(m, t);
    mm.save(m);
    Note note = col.newNote();
    note.setItem("Front", "1");
    note.setItem("Back", "2");
    col.addNote(note);
    assertEquals(2, col.cardCount());
    List<Card> cards = note.cards();
    assertEquals(2, cards.size());
    Card c = cards.get(0);
    Card c2 = cards.get(1);
    // first card should have first ord
    assertEquals(0, c.getOrd());
    assertEquals(1, c2.getOrd());
    // switch templates
    col.getModels().moveTemplate(m, c.template(), 1);
    c.load();
    c2.load();
    assertEquals(1, c.getOrd());
    assertEquals(0, c2.getOrd());
    // removing a template should delete its cards
    col.getModels().remTemplate(m, m.getJSONArray("tmpls").getJSONObject(0));
    assertEquals(1, col.cardCount());
    // and should have updated the other cards' ordinals
    c = note.cards().get(0);
    assertEquals(0, c.getOrd());
    assertEquals("1", stripHTML(c.q()));
    // it shouldn't be possible to orphan notes by removing templates
    t = Models.newTemplate("template name");
    mm.addTemplateModChanged(m, t);
    col.getModels().remTemplate(m, m.getJSONArray("tmpls").getJSONObject(0));
    assertEquals(0, col.getDb().queryLongScalar("select count() from cards where nid not in (select id from notes)"));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 69 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project Anki-Android by ankidroid.

the class ModelTest method test_fields.

@Test
public void test_fields() throws ConfirmModSchemaException {
    Collection col = getCol();
    Note note = col.newNote();
    note.setItem("Front", "1");
    note.setItem("Back", "2");
    col.addNote(note);
    Model m = col.getModels().current();
    // make sure renaming a field updates the templates
    col.getModels().renameField(m, m.getJSONArray("flds").getJSONObject(0), "NewFront");
    assertThat(m.getJSONArray("tmpls").getJSONObject(0).getString("qfmt"), containsString("{{NewFront}}"));
    String h = col.getModels().scmhash(m);
    // add a field
    JSONObject field = col.getModels().newField("foo");
    col.getModels().addField(m, field);
    assertArrayEquals(new String[] { "1", "2", "" }, col.getNote(col.getModels().nids(m).get(0)).getFields());
    assertNotEquals(h, col.getModels().scmhash(m));
    // rename it
    field = m.getJSONArray("flds").getJSONObject(2);
    col.getModels().renameField(m, field, "bar");
    assertEquals("", col.getNote(col.getModels().nids(m).get(0)).getItem("bar"));
    // delete back
    col.getModels().remField(m, m.getJSONArray("flds").getJSONObject(1));
    assertArrayEquals(new String[] { "1", "" }, col.getNote(col.getModels().nids(m).get(0)).getFields());
    // move 0 -> 1
    col.getModels().moveField(m, m.getJSONArray("flds").getJSONObject(0), 1);
    assertArrayEquals(new String[] { "", "1" }, col.getNote(col.getModels().nids(m).get(0)).getFields());
    // move 1 -> 0
    col.getModels().moveField(m, m.getJSONArray("flds").getJSONObject(1), 0);
    assertArrayEquals(new String[] { "1", "" }, col.getNote(col.getModels().nids(m).get(0)).getFields());
    // add another and put in middle
    field = col.getModels().newField("baz");
    col.getModels().addField(m, field);
    note = col.getNote(col.getModels().nids(m).get(0));
    note.setItem("baz", "2");
    note.flush();
    assertArrayEquals(new String[] { "1", "", "2" }, col.getNote(col.getModels().nids(m).get(0)).getFields());
    // move 2 -> 1
    col.getModels().moveField(m, m.getJSONArray("flds").getJSONObject(2), 1);
    assertArrayEquals(new String[] { "1", "2", "" }, col.getNote(col.getModels().nids(m).get(0)).getFields());
    // move 0 -> 2
    col.getModels().moveField(m, m.getJSONArray("flds").getJSONObject(0), 2);
    assertArrayEquals(new String[] { "2", "", "1" }, col.getNote(col.getModels().nids(m).get(0)).getFields());
    // move 0 -> 1
    col.getModels().moveField(m, m.getJSONArray("flds").getJSONObject(0), 1);
    assertArrayEquals(new String[] { "", "2", "1" }, col.getNote(col.getModels().nids(m).get(0)).getFields());
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Matchers.containsString(org.hamcrest.Matchers.containsString) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)39 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)26 JSONArray (com.ichi2.utils.JSONArray)23 Test (org.junit.Test)22 RobolectricTest (com.ichi2.anki.RobolectricTest)14 ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)12 Collection (com.ichi2.libanki.Collection)12 Model (com.ichi2.libanki.Model)12 Note (com.ichi2.libanki.Note)8 ArrayList (java.util.ArrayList)8 JSONException (com.ichi2.utils.JSONException)6 HashMap (java.util.HashMap)5 SdkSuppress (androidx.test.filters.SdkSuppress)4 TaskData (com.ichi2.async.TaskData)4 ModelManager (com.ichi2.libanki.ModelManager)4 TextImporter (com.ichi2.libanki.importer.TextImporter)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Cursor (android.database.Cursor)3 Uri (android.net.Uri)3 Bundle (android.os.Bundle)3