-
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathLiveTableFetcherTests.swift
More file actions
194 lines (153 loc) · 7.12 KB
/
LiveTableFetcherTests.swift
File metadata and controls
194 lines (153 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// LiveTableFetcherTests.swift
// TableProTests
//
// Tests for LiveTableFetcher schema provider cache integration.
//
import Foundation
import Testing
@testable import TablePro
// MARK: - Mock DatabaseDriver
private class MockDatabaseDriver: DatabaseDriver {
let connection: DatabaseConnection
var status: ConnectionStatus = .connected
var serverVersion: String? = nil
var tablesToReturn: [TableInfo] = []
var fetchTablesCallCount = 0
init(connection: DatabaseConnection = TestFixtures.makeConnection()) {
self.connection = connection
}
func connect() async throws {}
func disconnect() {}
func testConnection() async throws -> Bool { true }
func applyQueryTimeout(_ seconds: Int) async throws {}
func execute(query: String) async throws -> QueryResult { .empty }
func executeParameterized(query: String, parameters: [Any?]) async throws -> QueryResult { .empty }
func fetchRowCount(query: String) async throws -> Int { 0 }
func fetchRows(query: String, offset: Int, limit: Int) async throws -> QueryResult { .empty }
func fetchTables() async throws -> [TableInfo] {
fetchTablesCallCount += 1
return tablesToReturn
}
func fetchColumns(table: String) async throws -> [ColumnInfo] { [] }
func fetchAllColumns() async throws -> [String: [ColumnInfo]] { [:] }
func fetchIndexes(table: String) async throws -> [IndexInfo] { [] }
func fetchForeignKeys(table: String) async throws -> [ForeignKeyInfo] { [] }
func fetchApproximateRowCount(table: String) async throws -> Int? { nil }
func fetchTableDDL(table: String) async throws -> String { "" }
func fetchViewDefinition(view: String) async throws -> String { "" }
func fetchTableMetadata(tableName: String) async throws -> TableMetadata {
TableMetadata(
tableName: tableName, dataSize: nil, indexSize: nil, totalSize: nil,
avgRowLength: nil, rowCount: nil, comment: nil, engine: nil,
collation: nil, createTime: nil, updateTime: nil
)
}
func fetchDatabases() async throws -> [String] { [] }
func fetchSchemas() async throws -> [String] { [] }
func fetchDatabaseMetadata(_ database: String) async throws -> DatabaseMetadata {
DatabaseMetadata(
id: database, name: database, tableCount: nil, sizeBytes: nil,
lastAccessed: nil, isSystemDatabase: false, icon: "cylinder"
)
}
func createDatabase(name: String, charset: String, collation: String?) async throws {}
func cancelQuery() throws {}
func beginTransaction() async throws {}
func commitTransaction() async throws {}
func rollbackTransaction() async throws {}
}
// MARK: - Tests
@Suite("LiveTableFetcher")
struct LiveTableFetcherTests {
@Test("returns cached tables from schema provider when available")
func returnsCachedTablesFromSchemaProvider() async throws {
let expectedTables = [
TestFixtures.makeTableInfo(name: "users"),
TestFixtures.makeTableInfo(name: "orders"),
TestFixtures.makeTableInfo(name: "products")
]
let mockDriver = MockDatabaseDriver()
mockDriver.tablesToReturn = expectedTables
let provider = SQLSchemaProvider()
await provider.loadSchema(using: mockDriver)
let initialCallCount = mockDriver.fetchTablesCallCount
#expect(initialCallCount == 1)
let fetcher = LiveTableFetcher(connectionId: UUID(), schemaProvider: provider)
let result = try await fetcher.fetchTables(force: false)
#expect(result.count == 3)
#expect(result.map(\.name) == ["users", "orders", "products"])
#expect(mockDriver.fetchTablesCallCount == initialCallCount)
}
@Test("falls back to driver when schema provider has no cached tables")
func fallsBackWhenSchemaProviderEmpty() async throws {
let provider = SQLSchemaProvider()
let fetcher = LiveTableFetcher(connectionId: UUID(), schemaProvider: provider)
let result = try await fetcher.fetchTables(force: false)
#expect(result.isEmpty)
}
@Test("works without schema provider using direct driver fetch")
func worksWithoutSchemaProvider() async throws {
let fetcher = LiveTableFetcher(connectionId: UUID())
let result = try await fetcher.fetchTables(force: false)
#expect(result.isEmpty)
}
@Test("schema provider with loaded tables returns them directly")
func schemaProviderReturnsLoadedTablesConsistently() async throws {
let expectedTables = [
TestFixtures.makeTableInfo(name: "accounts"),
TestFixtures.makeTableInfo(name: "transactions")
]
let mockDriver = MockDatabaseDriver()
mockDriver.tablesToReturn = expectedTables
let provider = SQLSchemaProvider()
await provider.loadSchema(using: mockDriver)
let fetcher = LiveTableFetcher(connectionId: UUID(), schemaProvider: provider)
for _ in 0..<3 {
let result = try await fetcher.fetchTables(force: false)
#expect(result.count == 2)
#expect(result.map(\.name) == ["accounts", "transactions"])
}
#expect(mockDriver.fetchTablesCallCount == 1)
}
@Test("force: true bypasses schema provider cache and hits driver")
func forceBypassesCache() async throws {
let initialTables = [
TestFixtures.makeTableInfo(name: "users"),
TestFixtures.makeTableInfo(name: "orders")
]
let mockDriver = MockDatabaseDriver()
mockDriver.tablesToReturn = initialTables
let provider = SQLSchemaProvider()
await provider.loadSchema(using: mockDriver)
let freshTables = [
TestFixtures.makeTableInfo(name: "users"),
TestFixtures.makeTableInfo(name: "orders"),
TestFixtures.makeTableInfo(name: "new_table")
]
mockDriver.tablesToReturn = freshTables
let callCountBefore = mockDriver.fetchTablesCallCount
let fetcher = LiveTableFetcher(connectionId: UUID(), schemaProvider: provider)
let result = try await fetcher.fetchTables(force: true)
#expect(result.count == 3)
#expect(result.map(\.name) == ["users", "orders", "new_table"])
#expect(mockDriver.fetchTablesCallCount == callCountBefore + 1)
}
@Test("force: true writes fresh tables back into schema provider")
func forcedFetchUpdatesSchemaProvider() async throws {
let initialTables = [TestFixtures.makeTableInfo(name: "old_table")]
let mockDriver = MockDatabaseDriver()
mockDriver.tablesToReturn = initialTables
let provider = SQLSchemaProvider()
await provider.loadSchema(using: mockDriver)
let freshTables = [
TestFixtures.makeTableInfo(name: "alpha"),
TestFixtures.makeTableInfo(name: "beta")
]
mockDriver.tablesToReturn = freshTables
let fetcher = LiveTableFetcher(connectionId: UUID(), schemaProvider: provider)
_ = try await fetcher.fetchTables(force: true)
let cached = await provider.getTables()
#expect(cached.map(\.name).sorted() == ["alpha", "beta"])
}
}