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

Renamed existing patch and added support for new patch #9

Merged
merged 1 commit into from
Sep 4, 2023
Merged
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
73 changes: 70 additions & 3 deletions src/commonMain/kotlin/uk/gibby/driver/rpc/Update.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,50 @@ class UpdateBuilder(private val table: String, private val db: Surreal) {
return surrealJson.decodeFromJsonElement(response)
}

/**
* Patch as json
*
* Patches all records in a table with the given Json patch.
*
* @param patchBuilder A json patch DSL to build the patch
* @receiver The update builder
* @return The records changed by the patch
*/
suspend fun patchAsJson(patchBuilder: JsonPatch.Builder.() -> Unit): JsonArray {
val builder = JsonPatch.Builder()
builder.patchBuilder()
val response = db.sendRequest("patch", buildJsonArray {
add(table)
add(surrealJson.encodeToJsonElement(builder.build()))
add(false)
})
return surrealJson.decodeFromJsonElement(response)
}

/**
* Patch
*
* Patches all records in a table with the given Json patch.
*
* @param T The type of the returned records
* @param patchBuilder A json patch DSL to build the patch
* @receiver The update builder
* @return The records changed by the patch
*/
suspend inline fun <reified T>patch(noinline patchBuilder: JsonPatch.Builder.() -> Unit): List<T> {
val response = patchAsJson(patchBuilder)
return surrealJson.decodeFromJsonElement(response)
}

/**
* Patch with diff
*
* Patches all records in a table with the given Json patch.
*
* @param patchBuilder A json patch DSL to build the patch
* @return The patches applied to the records
*/
suspend fun patch(patchBuilder: JsonPatch.Builder.() -> Unit): List<List<JsonPatch>> {
suspend fun patchWithDiff(patchBuilder: JsonPatch.Builder.() -> Unit): List<List<JsonPatch>> {
val builder = JsonPatch.Builder()
builder.patchBuilder()
val response = db.sendRequest("patch", buildJsonArray {
Expand Down Expand Up @@ -260,9 +295,42 @@ class UpdateIdBuilder(private val id: String, private val db: Surreal) {
* Patches a single record in a table with the given Json patch.
*
* @param patchBuilder A json patch DSL to build the patch
* @return The records changed by the patch
*/
suspend fun patchAsJson(patchBuilder: JsonPatch.Builder.() -> Unit): JsonElement {
val builder = JsonPatch.Builder()
builder.patchBuilder()
return db.sendRequest("patch", buildJsonArray {
add(id)
add(surrealJson.encodeToJsonElement(builder.build()))
add(false)
})
}

/**
* Patch
*
* Patches a single record in a table with the given Json patch.
*
* @param T The type of the returned records
* @param patchBuilder A json patch DSL to build the patch
* @receiver The update builder
* @return The records changed by the patch
*/
suspend inline fun <reified T>patch(noinline patchBuilder: JsonPatch.Builder.() -> Unit): T {
val response = patchAsJson(patchBuilder)
return surrealJson.decodeFromJsonElement(response)
}

/**
* Patch with diff
*
* Patches a single record in a table with the given Json patch.
*
* @param patchBuilder A json patch DSL to build the patch
* @return The patches applied to the record
*/
suspend fun patch(patchBuilder: JsonPatch.Builder.() -> Unit): List<JsonPatch> {
suspend fun patchWithDiff(patchBuilder: JsonPatch.Builder.() -> Unit): List<JsonPatch> {
val builder = JsonPatch.Builder()
builder.patchBuilder()
val result = db.sendRequest("patch", buildJsonArray {
Expand All @@ -272,7 +340,6 @@ class UpdateIdBuilder(private val id: String, private val db: Surreal) {
})
return surrealJson.decodeFromJsonElement(result)
}

}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/commonTest/kotlin/LiveQueryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class LiveQueryTest {
val incoming = connection.observeLiveQuery<TestClass>("test")
connection.create("test", "first").content(TestClass("thing", 1))
connection.create("test", "second").content(TestClass("thing", 2))
connection.update("test", "first").patch {
connection.update("test", "first").patchWithDiff {
replace("myText", "thing2")
}
connection.delete("test", "second")
Expand Down Expand Up @@ -56,7 +56,7 @@ class LiveQueryTest {

connection.create("test", "first").content(TestClass("thing", 1))
connection.create("test", "second").content(TestClass("thing", 2))
connection.update("test", "first").patch {
connection.update("test", "first").patchWithDiff {
replace("myText", "thing2")
}
connection.delete("test", "second")
Expand Down Expand Up @@ -90,7 +90,7 @@ class LiveQueryTest {

connection.create("test", "first").content(TestClass("thing", 1))
connection.create("test", "second").content(TestClass("thing", 2))
connection.update("test", "first").patch {
connection.update("test", "first").patchWithDiff {
replace("myText", "thing2")
}
connection.delete("test", "second")
Expand Down
63 changes: 57 additions & 6 deletions src/commonTest/kotlin/PatchTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,98 @@ import kotlin.test.assertEquals
class PatchTest {

@Test
fun testBasicPatch() = runTest {
fun testBasicPatchReturningDiff() = runTest {
cleanDatabase()
val connection = Surreal("localhost")
connection.connect()
connection.signin("root", "root")
connection.use("test", "test")
connection.create("test").content(TestClass("first", 1))
connection.create("test").content(TestClass("second", 2))
val result = connection.update("test").patch {
val result = connection.update("test").patchWithDiff {
replace("myText", "updated")
}
assertEquals(2, result.size)
}

@Test
fun testPatchWithId() = runTest {
fun testPatchWithIdReturningDiff() = runTest {
cleanDatabase()
val connection = Surreal("localhost")
connection.connect()
connection.signin("root", "root")
connection.use("test", "test")
connection.create("test", "123").content(TestClass("first", 1))
connection.update("test", "123").patch {
connection.update("test", "123").patchWithDiff {
replace("myText", "updated")
}
val result = connection.select<TestClass>("test", "123")
assertEquals("updated", result.myText)
}

@Test
fun testPatchWithThing() = runTest {
fun testPatchWithThingReturningDiff() = runTest {
cleanDatabase()
val connection = Surreal("localhost")
connection.connect()
connection.signin("root", "root")
connection.use("test", "test")
val thing = connection.create("test").content(TestClass("first", 1))
connection.update(thing.id).patch {
connection.update(thing.id).patchWithDiff {
replace("myText", "updated")
}
val result = connection.select(thing.id)
assertEquals("updated", result.myText)
}

@Test
fun testPatchWithIdReturningRecords() = runTest {
cleanDatabase()
val connection = Surreal("localhost")
connection.connect()
connection.signin("root", "root")
connection.use("test", "test")
connection.create("test", "123").content(TestClass("first", 1))
connection.update("test", "123").patch<TestClass> {
replace("myText", "updated")
}
val result = connection.select<TestClass>("test", "123")
assertEquals("updated", result.myText)
assertEquals(1, result.myNumber)
}

@Test
fun testPatchWithThingReturningRecords() = runTest {
cleanDatabase()
val connection = Surreal("localhost")
connection.connect()
connection.signin("root", "root")
connection.use("test", "test")
val thing = connection.create("test").content(TestClass("first", 1))
connection.update(thing.id).patch<TestClass> {
replace("myText", "updated")
}
val result = connection.select(thing.id)
assertEquals("updated", result.myText)
assertEquals(1, result.myNumber)
}

@Test
fun testPatchReturningRecords() = runTest {
cleanDatabase()
val connection = Surreal("localhost")
connection.connect()
connection.signin("root", "root")
connection.use("test", "test")
connection.create("test").content(TestClass("first", 1))
connection.create("test").content(TestClass("second", 2))
val result = connection.update("test").patch<TestClass> {
replace("myText", "updated")
}.sortedBy { it.myNumber }
assertEquals(2, result.size)
assertEquals("updated", result[0].myText)
assertEquals(1, result[0].myNumber)
assertEquals("updated", result[1].myText)
assertEquals(2, result[1].myNumber)
}
}