Search in sources :

Example 51 with Model

use of com.ichi2.libanki.Model 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 52 with Model

use of com.ichi2.libanki.Model 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 53 with Model

use of com.ichi2.libanki.Model 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)

Example 54 with Model

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

the class SchedV2Test method test_review_limits.

@Test
public void test_review_limits() throws Exception {
    Collection col = getColV2();
    Deck parent = col.getDecks().get(col.getDecks().id("parent"));
    Deck child = col.getDecks().get(col.getDecks().id("parent::child"));
    DeckConfig pconf = col.getDecks().getConf(col.getDecks().confId("parentConf"));
    DeckConfig cconf = col.getDecks().getConf(col.getDecks().confId("childConf"));
    pconf.getJSONObject("rev").put("perDay", 5);
    col.getDecks().updateConf(pconf);
    col.getDecks().setConf(parent, pconf.getLong("id"));
    cconf.getJSONObject("rev").put("perDay", 10);
    col.getDecks().updateConf(cconf);
    col.getDecks().setConf(child, cconf.getLong("id"));
    Model m = col.getModels().current();
    m.put("did", child.getLong("id"));
    col.getModels().save(m, false);
    // add some cards
    for (int i = 0; i < 20; i++) {
        Note note = col.newNote();
        note.setItem("Front", "one");
        note.setItem("Back", "two");
        col.addNote(note);
        // make them reviews
        Card c = note.cards().get(0);
        c.setQueue(CARD_TYPE_REV);
        c.setType(QUEUE_TYPE_REV);
        c.setDue(0);
        c.flush();
    }
    // position 0 is default deck. Different from upstream
    DeckDueTreeNode tree = col.getSched().deckDueTree().get(1);
    // (('parent', 1514457677462, 5, 0, 0, (('child', 1514457677463, 5, 0, 0, ()),)))
    assertEquals("parent", tree.getFullDeckName());
    // paren, tree.review_count)t
    assertEquals(5, tree.getRevCount());
    assertEquals(5, tree.getChildren().get(0).getRevCount());
    // .counts() should match
    col.getDecks().select(child.getLong("id"));
    col.reset();
    assertArrayEquals(new int[] { 0, 0, 5 }, col.getSched().counts());
    // answering a card in the child should decrement parent count
    Card c = col.getSched().getCard();
    col.getSched().answerCard(c, 3);
    assertArrayEquals(new int[] { 0, 0, 4 }, col.getSched().counts());
    tree = col.getSched().deckDueTree().get(1);
    assertEquals(4, tree.getRevCount());
    assertEquals(4, tree.getChildren().get(0).getRevCount());
}
Also used : Note(com.ichi2.libanki.Note) Model(com.ichi2.libanki.Model) Collection(com.ichi2.libanki.Collection) Deck(com.ichi2.libanki.Deck) DeckConfig(com.ichi2.libanki.DeckConfig) Card(com.ichi2.libanki.Card) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Example 55 with Model

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

the class SchedV2Test method test_new_v2.

@Test
public void test_new_v2() throws Exception {
    Collection col = getColV2();
    col.reset();
    assertEquals(0, col.getSched().counts()[0]);
    // add a note
    Note note = col.newNote();
    note.setItem("Front", "one");
    note.setItem("Back", "two");
    col.addNote(note);
    col.reset();
    assertEquals(1, col.getSched().counts()[0]);
    // fetch it
    Card c = col.getSched().getCard();
    assertNotNull(c);
    assertEquals(QUEUE_TYPE_NEW, c.getQueue());
    assertEquals(CARD_TYPE_NEW, c.getType());
    // if we answer it, it should become a learn card
    long t = col.getTime().intTime();
    col.getSched().answerCard(c, 1);
    assertEquals(QUEUE_TYPE_LRN, c.getQueue());
    assertEquals(CARD_TYPE_LRN, c.getType());
    assertThat(c.getDue(), is(greaterThanOrEqualTo(t)));
// disabled for now, as the learn fudging makes this randomly fail
// // the default order should ensure siblings are not seen together, and
// // should show all cards
// Model m = col.getModels().current(); Models mm = col.getModels()
// JSONObject t = mm.newTemplate("Reverse")
// t['qfmt'] = "{{Back}}"
// t['afmt'] = "{{Front}}"
// mm.addTemplateModChanged(m, t)
// mm.save(m)
// note = col.newNote()
// note['Front'] = u"2"; note['Back'] = u"2"
// col.addNote(note)
// note = col.newNote()
// note['Front'] = u"3"; note['Back'] = u"3"
// col.addNote(note)
// col.reset()
// qs = ("2", "3", "2", "3")
// for (int n = 0; n < 4; n++) {
// c = col.getSched().getCard()
// assertTrue(qs[n] in c.q())
// col.getSched().answerCard(c, 2)
// }
}
Also used : Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection) Card(com.ichi2.libanki.Card) RobolectricTest(com.ichi2.anki.RobolectricTest) Test(org.junit.Test)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)124 Model (com.ichi2.libanki.Model)95 Test (org.junit.Test)82 JSONArray (com.ichi2.utils.JSONArray)79 Collection (com.ichi2.libanki.Collection)53 ArrayList (java.util.ArrayList)48 Note (com.ichi2.libanki.Note)40 RobolectricTest (com.ichi2.anki.RobolectricTest)38 JSONException (com.ichi2.utils.JSONException)32 Intent (android.content.Intent)30 Card (com.ichi2.libanki.Card)27 ConfirmModSchemaException (com.ichi2.anki.exception.ConfirmModSchemaException)26 HashMap (java.util.HashMap)22 Bundle (android.os.Bundle)20 NonNull (androidx.annotation.NonNull)20 SuppressLint (android.annotation.SuppressLint)16 View (android.view.View)16 ConfirmationDialog (com.ichi2.anki.dialogs.ConfirmationDialog)15 IOException (java.io.IOException)15 Nullable (androidx.annotation.Nullable)14