Skip to content

Commit

Permalink
Merge pull request #121 from nerdishbynature/fix-tests-and-decoding-i…
Browse files Browse the repository at this point in the history
…ssues

Fix tests and decoding issues
  • Loading branch information
pietbrauer authored Jan 30, 2021
2 parents 990a893 + 18f8a19 commit 9521cdf
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 32 deletions.
8 changes: 4 additions & 4 deletions OctoKit/Follow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public extension Octokit {
@discardableResult
func myFollowers(_ session: RequestKitURLSession = URLSession.shared, completion: @escaping (_ response: Response<[User]>) -> Void) -> URLSessionDataTaskProtocol? {
let router = FollowRouter.readAuthenticatedFollowers(configuration)
return router.load(session, expectedResultType: [User].self) { users, error in
return router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: [User].self) { users, error in
if let error = error {
completion(Response.failure(error))
} else {
Expand All @@ -34,7 +34,7 @@ public extension Octokit {
@discardableResult
func followers(_ session: RequestKitURLSession = URLSession.shared, name: String, completion: @escaping (_ response: Response<[User]>) -> Void) -> URLSessionDataTaskProtocol? {
let router = FollowRouter.readFollowers(name, configuration)
return router.load(session, expectedResultType: [User].self) { users, error in
return router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: [User].self) { users, error in
if let error = error {
completion(Response.failure(error))
} else {
Expand All @@ -53,7 +53,7 @@ public extension Octokit {
@discardableResult
func myFollowing(_ session: RequestKitURLSession = URLSession.shared, completion: @escaping (_ response: Response<[User]>) -> Void) -> URLSessionDataTaskProtocol? {
let router = FollowRouter.readAuthenticatedFollowing(configuration)
return router.load(session, expectedResultType: [User].self) { users, error in
return router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: [User].self) { users, error in
if let error = error {
completion(Response.failure(error))
} else {
Expand All @@ -73,7 +73,7 @@ public extension Octokit {
@discardableResult
func following(_ session: RequestKitURLSession = URLSession.shared, name: String, completion: @escaping (_ response: Response<[User]>) -> Void) -> URLSessionDataTaskProtocol? {
let router = FollowRouter.readFollowing(name, configuration)
return router.load(session, expectedResultType: [User].self) { users, error in
return router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: [User].self) { users, error in
if let error = error {
completion(Response.failure(error))
} else {
Expand Down
4 changes: 2 additions & 2 deletions OctoKit/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public extension Octokit {
@discardableResult
func user(_ session: RequestKitURLSession = URLSession.shared, name: String, completion: @escaping (_ response: Response<User>) -> Void) -> URLSessionDataTaskProtocol? {
let router = UserRouter.readUser(name, self.configuration)
return router.load(session, expectedResultType: User.self) { user, error in
return router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: User.self) { user, error in
if let error = error {
completion(Response.failure(error))
} else {
Expand All @@ -119,7 +119,7 @@ public extension Octokit {
@discardableResult
func me(_ session: RequestKitURLSession = URLSession.shared, completion: @escaping (_ response: Response<User>) -> Void) -> URLSessionDataTaskProtocol? {
let router = UserRouter.readAuthenticatedUser(self.configuration)
return router.load(session, expectedResultType: User.self) { user, error in
return router.load(session, dateDecodingStrategy: .formatted(Time.rfc3339DateFormatter), expectedResultType: User.self) { user, error in
if let error = error {
completion(Response.failure(error))
} else {
Expand Down
41 changes: 16 additions & 25 deletions Tests/OctoKitTests/TestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,20 @@ import OctoKit

internal class Helper {
internal class func stringFromFile(_ name: String) -> String? {
let path = jsonFilePath(resourceName: name)

let string = try? String(contentsOfFile: path, encoding: String.Encoding.utf8)
return string
let path = jsonFilePath(for: name)
return try! String(contentsOfFile: path, encoding: String.Encoding.utf8)
}

internal class func JSONFromFile(_ name: String) -> Any {
let path = jsonFilePath(resourceName: name)
let path = jsonFilePath(for: name)
let data = try! Data(contentsOf: URL(fileURLWithPath: path))
let dict: Any? = try? JSONSerialization.jsonObject(with: data,
options: JSONSerialization.ReadingOptions.mutableContainers)
return dict!
}

internal class func codableFromFile<T>(_ name: String, type: T.Type) -> T where T: Codable {
var path: String
let bundlePath = jsonFilePath(resourceName: name)
if FileManager.default.fileExists(atPath: bundlePath) {
path = bundlePath
}
else {
path = jsonFixturesFilePath(resourceName: name)
}
let path = jsonFilePath(for: name)
let data = try! Data(contentsOf: URL(fileURLWithPath: path))
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(Time.rfc3339DateFormatter)
Expand All @@ -39,18 +30,18 @@ internal class Helper {
]
}

private class func jsonFilePath(resourceName: String) -> String {
return URL(fileURLWithPath: #file)
private class func jsonFilePath(for resourceName: String) -> String {
let baseURL = URL(fileURLWithPath: #file)
.deletingLastPathComponent()
.appendingPathComponent("\(resourceName).json")
.path
}

private class func jsonFixturesFilePath(resourceName: String) -> String {
return URL(fileURLWithPath: #file)
.deletingLastPathComponent()
.appendingPathComponent("Fixtures")
.appendingPathComponent("\(resourceName).json")
.path
let bundlePath = baseURL.appendingPathComponent("\(resourceName).json").path

if FileManager.default.fileExists(atPath: bundlePath) {
return bundlePath
} else {
return baseURL
.appendingPathComponent("Fixtures")
.appendingPathComponent("\(resourceName).json")
.path
}
}
}
3 changes: 2 additions & 1 deletion Tests/OctoKitTests/UserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ class UserTests: XCTestCase {
switch response {
case .success(let user):
XCTAssertEqual(user.login, username)
XCTAssertNotNil(user.createdAt)
case .failure:
XCTAssert(false, "should not get an user")
XCTAssert(false, "should get a user")
}
}
XCTAssertNotNil(task)
Expand Down

0 comments on commit 9521cdf

Please sign in to comment.