Search in sources :

Example 11 with ModelManager

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

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 basic = col.getModels().current();
    ModelManager mm = col.getModels();
    JSONObject t = Models.newTemplate("Reverse");
    t.put("qfmt", "{{Back}}");
    t.put("afmt", "{{Front}}");
    mm.addTemplateModChanged(basic, t);
    mm.save(basic);
    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, 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, 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, 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, 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, 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, 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 12 with ModelManager

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

the class ModelTest method regression_test_pipe.

@Test
@Config(qualifiers = "en")
public void regression_test_pipe() {
    Collection col = getCol();
    ModelManager mm = col.getModels();
    Model basic = mm.byName("Basic");
    JSONObject template = basic.getJSONArray("tmpls").getJSONObject(0);
    template.put("qfmt", "{{|Front}}{{Front}}{{/Front}}{{Front}}");
    try {
        // in V16, the "save" throws, in V11, the "add" throws
        mm.save(basic, true);
        Note note = addNoteUsingBasicModel("foo", "bar");
        fail();
    } catch (Exception er) {
    }
}
Also used : JSONObject(com.ichi2.utils.JSONObject) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 13 with ModelManager

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

the class ModelTest method test_chained_mods.

@Test
public void test_chained_mods() throws ConfirmModSchemaException {
    Collection col = getCol();
    col.getModels().setCurrent(col.getModels().byName("Cloze"));
    Model m = col.getModels().current();
    ModelManager mm = col.getModels();
    // We replace the default Cloze template
    JSONObject t = Models.newTemplate("ChainedCloze");
    t.put("qfmt", "{{cloze:text:Text}}");
    t.put("afmt", "{{cloze:text:Text}}");
    mm.addTemplateModChanged(m, t);
    mm.save(m);
    col.getModels().remTemplate(m, m.getJSONArray("tmpls").getJSONObject(0));
    Note note = col.newNote();
    String q1 = "<span style=\"color:red\">phrase</span>";
    String a1 = "<b>sentence</b>";
    String q2 = "<span style=\"color:red\">en chaine</span>";
    String a2 = "<i>chained</i>";
    note.setItem("Text", "This {{c1::" + q1 + "::" + a1 + "}} demonstrates {{c1::" + q2 + "::" + a2 + "}} clozes.");
    assertEquals(1, col.addNote(note));
    String question = note.cards().get(0).q();
/* TODO: chained modifier
        assertThat("Question «"+question+"» does not contain the expected string", question, containsString("This <span class=cloze>[sentence]</span> demonstrates <span class=cloze>[chained]</span> clozes.")
                   );
        assertThat(note.cards().get(0).a(), containsString("This <span class=cloze>phrase</span> demonstrates <span class=cloze>en chaine</span> clozes."
                                                    ));

         */
}
Also used : JSONObject(com.ichi2.utils.JSONObject) Matchers.containsString(org.hamcrest.Matchers.containsString) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 14 with ModelManager

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

the class ModelTest method test_req.

@Test
public void test_req() {
    Collection col = getCol();
    ModelManager mm = col.getModels();
    Model basic = mm.byName("Basic");
    assertTrue(basic.has("req"));
    reqSize(basic);
    JSONArray r = basic.getJSONArray("req").getJSONArray(0);
    assertEquals(0, r.getInt(0));
    assertTrue(Arrays.asList(new String[] { REQ_ANY, REQ_ALL }).contains(r.getString(1)));
    assertEquals(1, r.getJSONArray(2).length());
    assertEquals(0, r.getJSONArray(2).getInt(0));
    Model opt = mm.byName("Basic (optional reversed card)");
    reqSize(opt);
    r = opt.getJSONArray("req").getJSONArray(0);
    assertTrue(Arrays.asList(new String[] { REQ_ANY, REQ_ALL }).contains(r.getString(1)));
    assertEquals(1, r.getJSONArray(2).length());
    assertEquals(0, r.getJSONArray(2).getInt(0));
    assertEquals(new JSONArray("[1,\"all\",[1,2]]"), opt.getJSONArray("req").getJSONArray(1));
    // testing any
    opt.getJSONArray("tmpls").getJSONObject(1).put("qfmt", "{{Back}}{{Add Reverse}}");
    mm.save(opt, true);
    assertEquals(new JSONArray("[1, \"any\", [1, 2]]"), opt.getJSONArray("req").getJSONArray(1));
    // testing null
    opt.getJSONArray("tmpls").getJSONObject(1).put("qfmt", "{{^Add Reverse}}{{/Add Reverse}}");
    mm.save(opt, true);
    assertEquals(new JSONArray("[1, \"none\", []]"), opt.getJSONArray("req").getJSONArray(1));
    opt = mm.byName("Basic (type in the answer)");
    reqSize(opt);
    r = opt.getJSONArray("req").getJSONArray(0);
    assertTrue(Arrays.asList(new String[] { REQ_ANY, REQ_ALL }).contains(r.getString(1)));
    if (col.getModels() instanceof ModelsV16) {
        assertEquals(new JSONArray("[0, 1]"), r.getJSONArray(2));
    } else {
        // TODO: Port anki@4e33775ed4346ef136ece6ef5efec5ba46057c6b
        assertEquals(new JSONArray("[0]"), r.getJSONArray(2));
    }
}
Also used : JSONArray(com.ichi2.utils.JSONArray) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 15 with ModelManager

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

the class ModelTest method nonEmptyFieldTest.

@Test
public void nonEmptyFieldTest() {
    Collection col = getCol();
    ModelManager mm = col.getModels();
    Model basic = mm.byName("Basic");
    Set s = new HashSet<>();
    assertEquals(s, basic.nonEmptyFields(new String[] { "", "" }));
    s.add("Front");
    // Html is not stripped to check for card generation
    assertEquals(s, basic.nonEmptyFields(new String[] { "<br/>", "   \t " }));
    assertEquals(s, basic.nonEmptyFields(new String[] { "P", "" }));
    s.add("Back");
    assertEquals(s, basic.nonEmptyFields(new String[] { "P", "A" }));
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Matchers.containsString(org.hamcrest.Matchers.containsString) HashSet(java.util.HashSet) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 JSONObject (com.ichi2.utils.JSONObject)14 RobolectricTest (com.ichi2.anki.RobolectricTest)12 Model (com.ichi2.libanki.Model)12 ModelManager (com.ichi2.libanki.ModelManager)11 Collection (com.ichi2.libanki.Collection)7 Note (com.ichi2.libanki.Note)6 JSONArray (com.ichi2.utils.JSONArray)5 JSONException (com.ichi2.utils.JSONException)4 Card (com.ichi2.libanki.Card)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 MatrixCursor (android.database.MatrixCursor)2 NonNull (androidx.annotation.NonNull)2 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)2 InstrumentedTest (com.ichi2.anki.tests.InstrumentedTest)2 List (java.util.List)2 Manifest (android.Manifest)1 BroadcastReceiver (android.content.BroadcastReceiver)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1