Skip to content

Commit

Permalink
Use a failable iterator when selecting records (#1917)
Browse files Browse the repository at this point in the history
* Use a failable iterator when selecting records
* Add test to verify failable iterator behaviour
  • Loading branch information
calvincestari authored Aug 26, 2021
1 parent 550f1b1 commit 45d382d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Apollo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
DED46042261CEA8A0086EF63 /* TestServerURLs.swift in Sources */ = {isa = PBXBuildFile; fileRef = DED45C172615308E0086EF63 /* TestServerURLs.swift */; };
DED46051261CEAD20086EF63 /* StarWarsAPI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9FCE2CFA1E6C213D00E34457 /* StarWarsAPI.framework */; };
E616B6D126C3335600DB049E /* ExecutionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E616B6D026C3335600DB049E /* ExecutionTests.swift */; };
E61DD76526D60C1800C41614 /* SQLiteDotSwiftDatabaseBehaviorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E61DD76426D60C1800C41614 /* SQLiteDotSwiftDatabaseBehaviorTests.swift */; };
E86D8E05214B32FD0028EFE1 /* JSONTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E86D8E03214B32DA0028EFE1 /* JSONTests.swift */; };
F16D083C21EF6F7300C458B8 /* QueryFromJSONBuildingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F16D083B21EF6F7300C458B8 /* QueryFromJSONBuildingTests.swift */; };
F82E62E122BCD223000C311B /* AutomaticPersistedQueriesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F82E62E022BCD223000C311B /* AutomaticPersistedQueriesTests.swift */; };
Expand Down Expand Up @@ -808,6 +809,7 @@
DED45FB2261CDE980086EF63 /* Apollo-CITestPlan.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = "Apollo-CITestPlan.xctestplan"; sourceTree = "<group>"; };
DED45FB3261CDEC60086EF63 /* Apollo-CodegenTestPlan.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = "Apollo-CodegenTestPlan.xctestplan"; sourceTree = "<group>"; };
E616B6D026C3335600DB049E /* ExecutionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExecutionTests.swift; sourceTree = "<group>"; };
E61DD76426D60C1800C41614 /* SQLiteDotSwiftDatabaseBehaviorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SQLiteDotSwiftDatabaseBehaviorTests.swift; sourceTree = "<group>"; };
E86D8E03214B32DA0028EFE1 /* JSONTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONTests.swift; sourceTree = "<group>"; };
F16D083B21EF6F7300C458B8 /* QueryFromJSONBuildingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QueryFromJSONBuildingTests.swift; sourceTree = "<group>"; };
F82E62E022BCD223000C311B /* AutomaticPersistedQueriesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AutomaticPersistedQueriesTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1494,6 +1496,7 @@
9FF90A6B1DDDEB420034C3B6 /* ReadFieldValueTests.swift */,
C338DF1622DD9DE9006AF33E /* RequestBodyCreatorTests.swift */,
9B96500824BE6201003C29C0 /* RequestChainTests.swift */,
E61DD76426D60C1800C41614 /* SQLiteDotSwiftDatabaseBehaviorTests.swift */,
9B9BBB1A24DB75E60021C30F /* UploadRequestTests.swift */,
5BB2C0222380836100774170 /* VersionNumberTests.swift */,
);
Expand Down Expand Up @@ -2625,6 +2628,7 @@
9F91CF8F1F6C0DB2008DD0BE /* MutatingResultsTests.swift in Sources */,
E616B6D126C3335600DB049E /* ExecutionTests.swift in Sources */,
9B9BBB1C24DB760B0021C30F /* UploadRequestTests.swift in Sources */,
E61DD76526D60C1800C41614 /* SQLiteDotSwiftDatabaseBehaviorTests.swift in Sources */,
9BC139A424EDCA6C00876D29 /* InterceptorTests.swift in Sources */,
F82E62E122BCD223000C311B /* AutomaticPersistedQueriesTests.swift in Sources */,
9BC139A824EDCE4F00876D29 /* RetryToCountThenSucceedInterceptor.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Sources/ApolloSQLite/SQLiteDotSwiftDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class SQLiteDotSwiftDatabase: SQLiteDatabase {

public func selectRawRows(forKeys keys: Set<CacheKey>) throws -> [DatabaseRow] {
let query = self.records.filter(keys.contains(keyColumn))
return try self.db.prepare(query).map { row in
return try self.db.prepareRowIterator(query).map { row in
let record = row[self.recordColumn]
let key = row[self.keyColumn]

Expand Down
26 changes: 26 additions & 0 deletions Tests/ApolloTests/SQLiteDotSwiftDatabaseBehaviorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import XCTest
@testable import ApolloSQLite
import ApolloTestSupport
import SQLite

class SQLiteDotSwiftDatabaseBehaviorTests: XCTestCase {

func testSelection_withForcedError_shouldThrow() throws {
let sqliteFileURL = SQLiteTestCacheProvider.temporarySQLiteFileURL()
let db = try! SQLiteDotSwiftDatabase(fileURL: sqliteFileURL)

try! db.createRecordsTableIfNeeded()
try! db.addOrUpdateRecordString("record", for: "key")

var rows = [DatabaseRow]()
XCTAssertNoThrow(rows = try db.selectRawRows(forKeys: ["key"]))
XCTAssertEqual(rows.count, 1)

// Use SQLite directly to manipulate the database (cannot be done with SQLiteDotSwiftDatabase)
let connection = try Connection(.uri(sqliteFileURL.absoluteString), readonly: false)
let table = Table(SQLiteDotSwiftDatabase.tableName)
try! connection.run(table.drop(ifExists: false))

XCTAssertThrowsError(try db.selectRawRows(forKeys: ["key"]))
}
}

0 comments on commit 45d382d

Please sign in to comment.