Skip to content

Commit 72a4989

Browse files
authored
Merge pull request #48 from EbenezerGH/write-tests-for-account-queries
Write tests for account queries
2 parents 592699c + 7854a26 commit 72a4989

File tree

6 files changed

+183
-19
lines changed

6 files changed

+183
-19
lines changed

etherscanapi/src/main/java/jfyg/account/Account.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class Account : AccountContract {
3434
account, "0", "99999999",
3535
"asc").let { query.fetchAccountTransaction()?.result }
3636

37+
//todo #47
38+
3739
override fun getNetworkStatus(): String? = genericNetworkQuery.let { query.fetchAccountBalance()?.status }
3840

3941
override fun getNetworkMessage(): String? = genericNetworkQuery.let { query.fetchAccountBalance()?.message }

etherscanapi/src/main/java/jfyg/model/Transactions.kt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ data class Transactions(var blockNumber: String? = null,
88

99
var hash: String? = null,
1010

11+
var nonce: String? = null,
12+
13+
var blockHash: String? = null,
14+
15+
var transactionIndex: String? = null,
16+
1117
@SerializedName("from")
1218
var transactionFrom: String? = null,
1319

@@ -16,16 +22,21 @@ data class Transactions(var blockNumber: String? = null,
1622

1723
var value: String? = null,
1824

19-
var contractAddress: String? = null,
25+
var gas: String? = null,
2026

21-
var input: String? = null,
27+
var gasPrice: String? = null,
2228

23-
var type: String? = null, var gas: String? = null,
29+
var isError: String? = null,
2430

25-
var gasUsed: String? = null,
31+
@SerializedName("txreceipt_status")
32+
var receiptStatus: String? = null,
2633

27-
var traceId: String? = null,
34+
var input: String? = null,
2835

29-
var isError: String? = null,
36+
var contractAddress: String? = null,
37+
38+
var cumulativeGasUsed: String? = null,
39+
40+
var gasUsed: String? = null,
3041

31-
var errCode: String? = null)
42+
var confirmations: String? = null)
Lines changed: 159 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,193 @@
11
package jfyg.account
22

3-
import org.junit.After
3+
import com.google.gson.Gson
4+
import com.google.gson.GsonBuilder
5+
import jfyg.response.BaseResponse
6+
import jfyg.response.account.AccountBalanceResponse
7+
import jfyg.response.account.AccountBlockResponse
8+
import jfyg.response.account.AccountMultiBalanceResponse
9+
import jfyg.response.account.AccountTransactionResponse
10+
import org.junit.Assert.assertEquals
11+
import org.junit.Assert
412
import org.junit.Before
513
import org.junit.Test
614

715
class AccountTest {
16+
17+
lateinit var gson: Gson
18+
19+
private val accountBalance = """
20+
{
21+
"status": "1",
22+
"message": "OK",
23+
"result": "670456215218885498951364"
24+
}"""
25+
26+
private val accountMultiBalance = """
27+
{
28+
"status": "1",
29+
"message": "OK",
30+
"result": [
31+
{
32+
"account": "0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a",
33+
"balance": "40807168564070000000000"
34+
},
35+
{
36+
"account": "0x63a9975ba31b0b9626b34300f7f627147df1f526",
37+
"balance": "332567136222827062478"
38+
},
39+
{
40+
"account": "0x198ef1ec325a96cc354c7266a038be8b5c558f67",
41+
"balance": "0"
42+
}
43+
]
44+
}"""
45+
private val accountBlocksMined = """
46+
{
47+
"status": "1",
48+
"message": "OK",
49+
"result": [
50+
{
51+
"blockNumber": "3462296",
52+
"timeStamp": "1491118514",
53+
"blockReward": "5194770940000000000"
54+
},
55+
{
56+
"blockNumber": "2691400",
57+
"timeStamp": "1480072029",
58+
"blockReward": "5086562212310617100"
59+
},
60+
{
61+
"blockNumber": "70606",
62+
"timeStamp": "1439322608",
63+
"blockReward": "5001164799033398000"
64+
}
65+
]
66+
}"""
67+
68+
private val accountTransactions = """
69+
{
70+
"status": "1",
71+
"message": "OK",
72+
"result": [
73+
{
74+
"blockNumber": "5273167",
75+
"timeStamp": "1521316197",
76+
"hash": "0x308d39f4223154fdf2db3b803400dea0da7468592aded1c6e9c01fd35835f23f",
77+
"nonce": "0",
78+
"blockHash": "0x315ee5a947d4c7519bd817465711fbb0b56c8ecafc8d9766aef1350e83c41521",
79+
"transactionIndex": "28",
80+
"from": "0x047250bd5ac59e6c45473cc0036d71737c885f6a",
81+
"to": "0x82e4499d4b2a669831a3881d61bb24f7b620c61a",
82+
"value": "18034800000000000",
83+
"gas": "21000",
84+
"gasPrice": "4000000000",
85+
"isError": "0",
86+
"txreceipt_status": "1",
87+
"input": "0x",
88+
"contractAddress": "",
89+
"cumulativeGasUsed": "1376343",
90+
"gasUsed": "21000",
91+
"confirmations": "150836"
92+
},
93+
{
94+
"blockNumber": "5273704",
95+
"timeStamp": "1521323646",
96+
"hash": "0x55a6922f90f0fce43896b9b2239ddddd3f829fb06291724bfb4be6fcc895d202",
97+
"nonce": "0",
98+
"blockHash": "0xeb96c9b68ae66c42dbf35f323e66534858d88dd1b107f77bb2c65504140e9752",
99+
"transactionIndex": "14",
100+
"from": "0x4a7341f16107521adca5d24009e7c2d8829787d3",
101+
"to": "0x82e4499d4b2a669831a3881d61bb24f7b620c61a",
102+
"value": "53297010000000000",
103+
"gas": "21000",
104+
"gasPrice": "4000000000",
105+
"isError": "0",
106+
"txreceipt_status": "1",
107+
"input": "0x",
108+
"contractAddress": "",
109+
"cumulativeGasUsed": "391843",
110+
"gasUsed": "21000",
111+
"confirmations": "150299"
112+
}
113+
]
114+
}"""
115+
116+
private val inputBadResponse = """
117+
{
118+
"status": "0",
119+
"message": "NOTOK",
120+
"result": "Error!"
121+
}"""
122+
123+
8124
@Before
9125
fun setUp() {
126+
val gb = GsonBuilder()
127+
gson = gb.create()
10128
}
11129

12-
@After
13-
fun tearDown() {
14-
}
15130

16131
@Test
17132
fun getBalance() {
133+
val response = gson.fromJson(accountBalance, AccountBalanceResponse::class.java)
134+
assertEquals("670456215218885498951364", response.result)
18135
}
19136

20137
@Test
21138
fun getMultiBalance() {
139+
val response = gson.fromJson(accountMultiBalance, AccountMultiBalanceResponse::class.java)
140+
assertEquals("0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a", response.result?.get(0)?.account)
141+
assertEquals("40807168564070000000000", response.result?.get(0)?.balance)
142+
assertEquals("0x63a9975ba31b0b9626b34300f7f627147df1f526", response.result?.get(1)?.account)
143+
assertEquals("332567136222827062478", response.result?.get(1)?.balance)
144+
assertEquals("0x198ef1ec325a96cc354c7266a038be8b5c558f67", response.result?.get(2)?.account)
145+
assertEquals("0", response.result?.get(2)?.balance)
22146
}
23147

24148
@Test
25149
fun getBlocks() {
150+
val response = gson.fromJson(accountBlocksMined, AccountBlockResponse::class.java)
151+
assertEquals("3462296", response.result?.get(0)?.blockNumber)
152+
assertEquals("1491118514", response.result?.get(0)?.timeStamp)
153+
assertEquals("5194770940000000000", response.result?.get(0)?.blockReward)
154+
assertEquals("2691400", response.result?.get(1)?.blockNumber)
155+
assertEquals("1480072029", response.result?.get(1)?.timeStamp)
156+
assertEquals("5086562212310617100", response.result?.get(1)?.blockReward)
26157
}
27158

28159
@Test
29160
fun getTransactions() {
161+
val response = gson.fromJson(accountTransactions, AccountTransactionResponse::class.java)
162+
assertEquals("0x047250bd5ac59e6c45473cc0036d71737c885f6a", response.result?.get(0)?.transactionFrom)
163+
assertEquals("0x82e4499d4b2a669831a3881d61bb24f7b620c61a", response.result?.get(0)?.transactionTo)
164+
assertEquals("18034800000000000", response.result?.get(0)?.value)
165+
assertEquals("21000", response.result?.get(1)?.gas)
166+
assertEquals("4000000000", response.result?.get(1)?.gasPrice)
167+
assertEquals("0", response.result?.get(1)?.isError)
168+
}
169+
170+
@Test
171+
fun networkStatusIsDown() {
172+
val response = gson.fromJson(inputBadResponse, BaseResponse::class.java)
173+
Assert.assertEquals("0", response.status)
30174
}
31175

32176
@Test
33-
fun getNetworkStatus() {
177+
fun networkStatusIsUp() {
178+
val response = gson.fromJson(inputBadResponse, BaseResponse::class.java)
179+
Assert.assertNotEquals("1", response.status)
34180
}
35181

36182
@Test
37-
fun getNetworkMessage() {
183+
fun networkMessageNotOk() {
184+
val response = gson.fromJson(inputBadResponse, BaseResponse::class.java)
185+
Assert.assertEquals("NOTOK", response.message)
38186
}
39187

188+
@Test
189+
fun networkMessageOk() {
190+
val response = gson.fromJson(inputBadResponse, BaseResponse::class.java)
191+
Assert.assertNotEquals("OK", response.message)
192+
}
40193
}

etherscanapi/src/test/java/jfyg/network/RestClientTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class RestClientTest {
4646

4747
mockWebServer.enqueue(MockResponse().setBody(fileName))
4848

49-
var networkService: NetworkService = retrofit.create(NetworkService::class.java)
49+
val networkService: NetworkService = retrofit.create(NetworkService::class.java)
5050

5151
val call = networkService.getStat("stats", "ethsupply", "")
5252
Assert.assertTrue(call.subscribe() != null)

etherscanapi/src/test/java/jfyg/stat/StatTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ import org.junit.Assert.assertEquals
1212
class StatTest {
1313
lateinit var gson: Gson
1414

15-
val inputSupply = """
15+
private val inputSupply = """
1616
{
1717
"status": "1",
1818
"message": "OK",
1919
"result": "98658682905300000000000000"
2020
}"""
2121

22-
val inputBadResponse = """
22+
private val inputBadResponse = """
2323
{
2424
"status": "0",
2525
"message": "NOTOK",
2626
"result": "Error!"
2727
}"""
2828

29-
val inputPrice = """
29+
private val inputPrice = """
3030
{
3131
"status": "1",
3232
"message": "OK",

settings.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
rootProject.name = 'etherscan'
2-
31
include ':app', ':etherscanapi'

0 commit comments

Comments
 (0)