-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowserFeatureTests.swift
More file actions
135 lines (124 loc) · 4.82 KB
/
BrowserFeatureTests.swift
File metadata and controls
135 lines (124 loc) · 4.82 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
import ComposableArchitecture
import XCTest
@testable import Storefront
@MainActor
final class BrowserFeatureTests: XCTestCase {
func testOnAppearLoadsTablesAndAutoSelectsFirst() async {
let sampleTables = [
TableInfo(name: "artists", kind: .table, rowCount: 275, classification: .standard),
TableInfo(name: "tracks", kind: .table, rowCount: 3_503, classification: .standard)
]
let samplePage = RowPage(
columns: [ColumnInfo(name: "id", declaredType: "INTEGER", isPrimaryKey: true, isNotNull: true, isSwiftDataEntity: false)],
rows: [RowSnapshot(index: 0, values: [.integer(1)])],
totalRows: 1,
offset: 0,
limit: 200
)
let url = URL(fileURLWithPath: "/tmp/sample.sqlite")
let store = TestStore(
initialState: BrowserFeature.State(databaseURL: url)
) {
BrowserFeature()
} withDependencies: {
$0.database.inspect = { @Sendable _ in DatabaseSchema(kind: .standard, tables: sampleTables) }
$0.database.page = { @Sendable _, _, _, _, _ in samplePage }
$0.database.close = { @Sendable _ in }
$0.fileWatcher.changes = { @Sendable _ in AsyncStream { _ in } }
}
store.exhaustivity = .off
await store.send(.onAppear) { $0.isLoading = true }
await store.receive(\.schemaLoaded) {
$0.isLoading = false
$0.tables = sampleTables
$0.databaseKind = .standard
$0.selectedTableID = "artists"
$0.isLoadingRows = true
}
await store.receive(\.rowsLoaded) {
$0.isLoadingRows = false
$0.currentPage = samplePage
}
await store.send(.onDisappear)
}
func testOnAppearPropagatesFailure() async {
struct SampleError: LocalizedError {
var errorDescription: String? { "disk corrupted" }
}
let url = URL(fileURLWithPath: "/tmp/broken.sqlite")
let store = TestStore(
initialState: BrowserFeature.State(databaseURL: url)
) {
BrowserFeature()
} withDependencies: {
$0.database.inspect = { @Sendable _ in throw SampleError() }
$0.database.page = { @Sendable _, _, _, _, _ in
throw SampleError()
}
$0.database.close = { @Sendable _ in }
$0.fileWatcher.changes = { @Sendable _ in AsyncStream { _ in } }
}
store.exhaustivity = .off
await store.send(.onAppear) { $0.isLoading = true }
await store.receive(\.schemaFailedToLoad) {
$0.isLoading = false
$0.loadErrorMessage = "disk corrupted"
}
await store.send(.onDisappear)
}
func testTableSelectionTriggersRowLoad() async {
let samplePage = RowPage(
columns: [ColumnInfo(name: "id", declaredType: "INTEGER", isPrimaryKey: true, isNotNull: true, isSwiftDataEntity: false)],
rows: [RowSnapshot(index: 0, values: [.integer(42)])],
totalRows: 1,
offset: 0,
limit: 200
)
let url = URL(fileURLWithPath: "/tmp/sample.sqlite")
let store = TestStore(
initialState: BrowserFeature.State(
databaseURL: url,
tables: [TableInfo(name: "a", kind: .table, rowCount: 1, classification: .standard)],
selectedTableID: nil
)
) {
BrowserFeature()
} withDependencies: {
$0.database.page = { @Sendable _, _, _, _, _ in samplePage }
}
await store.send(.tableSelected("a")) {
$0.selectedTableID = "a"
$0.isLoadingRows = true
}
await store.receive(\.rowsLoaded) {
$0.isLoadingRows = false
$0.currentPage = samplePage
}
}
func testFileChangedIncrementsToastCounter() async {
let url = URL(fileURLWithPath: "/tmp/sample.sqlite")
let store = TestStore(
initialState: BrowserFeature.State(
databaseURL: url,
tables: [TableInfo(name: "a", kind: .table, rowCount: 1, classification: .standard)],
selectedTableID: "a"
)
) {
BrowserFeature()
} withDependencies: {
$0.database.inspect = { @Sendable _ in
DatabaseSchema(
kind: .standard,
tables: [TableInfo(name: "a", kind: .table, rowCount: 2, classification: .standard)]
)
}
$0.database.page = { @Sendable _, _, _, _, _ in
RowPage(columns: [], rows: [], totalRows: 0, offset: 0, limit: 200)
}
}
store.exhaustivity = .off
await store.send(.fileChanged) {
$0.liveReloadToast = 1
}
}
}