-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBatchRequestTests.swift
More file actions
258 lines (229 loc) · 10.4 KB
/
BatchRequestTests.swift
File metadata and controls
258 lines (229 loc) · 10.4 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// BatchRequestTests.swift
// CoreDataRepository
//
// This source code is licensed under the MIT License (MIT) found in the
// LICENSE file in the root directory of this source tree.
import CoreData
import CoreDataRepository
import CustomDump
import Internal
import Testing
extension CoreDataRepositoryTests {
@Suite
struct BatchRequestTests: CoreDataTestSuite {
let container: NSPersistentContainer
let repositoryContext: NSManagedObjectContext
let repository: CoreDataRepository
let values: [[String: Any]] = [
ManagedIdUrlModel_UuidId.seeded(1).asDict,
ManagedIdUrlModel_UuidId.seeded(2).asDict,
ManagedIdUrlModel_UuidId.seeded(3).asDict,
ManagedIdUrlModel_UuidId.seeded(4).asDict,
ManagedIdUrlModel_UuidId.seeded(5).asDict,
]
let failureInsertMovies: [[String: Any]] = [
["id": "A", "title": 1, "releaseDate": "A"],
["id": "B", "title": 2, "releaseDate": "B"],
["id": "C", "title": 3, "releaseDate": "C"],
["id": "D", "title": 4, "releaseDate": "D"],
["id": "E", "title": 5, "releaseDate": "E"],
]
let failureCreateMovies: [[String: Any]] = [
["id": UUID(uniform: "A"), "title": "A", "releaseDate": Date()],
["id": UUID(uniform: "A"), "title": "B", "releaseDate": Date()],
["id": UUID(uniform: "A"), "title": "C", "releaseDate": Date()],
["id": UUID(uniform: "A"), "title": "D", "releaseDate": Date()],
["id": UUID(uniform: "A"), "title": "E", "releaseDate": Date()],
]
func mapDictToManagedModel(_ dict: [String: Any]) throws -> ManagedModel_UuidId {
try mapDictToUnmanagedModel(dict).asManagedModel(in: repositoryContext)
}
func mapDictToUnmanagedModel(_ dict: [String: Any]) throws -> ManagedIdUrlModel_UuidId {
let id = try #require(dict["id"] as? UUID)
let bool = try #require(dict["bool"] as? Bool)
let date = try #require(dict["date"] as? Date)
let decimal = try #require(dict["decimal"] as? Decimal)
let double = try #require(dict["double"] as? Double)
let float = try #require(dict["float"] as? Float)
let int = try #require(dict["int"] as? Int)
let string = try #require(dict["string"] as? String)
let uuid = try #require(dict["uuid"] as? UUID)
return .init(
bool: bool,
date: date,
decimal: decimal,
double: double,
float: float,
id: id,
int: int,
managedIdUrl: nil,
string: string,
uuid: uuid
)
}
@Test(arguments: [false, true])
func insertSuccess(inTransaction: Bool) async throws {
let fetchRequest = ManagedIdUrlModel_UuidId.managedFetchRequest()
try await repositoryContext.perform {
let count = try repositoryContext.count(for: fetchRequest)
expectNoDifference(count, 0, "Count of objects in CoreData should be zero at the start of each test.")
}
let historyTimeStamp = Date()
let transactionAuthor: String = #function
let request = try NSBatchInsertRequest(
entityName: #require(ManagedModel_UuidId.entity().name),
objects: values
)
let result = if inTransaction {
try await repository.withTransaction(transactionAuthor: transactionAuthor) { _ in
await repository
.insert(request)
}
} else {
await repository
.insert(request, transactionAuthor: transactionAuthor)
}
switch result {
case .success:
break
case .failure:
Issue.record("Not expecting a failure result")
}
try await repositoryContext.perform {
let data = try repositoryContext.fetch(fetchRequest)
expectNoDifference(
data.map(\.string).sorted(),
["1", "2", "3", "4", "5"],
"Inserted titles should match expectation"
)
}
// Transaction author refuses to be applied when going through a transaction. Need to investigate further.
if inTransaction {
withKnownIssue {
try verify(transactionAuthor: transactionAuthor, timeStamp: historyTimeStamp)
}
} else {
try verify(transactionAuthor: transactionAuthor, timeStamp: historyTimeStamp)
}
}
@Test(arguments: [false, true])
func insertFailure(inTransaction: Bool) async throws {
let fetchRequest = ManagedIdUrlModel_UuidId.managedFetchRequest()
try await repositoryContext.perform {
let count = try repositoryContext.count(for: fetchRequest)
expectNoDifference(count, 0, "Count of objects in CoreData should be zero at the start of each test.")
}
let request = try NSBatchInsertRequest(
entityName: #require(ManagedModel_UuidId.entity().name),
objects: failureInsertMovies
)
let result = if inTransaction {
try await repository.withTransaction { _ in
await repository.insert(request)
}
} else {
await repository.insert(request)
}
switch result {
case .success:
Issue.record("Not expecting a success result")
case .failure:
break
}
try await repositoryContext.perform {
let data = try repositoryContext.fetch(fetchRequest)
expectNoDifference(data.map(\.string).sorted(), [], "There should be no inserted values.")
}
}
@Test(arguments: [false, true])
func updateSuccess(inTransaction: Bool) async throws {
let fetchRequest = ManagedIdUrlModel_UuidId.managedFetchRequest()
try await repositoryContext.perform {
let count = try repositoryContext.count(for: fetchRequest)
expectNoDifference(count, 0, "Count of objects in CoreData should be zero at the start of each test.")
_ = try values
.map(mapDictToManagedModel(_:))
try repositoryContext.save()
}
let predicate = NSPredicate(value: true)
let request = try NSBatchUpdateRequest(entityName: #require(ManagedModel_UuidId.entity().name))
request.predicate = predicate
request.propertiesToUpdate = ["string": "Updated!", "int": 1]
let historyTimeStamp = Date()
let transactionAuthor: String = #function
_ = if inTransaction {
try await repository.withTransaction(transactionAuthor: transactionAuthor) { _ in
await repository
.update(request)
}
} else {
await repository
.update(request, transactionAuthor: transactionAuthor)
}
try await repositoryContext.perform {
let data = try repositoryContext.fetch(fetchRequest)
expectNoDifference(
data.map(\.string).sorted(),
["Updated!", "Updated!", "Updated!", "Updated!", "Updated!"],
"Updated titles should match request"
)
}
// Transaction author refuses to be applied when going through a transaction. Need to investigate further.
if inTransaction {
withKnownIssue {
try verify(transactionAuthor: transactionAuthor, timeStamp: historyTimeStamp)
}
} else {
try verify(transactionAuthor: transactionAuthor, timeStamp: historyTimeStamp)
}
}
@Test(arguments: [false, true])
func deleteSuccess(inTransaction: Bool) async throws {
let fetchRequest = ManagedIdUrlModel_UuidId.managedFetchRequest()
try await repositoryContext.perform {
let count = try repositoryContext.count(for: fetchRequest)
expectNoDifference(count, 0, "Count of objects in CoreData should be zero at the start of each test.")
_ = try values
.map(mapDictToManagedModel(_:))
try repositoryContext.save()
}
let request =
try NSBatchDeleteRequest(fetchRequest: NSFetchRequest<NSFetchRequestResult>(entityName: #require(
ManagedModel_UuidId
.entity().name
)))
let historyTimeStamp = Date()
let transactionAuthor: String = #function
_ = if inTransaction {
try await repository.withTransaction(transactionAuthor: transactionAuthor) { _ in
await repository
.delete(request, transactionAuthor: transactionAuthor)
}
} else {
await repository
.delete(request, transactionAuthor: transactionAuthor)
}
try await repositoryContext.perform {
let data = try repositoryContext.fetch(fetchRequest)
expectNoDifference(data.map(\.string).sorted(), [], "There should be no remaining values.")
}
// Transaction author refuses to be applied when going through a transaction. Need to investigate further.
if inTransaction {
withKnownIssue {
try verify(transactionAuthor: transactionAuthor, timeStamp: historyTimeStamp)
}
} else {
try verify(transactionAuthor: transactionAuthor, timeStamp: historyTimeStamp)
}
}
init(
container: NSPersistentContainer,
repositoryContext: NSManagedObjectContext,
repository: CoreDataRepository
) {
self.container = container
self.repositoryContext = repositoryContext
self.repository = repository
}
}
}