use of com.ichi2.libanki.Models in project AnkiChinaAndroid by ankichinateam.
the class ContentProviderTest method testInsertTemplate.
/**
* Check that inserting and removing a note into default deck works as expected
*/
@Test
public void testInsertTemplate() throws Exception {
// Get required objects for test
final ContentResolver cr = InstrumentationRegistry.getInstrumentation().getTargetContext().getContentResolver();
Collection col = getCol();
// Add a new basic model that we use for testing purposes (existing models could potentially be corrupted)
Model model = StdModels.basicModel.add(col, BASIC_MODEL_NAME);
long modelId = model.getLong("id");
// Add the note
Uri modelUri = ContentUris.withAppendedId(FlashCardsContract.Model.CONTENT_URI, modelId);
// choose the last one because not the same as the basic model template
int testIndex = TEST_MODEL_CARDS.length - 1;
int expectedOrd = model.getJSONArray("tmpls").length();
ContentValues cv = new ContentValues();
cv.put(FlashCardsContract.CardTemplate.NAME, TEST_MODEL_CARDS[testIndex]);
cv.put(FlashCardsContract.CardTemplate.QUESTION_FORMAT, TEST_MODEL_QFMT[testIndex]);
cv.put(FlashCardsContract.CardTemplate.ANSWER_FORMAT, TEST_MODEL_AFMT[testIndex]);
cv.put(FlashCardsContract.CardTemplate.BROWSER_QUESTION_FORMAT, TEST_MODEL_QFMT[testIndex]);
cv.put(FlashCardsContract.CardTemplate.BROWSER_ANSWER_FORMAT, TEST_MODEL_AFMT[testIndex]);
Uri templatesUri = Uri.withAppendedPath(modelUri, "templates");
Uri templateUri = cr.insert(templatesUri, cv);
// test that the changes are physically saved to the DB
col = reopenCol();
assertNotNull("Check template uri", templateUri);
assertEquals("Check template uri ord", expectedOrd, ContentUris.parseId(templateUri));
model = col.getModels().get(modelId);
assertNotNull("Check model", model);
JSONObject template = model.getJSONArray("tmpls").getJSONObject(expectedOrd);
assertEquals("Check template JSONObject ord", expectedOrd, template.getInt("ord"));
assertEquals("Check template name", TEST_MODEL_CARDS[testIndex], template.getString("name"));
assertEquals("Check qfmt", TEST_MODEL_QFMT[testIndex], template.getString("qfmt"));
assertEquals("Check afmt", TEST_MODEL_AFMT[testIndex], template.getString("afmt"));
assertEquals("Check bqfmt", TEST_MODEL_QFMT[testIndex], template.getString("bqfmt"));
assertEquals("Check bafmt", TEST_MODEL_AFMT[testIndex], template.getString("bafmt"));
col.getModels().rem(model);
}
use of com.ichi2.libanki.Models in project AnkiChinaAndroid by ankichinateam.
the class ContentProviderTest method testProviderProvidesDefaultForEmptyModelDeck.
/**
* Test that a null did will not crash the provider (#6378)
*/
@Test
public void testProviderProvidesDefaultForEmptyModelDeck() {
assumeTrue("This causes mild data corruption - should not be run on a collection you care about", isEmulator());
Collection col = getCol();
col.getModels().all().get(0).put("did", JSONObject.NULL);
col.save();
final ContentResolver cr = InstrumentationRegistry.getInstrumentation().getTargetContext().getContentResolver();
// Query all available models
final Cursor allModels = cr.query(FlashCardsContract.Model.CONTENT_URI, null, null, null, null);
assertNotNull(allModels);
}
use of com.ichi2.libanki.Models in project AnkiChinaAndroid by ankichinateam.
the class ImportTest method testCsv2.
@Test
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
public void testCsv2() throws IOException, ConfirmModSchemaException {
Models mm = testCol.getModels();
Model m = mm.current();
JSONObject f = mm.newField("Three");
mm.addField(m, f);
mm.save(m);
Note n = testCol.newNote();
n.setField(0, "1");
n.setField(1, "2");
n.setField(2, "3");
testCol.addNote(n);
// an update with unmapped fields should not clobber those fields
String file = Shared.getTestFilePath(InstrumentationRegistry.getInstrumentation().getTargetContext(), "text-update.txt");
TextImporter i = new TextImporter(testCol, file);
i.initMapping();
i.run();
n.load();
List<String> fields = Arrays.asList(n.getFields());
assertThat(fields, contains("1", "x", "3"));
}
use of com.ichi2.libanki.Models in project AnkiChinaAndroid by ankichinateam.
the class ImportTest method addFieldToCurrentModel.
private void addFieldToCurrentModel(String fieldName) throws ConfirmModSchemaException {
Models mm = testCol.getModels();
Model m = mm.current();
JSONObject f = mm.newField(fieldName);
mm.addField(m, f);
mm.save(m);
}
use of com.ichi2.libanki.Models in project AnkiChinaAndroid by ankichinateam.
the class ModelTest method bigQuery.
@Test
public void bigQuery() {
assumeTrue("This test is flaky on API29, ignoring", Build.VERSION.SDK_INT != 29);
Models models = testCol.getModels();
Model model = models.all().get(0);
final String testString = "test";
final int size = testString.length() * 1024 * 1024;
StringBuilder buf = new StringBuilder((int) (size * 1.01));
// * 1.01 for padding
for (int i = 0; i < 1024 * 1024; ++i) {
buf.append(testString);
}
model.put(testString, buf.toString());
// Buf should be more than 4MB, so at least two chunks from database.
models.flush();
// Reload models
testCol.load();
Model newModel = models.all().get(0);
assertEquals(newModel, model);
}
Aggregations