From c89f4b1ea09e5be0f7f22fca884300c60694d96d Mon Sep 17 00:00:00 2001 From: JaniruTEC <52893617+JaniruTEC@users.noreply.github.com> Date: Tue, 19 Mar 2024 14:55:53 +0100 Subject: [PATCH] Added test for "update" *This commit is related to issue #529 [1]* [1] https://github.com/cryptomator/android/issues/529 --- .../MappingSupportSQLiteDatabaseTest.kt | 80 ++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/data/src/test/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabaseTest.kt b/data/src/test/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabaseTest.kt index c17959338..148a8fb61 100644 --- a/data/src/test/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabaseTest.kt +++ b/data/src/test/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabaseTest.kt @@ -5,6 +5,7 @@ import org.mockito.Mockito.`when` as whenCalled import org.mockito.kotlin.any as reifiedAny import org.mockito.kotlin.anyOrNull as reifiedAnyOrNull import org.mockito.kotlin.argThat as reifiedArgThat +import android.content.ContentValues import android.database.Cursor import android.database.MatrixCursor import android.os.CancellationSignal @@ -23,6 +24,7 @@ import org.mockito.Mockito.mock import org.mockito.Mockito.verify import org.mockito.Mockito.verifyNoMoreInteractions import org.mockito.kotlin.anyArray +import org.mockito.kotlin.eq import org.mockito.kotlin.isNull import java.util.stream.Stream import kotlin.streams.asStream @@ -147,7 +149,18 @@ class MappingSupportSQLiteDatabaseTest { verifyNoMoreInteractions(delegateMock) } - /* TODO insert, update */ + /* TODO insert */ + + @ParameterizedTest + @MethodSource("org.cryptomator.data.db.sqlmapping.MappingSupportSQLiteDatabaseTestKt#sourceForTestUpdate") + fun testUpdate(contentValues: CallData, whereClauses: CallData, whereArgs: CallData?>) { + identityMapping.update("id_test", 1001, contentValues.idCall, whereClauses.idCall, whereArgs.idCall?.toTypedArray()) + commentMapping.update("comment_test", 1002, contentValues.commentCall, whereClauses.commentCall, whereArgs.commentCall?.toTypedArray()) + + verify(delegateMock).update(eq("id_test"), eq(1001), anyPseudoEquals(contentValues.idExpected, contentValuesProperties), eq(whereClauses.idExpected), eq(whereArgs.idExpected?.toTypedArray())) + verify(delegateMock).update(eq("comment_test"), eq(1002), anyPseudoEquals(contentValues.commentExpected, contentValuesProperties), eq(whereClauses.commentExpected), eq(whereArgs.commentExpected?.toTypedArray())) + verifyNoMoreInteractions(delegateMock) + } @Test fun testDelete() { @@ -276,6 +289,11 @@ private class CachingSupportSQLiteProgram : SupportSQLiteProgram { override fun close() = throw UnsupportedOperationException("Stub!") } +private val contentValuesProperties + get() = setOf( + ContentValues::valueSet + ) + private val DUMMY_CURSOR: Cursor get() = MatrixCursor(arrayOf()) @@ -286,6 +304,14 @@ private fun mockCancellationSignal(isCanceled: Boolean): CancellationSignal { return mock } +private fun mockContentValues(vararg elements: Pair): ContentValues { + val entries = mapOf(*elements) + val mock = mock(ContentValues::class.java) + whenCalled(mock.valueSet()).thenReturn(entries.entries) + whenCalled(mock.toString()).thenReturn("Mock${entries}") + return mock +} + data class CallData( val idCall: T, val commentCall: T, @@ -326,11 +352,63 @@ fun sourceForTestQueryCancelable(): Stream { return queries.cartesianProduct(signals).map { it.toList() }.toArgumentsStream() } +fun sourceForTestUpdate(): Stream { + val contentValues = sequenceOf( + CallData( + mockContentValues("key1" to "value1"), + mockContentValues("key2" to "value2"), + mockContentValues("key1" to "value1"), + mockContentValues("key2" to "value2") + ), + CallData( + mockContentValues("key1" to null), + mockContentValues(), + mockContentValues("key1" to null), + mockContentValues() + ) + ) + val whereClauses = listOf>( + CallData( + "`col1` = ?", + "`col2` = ?", + "`col1` = ?", + "`col2` = ? -- Comment!" + ), + CallData( + null, + null, + null, + "1 = 1 -- Comment!" + ) + ) + val whereArgs = listOf?>>( //Use List instead of Array to make result data more readable + CallData( + listOf(), + null, + listOf(), + null + ), + CallData( + listOf("val1"), + listOf("val2"), + listOf("val1"), + listOf("val2") + ) + ) + + return contentValues.cartesianProduct(whereClauses).cartesianProduct(whereArgs).map { it.toList() }.toArgumentsStream() +} + @JvmName("cartesianProductTwo") fun Sequence.cartesianProduct(other: Iterable): Sequence> = flatMap { a -> other.asSequence().map { b -> a to b } } +@JvmName("cartesianProductThree") +fun Sequence>.cartesianProduct(other: Iterable): Sequence> = flatMap { abPair -> + other.asSequence().map { c -> Triple(abPair.first, abPair.second, c) } +} + fun Sequence>.toArgumentsStream(): Stream = map { Arguments { it.toTypedArray() } }.asStream() \ No newline at end of file