-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add transaction and watch error handling
- Loading branch information
1 parent
b8fe242
commit 609c4d8
Showing
5 changed files
with
155 additions
and
50 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
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
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
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
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 |
---|---|---|
|
@@ -26,7 +26,7 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { | |
database = nil | ||
try await super.tearDown() | ||
} | ||
|
||
func testExecuteError() async throws { | ||
do { | ||
_ = try await database.execute( | ||
|
@@ -41,7 +41,7 @@ no such table: usersfail | |
""") | ||
} | ||
} | ||
|
||
func testInsertAndGet() async throws { | ||
_ = try await database.execute( | ||
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", | ||
|
@@ -63,7 +63,7 @@ no such table: usersfail | |
XCTAssertEqual(user.1, "Test User") | ||
XCTAssertEqual(user.2, "[email protected]") | ||
} | ||
|
||
func testGetError() async throws { | ||
do { | ||
let _ = try await database.get( | ||
|
@@ -109,7 +109,7 @@ no such table: usersfail | |
|
||
XCTAssertEqual(existing, "Test User") | ||
} | ||
|
||
func testGetOptionalError() async throws { | ||
do { | ||
let _ = try await database.getOptional( | ||
|
@@ -153,7 +153,7 @@ no such table: usersfail | |
XCTAssertEqual(users[1].0, "2") | ||
XCTAssertEqual(users[1].1, "User 2") | ||
} | ||
|
||
func testGetAllError() async throws { | ||
do { | ||
let _ = try await database.getAll( | ||
|
@@ -230,7 +230,7 @@ no such table: usersfail | |
XCTAssertEqual(finalResults.count, 2) | ||
XCTAssertEqual(finalResults[1], ["User 1", "User 2"]) | ||
} | ||
|
||
func testWatchError() async throws { | ||
do { | ||
let stream = try database.watch( | ||
|
@@ -239,12 +239,12 @@ no such table: usersfail | |
) { cursor in | ||
cursor.getString(index: 0)! | ||
} | ||
|
||
// Actually consume the stream to trigger the error | ||
for try await _ in stream { | ||
XCTFail("Should not receive any values") | ||
} | ||
|
||
XCTFail("Expected an error to be thrown") | ||
} catch { | ||
XCTAssertEqual(error.localizedDescription, """ | ||
|
@@ -256,12 +256,12 @@ no such table: usersfail | |
|
||
func testWriteTransaction() async throws { | ||
_ = try await database.writeTransaction { transaction in | ||
_ = transaction.execute( | ||
_ = try transaction.execute( | ||
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", | ||
parameters: ["1", "Test User", "[email protected]"] | ||
) | ||
|
||
_ = transaction.execute( | ||
_ = try transaction.execute( | ||
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", | ||
parameters: ["2", "Test User 2", "[email protected]"] | ||
) | ||
|
@@ -283,12 +283,12 @@ no such table: usersfail | |
|
||
_ = try await database.writeTransaction { transaction in | ||
for i in 1...loopCount { | ||
_ = transaction.execute( | ||
_ = try transaction.execute( | ||
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", | ||
parameters: [String(i), "Test User \(i)", "test\(i)@example.com"] | ||
) | ||
|
||
_ = transaction.execute( | ||
_ = try transaction.execute( | ||
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", | ||
parameters: [String(i*10000), "Test User \(i)-2", "test\(i)[email protected]"] | ||
) | ||
|
@@ -305,6 +305,51 @@ no such table: usersfail | |
XCTAssertEqual(result as! Int, 2 * loopCount) | ||
} | ||
|
||
func testWriteTransactionError() async throws { | ||
do { | ||
_ = try await database.writeTransaction { transaction in | ||
_ = try transaction.execute( | ||
sql: "INSERT INTO usersfail (id, name, email) VALUES (?, ?, ?)", | ||
parameters: ["2", "Test User 2", "[email protected]"] | ||
) | ||
} | ||
} catch { | ||
XCTAssertEqual(error.localizedDescription, """ | ||
error while compiling: INSERT INTO usersfail (id, name, email) VALUES (?, ?, ?) | ||
no such table: usersfail | ||
""") | ||
} | ||
} | ||
|
||
func testWriteTransactionErrorPerformsRollBack() async throws { | ||
do { | ||
_ = try await database.writeTransaction { transaction in | ||
_ = try transaction.execute( | ||
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", | ||
parameters: ["1", "Test User", "[email protected]"] | ||
) | ||
|
||
_ = try transaction.execute( | ||
sql: "INSERT INTO usersfail (id, name, email) VALUES (?, ?, ?)", | ||
parameters: ["2", "Test User 2", "[email protected]"] | ||
) | ||
} | ||
} catch { | ||
XCTAssertEqual(error.localizedDescription, """ | ||
error while compiling: INSERT INTO usersfail (id, name, email) VALUES (?, ?, ?) | ||
no such table: usersfail | ||
""") | ||
} | ||
|
||
let result = try await database.getOptional( | ||
sql: "SELECT COUNT(*) FROM users", | ||
parameters: [] | ||
) { cursor in try cursor.getLong(index: 0) | ||
} | ||
|
||
XCTAssertEqual(result, 0) | ||
} | ||
|
||
func testReadTransaction() async throws { | ||
_ = try await database.execute( | ||
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", | ||
|
@@ -313,7 +358,7 @@ no such table: usersfail | |
|
||
|
||
_ = try await database.readTransaction { transaction in | ||
let result = transaction.get( | ||
let result = try transaction.get( | ||
sql: "SELECT COUNT(*) FROM users", | ||
parameters: [] | ||
) { cursor in | ||
|
@@ -323,4 +368,22 @@ no such table: usersfail | |
XCTAssertEqual(result as! Int, 1) | ||
} | ||
} | ||
|
||
func testReadTransactionError() async throws { | ||
do { | ||
_ = try await database.readTransaction { transaction in | ||
let result = try transaction.get( | ||
sql: "SELECT COUNT(*) FROM usersfail", | ||
parameters: [] | ||
) { cursor in | ||
cursor.getLong(index: 0) | ||
} | ||
} | ||
} catch { | ||
XCTAssertEqual(error.localizedDescription, """ | ||
error while compiling: SELECT COUNT(*) FROM usersfail | ||
no such table: usersfail | ||
""") | ||
} | ||
} | ||
} |