This repository has been archived by the owner on Mar 14, 2021. It is now read-only.
/
APIClientTests.swift
234 lines (192 loc) · 7.52 KB
/
APIClientTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//
// APIClientTests.swift
//
// Copyright © 2018 Nervos Foundation. All rights reserved.
//
import XCTest
@testable import CKB
class APIClientTests: RPCTestSkippable {
func testSettingId() throws {
let result = try client.load(APIRequest<H256>(id: 10, method: "get_block_hash", params: ["0"]))
XCTAssertNotNil(result)
}
func testGenesisBlockHash() {
let result = try? client.genesisBlockHash()
XCTAssertNotNil(result)
}
func testGenesisBlock() throws {
let result = try client.genesisBlock()
XCTAssertNotNil(result)
}
func testGetBlock() throws {
let hash = try client.genesisBlockHash()
let result = try client.getBlock(hash: hash)
XCTAssertNotNil(result)
XCTAssertEqual(hash, result.header.hash)
XCTAssertNil(try? client.getBlock(hash: nonexistentHash))
}
func testGetBlockByNumber() throws {
let result = try client.getBlockByNumber(number: "0")
XCTAssertNotNil(result)
XCTAssertEqual("0", result.header.number)
}
func testGetBlockPerformance() throws {
measure {
_ = try! client.genesisBlock()
}
}
func testGetTransaction() throws {
let genesisBlock = try client.genesisBlock()
if let tx = genesisBlock.transactions.first {
let result = try client.getTransaction(hash: tx.hash)
XCTAssertNotNil(result)
XCTAssertEqual(tx.hash, result.transaction.hash)
}
XCTAssertNil(try? client.getTransaction(hash: nonexistentHash))
}
func testGetCellbaseOutputCapacityDetails() throws {
let tipHeader = try client.getTipHeader()
let result = try client.getCellbaseOutputCapacityDetails(blockHash: tipHeader.hash)
XCTAssertNotNil(result)
XCTAssert(Int64(result.txFee)! >= 0)
}
func testGetBlockHash() throws {
let result = try client.getBlockHash(number: "0")
XCTAssertNotNil(result)
}
func testGetTipHeader() throws {
let result = try client.getTipHeader()
XCTAssertNotNil(result)
XCTAssertTrue(Int64(result.number)! >= 0)
}
func testGetHeader() throws {
let tipHeader = try client.getTipHeader()
let hash = tipHeader.hash
let result = try client.getHeader(blockHash: hash)
XCTAssertNotNil(result)
XCTAssertEqual(result.hash, hash)
}
func testGetHeaderByNumber() throws {
let tipHeader = try client.getTipHeader()
let number = tipHeader.number
let result = try client.getHeaderByNumber(number: number)
XCTAssertNotNil(result)
XCTAssertEqual(result.number, number)
}
func testGetCellsByLockHash() throws {
let result = try client.getCellsByLockHash(lockHash: "0x321c1ca2887fb8eddaaa7e917399f71e63e03a1c83ff75ed12099a01115ea2ff", from: "1", to: "100")
XCTAssertNotNil(result)
XCTAssertTrue(try client.getCellsByLockHash(lockHash: nonexistentHash, from: "1", to: "100").isEmpty)
}
func testGetLiveCell() throws {
let cells = try client.getCellsByLockHash(lockHash: "0x321c1ca2887fb8eddaaa7e917399f71e63e03a1c83ff75ed12099a01115ea2ff", from: "1", to: "100")
if let cell = cells.first {
let result = try client.getLiveCell(outPoint: cell.outPoint)
XCTAssertNotNil(result)
}
XCTAssertTrue(try client.getCellsByLockHash(lockHash: nonexistentHash, from: "1", to: "100").isEmpty)
}
func testGetTipBlockNumber() throws {
let result = try client.getTipBlockNumber()
XCTAssertTrue(Int64(result)! >= 0)
}
func testGetCurrentEpoch() throws {
let result = try client.getCurrentEpoch()
XCTAssertTrue(UInt64(result.difficulty.dropFirst(2), radix: 16)! >= 0)
}
func testGetEpochByNumber() throws {
let number = try client.getCurrentEpoch().number
let result = try client.getEpochByNumber(number: number)
XCTAssertNotNil(result)
XCTAssertNil(try? client.getEpochByNumber(number: String(UInt64(number)! + 10_000)))
}
func testSendTransactionEmpty() throws {
let tx = Transaction()
let result = try? client.sendTransaction(transaction: tx)
XCTAssertNil(result)
}
func testTxPoolInfo() throws {
let result = try client.txPoolInfo()
XCTAssertNotNil(result)
XCTAssert(UInt32(result.pending)! >= 0)
}
func testGetBlockchainInfo() throws {
let result = try client.getBlockchainInfo()
XCTAssertNotNil(result)
XCTAssertFalse(result.chain.isEmpty)
}
func testGetPeersState() throws {
let result = try client.getPeersState()
XCTAssertNotNil(result)
XCTAssert(result.count >= 0)
}
func testLocalNodeInfo() throws {
let result = try client.localNodeInfo()
XCTAssertFalse(result.addresses.isEmpty)
XCTAssertFalse(result.nodeId.isEmpty)
}
func testGetPeers() throws {
let result = try client.getPeers()
XCTAssertNotNil(result)
}
func testSetBan() throws {
let result = try client.setBan(address: "192.168.0.1", command: "insert", banTime: nil, absolute: nil, reason: "a reason")
XCTAssertNil(result)
}
func testGetBannedAddress() throws {
let result = try client.getBannedAddresses()
XCTAssertNotNil(result)
XCTAssert(result.count >= 0)
}
func testComputeTransactionHash() throws {
let tx = Transaction()
let result = try client.computeTransactionHash(transaction: tx)
XCTAssertNotNil(result)
}
func testDryRunTransaction() throws {
let tx = Transaction()
let result = try client.dryRunTransaction(transaction: tx)
XCTAssertNotNil(result)
}
func testIndexLockHash() throws {
let lockHash = "0x9a9a6bdbc38d4905eace1822f85237e3a1e238bb3f277aa7b7c8903441123510"
var result = try client.indexLockHash(lockHash: lockHash, indexFrom: "0")
XCTAssertNotNil(result)
XCTAssertEqual(result.lockHash, lockHash)
XCTAssertEqual(result.blockNumber, "0")
result = try client.indexLockHash(lockHash: lockHash)
XCTAssertNotNil(result)
XCTAssertEqual(result.lockHash, lockHash)
}
func testDeindexLockHash() throws {
let lockHash = "0x9a9a6bdbc38d4905eace1822f85237e3a1e238bb3f277aa7b7c8903441123510"
let result = try client.deindexLockHash(lockHash: lockHash)
XCTAssertNil(result)
}
func testGetLockHashIndexStates() throws {
let result = try client.getLockHashIndexStates()
XCTAssertNotNil(result)
XCTAssert(result.count >= 0)
}
func testGetLiveCellsByLockHash() throws {
let result = try client.getLiveCellsByLockHash(lockHash: "0x9a9a6bdbc38d4905eace1822f85237e3a1e238bb3f277aa7b7c8903441123510", page: "0", pageSize: "2", reverseOrder: false)
XCTAssertNotNil(result)
XCTAssert(result.count >= 0)
}
func testGetTransactionssByLockHash() throws {
let result = try client.getTransactionsByLockHash(lockHash: "0x9a9a6bdbc38d4905eace1822f85237e3a1e238bb3f277aa7b7c8903441123510", page: "0", pageSize: "2", reverseOrder: false)
XCTAssertNotNil(result)
XCTAssert(result.count >= 0)
}
}
private extension APIClientTests {
var node: URL {
return APIClient.defaultLocalURL
}
var client: APIClient {
return APIClient(url: node)
}
var nonexistentHash: H256 {
return "0x00000000000000000000000067d82224e6619896c7f12bb73a14dd9329af9c8d"
}
}