|
| 1 | +package test.cask |
| 2 | + |
| 3 | +import cask.database.transactional |
| 4 | +import scalasql.DbApi.Txn |
| 5 | +import scalasql.core.DbClient |
| 6 | +import scalasql.simple.{*, given} |
| 7 | +import scalasql.SqliteDialect._ |
| 8 | +import utest._ |
| 9 | + |
| 10 | +object DatabaseTests extends TestSuite { |
| 11 | + |
| 12 | + // Test database setup |
| 13 | + val tmpDb = java.nio.file.Files.createTempDirectory("test-cask-sqlite") |
| 14 | + val sqliteDataSource = new org.sqlite.SQLiteDataSource() |
| 15 | + sqliteDataSource.setUrl(s"jdbc:sqlite:$tmpDb/test.db") |
| 16 | + |
| 17 | + given testDbClient: DbClient = new DbClient.DataSource( |
| 18 | + sqliteDataSource, |
| 19 | + config = new {} |
| 20 | + ) |
| 21 | + |
| 22 | + case class TestItem(id: Int, name: String, value: Int) |
| 23 | + object TestItem extends SimpleTable[TestItem] |
| 24 | + |
| 25 | + // Initialize test table |
| 26 | + testDbClient.getAutoCommitClientConnection.updateRaw( |
| 27 | + """DROP TABLE IF EXISTS test_item; |
| 28 | + |CREATE TABLE test_item ( |
| 29 | + | id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 30 | + | name TEXT, |
| 31 | + | value INTEGER |
| 32 | + |)""".stripMargin |
| 33 | + ) |
| 34 | + |
| 35 | + def cleanTable() = { |
| 36 | + testDbClient.getAutoCommitClientConnection.updateRaw("DELETE FROM test_item") |
| 37 | + } |
| 38 | + |
| 39 | + val tests = Tests { |
| 40 | + test("basic transaction commits on success") { |
| 41 | + cleanTable() |
| 42 | + |
| 43 | + var committed = false |
| 44 | + testDbClient.transaction { txn => |
| 45 | + txn.run(TestItem.insert.columns(_.name := "test1", _.value := 100)) |
| 46 | + committed = true |
| 47 | + } |
| 48 | + |
| 49 | + assert(committed) |
| 50 | + val items = testDbClient.getAutoCommitClientConnection.run(TestItem.select) |
| 51 | + assert(items.length == 1) |
| 52 | + assert(items.head.name == "test1") |
| 53 | + } |
| 54 | + |
| 55 | + test("basic transaction rolls back on exception") { |
| 56 | + cleanTable() |
| 57 | + |
| 58 | + try { |
| 59 | + testDbClient.transaction { txn => |
| 60 | + txn.run(TestItem.insert.columns(_.name := "fail", _.value := 999)) |
| 61 | + throw new Exception("Forced failure") |
| 62 | + } |
| 63 | + } catch { |
| 64 | + case _: Exception => // Expected |
| 65 | + } |
| 66 | + |
| 67 | + val items = testDbClient.getAutoCommitClientConnection.run(TestItem.select) |
| 68 | + assert(items.isEmpty) |
| 69 | + } |
| 70 | + |
| 71 | + test("transactional decorator instantiation") { |
| 72 | + val decorator = new transactional(using testDbClient) |
| 73 | + assert(decorator != null) |
| 74 | + } |
| 75 | + |
| 76 | + test("decorator wrapFunction with success") { |
| 77 | + cleanTable() |
| 78 | + |
| 79 | + val decorator = new transactional(using testDbClient) |
| 80 | + |
| 81 | + // Simulate successful route execution |
| 82 | + testDbClient.transaction { txn => |
| 83 | + txn.run(TestItem.insert.columns(_.name := "decorator-test", _.value := 42)) |
| 84 | + // Transaction commits automatically |
| 85 | + } |
| 86 | + |
| 87 | + val items = testDbClient.getAutoCommitClientConnection.run(TestItem.select) |
| 88 | + assert(items.length == 1) |
| 89 | + assert(items.head.name == "decorator-test") |
| 90 | + } |
| 91 | + |
| 92 | + test("decorator rollback on exception") { |
| 93 | + cleanTable() |
| 94 | + |
| 95 | + val decorator = new transactional(using testDbClient) |
| 96 | + |
| 97 | + try { |
| 98 | + testDbClient.transaction { txn => |
| 99 | + txn.run(TestItem.insert.columns(_.name := "rollback-test", _.value := 500)) |
| 100 | + throw new Exception("Simulated error") |
| 101 | + } |
| 102 | + } catch { |
| 103 | + case _: Exception => // Expected |
| 104 | + } |
| 105 | + |
| 106 | + val items = testDbClient.getAutoCommitClientConnection.run(TestItem.select) |
| 107 | + assert(items.isEmpty) |
| 108 | + } |
| 109 | + |
| 110 | + test("multiple inserts in single transaction") { |
| 111 | + cleanTable() |
| 112 | + |
| 113 | + testDbClient.transaction { txn => |
| 114 | + txn.run(TestItem.insert.columns(_.name := "item1", _.value := 1)) |
| 115 | + txn.run(TestItem.insert.columns(_.name := "item2", _.value := 2)) |
| 116 | + txn.run(TestItem.insert.columns(_.name := "item3", _.value := 3)) |
| 117 | + } |
| 118 | + |
| 119 | + val items = testDbClient.getAutoCommitClientConnection.run(TestItem.select) |
| 120 | + assert(items.length == 3) |
| 121 | + } |
| 122 | + |
| 123 | + test("rollback discards all changes in transaction") { |
| 124 | + cleanTable() |
| 125 | + |
| 126 | + try { |
| 127 | + testDbClient.transaction { txn => |
| 128 | + txn.run(TestItem.insert.columns(_.name := "item1", _.value := 1)) |
| 129 | + txn.run(TestItem.insert.columns(_.name := "item2", _.value := 2)) |
| 130 | + throw new Exception("Rollback all") |
| 131 | + } |
| 132 | + } catch { |
| 133 | + case _: Exception => // Expected |
| 134 | + } |
| 135 | + |
| 136 | + val items = testDbClient.getAutoCommitClientConnection.run(TestItem.select) |
| 137 | + assert(items.isEmpty) |
| 138 | + } |
| 139 | + |
| 140 | + test("DbClient given context is available") { |
| 141 | + // Verify implicit DbClient is properly resolved |
| 142 | + val decorator = new transactional(using testDbClient) |
| 143 | + assert(decorator != null) |
| 144 | + |
| 145 | + // Verify we can use it in a transaction |
| 146 | + cleanTable() |
| 147 | + testDbClient.transaction { txn => |
| 148 | + val count = txn.run(TestItem.select).length |
| 149 | + assert(count == 0) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments