SPB Git

Repository Public

WorkspaceTests.swift 16.4 KB 296 lines swift

1import Foundation2import Testing3import SheetCore4import SheetData5import SheetUI6@testable import FeatureData78@MainActor @Suite struct DatasetDescriptorTests {9    @Test func catalogRoundTripsThroughWorkbookAttachments() throws {10        let workbook = Workbook(sheetNames: ["S"])11        #expect(DatasetCatalog.load(from: workbook).datasets.isEmpty)12        var options = ImportOptions()13        options.delimiter = ";"; options.hasHeader = true; options.sourceTable = "t"14        var catalog = DatasetCatalog()15        catalog.upsert(DatasetDescriptor(name: "orders", origin: .file(path: "/tmp/orders.csv", bookmark: Data([1, 2, 3]), format: .csv, options: options, lazy: true)))16        catalog.upsert(DatasetDescriptor(name: "range", origin: .range(sheetID: 3, range: RangeRef(r0: 0, c0: 0, r1: 9, c1: 3), hasHeader: true, dateColumns: [2])))17        catalog.upsert(DatasetDescriptor(name: "top", origin: .query(sql: "SELECT 1")))18        catalog.save(to: workbook)19        #expect(workbook.attachments[DatasetCatalog.attachmentKey] != nil)20        let loaded = DatasetCatalog.load(from: workbook)21        #expect(loaded == catalog)22        #expect(loaded["orders"]?.isLazy == true)23        #expect(loaded["orders"]?.sourcePath == "/tmp/orders.csv")2425        catalog.rename("orders", to: "sales")26        catalog.remove("top")27        #expect(catalog.datasets.map(\.name) == ["sales", "range"])28        catalog.upsert(DatasetDescriptor(name: "sales", origin: .query(sql: "SELECT 2")))     // replaced in place29        #expect(catalog.datasets.map(\.name) == ["sales", "range"])30        DatasetCatalog().save(to: workbook)31        #expect(workbook.attachments[DatasetCatalog.attachmentKey] == nil)                    // an empty catalog leaves no attachment32        workbook.attachments[DatasetCatalog.attachmentKey] = Data("not json".utf8)33        #expect(DatasetCatalog.load(from: workbook).datasets.isEmpty)34    }3536    @Test func resolvesMissingAndReconnectedSources() throws {37        let dir = TestFiles.directory()38        defer { try? FileManager.default.removeItem(at: dir) }39        let url = TestFiles.ordersCSV(10, in: dir)40        var d = DatasetDescriptor.file(name: "orders", url: url, format: .csv, options: .init(), lazy: false)41        guard case .found(let found, let moved) = d.resolveSource() else { Issue.record("the source should resolve"); return }42        #expect(found.resolvingSymlinksInPath().path == url.resolvingSymlinksInPath().path && !moved)43        try FileManager.default.removeItem(at: url)44        #expect(d.resolveSource() == .missing(path: url.path))45        let other = TestFiles.ordersCSV(10, in: dir, name: "elsewhere.csv")46        let reconnected = d.reconnect(to: other)47        #expect(reconnected)48        #expect(d.sourcePath == other.path)49        if case .missing = d.resolveSource() { Issue.record("the reconnected source should resolve") }50        var range = DatasetDescriptor(name: "r", origin: .range(sheetID: 1, range: RangeRef(CellRef(0, 0)), hasHeader: false, dateColumns: []))51        #expect(range.resolveSource() == .notAFile)52        let refused = !range.reconnect(to: other)53        #expect(refused)54    }55}5657@MainActor @Suite struct WorkspaceTests {58    @Test func oneWorkspacePerSession() {59        let session = makeSession()60        let a = DataWorkspace.of(session), b = DataWorkspace.of(session)61        #expect(a === b)62        #expect(DataWorkspace.existing(for: session) === a)63        let other = makeSession()64        #expect(DataWorkspace.existing(for: other) == nil)65        #expect(DataWorkspace.of(other) !== a)66        #expect(a.session === session)67    }6869    @Test func importRecordsALinkAndReopeningRebuildsTheDatasets() async throws {70        let dir = TestFiles.directory()71        defer { try? FileManager.default.removeItem(at: dir) }72        let csv = TestFiles.ordersCSV(1_000, in: dir)73        let session = makeSession()74        fillSales(session)75        var changes = 076        session.onChange = { changes += 1 }77        let ws = DataWorkspace(session: session)78        let info = try await ws.importFile(ImportRequest(url: csv, name: "orders", format: .csv))79        #expect(info.rowCount == 1_000)80        #expect(ws.datasets.map(\.name) == ["orders"])81        #expect(ws.catalog["orders"]?.sourcePath == csv.path)82        #expect(changes > 0)                                                             // the document is marked edited83        try await ws.importFile(ImportRequest(url: csv, name: "orders_view", format: .csv, lazy: true))84        await waitUntil("view count") { ws.rowCount("orders_view") == 1_000 }85        #expect(ws.info("orders_view")?.kind == .view)86        try ws.importRange(sheetIndex: 0, range: RangeRef(r0: 0, c0: 0, r1: 24, c1: 3), hasHeader: true, name: "sales")87        try await ws.createDataset("by_region", from: "SELECT region, count(*) AS n FROM orders GROUP BY region")88        #expect(Set(ws.datasetNames) == ["orders", "orders_view", "sales", "by_region"])89        #expect(ws.rowCount("sales") == 24)90        #expect(!ws.isBusy)9192        // "reopen": a new workspace over the same workbook starts with an empty engine and the stored links93        let reopened = DataWorkspace(session: session)94        #expect(reopened.catalog.datasets.count == 4)95        #expect(reopened.entries.allSatisfy { $0.status == .loading && $0.info == nil })96        await reopened.restoreAll()97        #expect(Set(reopened.datasetNames) == ["orders", "orders_view", "sales", "by_region"])98        #expect(reopened.entries.allSatisfy { $0.status == .ready })99        #expect(reopened.rowCount("orders") == 1_000)100        #expect(reopened.rowCount("by_region") == 4)101        #expect(reopened.info("orders_view")?.kind == .view)102    }103104    @Test func missingSourceIsReportedAndReconnectLoadsIt() async throws {105        let dir = TestFiles.directory()106        defer { try? FileManager.default.removeItem(at: dir) }107        let csv = TestFiles.ordersCSV(50, in: dir)108        let session = makeSession()109        let ws = DataWorkspace(session: session)110        try await ws.importFile(ImportRequest(url: csv, name: "orders", format: .csv))111        try FileManager.default.removeItem(at: csv)112113        let reopened = DataWorkspace(session: session)114        await reopened.restoreAll()115        #expect(reopened.datasets.isEmpty)116        #expect(reopened.entry("orders")?.status == .missingSource(csv.path))117        await #expect(throws: DataError.self) { try await reopened.refresh("orders") }118119        let moved = TestFiles.ordersCSV(80, in: dir, name: "moved.csv")120        try await reopened.reconnect("orders", to: moved)121        #expect(reopened.entry("orders")?.status == .ready)122        #expect(reopened.rowCount("orders") == 80)123        #expect(reopened.catalog["orders"]?.sourcePath == moved.path)124        #expect(DatasetCatalog.load(from: session.workbook)["orders"]?.sourcePath == moved.path)125    }126127    @Test func refreshRereadsTheSource() async throws {128        let dir = TestFiles.directory()129        defer { try? FileManager.default.removeItem(at: dir) }130        let csv = TestFiles.ordersCSV(10, in: dir)131        let ws = DataWorkspace(session: makeSession())132        try await ws.importFile(ImportRequest(url: csv, name: "orders", format: .csv))133        TestFiles.ordersCSV(25, in: dir)134        let before = ws.datasetsRevision135        try await ws.refresh("orders")136        #expect(ws.rowCount("orders") == 25)137        #expect(ws.datasetsRevision != before)138        try await ws.run("create") { try $0.execute("CREATE TABLE adhoc AS SELECT 1 AS x") }139        await ws.refreshDatasets()140        #expect(ws.datasetNames.contains("adhoc"))141        await #expect(throws: DataError.self) { try await ws.refresh("adhoc") }               // no recorded source142    }143144    @Test func renameAndRemove() async throws {145        let dir = TestFiles.directory()146        defer { try? FileManager.default.removeItem(at: dir) }147        let csv = TestFiles.ordersCSV(10, in: dir)148        let session = makeSession()149        let ws = DataWorkspace(session: session)150        try await ws.importFile(ImportRequest(url: csv, name: "orders", format: .csv))151        try await ws.importFile(ImportRequest(url: csv, name: "lazy", format: .csv, lazy: true))152        try await ws.rename("orders", to: "sales 2024")153        try await ws.rename("lazy", to: "lazy2")154        #expect(Set(ws.datasetNames) == ["sales 2024", "lazy2"])155        #expect(ws.info("sales 2024")?.sourceURL != nil)                                     // the engine catalog followed the rename156        #expect(ws.catalog["sales 2024"] != nil && ws.catalog["orders"] == nil)157        await #expect(throws: DataError.self) { try await ws.rename("lazy2", to: "sales 2024") }158        await #expect(throws: DataError.self) { try await ws.rename("lazy2", to: "orix_src_x") }159        try await ws.remove("sales 2024")160        #expect(ws.datasetNames == ["lazy2"])161        #expect(DatasetCatalog.load(from: session.workbook).datasets.map(\.name) == ["lazy2"])162        #expect(ws.uniqueName("Lazy2") == "lazy2_2")163    }164165    @Test func errorsAreSpecificAndArrowIsRefusedWithTheEngineReason() async throws {166        let dir = TestFiles.directory()167        defer { try? FileManager.default.removeItem(at: dir) }168        let ws = DataWorkspace(session: makeSession())169        let arrow = dir.appendingPathComponent("data.arrow")170        try Data("ARROW1".utf8).write(to: arrow)171        do {172            try await ws.importFile(ImportRequest(url: arrow, name: "a", format: .arrowIPC))173            Issue.record("Arrow import should fail")174        } catch {175            #expect(Fmt.error(error).contains("arrow extension"))176        }177        #expect(ws.datasets.isEmpty && ws.catalog.datasets.isEmpty && ws.entries.isEmpty)178        do {179            try await ws.importFile(ImportRequest(url: dir.appendingPathComponent("nope.csv"), name: "n", format: .csv))180            Issue.record("a missing file should fail")181        } catch {}182        #expect(ws.entries.isEmpty)183    }184185    @Test func longWorkReportsAnActivityAndCanBeCancelled() async throws {186        let ws = DataWorkspace(session: makeSession())187        let task = Task { try await ws.run("Slow query") { try $0.query("SELECT count(*) FROM range(60000000000) t(i) WHERE i % 7 = 3") } }188        await waitUntil("activity") { ws.isBusy }189        #expect(ws.activity?.title == "Slow query")190        #expect(throws: DataError.self) { try ws.importRange(sheetIndex: 0, range: RangeRef(CellRef(0, 0)), hasHeader: false, name: "x") }   // refused while busy191        try await Task.sleep(for: .milliseconds(150))192        ws.cancel()193        do {194            _ = try await task.value195            Issue.record("the query should have been cancelled")196        } catch let e as DataError {197            guard case .interrupted = e else { Issue.record("unexpected error \(e)"); return }198        }199        #expect(!ws.isBusy)200        #expect(try await ws.run("after") { try $0.query("SELECT 41 + 1").value(row: 0, column: 0) } == .int(42))201    }202}203204@MainActor @Suite struct MaterializeTests {205    @Test func planChecksTheSheetLimits() {206        let fits = MaterializePlan(dataRows: 1_048_575, columns: 16_384, includeHeader: true, anchor: CellRef(0, 0))207        #expect(fits.fits && fits.refusal == nil && fits.rows == 1_048_576)208        let tooTall = MaterializePlan(dataRows: 1_048_576, columns: 3, includeHeader: true, anchor: CellRef(0, 0))209        #expect(!tooTall.fits)210        #expect(tooTall.refusal?.contains(Fmt.count(1_048_577)) == true)211        #expect(!MaterializePlan(dataRows: 10, columns: 3, includeHeader: true, anchor: CellRef(1_048_570, 0)).fits)212        #expect(!MaterializePlan(dataRows: 10, columns: 3, includeHeader: false, anchor: CellRef(0, 16_382)).fits)213        #expect(MaterializePlan(dataRows: 10, columns: 3, includeHeader: false, anchor: CellRef(0, 16_381)).fits)214        let empty = MaterializePlan(dataRows: 0, columns: 3, includeHeader: false, anchor: CellRef(0, 0))215        #expect(empty.isEmpty && empty.refusal != nil)216        #expect(MaterializePlan(dataRows: 1_000_000, columns: 8, includeHeader: true, anchor: CellRef(0, 0)).isLarge)217        #expect(MaterializePlan(dataRows: 9, columns: 2, includeHeader: true, anchor: CellRef(2, 1)).range == RangeRef(r0: 2, c0: 1, r1: 11, c1: 2))218    }219220    @Test func insertsAtACellAndAsANewSheetAsSingleUndoSteps() async throws {221        let session = makeSession()222        let ws = DataWorkspace(session: session)223        try await ws.run("make") { try $0.execute("CREATE TABLE t AS SELECT i AS n, 'r' || i AS label, DATE '2024-01-01' + CAST(i AS INTEGER) AS day FROM range(50) x(i)") }224        let counts = try await ws.measure(sql: "SELECT * FROM t WHERE n < 20")225        #expect(counts.rows == 20 && counts.columns == 3)226227        let depth = session.engine.changeCount228        let placed = try await ws.materialize(sql: "SELECT * FROM t WHERE n < 20 ORDER BY n", destination: .cell(CellRef(2, 1)), label: "Insert Data")229        #expect(placed.sheet == 0 && placed.range == RangeRef(r0: 2, c0: 1, r1: 22, c1: 3))230        #expect(session.sheet.value(2, 1) == .text("n"))231        #expect(session.workbook.styles[session.sheet.styleID(2, 1)].bold)232        #expect(session.sheet.value(3, 1) == .number(0))233        #expect(session.sheet.value(22, 2) == .text("r19"))234        #expect(session.workbook.styles[session.sheet.styleID(3, 3)].numberFormat == "yyyy-mm-dd")235        #expect(session.selection.primary == placed.range)236        #expect(session.engine.changeCount == depth + 1)237        session.undo()238        #expect(session.sheet.cells.usedRange() == nil)239240        let added = try await ws.materialize(sql: "SELECT * FROM t", destination: .newSheet(name: "t"), label: "Insert Data")241        #expect(session.workbook.sheets.count == 2 && added.sheet == 1)242        #expect(session.workbook.sheets[1].name == "t")243        #expect(session.sheetIndex == 1)244        #expect(session.workbook.sheets[1].value(50, 0) == .number(49))245        session.undo()                                                                // one step removes the sheet and its cells246        #expect(session.workbook.sheets.count == 1)247        session.redo()248        #expect(session.workbook.sheets.count == 2 && session.workbook.sheets[1].value(1, 1) == .text("r0"))249    }250251    @Test func refusesResultsLargerThanASheetWithoutWritingAnything() async throws {252        let session = makeSession()253        let ws = DataWorkspace(session: session)254        let sql = "SELECT i FROM range(1048600) x(i)"255        let counts = try await ws.measure(sql: sql)256        #expect(counts.rows == 1_048_600)257        #expect(!MaterializePlan(dataRows: counts.rows, columns: counts.columns, includeHeader: true, anchor: CellRef(0, 0)).fits)258        let before = session.engine.changeCount259        do {260            try await ws.materialize(sql: sql, destination: .newSheet(name: "big"), label: "Insert Data")261            Issue.record("the insert should be refused")262        } catch let e as DataError {263            guard case .exceedsSheetLimits(let rows, let cols, _) = e else { Issue.record("unexpected \(e)"); return }264            #expect(rows == 1_048_601 && cols == 1)265            #expect(e.description.contains("exceeds the worksheet limits"))266        }267        #expect(session.workbook.sheets.count == 1)268        #expect(session.engine.changeCount == before)269        #expect(session.sheet.cells.count == 0)270        // a small result near the bottom of the sheet is refused too271        await #expect(throws: DataError.self) {272            try await ws.materialize(sql: "SELECT i FROM range(10) x(i)", destination: .cell(CellRef(1_048_570, 0)), label: "Insert Data")273        }274        #expect(throws: DataError.self) {275            try SheetWriter.insert(DataTable(columns: [DataColumn(name: "a", storage: .int64([]))]), session: session, destination: .cell(CellRef(0, 0)), includeHeader: false, label: "x")276        }277        #expect(SheetWriter.uniqueSheetName("Sheet1", in: session.workbook) == "Sheet1 2")278        #expect(SheetWriter.uniqueSheetName("a/b:c", in: session.workbook) == "a_b_c")279    }280}281282@MainActor @Suite struct WorkspaceLifetimeTests {283    @Test func closingASessionReleasesItsEngine() async throws {284        let session = makeSession()285        let ws = DataWorkspace.of(session)286        _ = DataStudioModel.of(session)287        try await ws.run("make") { try $0.execute("CREATE TABLE t AS SELECT 1 AS x") }288        let staging = try ws.engine().stagingDirectory289        #expect(FileManager.default.fileExists(atPath: staging.path))290        DataWorkspace.close(session)291        #expect(DataWorkspace.existing(for: session) == nil)292        #expect(ws.engineIfOpen == nil)293        #expect(DataWorkspace.of(session) !== ws)                    // a later use starts a fresh engine294    }295}296
Files main
Menu

Theme