From 0515b49b984c4e407de382cd1d47816614bc85da Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 20 Mar 2025 18:02:44 +0600 Subject: [PATCH] add transactions tests --- .../commonTest/kotlin/TransactionsDSLTests.kt | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 transactions/src/commonTest/kotlin/TransactionsDSLTests.kt diff --git a/transactions/src/commonTest/kotlin/TransactionsDSLTests.kt b/transactions/src/commonTest/kotlin/TransactionsDSLTests.kt new file mode 100644 index 00000000000..5c1ab7c9973 --- /dev/null +++ b/transactions/src/commonTest/kotlin/TransactionsDSLTests.kt @@ -0,0 +1,93 @@ +import dev.inmo.micro_utils.transactions.doSuspendTransaction +import dev.inmo.micro_utils.transactions.rollbackableOperation +import kotlinx.coroutines.test.runTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +class TransactionsDSLTests { + @Test + fun successfulTest() = runTest { + val dataCollections = Array(100) { + Triple( + it, // expected data + false, // has rollback happen or not + -1 // actual data + ) + } + + val actionResult = doSuspendTransaction { + dataCollections.forEachIndexed { i, _ -> + val resultData = rollbackableOperation({ + dataCollections[i] = actionResult.copy(second = true) + }) { + val result = dataCollections[i] + dataCollections[i] = result.copy( + third = i + ) + dataCollections[i] + } + assertEquals(dataCollections[i], resultData) + assertTrue(dataCollections[i] === resultData) + } + true + }.getOrThrow() + + dataCollections.forEachIndexed { i, triple -> + assertFalse(triple.second) + assertEquals(triple.first, i) + assertEquals(i, triple.third) + } + assertTrue(actionResult) + } + @Test + fun fullTest() = runTest { + val testsCount = 100 + for (testNumber in 0 until testsCount) { + val error = IllegalStateException("Test must fail at $testNumber") + val dataCollections = Array(testsCount) { + Triple( + it, // expected data + false, // has rollback happen or not + -1 // actual data + ) + } + + val actionResult = doSuspendTransaction { + dataCollections.forEachIndexed { i, _ -> + val resultData = rollbackableOperation({ + assertTrue(error === this.error) + dataCollections[i] = actionResult.copy(second = true) + }) { + if (i == testNumber) throw error + val result = dataCollections[i] + dataCollections[i] = result.copy( + third = i + ) + dataCollections[i] + } + assertEquals(dataCollections[i], resultData) + assertTrue(dataCollections[i] === resultData) + } + true + }.getOrElse { + assertTrue(it === error) + true + } + + dataCollections.forEachIndexed { i, triple -> + if (i < testNumber) { + assertTrue(triple.second) + assertEquals(triple.first, i) + assertEquals(i, triple.third) + } else { + assertFalse(triple.second) + assertEquals(triple.first, i) + assertEquals(-1, triple.third) + } + } + assertTrue(actionResult) + } + } +} \ No newline at end of file