Search in sources :

Example 51 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.

the class CardTest method test_gen_or.

@Test
public void test_gen_or() throws ConfirmModSchemaException {
    Collection col = getCol();
    Models models = col.getModels();
    Model model = models.byName("Basic");
    JSONArray flds = model.getJSONArray("flds");
    models.renameField(model, flds.getJSONObject(0), "A");
    models.renameField(model, flds.getJSONObject(1), "B");
    JSONObject fld2 = models.newField("C");
    fld2.put("ord", null);
    models.addField(model, fld2);
    JSONArray tmpls = model.getJSONArray("tmpls");
    tmpls.getJSONObject(0).put("qfmt", "{{A}}{{B}}{{C}}");
    // ensure first card is always generated,
    // because at last one card is generated
    JSONObject tmpl = models.newTemplate("AND_OR");
    tmpl.put("qfmt", "        {{A}}    {{#B}}        {{#C}}            {{B}}        {{/C}}    {{/B}}");
    models.addTemplate(model, tmpl);
    models.save(model);
    models.setCurrent(model);
    Note note = col.newNote();
    note.setItem("A", "foo");
    col.addNote(note);
    assertNoteOrdinalAre(note, new Integer[] { 0, 1 });
    note = col.newNote();
    note.setItem("B", "foo");
    note.setItem("C", "foo");
    col.addNote(note);
    assertNoteOrdinalAre(note, new Integer[] { 0, 1 });
    note = col.newNote();
    note.setItem("B", "foo");
    col.addNote(note);
    assertNoteOrdinalAre(note, new Integer[] { 0 });
    note = col.newNote();
    note.setItem("C", "foo");
    col.addNote(note);
    assertNoteOrdinalAre(note, new Integer[] { 0 });
    note = col.newNote();
    note.setItem("A", "foo");
    note.setItem("B", "foo");
    note.setItem("C", "foo");
    col.addNote(note);
    assertNoteOrdinalAre(note, new Integer[] { 0, 1 });
    note = col.newNote();
    col.addNote(note);
    assertNoteOrdinalAre(note, new Integer[] { 0 });
// First card is generated if no other card
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 52 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.

the class CardTest method test_gen_not.

@Test
public void test_gen_not() throws ConfirmModSchemaException {
    Collection col = getCol();
    Models models = col.getModels();
    Model model = models.byName("Basic");
    JSONArray flds = model.getJSONArray("flds");
    JSONArray tmpls = model.getJSONArray("tmpls");
    models.renameField(model, flds.getJSONObject(0), "First");
    models.renameField(model, flds.getJSONObject(1), "Front");
    JSONObject fld2 = models.newField("AddIfEmpty");
    fld2.put("name", "AddIfEmpty");
    models.addField(model, fld2);
    // ensure first card is always generated,
    // because at last one card is generated
    tmpls.getJSONObject(0).put("qfmt", "{{AddIfEmpty}}{{Front}}{{First}}");
    JSONObject tmpl = models.newTemplate("NOT");
    tmpl.put("qfmt", "    {{^AddIfEmpty}}        {{Front}}    {{/AddIfEmpty}}    ");
    models.addTemplate(model, tmpl);
    models.save(model);
    models.setCurrent(model);
    Note note = col.newNote();
    note.setItem("First", "foo");
    note.setItem("AddIfEmpty", "foo");
    note.setItem("Front", "foo");
    col.addNote(note);
    assertNoteOrdinalAre(note, new Integer[] { 0 });
    note = col.newNote();
    note.setItem("First", "foo");
    note.setItem("AddIfEmpty", "foo");
    col.addNote(note);
    assertNoteOrdinalAre(note, new Integer[] { 0 });
    note = col.newNote();
    // ensure first note generated
    note.setItem("First", "foo");
    col.addNote(note);
    assertNoteOrdinalAre(note, new Integer[] { 0 });
    note = col.newNote();
    note.setItem("First", "foo");
    note.setItem("Front", "foo");
    col.addNote(note);
    assertNoteOrdinalAre(note, new Integer[] { 0, 1 });
}
Also used : JSONObject(com.ichi2.utils.JSONObject) JSONArray(com.ichi2.utils.JSONArray) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 53 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.

the class ModelTest method test_cloze_ordinals.

@Test
public void test_cloze_ordinals() throws ConfirmModSchemaException {
    Collection col = getCol();
    col.getModels().setCurrent(col.getModels().byName("Cloze"));
    Model m = col.getModels().current();
    Models mm = col.getModels();
    // We replace the default Cloze template
    JSONObject t = Models.newTemplate("ChainedCloze");
    t.put("qfmt", "{{text:cloze:Text}}");
    t.put("afmt", "{{text:cloze:Text}}");
    mm.addTemplateModChanged(m, t);
    mm.save(m);
    col.getModels().remTemplate(m, m.getJSONArray("tmpls").getJSONObject(0));
    Note note = col.newNote();
    note.setItem("Text", "{{c1::firstQ::firstA}}{{c2::secondQ::secondA}}");
    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());
}
Also used : JSONObject(com.ichi2.utils.JSONObject) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 54 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.

the class ModelTest method test_modelChange.

@Test
public void test_modelChange() throws ConfirmModSchemaException {
    Collection col = getCol();
    Model cloze = col.getModels().byName("Cloze");
    // enable second template and add a note
    Model m = col.getModels().current();
    Models mm = col.getModels();
    JSONObject t = Models.newTemplate("Reverse");
    t.put("qfmt", "{{Back}}");
    t.put("afmt", "{{Front}}");
    mm.addTemplateModChanged(m, t);
    mm.save(m);
    Model basic = m;
    Note note = col.newNote();
    note.setItem("Front", "note");
    note.setItem("Back", "b123");
    col.addNote(note);
    // switch fields
    Map<Integer, Integer> map = new HashMap<>();
    map.put(0, 1);
    map.put(1, 0);
    col.getModels().change(basic, new long[] { note.getId() }, basic, map, null);
    note.load();
    assertEquals("b123", note.getItem("Front"));
    assertEquals("note", note.getItem("Back"));
    // switch cards
    Card c0 = note.cards().get(0);
    Card c1 = note.cards().get(1);
    assertThat(c0.q(), containsString("b123"));
    assertThat(c1.q(), containsString("note"));
    assertEquals(0, c0.getOrd());
    assertEquals(1, c1.getOrd());
    col.getModels().change(basic, new long[] { note.getId() }, basic, null, map);
    note.load();
    c0.load();
    c1.load();
    assertThat(c0.q(), containsString("note"));
    assertThat(c1.q(), containsString("b123"));
    assertEquals(1, c0.getOrd());
    assertEquals(0, c1.getOrd());
    // .cards() returns cards in order
    assertEquals(c1.getId(), note.cards().get(0).getId());
    // delete first card
    map = new HashMap<>();
    map.put(0, null);
    map.put(1, 1);
    // if (isWin) {
    // // The low precision timer on Windows reveals a race condition
    // time.sleep(0.05);
    // }
    col.getModels().change(basic, new long[] { note.getId() }, basic, null, map);
    note.load();
    c0.load();
    // the card was deleted
    // but we have two cards, as a new one was generated
    assertEquals(2, note.numberOfCards());
    // an unmapped field becomes blank
    assertEquals("b123", note.getItem("Front"));
    assertEquals("note", note.getItem("Back"));
    col.getModels().change(basic, new long[] { note.getId() }, basic, map, null);
    note.load();
    assertEquals("", note.getItem("Front"));
    assertEquals("note", note.getItem("Back"));
    // another note to try model conversion
    note = col.newNote();
    note.setItem("Front", "f2");
    note.setItem("Back", "b2");
    col.addNote(note);
    // counts = col.getModels().all_use_counts();
    // Using older version of the test
    assertEquals(2, col.getModels().useCount(basic));
    assertEquals(0, col.getModels().useCount(cloze));
    // Identity map
    map = new HashMap<>();
    map.put(0, 0);
    map.put(1, 1);
    col.getModels().change(basic, new long[] { note.getId() }, cloze, map, map);
    note.load();
    assertEquals("f2", note.getItem("Text"));
    assertEquals(2, note.numberOfCards());
    // back the other way, with deletion of second ord
    col.getModels().remTemplate(basic, basic.getJSONArray("tmpls").getJSONObject(1));
    assertEquals(2, col.getDb().queryScalar("select count() from cards where nid = ?", note.getId()));
    map = new HashMap<>();
    map.put(0, 0);
    col.getModels().change(cloze, new long[] { note.getId() }, basic, map, map);
    assertEquals(1, col.getDb().queryScalar("select count() from cards where nid = ?", note.getId()));
}
Also used : JSONObject(com.ichi2.utils.JSONObject) HashMap(java.util.HashMap) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 55 with ConfirmModSchemaException

use of com.ichi2.anki.exception.ConfirmModSchemaException in project AnkiChinaAndroid by ankichinateam.

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