-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07d902b
commit cf34824
Showing
5 changed files
with
122 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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,86 @@ | ||
import com.mongodb.kotlin.client.coroutine.MongoClient | ||
import com.mongodb.client.model.Filters.eq | ||
import com.mongodb.client.model.Updates.inc | ||
import config.getConfig | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.jupiter.api.AfterAll | ||
import org.junit.jupiter.api.Assertions.assertNotNull | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.TestInstance | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class TransactionsTest { | ||
|
||
// :snippet-start: data-class | ||
data class Account( | ||
val accountId: String, | ||
val amount: Int | ||
) | ||
// :snippet-end: | ||
|
||
companion object { | ||
val config = getConfig() | ||
val client = MongoClient.create(config.connectionUri) | ||
val database = client.getDatabase("bank") | ||
|
||
@BeforeAll | ||
@JvmStatic | ||
fun beforeAll() { | ||
runBlocking { | ||
val savAcct = Account("9876", 900) | ||
database.getCollection<Account>("savings_accounts").insertOne(savAcct) | ||
|
||
val chkAcct = Account("9876", 50) | ||
database.getCollection<Account>("checking_accounts").insertOne(chkAcct) | ||
} | ||
} | ||
|
||
@AfterAll | ||
@JvmStatic | ||
fun afterAll() { | ||
runBlocking { | ||
database.drop() | ||
client.close() | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun transactionTest() = runBlocking { | ||
// :snippet-start: transaction-function | ||
// Set up the session | ||
val session = client.startSession() | ||
|
||
try { | ||
session.startTransaction() | ||
|
||
val savingsColl = database | ||
.getCollection<Account>("savings_accounts") | ||
val checkingColl = database | ||
.getCollection<Account>("checking_accounts") | ||
|
||
savingsColl.findOneAndUpdate( | ||
session, | ||
eq(Account::accountId.name, "9876"), | ||
inc(Account::amount.name, -100), | ||
) | ||
|
||
checkingColl.findOneAndUpdate( | ||
session, | ||
eq(Account::accountId.name, "9876"), | ||
inc(Account::amount.name, 100) | ||
) | ||
|
||
// Commit the transaction | ||
val result = session.commitTransaction() | ||
println("Transaction committed.") | ||
assertNotNull(result) // :remove: | ||
} catch (error: Exception) { | ||
println("An error occurred during the transaction: ${error.message}") | ||
// Abort the transaction | ||
session.abortTransaction() | ||
} | ||
// :snippet-end: | ||
} | ||
} |
7 changes: 4 additions & 3 deletions
7
source/examples/generated/TransactionsTest.snippet.data-class.kt
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
data class SavingsAccount(val accountId: String, val amount: Int) | ||
data class CheckingAccount(val accountId: String, val amount: Int) | ||
|
||
data class Account( | ||
val accountId: String, | ||
val amount: Int | ||
) |
31 changes: 31 additions & 0 deletions
31
source/examples/generated/TransactionsTest.snippet.transaction-function.kt
This file contains 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,31 @@ | ||
// Set up the session | ||
val session = client.startSession() | ||
|
||
try { | ||
session.startTransaction() | ||
|
||
val savingsColl = database | ||
.getCollection<Account>("savings_accounts") | ||
val checkingColl = database | ||
.getCollection<Account>("checking_accounts") | ||
|
||
savingsColl.findOneAndUpdate( | ||
session, | ||
eq(Account::accountId.name, "9876"), | ||
inc(Account::amount.name, -100), | ||
) | ||
|
||
checkingColl.findOneAndUpdate( | ||
session, | ||
eq(Account::accountId.name, "9876"), | ||
inc(Account::amount.name, 100) | ||
) | ||
|
||
// Commit the transaction | ||
val result = session.commitTransaction() | ||
println("Transaction committed.") | ||
} catch (error: Exception) { | ||
println("An error occurred during the transaction: ${error.message}") | ||
// Abort the transaction | ||
session.abortTransaction() | ||
} |
32 changes: 0 additions & 32 deletions
32
source/examples/generated/TransactionsTest.snippet.transaction.kt
This file was deleted.
Oops, something went wrong.
This file contains 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