♻️ Revise API for local JSON fixtures

Bundle.url(for:)
This commit is contained in:
Ben Chatelain 2020-09-06 14:15:02 -06:00
parent 3fefdbd104
commit 1286b70e7e
3 changed files with 56 additions and 43 deletions

View file

@ -12,50 +12,53 @@ import Quick
class MasStoreSearchSpec: QuickSpec {
override func spec() {
describe("store search") {
it("can find slack") {
let networkSession = NetworkSessionMockFromFile(responseFile: "search/slack.json")
let storeSearch = MasStoreSearch(networkManager: NetworkManager(session: networkSession))
describe("store") {
context("when searched") {
it("can find slack") {
let networkSession = NetworkSessionMockFromFile(responseFile: "search/slack.json")
let storeSearch = MasStoreSearch(networkManager: NetworkManager(session: networkSession))
var searchList: SearchResultList
do {
searchList = try storeSearch.search(for: "slack")
expect(searchList.resultCount) == 6
expect(searchList.results.count) == 6
} catch {
let maserror = error as! MASError
if case let .jsonParsing(nserror) = maserror {
fail("\(maserror) \(nserror!)")
var searchList: SearchResultList
do {
searchList = try storeSearch.search(for: "slack")
expect(searchList.resultCount) == 39
expect(searchList.results.count) == 39
} catch {
let maserror = error as! MASError
if case let .jsonParsing(nserror) = maserror {
fail("\(maserror) \(nserror!)")
}
}
}
}
}
describe("store lookup") {
it("can find slack") {
let appId = 803_453_959
let networkSession = NetworkSessionMockFromFile(responseFile: "lookup/slack.json")
let storeSearch = MasStoreSearch(networkManager: NetworkManager(session: networkSession))
var lookup: SearchResult?
do {
lookup = try storeSearch.lookup(app: appId)
} catch {
let maserror = error as! MASError
if case let .jsonParsing(nserror) = maserror {
fail("\(maserror) \(nserror!)")
context("when lookup used") {
it("can find slack") {
let appId = 803_453_959
let networkSession = NetworkSessionMockFromFile(responseFile: "lookup/slack.json")
let storeSearch = MasStoreSearch(networkManager: NetworkManager(session: networkSession))
var lookup: SearchResult?
do {
lookup = try storeSearch.lookup(app: appId)
} catch {
let maserror = error as! MASError
if case let .jsonParsing(nserror) = maserror {
fail("\(maserror) \(nserror!)")
}
}
guard let result = lookup else { fatalError("lookup result was nil") }
expect(result.trackId) == appId
expect(result.bundleId) == "com.tinyspeck.slackmacgap"
expect(result.price) == 0
expect(result.sellerName) == "Slack Technologies, Inc."
expect(result.sellerUrl) == "https://slack.com"
expect(result.trackName) == "Slack"
expect(result.trackViewUrl) == "https://itunes.apple.com/us/app/slack/id803453959?mt=12&uo=4"
expect(result.version) == "3.3.3"
}
guard let result = lookup else { fatalError("lookup result was nil") }
expect(result.trackId) == appId
expect(result.bundleId) == "com.tinyspeck.slackmacgap"
expect(result.price) == 0
expect(result.sellerName) == "Slack Technologies, Inc."
expect(result.sellerUrl) == "https://slack.com"
expect(result.trackName) == "Slack"
expect(result.trackViewUrl) == "https://itunes.apple.com/us/app/slack/id803453959?mt=12&uo=4"
expect(result.version) == "3.3.3"
}
}
}

View file

@ -8,24 +8,34 @@
import Foundation
extension Data {
/// Unsafe initializer for loading data from string paths.
/// - Parameter file: Relative path within the JSON folder
init(from fileName: String) {
let fileURL = Bundle.url(for: fileName)!
print("fileURL: \(fileURL)")
try! self.init(contentsOf: fileURL, options: .mappedIfSafe)
}
}
extension Bundle {
/// Locates a JSON response file from the test bundle.
///
/// - Parameter fileName: Name of file to locate.
/// - Returns: URL to file.
static func jsonResponse(fileName: String) -> URL? {
return Bundle(for: NetworkSessionMock.self).fileURL(fileName: fileName)
static func url(for fileName: String) -> URL? {
return Bundle(for: NetworkSessionMock.self).url(for: fileName)
}
/// Builds a URL for a file in the JSON directory of the current bundle.
///
/// - Parameter fileName: Name of file to locate.
/// - Returns: URL to file.
func fileURL(fileName: String) -> URL? {
func url(for fileName: String) -> URL? {
guard let path = self.path(forResource: fileName.fileNameWithoutExtension,
ofType: fileName.fileExtension,
inDirectory: "JSON")
else { fatalError("Unable to load file \(fileName)") }
else { fatalError("Unable to load file \(fileName)") }
return URL(fileURLWithPath: path)
}

View file

@ -26,8 +26,8 @@ class NetworkSessionMockFromFile: NetworkSessionMock {
/// - url: unused
/// - completionHandler: Closure which is delivered either data or an error.
@objc override func loadData(from _: URL, completionHandler: @escaping (Data?, Error?) -> Void) {
guard let fileURL = Bundle.jsonResponse(fileName: responseFile)
else { fatalError("Unable to load file \(responseFile)") }
guard let fileURL = Bundle.url(for: responseFile)
else { fatalError("Unable to load file \(responseFile)") }
do {
let data = try Data(contentsOf: fileURL, options: .mappedIfSafe)