Skip to content

Commit

Permalink
Add tests to uphold client
Browse files Browse the repository at this point in the history
  • Loading branch information
diogoguimaraes committed Nov 6, 2015
1 parent 0650c86 commit 7810f7a
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 47 deletions.
93 changes: 93 additions & 0 deletions Tests/Integration/Client/UpholdClientTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import XCTest
import ObjectMapper
import PromiseKit
@testable import UpholdSdk
@testable import SwiftClient

/// UpholdClient integration tests.
class UpholdClientTest: UpholdTestCase {

func testGetTickersShouldReturnTheArrayOfTickers() {
let expectation = expectationWithDescription("Uphold client test.")

let json: String = "[" +
"{" +
"\"ask\": \"foo\"," +
"\"bid\": \"bar\"," +
"\"currency\": \"foobar\"," +
"\"pair\": \"foobiz\"" +
"}, {" +
"\"ask\": \"fiz\"," +
"\"bid\": \"biz\"," +
"\"currency\": \"foobiz\"," +
"\"pair\": \"bar\"" +
"}, {" +
"\"ask\": \"foobar\"," +
"\"bid\": \"foobaz\"," +
"\"currency\": \"bar\"," +
"\"pair\": \"foo\"" +
"}" +
"]"

let client = UpholdClient()
client.token.adapter = MockRestAdapter(body: json)

client.getTickers().then { (rates: [Rate]) -> () in
XCTAssertEqual(rates.count, 3, "Failed: Wrong response object size.")
XCTAssertEqual(rates[0].ask, "foo", "Failed: Wrong response object attribute.")
XCTAssertEqual(rates[0].bid, "bar", "Failed: Wrong response object attribute.")
XCTAssertEqual(rates[0].currency, "foobar", "Failed: Wrong response object attribute.")
XCTAssertEqual(rates[0].pair, "foobiz", "Failed: Wrong response object attribute.")
XCTAssertEqual(rates[1].ask, "fiz", "Failed: Wrong response object attribute.")
XCTAssertEqual(rates[1].bid, "biz", "Failed: Wrong response object attribute.")
XCTAssertEqual(rates[1].currency, "foobiz", "Failed: Wrong response object attribute.")
XCTAssertEqual(rates[1].pair, "bar", "Failed: Wrong response object attribute.")
XCTAssertEqual(rates[2].ask, "foobar", "Failed: Wrong response object attribute.")
XCTAssertEqual(rates[2].bid, "foobaz", "Failed: Wrong response object attribute.")
XCTAssertEqual(rates[2].currency, "bar", "Failed: Wrong response object attribute.")
XCTAssertEqual(rates[2].pair, "foo", "Failed: Wrong response object attribute.")

expectation.fulfill()
}

wait()
}

func testGetTickersByCurrencyShouldReturnTheArrayOfTickers() {
let expectation = expectationWithDescription("Uphold client test.")

let json: String = "[" +
"{" +
"\"ask\": \"foo\"," +
"\"bid\": \"bar\"," +
"\"currency\": \"foobar\"," +
"\"pair\": \"foobiz\"" +
"}, {" +
"\"ask\": \"fiz\"," +
"\"bid\": \"biz\"," +
"\"currency\": \"foobiz\"," +
"\"pair\": \"bar\"" +
"}, {" +
"\"ask\": \"foobar\"," +
"\"bid\": \"foobaz\"," +
"\"currency\": \"bar\"," +
"\"pair\": \"foo\"" +
"}" +
"]"

let client = UpholdClient()
client.token.adapter = MockRestAdapter(body: json)

client.getTickersByCurrency("USD").then { (rates: [Rate]) -> () in
XCTAssertEqual(rates.count, 3, "Failed: Wrong response object size.")
XCTAssertEqual(rates[0].ask, "foo", "Failed: Wrong response object attribute.")
XCTAssertEqual(rates[1].ask, "fiz", "Failed: Wrong response object attribute.")
XCTAssertEqual(rates[2].ask, "foobar", "Failed: Wrong response object attribute.")

expectation.fulfill()
}

wait()
}

}
62 changes: 30 additions & 32 deletions Tests/Integration/Client/UpholdRestAdapterTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import UpholdSdk
/// UpholdRestAdapter integration tests.
class UpholdRestAdapterTest: UpholdTestCase {

var expectation: XCTestExpectation!

func testBuildRequestShouldReturnRequest() {
let mockRequest = MockRequest(body: nil, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
let request = UpholdRestAdapter().buildRequest(mockRequest)
Expand All @@ -19,7 +17,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
}

func testBuildEmptyResponseShouldReturnBadRequestError() {
self.expectation = expectationWithDescription("REST adapter response.")
let expectation = expectationWithDescription("Uphold REST adapter response test.")

let mockRequest = MockRequest(body: nil, code: 400, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
let request = UpholdRestAdapter().buildRequest(mockRequest)
Expand All @@ -36,7 +34,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
XCTAssertEqual(badRequestError.code, 400, "Failed: Wrong response HTTP status code.")
XCTAssertEqual(badRequestError.info["Bad request error"], "HTTP error 400 - Bad request.", "Failed: Wrong message.")

self.expectation.fulfill()
expectation.fulfill()

return promise
}
Expand All @@ -45,7 +43,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
}

func testBuildEmptyResponseShouldReturnFulfilledPromise() {
self.expectation = expectationWithDescription("REST adapter response.")
let expectation = expectationWithDescription("Uphold REST adapter response test.")

let mockRequest = MockRequest(body: nil, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
let request = UpholdRestAdapter().buildRequest(mockRequest)
Expand All @@ -55,14 +53,14 @@ class UpholdRestAdapterTest: UpholdTestCase {
XCTAssertEqual(response.basicStatus, Response.BasicResponseType.OK, "Failed: Wrong response basic status code.")
XCTAssertNil(response.text, "Failed: Wrong response body.")

self.expectation.fulfill()
expectation.fulfill()
}

wait()
}

func testBuildEmptyResponseShouldReturnLogicError() {
self.expectation = expectationWithDescription("REST adapter response.")
let expectation = expectationWithDescription("Uphold REST adapter response test.")

let mockRequest = MockRequest(body: "foobar", code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
let request = UpholdRestAdapter().buildRequest(mockRequest)
Expand All @@ -79,7 +77,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
XCTAssertEqual(logicError.info, ["Logic error": "Response body should be empty."], "Failed: Wrong error message.")
XCTAssertNil(logicError.code, "Failed: Wrong response HTTP status code.")

self.expectation.fulfill()
expectation.fulfill()

return promise
}
Expand All @@ -88,15 +86,15 @@ class UpholdRestAdapterTest: UpholdTestCase {
}

func testBuildResponseShouldReturnBadRequestError() {
self.expectation = expectationWithDescription("REST adapter response.")
let expectation = expectationWithDescription("Uphold REST adapter response test.")

let jsonRate = "{" +
let json = "{" +
"\"ask\":\"1.2\"," +
"\"bid\":\"1\"," +
"\"currency\":\"BTC\"," +
"\"pair\":\"BTCBTC\"" +
"}"
let mockRequest = MockRequest(body: jsonRate, code: 400, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
let mockRequest = MockRequest(body: json, code: 400, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
let request = UpholdRestAdapter().buildRequest(mockRequest)
let promise: Promise<Rate> = UpholdRestAdapter().buildResponse(request)

Expand All @@ -111,7 +109,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
XCTAssertEqual(badRequestError.code, 400, "Failed: Wrong response HTTP status code.")
XCTAssertEqual(badRequestError.info["Bad request error"], "HTTP error 400 - Bad request.", "Failed: Wrong message.")

self.expectation.fulfill()
expectation.fulfill()

return promise
}
Expand All @@ -120,7 +118,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
}

func testBuildResponseShouldReturnEmptyBodyLogicError() {
self.expectation = expectationWithDescription("REST adapter response.")
let expectation = expectationWithDescription("Uphold REST adapter response test.")

let mockRequest = MockRequest(body: nil, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
let request = UpholdRestAdapter().buildRequest(mockRequest)
Expand All @@ -137,7 +135,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
XCTAssertEqual(logicError.info, ["Logic error": "Response body should not be empty."], "Failed: Wrong error message.")
XCTAssertNil(logicError.code, "Failed: Wrong response HTTP status code.")

self.expectation.fulfill()
expectation.fulfill()

return promise
}
Expand All @@ -146,15 +144,15 @@ class UpholdRestAdapterTest: UpholdTestCase {
}

func testBuildResponseShouldReturnFailedMapLogicError() {
self.expectation = expectationWithDescription("REST adapter response.")
let expectation = expectationWithDescription("Uphold REST adapter response test.")

let jsonRate = "{[" +
let json = "{[" +
"\"ask\":\"1.2\"," +
"\"bid\":\"1\"," +
"\"currency\":\"BTC\"," +
"\"pair\":\"BTCBTC\"" +
"}"
let mockRequest = MockRequest(body: jsonRate, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
let mockRequest = MockRequest(body: json, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
let request = UpholdRestAdapter().buildRequest(mockRequest)
let promise: Promise<Rate> = UpholdRestAdapter().buildResponse(request)

Expand All @@ -169,7 +167,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
XCTAssertEqual(logicError.info, ["Logic error": "Failed to map the JSON object."], "Failed: Wrong error message.")
XCTAssertNil(logicError.code, "Failed: Wrong response HTTP status code.")

self.expectation.fulfill()
expectation.fulfill()

return promise
}
Expand All @@ -178,15 +176,15 @@ class UpholdRestAdapterTest: UpholdTestCase {
}

func testBuildResponseShouldReturnFulfilledPromise() {
self.expectation = expectationWithDescription("REST adapter response.")
let expectation = expectationWithDescription("Uphold REST adapter response test.")

let jsonRate = "{" +
let json = "{" +
"\"ask\":\"1.2\"," +
"\"bid\":\"1\"," +
"\"currency\":\"BTC\"," +
"\"pair\":\"BTCBTC\"" +
"}"
let mockRequest = MockRequest(body: jsonRate, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
let mockRequest = MockRequest(body: json, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
let request = UpholdRestAdapter().buildRequest(mockRequest)
let promise: Promise<Rate> = UpholdRestAdapter().buildResponse(request)

Expand All @@ -196,17 +194,17 @@ class UpholdRestAdapterTest: UpholdTestCase {
XCTAssertEqual(rate.currency, "BTC", "Failed: Wrong response object attribute.")
XCTAssertEqual(rate.pair, "BTCBTC", "Failed: Wrong response object attribute.")

self.expectation.fulfill()
expectation.fulfill()
}

wait()
}

func testBuildResponseArrayShouldReturnBadRequestError() {
self.expectation = expectationWithDescription("REST adapter response.")
let expectation = expectationWithDescription("Uphold REST adapter response test.")

let jsonRates = "[{\"ask\":\"1\"}, {\"ask\":\"440.99\"}]"
let mockRequest = MockRequest(body: jsonRates, code: 400, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
let json = "[{\"ask\":\"1\"}, {\"ask\":\"440.99\"}]"
let mockRequest = MockRequest(body: json, code: 400, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
let request = UpholdRestAdapter().buildRequest(mockRequest)
let promise: Promise<[Rate]> = UpholdRestAdapter().buildResponse(request)

Expand All @@ -221,7 +219,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
XCTAssertEqual(badRequestError.code, 400, "Failed: Wrong response HTTP status code.")
XCTAssertEqual(badRequestError.info["Bad request error"], "HTTP error 400 - Bad request.", "Failed: Wrong message.")

self.expectation.fulfill()
expectation.fulfill()

return promise
}
Expand All @@ -230,7 +228,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
}

func testBuildResponseArrayShouldReturnEmptyBodyLogicError() {
self.expectation = expectationWithDescription("REST adapter response.")
let expectation = expectationWithDescription("Uphold REST adapter response test.")

let mockRequest = MockRequest(body: nil, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
let request = UpholdRestAdapter().buildRequest(mockRequest)
Expand All @@ -247,7 +245,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
XCTAssertEqual(logicError.info, ["Logic error": "Response body should not be empty."], "Failed: Wrong error message.")
XCTAssertNil(logicError.code, "Failed: Wrong response HTTP status code.")

self.expectation.fulfill()
expectation.fulfill()

return promise
}
Expand All @@ -256,7 +254,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
}

func testBuildResponseArrayShouldReturnFailedMapLogicError() {
self.expectation = expectationWithDescription("REST adapter response.")
let expectation = expectationWithDescription("Uphold REST adapter response test.")

let jsonRates = "[[{\"ask\":\"1\"}, {\"ask\":\"440.99\"}]"
let mockRequest = MockRequest(body: jsonRates, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
Expand All @@ -274,7 +272,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
XCTAssertEqual(logicError.info, ["Logic error": "Failed to map the JSON object."], "Failed: Wrong error message.")
XCTAssertNil(logicError.code, "Failed: Wrong response HTTP status code.")

self.expectation.fulfill()
expectation.fulfill()

return promise
}
Expand All @@ -283,7 +281,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
}

func testBuildResponseArrayShouldReturnFulfilledPromise() {
self.expectation = expectationWithDescription("REST adapter response.")
let expectation = expectationWithDescription("Uphold REST adapter response test.")

let jsonRates = "[{\"ask\":\"1\"}, {\"ask\":\"440.99\"}]"
let mockRequest = MockRequest(body: jsonRates, code: 200, errorHandler: {(error: NSError) -> Void in}, headers: nil, method: "foo")
Expand All @@ -295,7 +293,7 @@ class UpholdRestAdapterTest: UpholdTestCase {
XCTAssertEqual(rates[0].ask, "1", "Failed: Wrong response object attribute.")
XCTAssertEqual(rates[1].ask, "440.99", "Failed: Wrong response object attribute.")

self.expectation.fulfill()
expectation.fulfill()
}

wait()
Expand Down
24 changes: 9 additions & 15 deletions Tests/Util/MockRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,22 @@ public class MockRequest: Request {
return .Success(response, body.dataUsingEncoding(NSUTF8StringEncoding))
}

/// Mocked SwiftClient Request class end method.
/**
Mock SwiftClient Request class end method.

- parameter done: The completion handler.
- parameter errorHandler: The error handler.
*/
public override func end(done: (SwiftClient.Response) -> Void, onError errorHandler: ((NSError) -> Void)? = nil) {
let request = NSMutableURLRequest(URL: NSURL(string: self.mockURL)!)
request.HTTPMethod = super.method
let response = builder(request)
let stubError = NSError(domain: "Stub error", code: -1, userInfo: ["Stub error": "Could not create stub."])

switch response {
switch builder(request) {
case let .Success(response, data):
guard let mockResponse = response as? NSHTTPURLResponse else {
errorHandler!(stubError)

return
}
return done(self.transformer(SwiftClient.Response((response as? NSHTTPURLResponse)!, self, data)))

done(self.transformer(SwiftClient.Response(mockResponse, self, data)))

break
default:
errorHandler!(stubError)

break
return
}
}

Expand Down
Loading

0 comments on commit 7810f7a

Please sign in to comment.