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);
}
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));
}
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)"));
}
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());
}
Aggregations