Skip to content

Commit

Permalink
Unit Tests for getNoteTags and setNoteTags API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Haz3-jolt committed Jan 18, 2025
1 parent 7f65fde commit 633f2a3
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
79 changes: 79 additions & 0 deletions AnkiDroid/src/test/java/com/ichi2/anki/AnkiDroidJsAPITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import com.ichi2.utils.BASIC_MODEL_NAME
import net.ankiweb.rsdroid.withoutUnicodeIsolation
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Ignore
import org.junit.Test
Expand Down Expand Up @@ -417,6 +418,68 @@ class AnkiDroidJsAPITest : RobolectricTest() {
assertEquals(CardType.New, cardAfterReset.type, "Card type after reset")
}

@Test
fun ankiGetNoteTagsTest() =
runTest {
val n =
addBasicNote("Front", "Back").update {
tags = mutableListOf("tag1", "tag2", "tag3")
}

val reviewer: Reviewer = startReviewer()
waitForAsyncTasksToComplete()

val jsapi = reviewer.jsApi

// test get tags for note
val expectedTags = n.tags
val response = getDataFromRequest("getNoteTags", jsapi, jsonObjectOf("noteId" to n.id))
val jsonResponse = JSONObject(response)
val actualTags = JSONArray(jsonResponse.getString("value"))

assertEquals(expectedTags.size, actualTags.length())
for (i in 0 until actualTags.length()) {
assertEquals(expectedTags[i], actualTags.getString(i))
}
}

@Test
fun ankiSetNoteTagsTest() =
runTest {
val n =
addBasicNote("Front", "Back").update {
tags = mutableListOf("tag1", "tag2", "tag3")
}

val reviewer: Reviewer = startReviewer()
waitForAsyncTasksToComplete()

val jsapi = reviewer.jsApi

// test set tags for note
val newTags =
JSONArray().apply {
put("tag4")
put("tag5")
put("tag6")
}

assertThat(
getDataFromRequest("setNoteTags", jsapi, jsonObjectOf("noteId" to n.id, "tags" to newTags)),
equalTo(formatApiResult(true)),
)
waitForAsyncTasksToComplete()

// Reload the note to ensure the tags are updated
val updatedNote = col.getNote(n.id)

// Verify the tags are updated
assertEquals(newTags.length(), updatedNote.tags.size)
for (i in 0 until newTags.length()) {
assertEquals(newTags.getString(i), updatedNote.tags[i])
}
}

companion object {
fun jsApiContract(data: String = ""): ByteArray =
JSONObject()
Expand Down Expand Up @@ -450,5 +513,21 @@ class AnkiDroidJsAPITest : RobolectricTest() {
jsAPI
.handleJsApiRequest(methodName, jsApiContract(apiData), false)
.decodeToString()

suspend fun getDataFromRequest(
methodName: String,
jsAPI: AnkiDroidJsAPI,
apiData: JSONObject,
): String =
jsAPI
.handleJsApiRequest(methodName, jsApiContract(apiData.toString()), false)
.decodeToString()
}
}

private fun jsonObjectOf(vararg pairs: Pair<String, Any>): JSONObject =
JSONObject().apply {
for ((key, value) in pairs) {
put(key, value)
}
}
7 changes: 7 additions & 0 deletions AnkiDroid/src/test/java/com/ichi2/testutils/TestClass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ interface TestClass {
col.decks.save(deckConfig)
}

/** Helper method to update a note */
fun Note.update(block: Note.() -> Unit): Note {
block(this)
col.updateNote(this)
return this
}

/** Helper method to all cards of a note */
fun Note.updateCards(update: Card.() -> Unit): Note {
cards().forEach { it.update(update) }
Expand Down

0 comments on commit 633f2a3

Please sign in to comment.