[0.1.4] String path tests

This commit is contained in:
Shial 2018-02-01 10:51:13 +11:00
parent 95c6c785eb
commit 0b6329e66d
6 changed files with 61 additions and 5 deletions

View File

@ -24,6 +24,7 @@
623922782002D8C30018982A /* LazeConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 623922762002D8C30018982A /* LazeConfiguration.swift */; };
6239227E2002DF1A0018982A /* Laze.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6239227D2002DF1A0018982A /* Laze.swift */; };
625B23AD1FF3462B001976C3 /* Object.swift in Sources */ = {isa = PBXBuildFile; fileRef = 625B23AB1FF34625001976C3 /* Object.swift */; };
625B5F8F202288E700E72BCF /* StringPathTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 625B5F8E202288E700E72BCF /* StringPathTests.swift */; };
OBJ_32 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_6 /* Package.swift */; };
OBJ_38 /* Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_21 /* Model.swift */; };
OBJ_39 /* SLazeKitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_22 /* SLazeKitTests.swift */; };
@ -59,6 +60,7 @@
623922762002D8C30018982A /* LazeConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LazeConfiguration.swift; sourceTree = "<group>"; };
6239227D2002DF1A0018982A /* Laze.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Laze.swift; sourceTree = "<group>"; };
625B23AB1FF34625001976C3 /* Object.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Object.swift; sourceTree = "<group>"; };
625B5F8E202288E700E72BCF /* StringPathTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringPathTests.swift; sourceTree = "<group>"; };
OBJ_10 /* Decodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Decodable.swift; sourceTree = "<group>"; };
OBJ_11 /* NSManagedObject.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSManagedObject.swift; sourceTree = "<group>"; };
OBJ_12 /* NSManagedObjectContext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSManagedObjectContext.swift; sourceTree = "<group>"; };
@ -117,6 +119,7 @@
OBJ_20 /* Models */,
OBJ_22 /* SLazeKitTests.swift */,
OBJ_23 /* StringInitializableTests.swift */,
625B5F8E202288E700E72BCF /* StringPathTests.swift */,
);
name = SLazeKitTests;
path = Tests/SLazeKitTests;
@ -276,6 +279,7 @@
OBJ_39 /* SLazeKitTests.swift in Sources */,
625B23AD1FF3462B001976C3 /* Object.swift in Sources */,
OBJ_40 /* StringInitializableTests.swift in Sources */,
625B5F8F202288E700E72BCF /* StringPathTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -13,7 +13,7 @@ extension KeyedDecodingContainerProtocol {
/// - type: Type to decode
/// - key: JSON key under given type should be stored
/// - Returns: Desired type value
public func decodeWith<T: Decodable & StringInitializable>(_ type: T.Type = T.self, forKey key: Key) throws -> T? {
public func decodeUnstable<T: Decodable & StringInitializable>(_ type: T.Type = T.self, forKey key: Key) throws -> T? {
guard contains(key) else { return nil }
guard !(try decodeNil(forKey: key)) else { return nil }
@ -35,7 +35,7 @@ extension KeyedDecodingContainerProtocol {
/// - type: Type to decode
/// - key: JSON key under given type should be stored
/// - Returns: Desired type value
public func decodeWith<T: Decodable & StringInitializable>(_ type: T.Type = T.self, forKey key: Key) throws -> T {
public func decodeUnstable<T: Decodable & StringInitializable>(_ type: T.Type = T.self, forKey key: Key) throws -> T {
if let string = try? decode(String.self, forKey: key) {
guard let value = T.init(rawValue: string) else {
throw DecodingError.dataCorruptedError(forKey: key,

View File

@ -63,7 +63,7 @@ class Model: NSManagedObject {
/// SLazeKit request are done i background. To handle response on main thread we need to dispatch it.
let _ = SLazeKit<Default>.post(path: PathPattern.create.patternToPath(with: ["modelId":model.id]), body: ModelRequest(with: model)) { (response, error) in
guard error == nil else {
DispatchQueue.main.async { failure?(response.urlResponse?.statusCode ?? -1,error) }
DispatchQueue.main.async { failure?(response.http?.statusCode ?? -1,error) }
return
}
DispatchQueue.main.async { success() }
@ -78,7 +78,7 @@ class Model: NSManagedObject {
class func remove(for modelId: String, success: @escaping (() ->()), failure: ((_ statusCode: Int, _ error: Error?) ->())? = nil) {
let _ = SLazeKit<Default>.delete(path: PathPattern.delete.patternToPath(with: ["modelId":modelId])) { (response, error) in
guard error == nil else {
DispatchQueue.main.async { failure?(response.urlResponse?.statusCode ?? -1,error) }
DispatchQueue.main.async { failure?(response.http?.statusCode ?? -1,error) }
return
}
DispatchQueue.main.async { success() }

View File

@ -1,3 +1,4 @@
import Foundation
import XCTest
@testable import SLazeKit
@ -25,6 +26,7 @@ class SLazeKitTests: XCTestCase {
static var allTests = [
("testBasePath", testBasePath),
("testBasePort", testBasePort),
("testRequestSetup", testRequestSetup),
]
func testBasePath() {
@ -34,4 +36,10 @@ class SLazeKitTests: XCTestCase {
func testBasePort() {
XCTAssertTrue(Configuration.basePort == 8765)
}
func testRequestSetup() {
let request = Configuration.setup(URLRequest(url: URL(string: "www.yourdomain.com")!))
XCTAssertTrue(request.allHTTPHeaderFields?["X-Access-Token"] == "Your token")
XCTAssertTrue(request.allHTTPHeaderFields?["Content-Type"] == "application/json")
}
}

View File

@ -1,3 +1,4 @@
import Foundation
import XCTest
@testable import SLazeKit
@ -60,7 +61,6 @@ class StringInitializableTests: XCTestCase {
}
""".data(using: .utf8)!
do {
let decoder = JSONDecoder()
let test = try decoder.decode(Model.self, from: json)

View File

@ -0,0 +1,44 @@
import Foundation
import XCTest
@testable import SLazeKit
class StringPathTests: XCTestCase {
static var allTests = [
("testPath", testPath),
("testPathTwoElements", testPathTwoElements),
("testPathThreeElements", testPathThreeElements),
("testPathFourElements", testPathFourElements),
]
func testPath() {
let path: String = "www.yourdomain.com/:object/59BA56C1-4AC4-4FE1-BA7D-366806F50A06"
XCTAssertTrue(path.patternToPath(with: ["object":"MyModel"]) == "www.yourdomain.com/MyModel/59BA56C1-4AC4-4FE1-BA7D-366806F50A06")
}
func testPathTwoElements() {
let path: String = "www.yourdomain.com/:object/59BA56C1-4AC4-4FE1-BA7D-366806F50A06/:date"
XCTAssertTrue(path.patternToPath(with: [
"object":"MyModel",
"date":"2018-02-01 10:10:04"
]) == "www.yourdomain.com/MyModel/59BA56C1-4AC4-4FE1-BA7D-366806F50A06/2018-02-01 10:10:04")
}
func testPathThreeElements() {
let path: String = "www.yourdomain.com/type/:object/:type/object/59BA56C1-4AC4-4FE1-BA7D-366806F50A06/:date"
XCTAssertTrue(path.patternToPath(with: [
"object":"MyModel",
"date":"2018-02-01 10:10:04",
"type":"example"
]) == "www.yourdomain.com/type/MyModel/example/object/59BA56C1-4AC4-4FE1-BA7D-366806F50A06/2018-02-01 10:10:04")
}
func testPathFourElements() {
let path: String = "www.yourdomain.com/type/:object/:type/object/59BA56C1-4AC4-4FE1-BA7D-366806F50A06/id/id:/:id/:date"
XCTAssertTrue(path.patternToPath(with: [
"object":"MyModel",
"date":"2018-02-01 10:10:04",
"type":"example",
"id":"12",
]) == "www.yourdomain.com/type/MyModel/example/object/59BA56C1-4AC4-4FE1-BA7D-366806F50A06/id/id:/12/2018-02-01 10:10:04")
}
}