Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add joins test #19

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase {
schema = Schema(tables: [
Table(name: "users", columns: [
.text("name"),
.text("email"),
.text("email")
]),
Table(name: "tasks", columns: [
.text("user_id"),
.text("description"),
.text("tags")
]),
Table(name: "comments", columns: [
.text("task_id"),
.text("comment"),
])
])

database = KotlinPowerSyncDatabaseImpl(
Expand Down Expand Up @@ -426,4 +435,64 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase {
""")
}
}

func testJoin() async throws {
struct JoinOutput: Equatable {
var name: String
var description: String
var comment: String
}


_ = 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 tasks (id, user_id, description) VALUES (?, ?, ?)",
parameters: ["1", "1", "task 1"]
)

_ = try transaction.execute(
sql: "INSERT INTO tasks (id, user_id, description) VALUES (?, ?, ?)",
parameters: ["2", "1", "task 2"]
)

_ = try transaction.execute(
sql: "INSERT INTO comments (id, task_id, comment) VALUES (?, ?, ?)",
parameters: ["1", "1", "comment 1"]
)

_ = try 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: ""))
}
}