Repository navigation
refactor: use kotlinx.serialization for DB requests #755
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
rsdroid/src/test/java/net/ankiweb/DbRequestJsonCompatibilityTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
| package net.ankiweb | ||
|
|
||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import anki.ankidroid.SqlValue.DataCase | ||
| import net.ankiweb.rsdroid.BackendException | ||
| import net.ankiweb.rsdroid.BackendFactory.getBackend | ||
| import net.ankiweb.rsdroid.testing.RustBackendLoader.ensureSetup | ||
| import org.json.JSONArray | ||
| import org.json.JSONObject | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertThrows | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
|
|
||
| /** Covers argument types that Android's org.json used to convert for us. */ | ||
| @RunWith(AndroidJUnit4::class) | ||
| class DbRequestJsonCompatibilityTest { | ||
| @Test | ||
| fun arraysAndCollectionsRemainBlobs() { | ||
| val arguments = | ||
| listOf( | ||
| intArrayOf(0, 128, 255), | ||
| longArrayOf(0, 128, 255), | ||
| shortArrayOf(0, 128, 255), | ||
| arrayOf(0, 128, 255), | ||
| listOf(0, 128, 255), | ||
| linkedSetOf(0, 128, 255), | ||
| JSONArray(listOf(0, 128, 255)), | ||
| object : JSONArray(listOf(0, 128, 255)) {}, | ||
| ) | ||
| ensureSetup() | ||
| getBackend().use { backend -> | ||
| backend.openCollection(":memory:") | ||
| for (argument in arguments) { | ||
| val result = backend.fullQueryProto("SELECT typeof(?1), hex(?1)", arrayOf(argument)) | ||
| val fields = result.result.getRows(0).fieldsList | ||
| assertEquals(argument.javaClass.name, "blob", fields[0].stringValue) | ||
| assertEquals(argument.javaClass.name, "0080FF", fields[1].stringValue) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun jsonNullAndUnsupportedObjectsRemainNull() { | ||
| val arguments = | ||
| listOf( | ||
| JSONObject.NULL, | ||
| object { | ||
| override fun toString() = "should not be bound as text" | ||
| }, | ||
| ) | ||
| ensureSetup() | ||
| getBackend().use { backend -> | ||
| backend.openCollection(":memory:") | ||
| for (argument in arguments) { | ||
| val result = backend.fullQueryProto("SELECT typeof(?1), ?1", arrayOf(argument)) | ||
| val fields = result.result.getRows(0).fieldsList | ||
| assertEquals(argument.javaClass.name, "null", fields[0].stringValue) | ||
| assertEquals(argument.javaClass.name, DataCase.DATA_NOT_SET, fields[1].dataCase) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun mapsAndJsonObjectsAreStillRejected() { | ||
| val arguments = listOf(mapOf("value" to 1), JSONObject().put("value", 1)) | ||
| ensureSetup() | ||
| getBackend().use { backend -> | ||
| backend.openCollection(":memory:") | ||
| for (argument in arguments) { | ||
| assertThrows(BackendException::class.java) { | ||
| backend.fullQueryProto("SELECT ?", arrayOf(argument)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| package net.ankiweb | ||
|
|
||
| import net.ankiweb.rsdroid.dbRequestJson | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertThrows | ||
| import org.junit.Test | ||
|
|
||
| /** | ||
| * Tests the JSON sent to the backend. | ||
| * `/` is no longer escaped; serde treats it equivalently. | ||
| */ | ||
| class DbRequestJsonTest { | ||
| @Test | ||
| fun requestMatchesOrgJsonFormat() { | ||
| // The backend rejects booleans, but their JSON should match the previous output. | ||
| val json = | ||
| dbRequestJson( | ||
| "select ?, ?, ?, ?, ?, ?", | ||
| arrayOf<Any?>(null, "quote\" emoji🎴", 42L, 1.0, 2.5, true), | ||
| ).toStringUtf8() | ||
| assertEquals( | ||
| """{"kind":"query","sql":"select ?, ?, ?, ?, ?, ?","args":[null,"quote\" emoji🎴",42,1,2.5,true],"first_row_only":false}""", | ||
| json, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun wholeDoublesBindAsIntegers() { | ||
| val json = dbRequestJson("select ?", arrayOf<Any?>(3.0)).toStringUtf8() | ||
| assertEquals("""{"kind":"query","sql":"select ?","args":[3],"first_row_only":false}""", json) | ||
| } | ||
|
|
||
| @Test | ||
| fun wholeDoublesAboveTenMillionStillBindAsIntegers() { | ||
| // Double.toString uses scientific notation from 1e7, but org.json wrote | ||
| // whole values as integers. This includes timestamps in milliseconds. | ||
| val json = | ||
| dbRequestJson( | ||
| "select ?, ?, ?", | ||
| arrayOf<Any?>(1.0E7, 1751234567890.0, 9.2233720368547758E18), | ||
| ).toStringUtf8() | ||
| assertEquals( | ||
| """{"kind":"query","sql":"select ?, ?, ?","args":[10000000,1751234567890,9223372036854775807],"first_row_only":false}""", | ||
| json, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun floatsMatchOrgJson() { | ||
| val json = dbRequestJson("select ?, ?, ?, ?", arrayOf<Any?>(1.5f, 2.0f, 1.0E8f, 0.1f)).toStringUtf8() | ||
| assertEquals( | ||
| """{"kind":"query","sql":"select ?, ?, ?, ?","args":[1.5,2,100000000,0.1],"first_row_only":false}""", | ||
| json, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun negativeZeroMatchesOrgJson() { | ||
| // org.json wrote Double -0.0 as -0, but Float -0.0f as 0. | ||
| val json = dbRequestJson("select ?, ?", arrayOf<Any?>(-0.0, -0.0f)).toStringUtf8() | ||
| assertEquals("""{"kind":"query","sql":"select ?, ?","args":[-0,0],"first_row_only":false}""", json) | ||
|
david-allison marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Test | ||
| fun positiveZeroMatchesOrgJson() { | ||
| val json = dbRequestJson("select ?, ?", arrayOf<Any?>(0.0, 0.0f)).toStringUtf8() | ||
| assertEquals("""{"kind":"query","sql":"select ?, ?","args":[0,0],"first_row_only":false}""", json) | ||
| } | ||
|
|
||
| @Test | ||
| fun longLimitsArePreserved() { | ||
| val json = | ||
| dbRequestJson( | ||
| "select ?, ?, ?", | ||
| arrayOf<Any?>(-1L, Long.MAX_VALUE, Long.MIN_VALUE), | ||
| ).toStringUtf8() | ||
| assertEquals( | ||
| """{"kind":"query","sql":"select ?, ?, ?","args":[-1,9223372036854775807,-9223372036854775808],"first_row_only":false}""", | ||
| json, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun stringsAreEscapedCorrectly() { | ||
| val json = | ||
| dbRequestJson( | ||
| "select ?, ?", | ||
| arrayOf<Any?>("a/b", "line\nbreak\ttab\\bs\u0001"), | ||
| ).toStringUtf8() | ||
| assertEquals( | ||
| """{"kind":"query","sql":"select ?, ?","args":["a/b","line\nbreak\ttab\\bs\u0001"],"first_row_only":false}""", | ||
| json, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun nonFiniteNumbersAreRejected() { | ||
| for (value in listOf<Any>(Double.NaN, Double.POSITIVE_INFINITY, Float.NaN)) { | ||
| assertThrows(IllegalArgumentException::class.java) { | ||
| dbRequestJson("select ?", arrayOf<Any?>(value)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun blobsKeepTheirArrayEncoding() { | ||
| val json = dbRequestJson("select ?", arrayOf<Any?>(byteArrayOf(0, 1, 127))).toStringUtf8() | ||
| assertEquals( | ||
| """{"kind":"query","sql":"select ?","args":[[0,1,127]],"first_row_only":false}""", | ||
| json, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun firstRowOnlyIsSet() { | ||
| val json = dbRequestJson("select 1", emptyArray(), firstRowOnly = true).toStringUtf8() | ||
| assertEquals("""{"kind":"query","sql":"select 1","args":[],"first_row_only":true}""", json) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JsonPrimitive(this as Number) and JsonPrimitive(asLong) give identical output
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.