|
1 | 1 | import Foundation |
2 | 2 | @testable import TablePro |
| 3 | +import TableProPluginKit |
3 | 4 | import Testing |
4 | 5 |
|
5 | 6 | @Suite("DatabaseTreeMetadataService") |
@@ -53,3 +54,82 @@ struct DatabaseTreeMetadataServiceTests { |
53 | 54 | #expect(keys == [mine]) |
54 | 55 | } |
55 | 56 | } |
| 57 | + |
| 58 | +@Suite("DatabaseTreeMetadataService refreshLoadedTables") |
| 59 | +@MainActor |
| 60 | +struct DatabaseTreeMetadataServiceRefreshTests { |
| 61 | + @Test("reload drops previously loaded tables and refetches the current list") |
| 62 | + func refreshReloadsLoadedTables() async { |
| 63 | + let connection = TestFixtures.makeConnection() |
| 64 | + let driver = MockDatabaseDriver(connection: connection) |
| 65 | + driver.schemaTablesToReturn = ["public": [TestFixtures.makeTableInfo(name: "users")]] |
| 66 | + |
| 67 | + var session = ConnectionSession(connection: connection, driver: driver) |
| 68 | + session.status = .connected |
| 69 | + DatabaseManager.shared.injectSession(session, for: connection.id) |
| 70 | + |
| 71 | + let service = DatabaseTreeMetadataService.shared |
| 72 | + let database = connection.database |
| 73 | + |
| 74 | + await service.loadTables(connectionId: connection.id, database: database, schema: "public") |
| 75 | + let initial = service.tables(connectionId: connection.id, database: database, schema: "public") |
| 76 | + #expect(initial.map(\.name) == ["users"]) |
| 77 | + |
| 78 | + driver.schemaTablesToReturn = ["public": []] |
| 79 | + await service.refreshLoadedTables(connectionId: connection.id) |
| 80 | + |
| 81 | + let refreshed = service.tables(connectionId: connection.id, database: database, schema: "public") |
| 82 | + #expect(refreshed.isEmpty) |
| 83 | + |
| 84 | + await service.handleDisconnect(connectionId: connection.id) |
| 85 | + DatabaseManager.shared.removeSession(for: connection.id) |
| 86 | + } |
| 87 | + |
| 88 | + @Test("reload refetches every loaded schema, not just the one that changed") |
| 89 | + func refreshReloadsAllLoadedSchemas() async { |
| 90 | + let connection = TestFixtures.makeConnection() |
| 91 | + let driver = MockDatabaseDriver(connection: connection) |
| 92 | + driver.schemaTablesToReturn = [ |
| 93 | + "public": [TestFixtures.makeTableInfo(name: "users")], |
| 94 | + "sales": [TestFixtures.makeTableInfo(name: "orders")] |
| 95 | + ] |
| 96 | + |
| 97 | + var session = ConnectionSession(connection: connection, driver: driver) |
| 98 | + session.status = .connected |
| 99 | + DatabaseManager.shared.injectSession(session, for: connection.id) |
| 100 | + |
| 101 | + let service = DatabaseTreeMetadataService.shared |
| 102 | + let database = connection.database |
| 103 | + |
| 104 | + await service.loadTables(connectionId: connection.id, database: database, schema: "public") |
| 105 | + await service.loadTables(connectionId: connection.id, database: database, schema: "sales") |
| 106 | + |
| 107 | + driver.schemaTablesToReturn = [ |
| 108 | + "public": [], |
| 109 | + "sales": [TestFixtures.makeTableInfo(name: "orders"), TestFixtures.makeTableInfo(name: "invoices")] |
| 110 | + ] |
| 111 | + await service.refreshLoadedTables(connectionId: connection.id) |
| 112 | + |
| 113 | + let publicTables = service.tables(connectionId: connection.id, database: database, schema: "public") |
| 114 | + let salesTables = service.tables(connectionId: connection.id, database: database, schema: "sales") |
| 115 | + #expect(publicTables.isEmpty) |
| 116 | + #expect(salesTables.map(\.name) == ["orders", "invoices"]) |
| 117 | + |
| 118 | + await service.handleDisconnect(connectionId: connection.id) |
| 119 | + DatabaseManager.shared.removeSession(for: connection.id) |
| 120 | + } |
| 121 | + |
| 122 | + @Test("refresh is a no-op when no tables are loaded for the connection") |
| 123 | + func refreshWithoutLoadedTablesIsNoOp() async { |
| 124 | + let connection = TestFixtures.makeConnection() |
| 125 | + |
| 126 | + await DatabaseTreeMetadataService.shared.refreshLoadedTables(connectionId: connection.id) |
| 127 | + |
| 128 | + let tables = DatabaseTreeMetadataService.shared.tables( |
| 129 | + connectionId: connection.id, |
| 130 | + database: connection.database, |
| 131 | + schema: "public" |
| 132 | + ) |
| 133 | + #expect(tables.isEmpty) |
| 134 | + } |
| 135 | +} |
0 commit comments