@@ -11,6 +11,15 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase {
11
11
Table ( name: " users " , columns: [
12
12
. text( " name " ) ,
13
13
. text( " email " )
14
+ ] ) ,
15
+ Table ( name: " tasks " , columns: [
16
+ . text( " user_id " ) ,
17
+ . text( " description " ) ,
18
+ . text( " tags " )
19
+ ] ) ,
20
+ Table ( name: " comments " , columns: [
21
+ . text( " task_id " ) ,
22
+ . text( " comment " ) ,
14
23
] )
15
24
] )
16
25
@@ -222,4 +231,64 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase {
222
231
XCTAssertEqual ( result as! Int , 1 )
223
232
}
224
233
}
234
+
235
+ func testJoin( ) async throws {
236
+ struct JoinOutput : Equatable {
237
+ var name : String
238
+ var description : String
239
+ var comment : String
240
+ }
241
+
242
+
243
+ _ = try await database. writeTransaction { transaction in
244
+ _ = transaction. execute (
245
+ sql: " INSERT INTO users (id, name, email) VALUES (?, ?, ?) " ,
246
+ parameters
: [ " 1 " , " Test User " , " [email protected] " ]
247
+ )
248
+
249
+ _ = transaction. execute (
250
+ sql: " INSERT INTO tasks (id, user_id, description) VALUES (?, ?, ?) " ,
251
+ parameters: [ " 1 " , " 1 " , " task 1 " ]
252
+ )
253
+
254
+ _ = transaction. execute (
255
+ sql: " INSERT INTO tasks (id, user_id, description) VALUES (?, ?, ?) " ,
256
+ parameters: [ " 2 " , " 1 " , " task 2 " ]
257
+ )
258
+
259
+ _ = transaction. execute (
260
+ sql: " INSERT INTO comments (id, task_id, comment) VALUES (?, ?, ?) " ,
261
+ parameters: [ " 1 " , " 1 " , " comment 1 " ]
262
+ )
263
+
264
+ _ = transaction. execute (
265
+ sql: " INSERT INTO comments (id, task_id, comment) VALUES (?, ?, ?) " ,
266
+ parameters: [ " 2 " , " 1 " , " comment 2 " ]
267
+ )
268
+ }
269
+
270
+ let result = try await database. getAll (
271
+ sql: """
272
+ SELECT
273
+ users.name as name,
274
+ tasks.description as description,
275
+ comments.comment as comment
276
+ FROM users
277
+ LEFT JOIN tasks ON users.id = tasks.user_id
278
+ LEFT JOIN comments ON tasks.id = comments.task_id;
279
+ """ ,
280
+ parameters: [ ]
281
+ ) { cursor in
282
+ JoinOutput (
283
+ name: try cursor. getString ( name: " name " ) ,
284
+ description: try cursor. getString ( name: " description " ) ,
285
+ comment: try cursor. getStringOptional ( name: " comment " ) ?? " "
286
+ )
287
+ }
288
+
289
+ XCTAssertEqual ( result. count, 3 )
290
+ XCTAssertEqual ( result [ 0 ] , JoinOutput ( name: " Test User " , description: " task 1 " , comment: " comment 1 " ) )
291
+ XCTAssertEqual ( result [ 1 ] , JoinOutput ( name: " Test User " , description: " task 1 " , comment: " comment 2 " ) )
292
+ XCTAssertEqual ( result [ 2 ] , JoinOutput ( name: " Test User " , description: " task 2 " , comment: " " ) )
293
+ }
225
294
}
0 commit comments