Skip to content

Commit

Permalink
Add tests to the reserve model
Browse files Browse the repository at this point in the history
  • Loading branch information
diogoguimaraes committed Nov 10, 2015
1 parent b97b6b6 commit 0d820a4
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 5 deletions.
151 changes: 151 additions & 0 deletions Tests/Integration/Model/ReserveTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import XCTest
import ObjectMapper
import PromiseKit
@testable import UpholdSdk
@testable import SwiftClient

/// Reserve integration tests.
class ReserveTest: UpholdTestCase {

func testGetLedgerShouldReturnTheArrayWithDeposits() {
let expectation = expectationWithDescription("Reserve test.")
let json: String = "[{" +
"\"type\": \"foo\"," +
"\"out\": {" +
"\"amount\": \"foobar\"," +
"\"currency\": \"fuz\"" +
"}," +
"\"in\": {" +
"\"amount\": \"foobiz\"," +
"\"currency\": \"fiz\"" +
"}," +
"\"createdAt\": \"2015-04-20T14:57:12.398Z\"" +
"},{" +
"\"type\": \"bar\"," +
"\"out\": {" +
"\"amount\": \"foobiz\"," +
"\"currency\": \"buz\"" +
"}," +
"\"in\": {" +
"\"amount\": \"foobar\"," +
"\"currency\": \"biz\"" +
"}," +
"\"TransactionId\": \"foobar\"," +
"\"createdAt\": \"2015-04-21T14:57:12.398Z\"" +
"}]";
let reserve = UpholdClient().getReserve()
reserve.adapter = MockRestAdapter(body: json)

reserve.getLedger(0, end: 5).then { (deposits: [Deposit]) -> () in
XCTAssertEqual(deposits[0].createdAt, "2015-04-20T14:57:12.398Z", "Failed: DepositMovement in currency didn't match.")
XCTAssertEqual(deposits[0].input?.amount, "foobiz", "Failed: DepositMovement in amount didn't match.")
XCTAssertEqual(deposits[0].input?.currency, "fiz", "Failed: DepositMovement in currency didn't match.")
XCTAssertEqual(deposits[0].output?.amount, "foobar", "Failed: DepositMovement out amount didn't match.")
XCTAssertEqual(deposits[0].output?.currency, "fuz", "Failed: DepositMovement out currency didn't match.")
XCTAssertEqual(deposits[0].type, "foo", "Failed: Deposit type didn't match.")
XCTAssertEqual(deposits[1].createdAt, "2015-04-21T14:57:12.398Z", "Failed: CreatedAt didn't match.")
XCTAssertEqual(deposits[1].input?.amount, "foobar", "Failed: DepositMovement in amount didn't match.")
XCTAssertEqual(deposits[1].input?.currency, "biz", "Failed: DepositMovement in currency didn't match.")
XCTAssertEqual(deposits[1].output?.amount, "foobiz", "Failed: DepositMovement out amount didn't match.")
XCTAssertEqual(deposits[1].output?.currency, "buz", "Failed: DepositMovement out currency didn't match.")
XCTAssertEqual(deposits[1].transactionId, "foobar", "Failed: TransactionId didn't match.")
XCTAssertEqual(deposits[1].type, "bar", "Failed: Deposit type didn't match.")

expectation.fulfill()
}

wait()
}

func testGetStatisticsShouldReturnTheListWithReserveStatistics() {
let expectation = expectationWithDescription("Reserve test.")
let json: String = "[{" +
"\"currency\": \"FOO\"," +
"\"values\": [{" +
"\"assets\": \"foobar\"," +
"\"currency\": \"foo\"," +
"\"liabilities\": \"bar\"," +
"\"rate\": \"biz\"" +
"}]," +
"\"totals\": {" +
"\"commissions\": \"foo\"," +
"\"transactions\": \"bar\"," +
"\"assets\": \"foobar\"," +
"\"liabilities\": \"foobiz\"" +
"}" +
"}, {" +
"\"currency\": \"BAR\"," +
"\"values\": [{" +
"\"assets\": \"foobiz\"," +
"\"currency\": \"biz\"," +
"\"liabilities\": \"buz\"," +
"\"rate\": \"foo\"" +
"}]," +
"\"totals\": {" +
"\"commissions\": \"fiz\"," +
"\"transactions\": \"biz\"," +
"\"assets\": \"fuz\"," +
"\"liabilities\": \"buz\"" +
"}" +
"}]"
let reserve = UpholdClient().getReserve()
reserve.adapter = MockRestAdapter(body: json)

reserve.getStatistics().then { (statistics: [ReserveStatistics]) -> () in
XCTAssertEqual(statistics[0].currency, "FOO", "Failed: Currency didn't match.")
XCTAssertEqual(statistics[0].totals?.assets, "foobar", "Failed: Totals assets didn't match.")
XCTAssertEqual(statistics[0].totals?.commissions, "foo", "Failed: Totals comissions didn't match.")
XCTAssertEqual(statistics[0].totals?.transactions, "bar", "Failed: Totals transactions didn't match.")
XCTAssertEqual(statistics[0].totals?.liabilities, "foobiz", "Failed: Totals liabilities didn't match.")
XCTAssertEqual(statistics[0].values?.first?.assets, "foobar", "Failed: Values assests didn't match.")
XCTAssertEqual(statistics[0].values?.first?.currency, "foo", "Failed: Values currency didn't match.")
XCTAssertEqual(statistics[0].values?.first?.liabilities, "bar", "Failed: Values liabilities didn't match.")
XCTAssertEqual(statistics[0].values?.first?.rate, "biz", "Failed: Values rate didn't match.")
XCTAssertEqual(statistics[1].currency, "BAR", "Failed: Currency didn't match.")
XCTAssertEqual(statistics[1].totals?.assets, "fuz", "Failed: Totals assets didn't match.")
XCTAssertEqual(statistics[1].totals?.commissions, "fiz", "Failed: Totals comissions didn't match.")
XCTAssertEqual(statistics[1].totals?.transactions, "biz", "Failed: Totals transactions didn't match.")
XCTAssertEqual(statistics[1].totals?.liabilities, "buz", "Failed: Totals liabilities didn't match.")
XCTAssertEqual(statistics[1].values?.first?.assets, "foobiz", "Failed: Values assests didn't match.")
XCTAssertEqual(statistics[1].values?.first?.currency, "biz", "Failed: Values currency didn't match.")
XCTAssertEqual(statistics[1].values?.first?.liabilities, "buz", "Failed: Values liabilities didn't match.")
XCTAssertEqual(statistics[1].values?.first?.rate, "foo", "Failed: Values rate didn't match.")

expectation.fulfill()
}

wait()
}

func testGetTransactionsByIdShouldReturnTheTransaction() {
let expectation = expectationWithDescription("Reserve test.")
let json: String = "{ \"id\": \"foobar\" }"
let reserve = UpholdClient().getReserve()
reserve.adapter = MockRestAdapter(body: json)

reserve.getTransactionById("foobar").then { (transaction: Transaction) -> () in
XCTAssertEqual(transaction.id, "foobar", "Failed: TransactionId didn't match.")

expectation.fulfill()
}

wait()
}

func testGetTransactionsShouldReturnTheListOfTransactions() {
let expectation = expectationWithDescription("Reserve test.")
let json: String = "[{ \"id\": \"foobar\" }, { \"id\": \"foobiz\" }]"
let reserve = UpholdClient().getReserve()
reserve.adapter = MockRestAdapter(body: json)

reserve.getTransactions(0, end: 5).then { (transaction: [Transaction]) -> () in
XCTAssertEqual(transaction.first?.id, "foobar", "Failed: TransactionId didn't match.")
XCTAssertEqual(transaction.last?.id, "foobiz", "Failed: TransactionId didn't match.")

expectation.fulfill()
}

wait()
}

}
15 changes: 10 additions & 5 deletions Tests/Integration/Util/HeaderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ import UpholdSdk
/// Header integration tests.
class HeaderTest: XCTestCase {

func testBuildRangeHeaderShouldReturnRange() {
XCTAssertEqual(Header.buildRangeHeader(0, end: 5), "items=0-5", "Failed: Wrong value.")
}

func testGetDefaultHeadersShouldReturnHeaders() {
let headers: [String: String] = Header.getDefaultHeaders()

XCTAssertEqual(headers["User-Agent"], String(format: "uphold-ios-sdk/%@ (%@)", GlobalConfigurations.UPHOLD_SDK_VERSION, GlobalConfigurations.SDK_GITHUB_URL), "Failed: Wrong header")
XCTAssertEqual(headers["User-Agent"], String(format: "uphold-ios-sdk/%@ (%@)", GlobalConfigurations.UPHOLD_SDK_VERSION, GlobalConfigurations.SDK_GITHUB_URL), "Failed: Wrong header.")
}

func testGetRateLimitValueShouldReturnRateLimit() {
XCTAssertEqual(Header.getRateLimitValue(["x-ratelimit-limit": "300"]), "300", "Failed: Wrong value")
XCTAssertNotEqual(Header.getRateLimitValue(["foobar": "300"]), "300", "Failed: Wrong value")
XCTAssertEqual(Header.getRateLimitValue(["x-ratelimit-limit": "300"]), "300", "Failed: Wrong value.")
XCTAssertNotEqual(Header.getRateLimitValue(["foobar": "300"]), "300", "Failed: Wrong value.")
}

func testGetSecondsUntilRateLimitResetShouldReturnResetTime() {
XCTAssertEqual(Header.getSecondsUntilRateLimitReset(["retry-after": "10"]), "10", "Failed: Wrong value")
XCTAssertNotEqual(Header.getSecondsUntilRateLimitReset(["foobar": "10"]), "10", "Failed: Wrong value")
XCTAssertEqual(Header.getSecondsUntilRateLimitReset(["retry-after": "10"]), "10", "Failed: Wrong value.")
XCTAssertNotEqual(Header.getSecondsUntilRateLimitReset(["foobar": "10"]), "10", "Failed: Wrong value.")
}

}
4 changes: 4 additions & 0 deletions UpholdSdk.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
BC91E8A21BECD8270074C538 /* ReserveStatistics.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC91E89D1BECD8270074C538 /* ReserveStatistics.swift */; };
BC91E8A31BECD8270074C538 /* Total.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC91E89E1BECD8270074C538 /* Total.swift */; };
BC91E8A41BECD8270074C538 /* Value.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC91E89F1BECD8270074C538 /* Value.swift */; };
BC91E8A61BECDB030074C538 /* ReserveTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = BC91E8A51BECDB030074C538 /* ReserveTest.swift */; };
BCB089911BD52DC20097C475 /* UpholdRestAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCB089901BD52DC20097C475 /* UpholdRestAdapter.swift */; };
BCB089B81BD52F270097C475 /* PromiseKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BCB089A71BD52EEC0097C475 /* PromiseKit.framework */; };
BCC1EB2F1BE3939800F37B7D /* UpholdClientErrorHandlingTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCC1EB2E1BE3939800F37B7D /* UpholdClientErrorHandlingTest.swift */; };
Expand Down Expand Up @@ -271,6 +272,7 @@
BC91E89D1BECD8270074C538 /* ReserveStatistics.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ReserveStatistics.swift; path = Reserve/ReserveStatistics.swift; sourceTree = "<group>"; };
BC91E89E1BECD8270074C538 /* Total.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Total.swift; path = Reserve/Total.swift; sourceTree = "<group>"; };
BC91E89F1BECD8270074C538 /* Value.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Value.swift; path = Reserve/Value.swift; sourceTree = "<group>"; };
BC91E8A51BECDB030074C538 /* ReserveTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReserveTest.swift; sourceTree = "<group>"; };
BCB089901BD52DC20097C475 /* UpholdRestAdapter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UpholdRestAdapter.swift; sourceTree = "<group>"; };
BCB0899A1BD52EEC0097C475 /* PromiseKit.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = PromiseKit.xcodeproj; path = Carthage/Checkouts/PromiseKit/PromiseKit.xcodeproj; sourceTree = "<group>"; };
BCC1EB2E1BE3939800F37B7D /* UpholdClientErrorHandlingTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UpholdClientErrorHandlingTest.swift; path = ErrorHandling/UpholdClientErrorHandlingTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -437,6 +439,7 @@
children = (
BC23817B1BB4627C0060CC80 /* CardTest.swift */,
BC23817C1BB4627C0060CC80 /* RateTest.swift */,
BC91E8A51BECDB030074C538 /* ReserveTest.swift */,
BC23817D1BB4627C0060CC80 /* TransactionTest.swift */,
BC23817E1BB4627C0060CC80 /* UserTest.swift */,
);
Expand Down Expand Up @@ -928,6 +931,7 @@
BC38DA001BB97E4F0044239A /* TickerServiceTest.swift in Sources */,
BC2381821BB4627C0060CC80 /* TransactionTest.swift in Sources */,
BC2381801BB4627C0060CC80 /* CardTest.swift in Sources */,
BC91E8A61BECDB030074C538 /* ReserveTest.swift in Sources */,
BC7E73FD1BEA7BB6006AAE0E /* UpholdClientTest.swift in Sources */,
BCC9E0B01BD10F4300C5C3C8 /* HeaderTest.swift in Sources */,
BC38D9FF1BB97E4F0044239A /* ReserveServiceTest.swift in Sources */,
Expand Down

0 comments on commit 0d820a4

Please sign in to comment.