-
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: make execute and watch throwable in swift
- Loading branch information
1 parent
72dd7e1
commit b8fe242
Showing
4 changed files
with
150 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,22 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { | |
database = nil | ||
try await super.tearDown() | ||
} | ||
|
||
|
||
func testExecuteError() async throws { | ||
do { | ||
_ = try await database.execute( | ||
sql: "INSERT INTO usersfail (id, name, email) VALUES (?, ?, ?)", | ||
parameters: ["1", "Test User", "[email protected]"] | ||
) | ||
XCTFail("Expected an error to be thrown") | ||
} catch { | ||
XCTAssertEqual(error.localizedDescription, """ | ||
error while compiling: INSERT INTO usersfail (id, name, email) VALUES (?, ?, ?) | ||
no such table: usersfail | ||
""") | ||
} | ||
} | ||
|
||
func testInsertAndGet() async throws { | ||
_ = try await database.execute( | ||
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", | ||
|
@@ -48,6 +63,27 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { | |
XCTAssertEqual(user.1, "Test User") | ||
XCTAssertEqual(user.2, "[email protected]") | ||
} | ||
|
||
func testGetError() async throws { | ||
do { | ||
let _ = try await database.get( | ||
sql: "SELECT id, name, email FROM usersfail WHERE id = ?", | ||
parameters: ["1"] | ||
) { cursor in | ||
( | ||
try cursor.getString(name: "id"), | ||
try cursor.getString(name: "name"), | ||
try cursor.getString(name: "email") | ||
) | ||
} | ||
XCTFail("Expected an error to be thrown") | ||
} catch { | ||
XCTAssertEqual(error.localizedDescription, """ | ||
error while compiling: SELECT id, name, email FROM usersfail WHERE id = ? | ||
no such table: usersfail | ||
""") | ||
} | ||
} | ||
|
||
func testGetOptional() async throws { | ||
let nonExistent: String? = try await database.getOptional( | ||
|
@@ -73,6 +109,27 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { | |
|
||
XCTAssertEqual(existing, "Test User") | ||
} | ||
|
||
func testGetOptionalError() async throws { | ||
do { | ||
let _ = try await database.getOptional( | ||
sql: "SELECT id, name, email FROM usersfail WHERE id = ?", | ||
parameters: ["1"] | ||
) { cursor in | ||
( | ||
try cursor.getString(name: "id"), | ||
try cursor.getString(name: "name"), | ||
try cursor.getString(name: "email") | ||
) | ||
} | ||
XCTFail("Expected an error to be thrown") | ||
} catch { | ||
XCTAssertEqual(error.localizedDescription, """ | ||
error while compiling: SELECT id, name, email FROM usersfail WHERE id = ? | ||
no such table: usersfail | ||
""") | ||
} | ||
} | ||
|
||
func testGetAll() async throws { | ||
_ = try await database.execute( | ||
|
@@ -96,6 +153,27 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { | |
XCTAssertEqual(users[1].0, "2") | ||
XCTAssertEqual(users[1].1, "User 2") | ||
} | ||
|
||
func testGetAllError() async throws { | ||
do { | ||
let _ = try await database.getAll( | ||
sql: "SELECT id, name, email FROM usersfail WHERE id = ?", | ||
parameters: ["1"] | ||
) { cursor in | ||
( | ||
try cursor.getString(name: "id"), | ||
try cursor.getString(name: "name"), | ||
try cursor.getString(name: "email") | ||
) | ||
} | ||
XCTFail("Expected an error to be thrown") | ||
} catch { | ||
XCTAssertEqual(error.localizedDescription, """ | ||
error while compiling: SELECT id, name, email FROM usersfail WHERE id = ? | ||
no such table: usersfail | ||
""") | ||
} | ||
} | ||
|
||
func testWatchTableChanges() async throws { | ||
let expectation = XCTestExpectation(description: "Watch changes") | ||
|
@@ -119,15 +197,15 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { | |
|
||
let resultsStore = ResultsStore() | ||
|
||
let stream = database.watch( | ||
let stream = try database.watch( | ||
sql: "SELECT name FROM users ORDER BY id", | ||
parameters: nil | ||
) { cursor in | ||
cursor.getString(index: 0)! | ||
} | ||
|
||
let watchTask = Task { | ||
for await names in stream { | ||
for try await names in stream { | ||
await resultsStore.append(names) | ||
if await resultsStore.count() == 2 { | ||
expectation.fulfill() | ||
|
@@ -152,6 +230,29 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { | |
XCTAssertEqual(finalResults.count, 2) | ||
XCTAssertEqual(finalResults[1], ["User 1", "User 2"]) | ||
} | ||
|
||
func testWatchError() async throws { | ||
do { | ||
let stream = try database.watch( | ||
sql: "SELECT name FROM usersfail ORDER BY id", | ||
parameters: nil | ||
) { 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, """ | ||
error while compiling: EXPLAIN SELECT name FROM usersfail ORDER BY id | ||
no such table: usersfail | ||
""") | ||
} | ||
} | ||
|
||
func testWriteTransaction() async throws { | ||
_ = try await database.writeTransaction { transaction in | ||
|