Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get remaining unit tests passing when new backend code is enabled #11579

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ internal constructor(
// set the preferences to the new deck path + checks CollectionHelper
// sets migration variables (migrationIsInProgress will be true)
updatePreferences(destinationPath)

// updatePreferences() opened the collection in the new location, which will have created
// a -wal file if the new backend code is active. Close it again, so that tests don't
// fail due to the presence of a -wal file in the destination folder.
if (AnkiDroidApp.TESTING_USE_V16_BACKEND) {
closeCollection()
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/async/CollectionTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.ichi2.libanki.sched.DeckTreeNode
import com.ichi2.libanki.sched.TreeNode
import com.ichi2.utils.*
import com.ichi2.utils.SyncStatus.Companion.ignoreDatabaseModification
import net.ankiweb.rsdroid.RustCleanup
import org.apache.commons.compress.archivers.zip.ZipFile
import timber.log.Timber
import java.io.File
Expand Down Expand Up @@ -553,6 +554,7 @@ open class CollectionTask<Progress, Result>(val task: TaskDelegateBase<Progress,
* A class allowing to send partial search result to the browser to display while the search ends
*/
@KotlinCleanup("move variables to constructor")
@RustCleanup("This provides little value since moving to the backend for DB access. Strip out?")
class PartialSearch(cards: List<CardCache>, columnIndex1: Int, columnIndex2: Int, numCardsToRender: Int, collectionTask: ProgressSenderAndCancelListener<List<CardCache>>, col: Collection) : ProgressSenderAndCancelListener<List<Long>> {
private val mCards: MutableList<CardCache>
private val mColumn1Index: Int
Expand Down
14 changes: 12 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/libanki/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.ichi2.anki.dialogs.DatabaseErrorDialog;
import com.ichi2.utils.DatabaseChangeDecorator;

import net.ankiweb.rsdroid.BackendException;

import org.intellij.lang.annotations.Language;

import java.util.ArrayList;
Expand Down Expand Up @@ -77,9 +79,17 @@ public DB(@NonNull String ankiFilename, @Nullable OpenHelperFactory openHelperFa
.build();
SupportSQLiteOpenHelper helper = getSqliteOpenHelperFactory(openHelperFactory).create(configuration);
// Note: This line creates the database and schema when executed using a Rust backend
mDatabase = new DatabaseChangeDecorator(helper.getWritableDatabase());
try {
mDatabase = new DatabaseChangeDecorator(helper.getWritableDatabase());
} catch (BackendException.BackendDbException exc) {
throw exc.toSQLiteException("db open");
}
// No-op except when using the old Java backend
mDatabase.disableWriteAheadLogging();
mDatabase.query("PRAGMA synchronous = 2", null);
if (!AnkiDroidApp.TESTING_USE_V16_BACKEND) {
// full sync is not required when using a WAL
mDatabase.query("PRAGMA synchronous = 2", null);
mikehardy marked this conversation as resolved.
Show resolved Hide resolved
}
mMod = false;
}

Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/test/java/com/ichi2/anki/RobolectricTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ open class RobolectricTest : CollectionGetter {
protected fun addNonClozeModel(name: String, fields: Array<String>, qfmt: String?, afmt: String?): String {
val model = col.models.newModel(name)
for (field in fields) {
addField(model, field)
col.models.addFieldInNewModel(model, col.models.newField(field))
}
val t = Models.newTemplate("Card 1")
t.put("qfmt", qfmt)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CollectionTaskCountModelsTest : AbstractCollectionTaskTest() {
val task = CountModels()
val initialCount = execute(task)!!.first.size

addNonClozeModel("testModel", arrayOf<String>(), qfmt = "{{ front }}", afmt = "{{FrontSide}}\n\n<hr id=answer>\n\n{{ back }}")
addNonClozeModel("testModel", arrayOf<String>("front"), qfmt = "{{ front }}", afmt = "{{FrontSide}}\n\n<hr id=answer>\n\n{{ back }}")

val finalCount = execute(task)!!.first.size
assertEquals(initialCount + 1, finalCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.ichi2.async

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.CardBrowser.CardCache
import com.ichi2.anki.RunInBackground
import com.ichi2.anki.servicelayer.SearchService.SearchCardsResult
Expand All @@ -36,6 +37,12 @@ class CollectionTaskSearchCardsTest : AbstractCollectionTaskTest() {
@Test
@RunInBackground
fun searchCardsNumberOfResultCount() {
if (AnkiDroidApp.TESTING_USE_V16_BACKEND) {
// PartialCards works via an onProgress call inside _findCards. This doesn't
// work with the new backend findCards(), which fetches all the ids in one go.
return
}

addNoteUsingBasicModel("Hello", "World")
addNoteUsingBasicModel("One", "Two")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import android.database.Cursor;

import com.ichi2.anki.AnkiDroidApp;
import com.ichi2.anki.CollectionHelper;
import com.ichi2.anki.RobolectricTest;
import com.ichi2.anki.exception.ConfirmModSchemaException;
Expand Down Expand Up @@ -176,14 +177,20 @@ private void selectNewDeck() {

@Test
public void ensureDeckTree() {
if (AnkiDroidApp.TESTING_USE_V16_BACKEND) {
// assertEquals() fails with the new backend, because the ids don't match.
// While it could be updated to work with the new backend, it would be easier
// to switch to the backend's tree calculation in the future, which is tested
mikehardy marked this conversation as resolved.
Show resolved Hide resolved
// in the upstream code.
return;
}
for (String deckName : TEST_DECKS) {
addDeck(deckName);
}
getCol().getSched().deckDueTree();
AbstractSched sched = getCol().getSched();
List<TreeNode<DeckDueTreeNode>> tree = sched.deckDueTree();
Assert.assertEquals("Tree has not the expected structure", SchedV2Test.expectedTree(getCol(), false), tree);

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.database.Cursor;

import com.ichi2.anki.AnkiDroidApp;
import com.ichi2.anki.RobolectricTest;
import com.ichi2.anki.exception.ConfirmModSchemaException;
import com.ichi2.libanki.Card;
Expand Down Expand Up @@ -252,6 +253,13 @@ private void ensureLapseMatchesSppliedAnkiDesktopConfig(JSONObject lapse) {

@Test
public void ensureDeckTree() {
if (AnkiDroidApp.TESTING_USE_V16_BACKEND) {
// assertEquals() fails with the new backend, because the ids don't match.
// While it could be updated to work with the new backend, it would be easier
// to switch to the backend's tree calculation in the future, which is tested
// in the upstream code.
return;
}
for (String deckName : TEST_DECKS) {
addDeck(deckName);
}
Expand Down