-
-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathSortCacheInvalidationTests.swift
More file actions
98 lines (82 loc) · 3.75 KB
/
SortCacheInvalidationTests.swift
File metadata and controls
98 lines (82 loc) · 3.75 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
//
// SortCacheInvalidationTests.swift
// TableProTests
//
// Locks the contract that row mutations invalidate querySortCache for the
// affected tab. Pre-merge, only the coordinator-side cache was invalidated;
// the view-side @State sortCache stayed stale, so a sorted small table
// returned out-of-date sortedIDs after add / undo / paste / delete. After
// the merge there is one cache and these tests guard the invalidation set.
//
import Foundation
import Testing
@testable import TablePro
@Suite("querySortCache invalidation on row mutations")
@MainActor
struct SortCacheInvalidationTests {
private func makeCoordinator() -> (MainContentCoordinator, QueryTabManager, UUID) {
let tabManager = QueryTabManager()
let coordinator = MainContentCoordinator(
connection: TestFixtures.makeConnection(),
tabManager: tabManager,
changeManager: DataChangeManager(),
filterStateManager: FilterStateManager(),
columnVisibilityManager: ColumnVisibilityManager(),
toolbarState: ConnectionToolbarState()
)
tabManager.addTableTab(tableName: "users")
let tabIndex = tabManager.selectedTabIndex ?? 0
tabManager.tabs[tabIndex].tableContext.isEditable = true
let tabId = tabManager.tabs[tabIndex].id
return (coordinator, tabManager, tabId)
}
private func seedCache(_ coordinator: MainContentCoordinator, for tabId: UUID) {
coordinator.querySortCache[tabId] = QuerySortCacheEntry(
sortedIDs: [.existing(0), .existing(1), .existing(2)],
columnIndex: 1,
direction: .ascending,
schemaVersion: 0
)
}
private func seedRows(_ coordinator: MainContentCoordinator, for tabId: UUID, count: Int) {
let columns = ["id", "name"]
let rows = (0..<count).map { i in ["\(i)", "name\(i)"] }
let columnTypes: [ColumnType] = Array(repeating: .text(rawType: nil), count: columns.count)
let tableRows = TableRows.from(queryRows: rows, columns: columns, columnTypes: columnTypes)
coordinator.setActiveTableRows(tableRows, for: tabId)
}
@Test("addNewRow clears querySortCache for the tab")
func addNewRowInvalidatesCache() {
let (coordinator, _, tabId) = makeCoordinator()
seedRows(coordinator, for: tabId, count: 3)
seedCache(coordinator, for: tabId)
coordinator.addNewRow()
#expect(coordinator.querySortCache[tabId] == nil)
}
@Test("deleteSelectedRows clears querySortCache when physically removing inserted rows")
func physicalDeleteInvalidatesCache() {
let (coordinator, _, tabId) = makeCoordinator()
seedRows(coordinator, for: tabId, count: 3)
coordinator.addNewRow()
let insertedIndex = coordinator.tableRowsStore.tableRows(for: tabId).count - 1
seedCache(coordinator, for: tabId)
coordinator.deleteSelectedRows(indices: [insertedIndex])
#expect(coordinator.querySortCache[tabId] == nil)
}
@Test("deleteSelectedRows preserves querySortCache on soft delete of existing rows")
func softDeletePreservesCache() {
let (coordinator, _, tabId) = makeCoordinator()
seedRows(coordinator, for: tabId, count: 5)
seedCache(coordinator, for: tabId)
coordinator.deleteSelectedRows(indices: [0, 1])
#expect(coordinator.querySortCache[tabId] != nil)
}
@Test("duplicateSelectedRow clears querySortCache for the tab")
func duplicateRowInvalidatesCache() {
let (coordinator, _, tabId) = makeCoordinator()
seedRows(coordinator, for: tabId, count: 3)
seedCache(coordinator, for: tabId)
coordinator.duplicateSelectedRow(index: 0)
#expect(coordinator.querySortCache[tabId] == nil)
}
}