-
-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathRowOperationsManagerCopyTests.swift
More file actions
204 lines (165 loc) · 6.44 KB
/
RowOperationsManagerCopyTests.swift
File metadata and controls
204 lines (165 loc) · 6.44 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
195
196
197
198
199
200
201
202
203
204
import Foundation
@testable import TablePro
import Testing
private final class MockClipboardProvider: ClipboardProvider {
var lastWrittenText: String?
var textToRead: String?
var lastWasGridRows = false
func readText() -> String? { textToRead }
func writeText(_ text: String) {
lastWrittenText = text
lastWasGridRows = false
}
func writeRows(tsv: String, html: String?) {
lastWrittenText = tsv
lastWasGridRows = true
}
var hasText: Bool { textToRead != nil }
var hasGridRows: Bool { lastWasGridRows }
}
@MainActor
@Suite("RowOperationsManager Copy")
struct RowOperationsManagerCopyTests {
private static let defaultColumns = ["id", "name", "email"]
private func makeManager() -> (RowOperationsManager, DataChangeManager) {
let changeManager = DataChangeManager()
changeManager.configureForTable(
tableName: "users",
columns: Self.defaultColumns,
primaryKeyColumns: ["id"],
databaseType: .mysql
)
let manager = RowOperationsManager(changeManager: changeManager)
return (manager, changeManager)
}
private func makeTableRows(rows: [[String?]], columns: [String]? = nil) -> TableRows {
let cols = columns ?? Self.defaultColumns
let columnTypes: [ColumnType] = Array(repeating: .text(rawType: nil), count: cols.count)
return TableRows.from(queryRows: rows, columns: cols, columnTypes: columnTypes)
}
private func copyAndCapture(
manager: RowOperationsManager,
indices: Set<Int>,
rows: [[String?]],
columns: [String]? = nil,
includeHeaders: Bool = false
) -> String? {
let clipboard = MockClipboardProvider()
ClipboardService.shared = clipboard
let tableRows = makeTableRows(rows: rows, columns: columns ?? Self.defaultColumns)
manager.copySelectedRowsToClipboard(
selectedIndices: indices,
tableRows: tableRows,
includeHeaders: includeHeaders
)
return clipboard.lastWrittenText
}
@Test("Single row copy produces tab-separated values")
func singleRowTSV() {
let (manager, _) = makeManager()
let rows: [[String?]] = [["1", "Alice", "alice@test.com"]]
let result = copyAndCapture(manager: manager, indices: [0], rows: rows)
#expect(result == "1\tAlice\talice@test.com")
}
@Test("Multiple rows separated by newlines in TSV format")
func multipleRowsTSV() {
let (manager, _) = makeManager()
let rows: [[String?]] = [
["1", "Alice", "a@test.com"],
["2", "Bob", "b@test.com"],
]
let result = copyAndCapture(manager: manager, indices: [0, 1], rows: rows)
#expect(result == "1\tAlice\ta@test.com\n2\tBob\tb@test.com")
}
@Test("NULL values rendered as literal NULL string")
func nullValuesRenderedAsNullString() {
let (manager, _) = makeManager()
let rows: [[String?]] = [[nil, "Alice", nil]]
let result = copyAndCapture(manager: manager, indices: [0], rows: rows)
#expect(result == "NULL\tAlice\tNULL")
}
@Test("Mixed NULL and non-NULL values in same row")
func mixedNullAndNonNull() {
let (manager, _) = makeManager()
let rows: [[String?]] = [
["1", nil, "a@test.com"],
[nil, "Bob", nil],
]
let result = copyAndCapture(manager: manager, indices: [0, 1], rows: rows)
let lines = result?.components(separatedBy: "\n")
#expect(lines?.count == 2)
#expect(lines?[0] == "1\tNULL\ta@test.com")
#expect(lines?[1] == "NULL\tBob\tNULL")
}
@Test("Empty selection produces no clipboard write")
func emptySelectionNoWrite() {
let (manager, _) = makeManager()
let rows = TestFixtures.makeRows(count: 3)
let clipboard = MockClipboardProvider()
ClipboardService.shared = clipboard
let tableRows = makeTableRows(rows: rows)
manager.copySelectedRowsToClipboard(
selectedIndices: [],
tableRows: tableRows
)
#expect(clipboard.lastWrittenText == nil)
}
@Test("Large row count produces correct first and last rows")
func largeRowCount() {
let (manager, _) = makeManager()
let count = 1_000
let rows: [[String?]] = (0..<count).map { i in
["\(i)", "name_\(i)", "email_\(i)"]
}
let result = copyAndCapture(
manager: manager,
indices: Set(0..<count),
rows: rows
)
let lines = result?.components(separatedBy: "\n") ?? []
#expect(lines.count == count)
#expect(lines.first == "0\tname_0\temail_0")
#expect(lines.last == "\(count - 1)\tname_\(count - 1)\temail_\(count - 1)")
}
@Test("Copied rows are in sorted index order regardless of selection order")
func rowsInSortedOrder() {
let (manager, _) = makeManager()
let rows: [[String?]] = [
["A"],
["B"],
["C"],
]
let result = copyAndCapture(manager: manager, indices: [2, 0], rows: rows, columns: ["letter"])
#expect(result == "A\nC")
}
@Test("Copy with headers prepends column names as first TSV line")
func copyWithHeaders() {
let (manager, _) = makeManager()
let rows: [[String?]] = [["1", "Alice", "a@test.com"]]
let result = copyAndCapture(
manager: manager,
indices: [0],
rows: rows,
columns: ["id", "name", "email"],
includeHeaders: true
)
let lines = result?.components(separatedBy: "\n") ?? []
#expect(lines.count == 2)
#expect(lines[0] == "id\tname\temail")
#expect(lines[1] == "1\tAlice\ta@test.com")
}
@Test("Out-of-bounds indices are skipped gracefully")
func outOfBoundsIndicesSkipped() {
let (manager, _) = makeManager()
let rows: [[String?]] = [["1", "Alice"]]
let result = copyAndCapture(manager: manager, indices: [0, 5, 10], rows: rows, columns: ["id", "name"])
#expect(result == "1\tAlice")
}
@Test("Row with all NULL values produces tab-separated NULL strings")
func allNullRow() {
let (manager, _) = makeManager()
let rows: [[String?]] = [[nil, nil, nil]]
let result = copyAndCapture(manager: manager, indices: [0], rows: rows)
#expect(result == "NULL\tNULL\tNULL")
}
}