Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more attributes to Wallet #54

Merged
merged 1 commit into from May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions OmiseGOTests/CodingTests/DecodeTests.swift
Expand Up @@ -277,6 +277,14 @@ class DecodeTests: XCTestCase {
XCTAssertTrue(decodedData.balances[0].token.encryptedMetadata.isEmpty)
XCTAssertEqual(decodedData.balances[0].token.createdAt, try "2018-01-01T00:00:00Z".toDate())
XCTAssertEqual(decodedData.balances[0].token.updatedAt, try "2018-01-01T00:00:00Z".toDate())
XCTAssertEqual(decodedData.name, "primary")
XCTAssertEqual(decodedData.identifier, "primary")
XCTAssertEqual(decodedData.userId!, "cec34607-0761-4a59-8357-18963e42a1aa")
XCTAssertEqual(decodedData.user!.id, "cec34607-0761-4a59-8357-18963e42a1aa")
XCTAssertEqual(decodedData.accountId, nil)
XCTAssertEqual(decodedData.account, nil)
XCTAssertTrue(decodedData.metadata.isEmpty)
XCTAssertTrue(decodedData.encryptedMetadata.isEmpty)
} catch let thrownError {
XCTFail(thrownError.localizedDescription)
}
Expand Down
19 changes: 18 additions & 1 deletion OmiseGOTests/FixtureTests/Fixtures/fixture/me.list_wallets.json
Expand Up @@ -38,7 +38,24 @@
"updated_at": "2018-01-01T00:00:00Z"
}
}
]
],
"name": "primary",
"identifier": "primary",
"user_id": "cec34607-0761-4a59-8357-18963e42a1aa",
"user": {
"id": "cec34607-0761-4a59-8357-18963e42a1aa",
"provider_user_id": "wijf-fbancomw-dqwjudb",
"username": "john.doe@example.com",
"socket_topic": "user:cec34607-0761-4a59-8357-18963e42a1aa",
"metadata": {},
"encrypted_metadata": {},
"created_at": "2018-01-01T00:00:00Z",
"updated_at": "2018-01-01T00:00:00Z"
},
"account_id": null,
"account": null,
"metadata": {},
"encrypted_metadata": {}
}
]
}
Expand Down
19 changes: 18 additions & 1 deletion OmiseGOTests/FixtureTests/Fixtures/objects/wallet.json
Expand Up @@ -14,5 +14,22 @@
"updated_at": "2018-01-01T00:00:00Z"
}
}
]
],
"name": "primary",
"identifier": "primary",
"user_id": "cec34607-0761-4a59-8357-18963e42a1aa",
"user": {
"id": "cec34607-0761-4a59-8357-18963e42a1aa",
"provider_user_id": "wijf-fbancomw-dqwjudb",
"username": "john.doe@example.com",
"socket_topic": "user:cec34607-0761-4a59-8357-18963e42a1aa",
"metadata": {},
"encrypted_metadata": {},
"created_at": "2018-01-01T00:00:00Z",
"updated_at": "2018-01-01T00:00:00Z"
},
"account_id": null,
"account": null,
"metadata": {},
"encrypted_metadata": {}
}
10 changes: 9 additions & 1 deletion OmiseGOTests/FixtureTests/WalletFixtureTests.swift
Expand Up @@ -33,7 +33,6 @@ class WalletFixtureTests: FixtureTestCase {
XCTAssertTrue(balance1.token.encryptedMetadata.isEmpty)
XCTAssertEqual(balance1.token.createdAt, "2018-01-01T00:00:00Z".toDate())
XCTAssertEqual(balance1.token.updatedAt, "2018-01-01T00:00:00Z".toDate())

XCTAssertEqual(balance2.amount, 133700)
XCTAssertEqual(balance2.token.id, "KNC:123")
XCTAssertEqual(balance2.token.symbol, "KNC")
Expand All @@ -42,6 +41,15 @@ class WalletFixtureTests: FixtureTestCase {
XCTAssertTrue(balance2.token.encryptedMetadata.isEmpty)
XCTAssertEqual(balance2.token.createdAt, "2018-01-01T00:00:00Z".toDate())
XCTAssertEqual(balance2.token.updatedAt, "2018-01-01T00:00:00Z".toDate())

XCTAssertEqual(wallet.name, "primary")
XCTAssertEqual(wallet.identifier, "primary")
XCTAssertEqual(wallet.userId!, "cec34607-0761-4a59-8357-18963e42a1aa")
XCTAssertEqual(wallet.user!.id, "cec34607-0761-4a59-8357-18963e42a1aa")
XCTAssertEqual(wallet.accountId, nil)
XCTAssertEqual(wallet.account, nil)
XCTAssertTrue(wallet.metadata.isEmpty)
XCTAssertTrue(wallet.encryptedMetadata.isEmpty)
case .fail(error: let error):
XCTFail("\(error)")
}
Expand Down
20 changes: 18 additions & 2 deletions OmiseGOTests/Helpers/StubGenerator.swift
Expand Up @@ -26,12 +26,28 @@ class StubGenerator {
}

class func wallet(address: String? = nil,
balances: [Balance]? = nil)
balances: [Balance]? = nil,
name: String? = nil,
identifier: String? = nil,
userId: String? = nil,
user: User? = nil,
accountId: String? = nil,
account: Account? = nil,
metadata: [String: Any] = [:],
encryptedMetadata: [String: Any] = [:])
-> Wallet {
let v: Wallet = self.stub(forResource: "wallet")
return Wallet(
address: address ?? v.address,
balances: [self.balance()]
balances: [self.balance()],
name: name ?? v.name,
identifier: identifier ?? v.identifier,
userId: userId ?? v.userId,
user: user ?? v.user,
accountId: accountId ?? v.accountId,
account: account ?? v.account,
metadata: metadata,
encryptedMetadata: encryptedMetadata
)
}

Expand Down
52 changes: 50 additions & 2 deletions Source/Models/Wallet.swift
Expand Up @@ -7,12 +7,60 @@
//

/// Represents a wallet containing a list of balances
public struct Wallet: Retrievable, Decodable {
public struct Wallet: Retrievable {

/// The address of the balances
/// The address of the wallet
public let address: String
/// The list of balances associated with that address
public let balances: [Balance]
/// The name of the wallet
public let name: String
/// The identifier of the wallet
public let identifier: String
/// The id of the user associated to this wallet if it's a user wallet. Nil if it's an account wallet.
public let userId: String?
/// The user associated to this wallet if it's a user wallet. Nil if it's an account wallet.
public let user: User?
/// The id of the account associated to this wallet if it's an account wallet. Nil if it's a user wallet.
public let accountId: String?
/// The account associated to this wallet if it's an account wallet. Nil if it's a user wallet.
public let account: Account?
/// Any additional metadata that need to be stored as a dictionary
public let metadata: [String: Any]
/// Any additional encrypted metadata that need to be stored as a dictionary
public let encryptedMetadata: [String: Any]
}

extension Wallet: Decodable {

private enum CodingKeys: String, CodingKey {
case id
case address
case balances
case name
case identifier
case userId = "user_id"
case user
case accountId = "account_id"
case account
case metadata
case encryptedMetadata = "encrypted_metadata"
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
address = try container.decode(String.self, forKey: .address)
balances = try container.decode([Balance].self, forKey: .balances)
name = try container.decode(String.self, forKey: .name)
identifier = try container.decode(String.self, forKey: .identifier)
userId = try container.decodeIfPresent(String.self, forKey: .userId)
user = try container.decodeIfPresent(User.self, forKey: .user)
accountId = try container.decodeIfPresent(String.self, forKey: .accountId)
account = try container.decodeIfPresent(Account.self, forKey: .account)
metadata = try container.decode([String: Any].self, forKey: .metadata)
encryptedMetadata = try container.decode([String: Any].self, forKey: .encryptedMetadata)
}

}

extension Wallet: Listable {
Expand Down