From 4a23c9abc00c3ec375d51c1febd5771c5741fc59 Mon Sep 17 00:00:00 2001 From: DominicGBauer Date: Fri, 7 Feb 2025 15:49:23 +0200 Subject: [PATCH 1/2] chore: add joins test --- .../KotlinPowerSyncDatabaseImplTests.swift | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift b/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift index 9490217..0e3d8d7 100644 --- a/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift +++ b/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift @@ -11,6 +11,15 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { Table(name: "users", columns: [ .text("name"), .text("email") + ]), + Table(name: "tasks", columns: [ + .text("user_id"), + .text("description"), + .text("tags") + ]), + Table(name: "comments", columns: [ + .text("task_id"), + .text("comment"), ]) ]) @@ -222,4 +231,64 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { XCTAssertEqual(result as! Int, 1) } } + + func testJoin() async throws { + struct JoinOutput: Equatable { + var name: String + var description: String + var comment: String + } + + + _ = try await database.writeTransaction { transaction in + _ = transaction.execute( + sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", + parameters: ["1", "Test User", "test@example.com"] + ) + + _ = transaction.execute( + sql: "INSERT INTO tasks (id, user_id, description) VALUES (?, ?, ?)", + parameters: ["1", "1", "task 1"] + ) + + _ = transaction.execute( + sql: "INSERT INTO tasks (id, user_id, description) VALUES (?, ?, ?)", + parameters: ["2", "1", "task 2"] + ) + + _ = transaction.execute( + sql: "INSERT INTO comments (id, task_id, comment) VALUES (?, ?, ?)", + parameters: ["1", "1", "comment 1"] + ) + + _ = transaction.execute( + sql: "INSERT INTO comments (id, task_id, comment) VALUES (?, ?, ?)", + parameters: ["2", "1", "comment 2"] + ) + } + + let result = try await database.getAll( + sql: """ + SELECT + users.name as name, + tasks.description as description, + comments.comment as comment + FROM users + LEFT JOIN tasks ON users.id = tasks.user_id + LEFT JOIN comments ON tasks.id = comments.task_id; + """, + parameters: [] + ) { cursor in + JoinOutput( + name: try cursor.getString(name: "name"), + description: try cursor.getString(name: "description"), + comment: try cursor.getStringOptional(name: "comment") ?? "" + ) + } + + XCTAssertEqual(result.count, 3) + XCTAssertEqual(result[0] , JoinOutput(name: "Test User", description: "task 1", comment: "comment 1")) + XCTAssertEqual(result[1] , JoinOutput(name: "Test User", description: "task 1", comment: "comment 2")) + XCTAssertEqual(result[2] , JoinOutput(name: "Test User", description: "task 2", comment: "")) + } } From 576a3c88955c34e63d128f5171f853f98cebe722 Mon Sep 17 00:00:00 2001 From: DominicGBauer Date: Wed, 19 Feb 2025 14:12:48 +0200 Subject: [PATCH 2/2] fix: issue --- .../Kotlin/KotlinPowerSyncDatabaseImplTests.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift b/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift index 2351925..045ec3b 100644 --- a/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift +++ b/Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift @@ -445,27 +445,27 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase { _ = try await database.writeTransaction { transaction in - _ = transaction.execute( + _ = try transaction.execute( sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)", parameters: ["1", "Test User", "test@example.com"] ) - _ = transaction.execute( + _ = try transaction.execute( sql: "INSERT INTO tasks (id, user_id, description) VALUES (?, ?, ?)", parameters: ["1", "1", "task 1"] ) - _ = transaction.execute( + _ = try transaction.execute( sql: "INSERT INTO tasks (id, user_id, description) VALUES (?, ?, ?)", parameters: ["2", "1", "task 2"] ) - _ = transaction.execute( + _ = try transaction.execute( sql: "INSERT INTO comments (id, task_id, comment) VALUES (?, ?, ?)", parameters: ["1", "1", "comment 1"] ) - _ = transaction.execute( + _ = try transaction.execute( sql: "INSERT INTO comments (id, task_id, comment) VALUES (?, ?, ?)", parameters: ["2", "1", "comment 2"] )