From 20c525ec116292a22f8fb2190fe419cd4a814aed Mon Sep 17 00:00:00 2001 From: Luca Meghnagi Date: Wed, 23 Sep 2026 15:34:39 +0200 Subject: [PATCH 1/2] add test --- .../SQLiteDataTests/JSONBDecodingTests.swift | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Tests/SQLiteDataTests/JSONBDecodingTests.swift diff --git a/Tests/SQLiteDataTests/JSONBDecodingTests.swift b/Tests/SQLiteDataTests/JSONBDecodingTests.swift new file mode 100644 index 00000000..c4fb659f --- /dev/null +++ b/Tests/SQLiteDataTests/JSONBDecodingTests.swift @@ -0,0 +1,76 @@ +import Foundation +import SQLiteData +import Testing + +@Suite struct JSONBDecodingTests { + @available(iOS 26, macOS 26, tvOS 26, watchOS 26, *) + @DatabaseFunction(as: ((AttributedString.JSONBRepresentation) -> String).self) + private func string(from attributedString: AttributedString) -> String { + String(attributedString.characters[...]) + } + + @available(iOS 26, macOS 26, tvOS 26, watchOS 26, *) + @Test func triggerWithJSONBArgument() throws { + var configuration = Configuration() + configuration.prepareDatabase { db in + db.add(function: $string) + } + let database = try DatabaseQueue(configuration: configuration) + + try database.write { db in + try #sql( + """ + CREATE TABLE "items" ( + "id" INTEGER PRIMARY KEY AUTOINCREMENT, + "text" BLOB NOT NULL + ) STRICT + """ + ) + .execute(db) + + try #sql( + """ + CREATE VIRTUAL TABLE "itemTexts" USING fts5( + "itemID" UNINDEXED, + "text", + tokenize='trigram' + ) + """ + ).execute(db) + } + + try database.write { db in + try Item.createTemporaryTrigger( + after: .insert { new in + ItemText.insert { + ItemText.Columns( + itemID: new.id, + text: $string(from: new.text) + ) + } + } + ).execute(db) + } + + try database.write { db in + try Item.insert { + Item.Draft(text: AttributedString("blob")) + }.execute(db) + } + } +} + +@available(iOS 26, macOS 26, tvOS 26, watchOS 26, *) +@Table +private struct Item: Identifiable { + let id: Int + @Column(as: AttributedString.JSONBRepresentation.self) + var text: AttributedString +} + +@available(iOS 26, macOS 26, tvOS 26, watchOS 26, *) +@Table +private struct ItemText: FTS5 { + @Column(primaryKey: true) let itemID: Item.ID + let text: String +} From a1f809eccb551ab745e69f880bf5dffc398ec72d Mon Sep 17 00:00:00 2001 From: Luca Meghnagi Date: Wed, 23 Sep 2026 16:07:22 +0200 Subject: [PATCH 2/2] update test --- ...NBDecodingTests.swift => JSONBTests.swift} | 38 +++---------------- 1 file changed, 6 insertions(+), 32 deletions(-) rename Tests/SQLiteDataTests/{JSONBDecodingTests.swift => JSONBTests.swift} (60%) diff --git a/Tests/SQLiteDataTests/JSONBDecodingTests.swift b/Tests/SQLiteDataTests/JSONBTests.swift similarity index 60% rename from Tests/SQLiteDataTests/JSONBDecodingTests.swift rename to Tests/SQLiteDataTests/JSONBTests.swift index c4fb659f..3c5a90d6 100644 --- a/Tests/SQLiteDataTests/JSONBDecodingTests.swift +++ b/Tests/SQLiteDataTests/JSONBTests.swift @@ -2,7 +2,7 @@ import Foundation import SQLiteData import Testing -@Suite struct JSONBDecodingTests { +@Suite struct JSONBTests { @available(iOS 26, macOS 26, tvOS 26, watchOS 26, *) @DatabaseFunction(as: ((AttributedString.JSONBRepresentation) -> String).self) private func string(from attributedString: AttributedString) -> String { @@ -10,7 +10,7 @@ import Testing } @available(iOS 26, macOS 26, tvOS 26, watchOS 26, *) - @Test func triggerWithJSONBArgument() throws { + @Test func databaseFunctionWithJSONBArgument() throws { var configuration = Configuration() configuration.prepareDatabase { db in db.add(function: $string) @@ -27,35 +27,16 @@ import Testing """ ) .execute(db) - - try #sql( - """ - CREATE VIRTUAL TABLE "itemTexts" USING fts5( - "itemID" UNINDEXED, - "text", - tokenize='trigram' - ) - """ - ).execute(db) - } - - try database.write { db in - try Item.createTemporaryTrigger( - after: .insert { new in - ItemText.insert { - ItemText.Columns( - itemID: new.id, - text: $string(from: new.text) - ) - } - } - ).execute(db) } try database.write { db in try Item.insert { Item.Draft(text: AttributedString("blob")) }.execute(db) + + _ = try Item.all + .select { $string($0.text) } + .fetchOne(db) } } } @@ -67,10 +48,3 @@ private struct Item: Identifiable { @Column(as: AttributedString.JSONBRepresentation.self) var text: AttributedString } - -@available(iOS 26, macOS 26, tvOS 26, watchOS 26, *) -@Table -private struct ItemText: FTS5 { - @Column(primaryKey: true) let itemID: Item.ID - let text: String -}