Search in sources :

Example 11 with Models

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

the class ReviewerTest method addNoteWithThreeCards.

private void addNoteWithThreeCards() throws ConfirmModSchemaException {
    Models 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);
    Note newNote = getCol().newNote();
    newNote.setField(0, "Hello");
    assertThat(newNote.model().get("name"), is("Three"));
    assertThat(getCol().addNote(newNote), is(3));
}
Also used : Note(com.ichi2.libanki.Note) Model(com.ichi2.libanki.Model) Models(com.ichi2.libanki.Models)

Example 12 with Models

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

the class CardTest method test_genrem.

@Test
public void test_genrem() {
    Collection col = getCol();
    Note note = col.newNote();
    note.setItem("Front", "1");
    note.setItem("Back", "");
    col.addNote(note);
    assertEquals(1, note.numberOfCards());
    Model m = col.getModels().current();
    Models mm = col.getModels();
    // adding a new template should automatically create cards
    JSONObject t = Models.newTemplate("rev");
    t.put("qfmt", "{{Front}}");
    t.put("afmt", "");
    mm.addTemplateModChanged(m, t);
    mm.save(m, true);
    assertEquals(2, note.numberOfCards());
    // if the template is changed to remove cards, they'll be removed
    t = m.getJSONArray("tmpls").getJSONObject(1);
    t.put("qfmt", "{{Back}}");
    mm.save(m, true);
    List<Long> rep = col.emptyCids();
    col.remCards(rep);
    assertEquals(1, note.numberOfCards());
    // if we add to the note, a card should be automatically generated
    note.load();
    note.setItem("Back", "1");
    note.flush();
    assertEquals(2, note.numberOfCards());
}
Also used : JSONObject(com.ichi2.utils.JSONObject) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 13 with Models

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

the class CollectionTest method test_noteAddDelete.

/**
 *****************
 ** autogenerated from https://github.com/ankitects/anki/blob/2c73dcb2e547c44d9e02c20a00f3c52419dc277b/pylib/tests/test_cards.py *
 ******************
 */
/*TODO
      @Test
      public void test_create_open(){
      (fd, path) = tempfile.mkstemp(suffix=".anki2", prefix="test_attachNew");
      try {
      os.close(fd);
      os.unlink(path);
      } catch (OSError) {
      }
      Collection col = aopen(path);
      // for open()
      String newPath = col.getPath();
      long newMod = col.getMod();
      col.close();

      // reopen
      col = aopen(newPath);
      assertEquals(newMod, col.getMod());
      col.close();

      // non-writeable dir
      if (isWin) {
      String dir = "c:\root.anki2";
      } else {
      String dir = "/attachroot.anki2";
      }
      assertException(Exception, lambda: aopen(dir));
      // reuse tmp file from before, test non-writeable file
      os.chmod(newPath, 0);
      assertException(Exception, lambda: aopen(newPath));
      os.chmod(newPath, 0o666);
      os.unlink(newPath);
      } */
@Test
public void test_noteAddDelete() {
    Collection col = getCol();
    // add a note
    Note note = col.newNote();
    note.setItem("Front", "one");
    note.setItem("Back", "two");
    int n = col.addNote(note);
    assertEquals(1, n);
    // test multiple cards - add another template
    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);
    // todo: remove true which is not upstream
    mm.save(m, true);
    assertEquals(2, col.cardCount());
    // creating new notes should use both cards
    note = col.newNote();
    note.setItem("Front", "three");
    note.setItem("Back", "four");
    n = col.addNote(note);
    assertEquals(2, n);
    assertEquals(4, col.cardCount());
    // check q/a generation
    Card c0 = note.cards().get(0);
    assertThat(c0.q(), containsString("three"));
    // it should not be a duplicate
    assertEquals(note.dupeOrEmpty(), Note.DupeOrEmpty.CORRECT);
    // now let's make a duplicate
    Note note2 = col.newNote();
    note2.setItem("Front", "one");
    note2.setItem("Back", "");
    assertNotEquals(note2.dupeOrEmpty(), Note.DupeOrEmpty.CORRECT);
    // empty first field should not be permitted either
    note2.setItem("Front", " ");
    assertNotEquals(note2.dupeOrEmpty(), Note.DupeOrEmpty.CORRECT);
}
Also used : JSONObject(com.ichi2.utils.JSONObject) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 14 with Models

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

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();
    Models 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 15 with Models

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

the class ModelTest method test_templates.

@Test
public void test_templates() throws ConfirmModSchemaException {
    Collection col = getCol();
    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);
    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)

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