@@ -13,9 +13,9 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase {
1313 columns: [
1414 . text( " name " ) ,
1515 . text( " email " ) ,
16- . text( " photo_id " ) ,
16+ . text( " photo_id " )
1717 ]
18- ) ,
18+ )
1919 ] )
2020
2121 database = KotlinPowerSyncDatabaseImpl (
@@ -551,4 +551,85 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase {
551551 // The warning should not be present due to the min severity
552552 XCTAssert ( warningIndex == nil )
553553 }
554+
555+ func testJoin( ) async throws {
556+ struct JoinOutput : Equatable {
557+ var name : String
558+ var description : String
559+ var comment : String
560+ }
561+
562+ try await database. updateSchema ( schema:
563+ Schema ( tables: [
564+ Table ( name: " users " , columns: [
565+ . text( " name " ) ,
566+ . text( " email " ) ,
567+ . text( " photo_id " )
568+ ] ) ,
569+ Table ( name: " tasks " , columns: [
570+ . text( " user_id " ) ,
571+ . text( " description " ) ,
572+ . text( " tags " )
573+ ] ) ,
574+ Table ( name: " comments " , columns: [
575+ . text( " task_id " ) ,
576+ . text( " comment " ) ,
577+ ] )
578+ ] )
579+ )
580+
581+ try await database. writeTransaction { transaction in
582+ let userId = UUID ( ) . uuidString
583+ try transaction. execute (
584+ sql: " INSERT INTO users (id, name, email) VALUES (?, ?, ?) " ,
585+ parameters
: [ userId
, " Test User " , " [email protected] " ] 586+ )
587+
588+ let task1Id = UUID ( ) . uuidString
589+ let task2Id = UUID ( ) . uuidString
590+
591+ try transaction. execute (
592+ sql: " INSERT INTO tasks (id, user_id, description) VALUES (?, ?, ?) " ,
593+ parameters: [ task1Id, userId, " task 1 " ]
594+ )
595+
596+ try transaction. execute (
597+ sql: " INSERT INTO tasks (id, user_id, description) VALUES (?, ?, ?) " ,
598+ parameters: [ task2Id, userId, " task 2 " ]
599+ )
600+
601+ try transaction. execute (
602+ sql: " INSERT INTO comments (id, task_id, comment) VALUES (uuid(), ?, ?) " ,
603+ parameters: [ task1Id, " comment 1 " ]
604+ )
605+
606+ try transaction. execute (
607+ sql: " INSERT INTO comments (id, task_id, comment) VALUES (uuid(), ?, ?) " ,
608+ parameters: [ task2Id, " comment 2 " ]
609+ )
610+ }
611+
612+ let result = try await database. getAll (
613+ sql: """
614+ SELECT
615+ users.name as name,
616+ tasks.description as description,
617+ comments.comment as comment
618+ FROM users
619+ LEFT JOIN tasks ON users.id = tasks.user_id
620+ LEFT JOIN comments ON tasks.id = comments.task_id;
621+ """ ,
622+ parameters: [ ]
623+ ) { cursor in
624+ try JoinOutput (
625+ name: cursor. getString ( name: " name " ) ,
626+ description: cursor. getString ( name: " description " ) ,
627+ comment: cursor. getStringOptional ( name: " comment " ) ?? " "
628+ )
629+ }
630+
631+ XCTAssertEqual ( result. count, 2 )
632+ XCTAssertEqual ( result [ 0 ] , JoinOutput ( name: " Test User " , description: " task 1 " , comment: " comment 1 " ) )
633+ XCTAssertEqual ( result [ 1 ] , JoinOutput ( name: " Test User " , description: " task 2 " , comment: " comment 2 " ) )
634+ }
554635}
0 commit comments