Skip to content

Commit 004dde9

Browse files
update tests
1 parent d19f601 commit 004dde9

File tree

1 file changed

+147
-133
lines changed

1 file changed

+147
-133
lines changed

Tests/PowerSyncTests/Kotlin/KotlinPowerSyncDatabaseImplTests.swift

Lines changed: 147 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase {
1212
.text("name"),
1313
.text("email"),
1414
.text("photo_id")
15-
]),
16-
Table(name: "tasks", columns: [
17-
.text("user_id"),
18-
.text("description"),
19-
.text("tags")
20-
]),
21-
Table(name: "comments", columns: [
22-
.text("task_id"),
23-
.text("comment"),
2415
])
2516
])
2617

@@ -448,37 +439,170 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase {
448439
}
449440
}
450441

442+
/// Transactions should return the value returned from the callback
443+
func testTransactionReturnValue() async throws {
444+
// Should pass through nil
445+
let txNil = try await database.writeTransaction { _ in
446+
nil as Any?
447+
}
448+
XCTAssertNil(txNil)
449+
450+
let txString = try await database.writeTransaction { _ in
451+
"Hello"
452+
}
453+
XCTAssertEqual(txString, "Hello")
454+
}
455+
456+
/// Transactions should return the value returned from the callback
457+
func testTransactionGenerics() async throws {
458+
// Should pass through nil
459+
try await database.writeTransaction { tx in
460+
let result = try tx.get(
461+
sql: "SELECT FALSE as col",
462+
parameters: []
463+
) { cursor in
464+
try cursor.getBoolean(name: "col")
465+
}
466+
467+
// result should be typed as Bool
468+
XCTAssertFalse(result)
469+
}
470+
}
471+
472+
func testFTS() async throws {
473+
let supported = try await database.get(
474+
"SELECT sqlite_compileoption_used('ENABLE_FTS5');"
475+
) { cursor in
476+
try cursor.getInt(index: 0)
477+
}
478+
479+
XCTAssertEqual(supported, 1)
480+
}
481+
482+
func testUpdatingSchema() async throws {
483+
_ = try await database.execute(
484+
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)",
485+
parameters: ["1", "Test User", "test@example.com"]
486+
)
487+
488+
let newSchema = Schema(tables: [
489+
Table(
490+
name: "users",
491+
columns: [
492+
.text("name"),
493+
.text("email"),
494+
],
495+
viewNameOverride: "people"
496+
),
497+
])
498+
499+
try await database.updateSchema(schema: newSchema)
500+
501+
let peopleCount = try await database.get(
502+
sql: "SELECT COUNT(*) FROM people",
503+
parameters: []
504+
) { cursor in try cursor.getInt(index: 0) }
505+
506+
XCTAssertEqual(peopleCount, 1)
507+
}
508+
509+
func testCustomLogger() async throws {
510+
let testWriter = TestLogWriterAdapter()
511+
let logger = DefaultLogger(minSeverity: LogSeverity.debug, writers: [testWriter])
512+
513+
let db2 = KotlinPowerSyncDatabaseImpl(
514+
schema: schema,
515+
dbFilename: ":memory:",
516+
logger: DatabaseLogger(logger)
517+
)
518+
519+
try await db2.close()
520+
521+
let warningIndex = testWriter.logs.firstIndex(
522+
where: { value in
523+
value.contains("warning: Multiple PowerSync instances for the same database have been detected")
524+
}
525+
)
526+
527+
XCTAssert(warningIndex! >= 0)
528+
}
529+
530+
func testMinimumSeverity() async throws {
531+
let testWriter = TestLogWriterAdapter()
532+
let logger = DefaultLogger(minSeverity: LogSeverity.error, writers: [testWriter])
533+
534+
let db2 = KotlinPowerSyncDatabaseImpl(
535+
schema: schema,
536+
dbFilename: ":memory:",
537+
logger: DatabaseLogger(logger)
538+
)
539+
540+
try await db2.close()
541+
542+
let warningIndex = testWriter.logs.firstIndex(
543+
where: { value in
544+
value.contains("warning: Multiple PowerSync instances for the same database have been detected")
545+
}
546+
)
547+
548+
// The warning should not be present due to the min severity
549+
XCTAssert(warningIndex == nil)
550+
}
551+
451552
func testJoin() async throws {
452553
struct JoinOutput: Equatable {
453554
var name: String
454555
var description: String
455556
var comment: String
456557
}
457558

458-
_ = try await database.writeTransaction { transaction in
559+
try await database.updateSchema(schema:
560+
Schema(tables: [
561+
Table(name: "users", columns: [
562+
.text("name"),
563+
.text("email"),
564+
.text("photo_id")
565+
]),
566+
Table(name: "tasks", columns: [
567+
.text("user_id"),
568+
.text("description"),
569+
.text("tags")
570+
]),
571+
Table(name: "comments", columns: [
572+
.text("task_id"),
573+
.text("comment"),
574+
])
575+
])
576+
)
577+
578+
try await database.writeTransaction { transaction in
579+
let userId = UUID().uuidString
459580
_ = try transaction.execute(
460581
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)",
461-
parameters: ["1", "Test User", "test@example.com"]
582+
parameters: [userId, "Test User", "test@example.com"]
462583
)
463584

464-
_ = try transaction.execute(
585+
let task1Id = UUID().uuidString
586+
let task2Id = UUID().uuidString
587+
588+
try transaction.execute(
465589
sql: "INSERT INTO tasks (id, user_id, description) VALUES (?, ?, ?)",
466-
parameters: ["1", "1", "task 1"]
590+
parameters: [task1Id, userId, "task 1"]
467591
)
468592

469-
_ = try transaction.execute(
593+
try transaction.execute(
470594
sql: "INSERT INTO tasks (id, user_id, description) VALUES (?, ?, ?)",
471-
parameters: ["2", "1", "task 2"]
595+
parameters: [task2Id, userId, "task 2"]
472596
)
473597

474-
_ = try transaction.execute(
475-
sql: "INSERT INTO comments (id, task_id, comment) VALUES (?, ?, ?)",
476-
parameters: ["1", "1", "comment 1"]
598+
try transaction.execute(
599+
sql: "INSERT INTO comments (id, task_id, comment) VALUES (uuid(), ?, ?)",
600+
parameters: [task1Id, "comment 1"]
477601
)
478602

479-
_ = try transaction.execute(
480-
sql: "INSERT INTO comments (id, task_id, comment) VALUES (?, ?, ?)",
481-
parameters: ["2", "1", "comment 2"]
603+
try transaction.execute(
604+
sql: "INSERT INTO comments (id, task_id, comment) VALUES (uuid(), ?, ?)",
605+
parameters: [task2Id, "comment 2"]
482606
)
483607
}
484608

@@ -501,118 +625,8 @@ final class KotlinPowerSyncDatabaseImplTests: XCTestCase {
501625
)
502626
}
503627

504-
XCTAssertEqual(result.count, 3)
628+
XCTAssertEqual(result.count, 2)
505629
XCTAssertEqual(result[0], JoinOutput(name: "Test User", description: "task 1", comment: "comment 1"))
506-
XCTAssertEqual(result[1], JoinOutput(name: "Test User", description: "task 1", comment: "comment 2"))
507-
XCTAssertEqual(result[2], JoinOutput(name: "Test User", description: "task 2", comment: ""))
508-
/// Transactions should return the value returned from the callback
509-
func testTransactionReturnValue() async throws {
510-
// Should pass through nil
511-
let txNil = try await database.writeTransaction { _ in
512-
nil as Any?
513-
}
514-
XCTAssertNil(txNil)
515-
516-
let txString = try await database.writeTransaction { _ in
517-
"Hello"
518-
}
519-
XCTAssertEqual(txString, "Hello")
520-
}
521-
522-
/// Transactions should return the value returned from the callback
523-
func testTransactionGenerics() async throws {
524-
// Should pass through nil
525-
try await database.writeTransaction { tx in
526-
let result = try tx.get(
527-
sql: "SELECT FALSE as col",
528-
parameters: []
529-
) { cursor in
530-
try cursor.getBoolean(name: "col")
531-
}
532-
533-
// result should be typed as Bool
534-
XCTAssertFalse(result)
535-
}
536-
}
537-
538-
func testFTS() async throws {
539-
let supported = try await database.get(
540-
"SELECT sqlite_compileoption_used('ENABLE_FTS5');"
541-
) { cursor in
542-
try cursor.getInt(index: 0)
543-
}
544-
545-
XCTAssertEqual(supported, 1)
546-
}
547-
548-
func testUpdatingSchema() async throws {
549-
_ = try await database.execute(
550-
sql: "INSERT INTO users (id, name, email) VALUES (?, ?, ?)",
551-
parameters: ["1", "Test User", "test@example.com"]
552-
)
553-
554-
let newSchema = Schema(tables: [
555-
Table(
556-
name: "users",
557-
columns: [
558-
.text("name"),
559-
.text("email"),
560-
],
561-
viewNameOverride: "people"
562-
),
563-
])
564-
565-
try await database.updateSchema(schema: newSchema)
566-
567-
let peopleCount = try await database.get(
568-
sql: "SELECT COUNT(*) FROM people",
569-
parameters: []
570-
) { cursor in try cursor.getInt(index: 0) }
571-
572-
XCTAssertEqual(peopleCount, 1)
573-
}
574-
575-
func testCustomLogger() async throws {
576-
let testWriter = TestLogWriterAdapter()
577-
let logger = DefaultLogger(minSeverity: LogSeverity.debug, writers: [testWriter])
578-
579-
let db2 = KotlinPowerSyncDatabaseImpl(
580-
schema: schema,
581-
dbFilename: ":memory:",
582-
logger: DatabaseLogger(logger)
583-
)
584-
585-
try await db2.close()
586-
587-
let warningIndex = testWriter.logs.firstIndex(
588-
where: { value in
589-
value.contains("warning: Multiple PowerSync instances for the same database have been detected")
590-
}
591-
)
592-
593-
XCTAssert(warningIndex! >= 0)
594-
}
595-
596-
func testMinimumSeverity() async throws {
597-
let testWriter = TestLogWriterAdapter()
598-
let logger = DefaultLogger(minSeverity: LogSeverity.error, writers: [testWriter])
599-
600-
let db2 = KotlinPowerSyncDatabaseImpl(
601-
schema: schema,
602-
dbFilename: ":memory:",
603-
logger: DatabaseLogger(logger)
604-
)
605-
606-
try await db2.close()
607-
608-
let warningIndex = testWriter.logs.firstIndex(
609-
where: { value in
610-
value.contains("warning: Multiple PowerSync instances for the same database have been detected")
611-
}
612-
)
613-
614-
// The warning should not be present due to the min severity
615-
XCTAssert(warningIndex == nil)
616-
}
630+
XCTAssertEqual(result[1], JoinOutput(name: "Test User", description: "task 2", comment: "comment 2"))
617631
}
618632
}

0 commit comments

Comments
 (0)