Skip to content

Commit bef0125

Browse files
Update tests to use non-promise reading + writing
1 parent df3ccf9 commit bef0125

2 files changed

Lines changed: 91 additions & 38 deletions

File tree

Tests/ApolloCacheDependentTests/ReadWriteFromStoreTests.swift

Lines changed: 86 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ class ReadWriteFromStoreTests: XCTestCase {
99
"QUERY_ROOT": ["hero": Reference(key: "hero")],
1010
"hero": ["__typename": "Droid", "name": "R2-D2"]
1111
]
12-
13-
try withCache(initialRecords: initialRecords) { (cache) in
12+
let expectation = self.expectation(description: "transaction'd")
13+
withCache(initialRecords: initialRecords) { (cache) in
1414
let store = ApolloStore(cache: cache)
1515

1616
let query = HeroNameQuery()
17-
18-
try await(store.withinReadTransaction { transaction in
17+
18+
store.withinReadTransaction({transaction in
1919
let data = try transaction.read(query: query)
2020

2121
XCTAssertEqual(data.hero?.__typename, "Droid")
2222
XCTAssertEqual(data.hero?.name, "R2-D2")
23+
expectation.fulfill()
2324
})
2425
}
26+
27+
self.waitForExpectations(timeout: 1, handler: nil)
2528
}
2629

2730
func testReadHeroNameQueryWithVariable() throws {
@@ -30,18 +33,22 @@ class ReadWriteFromStoreTests: XCTestCase {
3033
"hero(episode:JEDI)": ["__typename": "Droid", "name": "R2-D2"]
3134
]
3235

33-
try withCache(initialRecords: initialRecords) { (cache) in
36+
let expectation = self.expectation(description: "transaction'd")
37+
withCache(initialRecords: initialRecords) { (cache) in
3438
let store = ApolloStore(cache: cache)
3539

3640
let query = HeroNameQuery(episode: .jedi)
37-
38-
try await(store.withinReadTransaction { transaction in
41+
42+
store.withinReadTransaction({ transaction in
3943
let data = try transaction.read(query: query)
4044

4145
XCTAssertEqual(data.hero?.__typename, "Droid")
4246
XCTAssertEqual(data.hero?.name, "R2-D2")
47+
expectation.fulfill()
4348
})
4449
}
50+
51+
self.waitForExpectations(timeout: 1, handler: nil)
4552
}
4653

4754
func testReadHeroNameQueryWithMissingName() throws {
@@ -50,21 +57,25 @@ class ReadWriteFromStoreTests: XCTestCase {
5057
"hero": ["__typename": "Droid"]
5158
]
5259

53-
try withCache(initialRecords: initialRecords) { (cache) in
60+
let expectation = self.expectation(description: "transaction'd")
61+
withCache(initialRecords: initialRecords) { (cache) in
5462
let store = ApolloStore(cache: cache)
5563

5664
let query = HeroNameQuery()
5765

58-
try await(store.withinReadTransaction { transaction in
66+
store.withinReadTransaction({ transaction in
5967
XCTAssertThrowsError(try transaction.read(query: query)) { error in
6068
if case let error as GraphQLResultError = error {
6169
XCTAssertEqual(error.path, ["hero", "name"])
6270
XCTAssertMatch(error.underlying, JSONDecodingError.missingValue)
6371
} else {
6472
XCTFail("Unexpected error: \(error)")
6573
}
74+
expectation.fulfill()
6675
}
6776
})
77+
78+
self.waitForExpectations(timeout: 1, handler: nil)
6879
}
6980
}
7081

@@ -78,12 +89,16 @@ class ReadWriteFromStoreTests: XCTestCase {
7889
let store = ApolloStore(cache: cache)
7990

8091
let query = HeroNameQuery()
92+
let expectation = self.expectation(description: "transaction'd")
8193

82-
try await(store.withinReadWriteTransaction { transaction in
94+
store.withinReadWriteTransaction({ transaction in
8395
try transaction.update(query: query) { (data: inout HeroNameQuery.Data) in
8496
data.hero?.name = "Artoo"
97+
expectation.fulfill()
8598
}
8699
})
100+
101+
self.waitForExpectations(timeout: 1, handler: nil)
87102

88103
let result = try await(store.load(query: query))
89104

@@ -93,28 +108,38 @@ class ReadWriteFromStoreTests: XCTestCase {
93108
}
94109

95110
func testWriteHeroNameQueryWhenWriteErrorIsThrown() throws {
111+
let expectation = self.expectation(description: "transaction'd")
96112
do {
97-
try withCache(initialRecords: nil) { (cache) in
113+
withCache(initialRecords: nil) { (cache) in
98114
let store = ApolloStore(cache: cache)
99115

100-
try store.withinReadWriteTransaction { transaction in
116+
store.withinReadWriteTransaction({ transaction in
101117
let data = HeroNameQuery.Data(unsafeResultMap: [:])
102118
try transaction.write(data: data, forQuery: HeroNameQuery(episode: nil))
103-
}.await()
104-
XCTFail("write should fail")
105-
}
106-
} catch {
107-
guard let error = error as? GraphQLResultError,
108-
let jsonError = error.underlying as? JSONDecodingError else {
109-
XCTFail("unexpected error")
110-
return
111-
}
112-
113-
switch jsonError {
114-
case .missingValue: break
115-
default: XCTFail("unexpected error")
119+
}, completion: { result in
120+
defer {
121+
expectation.fulfill()
122+
}
123+
switch result {
124+
case .success:
125+
XCTFail("write should fail")
126+
case .failure(let error):
127+
guard let error = error as? GraphQLResultError,
128+
let jsonError = error.underlying as? JSONDecodingError else {
129+
XCTFail("unexpected error")
130+
return
131+
}
132+
133+
switch jsonError {
134+
case .missingValue: break
135+
default: XCTFail("unexpected error")
136+
}
137+
}
138+
})
116139
}
117140
}
141+
142+
self.waitForExpectations(timeout: 1, handler: nil)
118143
}
119144

120145
func testReadHeroAndFriendsNamesQuery() throws {
@@ -134,18 +159,22 @@ class ReadWriteFromStoreTests: XCTestCase {
134159
"1003": ["__typename": "Human", "name": "Leia Organa"],
135160
]
136161

137-
try withCache(initialRecords: initialRecords) { (cache) in
162+
withCache(initialRecords: initialRecords) { (cache) in
138163
let store = ApolloStore(cache: cache)
139164

140165
let query = HeroAndFriendsNamesQuery()
141166

142-
try await(store.withinReadTransaction { transaction in
167+
let expectation = self.expectation(description: "transaction'd")
168+
store.withinReadTransaction({ transaction in
143169
let data = try transaction.read(query: query)
144170

145171
XCTAssertEqual(data.hero?.name, "R2-D2")
146172
let friendsNames = data.hero?.friends?.compactMap { $0?.name }
147173
XCTAssertEqual(friendsNames, ["Luke Skywalker", "Han Solo", "Leia Organa"])
174+
expectation.fulfill()
148175
})
176+
177+
self.waitForExpectations(timeout: 1, handler: nil)
149178
}
150179
}
151180

@@ -171,11 +200,14 @@ class ReadWriteFromStoreTests: XCTestCase {
171200

172201
let query = HeroAndFriendsNamesQuery()
173202

174-
try await(store.withinReadWriteTransaction { transaction in
203+
let expectation = self.expectation(description: "transaction'd")
204+
store.withinReadWriteTransaction({ transaction in
175205
try transaction.update(query: query) { (data: inout HeroAndFriendsNamesQuery.Data) in
176206
data.hero?.friends?.append(.makeDroid(name: "C-3PO"))
207+
expectation.fulfill()
177208
}
178209
})
210+
self.waitForExpectations(timeout: 1, handler: nil)
179211

180212
let result = try await(store.load(query: query))
181213
guard let data = result.data else { XCTFail(); return }
@@ -208,11 +240,14 @@ class ReadWriteFromStoreTests: XCTestCase {
208240

209241
let query = HeroAndFriendsNamesQuery(episode: Episode.newhope)
210242

211-
try await(store.withinReadWriteTransaction { transaction in
243+
let expectation = self.expectation(description: "transaction'd")
244+
store.withinReadWriteTransaction({ transaction in
212245
try transaction.update(query: query) { (data: inout HeroAndFriendsNamesQuery.Data) in
213246
data.hero?.friends?.append(.makeDroid(name: "C-3PO"))
247+
expectation.fulfill()
214248
}
215249
})
250+
self.waitForExpectations(timeout: 1, handler: nil)
216251

217252
let result = try await(store.load(query: query))
218253
guard let data = result.data else { XCTFail(); return }
@@ -228,15 +263,19 @@ class ReadWriteFromStoreTests: XCTestCase {
228263
"2001": ["name": "R2-D2", "__typename": "Droid", "primaryFunction": "Protocol"]
229264
]
230265

231-
try withCache(initialRecords: initialRecords) { (cache) in
266+
withCache(initialRecords: initialRecords) { (cache) in
232267
let store = ApolloStore(cache: cache)
233268

234-
try await(store.withinReadTransaction { transaction in
269+
let expectation = self.expectation(description: "transaction'd")
270+
store.withinReadTransaction({ transaction in
235271
let r2d2 = try transaction.readObject(ofType: HeroDetails.self, withKey: "2001")
236272

237273
XCTAssertEqual(r2d2.name, "R2-D2")
238274
XCTAssertEqual(r2d2.asDroid?.primaryFunction, "Protocol")
275+
expectation.fulfill()
239276
})
277+
278+
self.waitForExpectations(timeout: 1, handler: nil)
240279
}
241280
}
242281

@@ -245,19 +284,23 @@ class ReadWriteFromStoreTests: XCTestCase {
245284
"2001": ["name": "R2-D2", "__typename": "Droid"]
246285
]
247286

248-
try withCache(initialRecords: initialRecords) { (cache) in
287+
withCache(initialRecords: initialRecords) { (cache) in
249288
let store = ApolloStore(cache: cache)
250289

251-
try await(store.withinReadTransaction { transaction in
290+
let expectation = self.expectation(description: "transaction'd")
291+
store.withinReadTransaction({ transaction in
252292
XCTAssertThrowsError(try transaction.readObject(ofType: HeroDetails.self, withKey: "2001")) { error in
253293
if case let error as GraphQLResultError = error {
254294
XCTAssertEqual(error.path, ["primaryFunction"])
255295
XCTAssertMatch(error.underlying, JSONDecodingError.missingValue)
256296
} else {
257297
XCTFail("Unexpected error: \(error)")
258298
}
299+
300+
expectation.fulfill()
259301
}
260302
})
303+
self.waitForExpectations(timeout: 1, handler: nil)
261304
}
262305
}
263306

@@ -278,15 +321,19 @@ class ReadWriteFromStoreTests: XCTestCase {
278321
"1003": ["__typename": "Human", "name": "Leia Organa"],
279322
]
280323

281-
try withCache(initialRecords: initialRecords) { (cache) in
324+
withCache(initialRecords: initialRecords) { (cache) in
282325
let store = ApolloStore(cache: cache)
283326

284-
try await(store.withinReadTransaction { transaction in
327+
let expectation = self.expectation(description: "transaction'd")
328+
store.withinReadTransaction({ transaction in
285329
let friendsNamesFragment = try transaction.readObject(ofType: FriendsNames.self, withKey: "2001")
286330

287331
let friendsNames = friendsNamesFragment.friends?.compactMap { $0?.name }
288332
XCTAssertEqual(friendsNames, ["Luke Skywalker", "Han Solo", "Leia Organa"])
333+
expectation.fulfill()
289334
})
335+
336+
self.waitForExpectations(timeout: 1, handler: nil)
290337
}
291338
}
292339

@@ -310,11 +357,14 @@ class ReadWriteFromStoreTests: XCTestCase {
310357
try withCache(initialRecords: initialRecords) { (cache) in
311358
let store = ApolloStore(cache: cache)
312359

313-
try await(store.withinReadWriteTransaction { transaction in
360+
let expectation = self.expectation(description: "transaction'd")
361+
store.withinReadWriteTransaction({ transaction in
314362
try transaction.updateObject(ofType: FriendsNames.self, withKey: "2001") { (friendsNames: inout FriendsNames) in
315363
friendsNames.friends?.append(.makeDroid(name: "C-3PO"))
364+
expectation.fulfill()
316365
}
317366
})
367+
self.waitForExpectations(timeout: 1, handler: nil)
318368

319369
let result = try await(store.load(query: HeroAndFriendsNamesQuery()))
320370
guard let data = result.data else { XCTFail(); return }

Tests/ApolloCacheDependentTests/WatchQueryTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ class WatchQueryTests: XCTestCase {
312312
"QUERY_ROOT.hero.friends.1": ["__typename": "Human", "name": "Han Solo"],
313313
"QUERY_ROOT.hero.friends.2": ["__typename": "Human", "name": "Leia Organa"],
314314
]
315-
try withCache(initialRecords: initialRecords) { (cache) in
315+
withCache(initialRecords: initialRecords) { (cache) in
316316
let networkTransport = MockNetworkTransport(body: [:])
317317

318318
let store = ApolloStore(cache: cache)
@@ -353,11 +353,14 @@ class WatchQueryTests: XCTestCase {
353353
waitForExpectations(timeout: 5, handler: nil)
354354

355355
let nameQuery = HeroNameQuery()
356-
try await(store.withinReadWriteTransaction { transaction in
356+
expectation = self.expectation(description: "transaction'd")
357+
store.withinReadWriteTransaction({ transaction in
357358
try transaction.update(query: nameQuery) { (data: inout HeroNameQuery.Data) in
358359
data.hero?.name = "Artoo"
359360
}
361+
expectation.fulfill()
360362
})
363+
self.waitForExpectations(timeout: 1, handler: nil)
361364

362365
verifyResult = { result in
363366
switch result {

0 commit comments

Comments
 (0)