Skip to content

Commit

Permalink
Merge pull request #79 from bandprotocol/unit-tests
Browse files Browse the repository at this point in the history
add some unit tests
  • Loading branch information
xxibcill authored May 15, 2023
2 parents 1429d20 + dcefa14 commit 7bfb8b2
Show file tree
Hide file tree
Showing 39 changed files with 2,059 additions and 1,364 deletions.
30 changes: 29 additions & 1 deletion __tests__/Address_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,30 @@ describe("Expect Address to work correctly", () => {
)
)

test("should be able to create address from fromBech32", () =>
test("should be able to get hexString with 0x prefix with Uppercase", () =>
expect(fromHex("f23391b5dbf982e37fb7dadea64aae21cae4c172")->toHex(~with0x=true, ~upper=true))->toEqual(
"0XF23391B5DBF982E37FB7DADEA64AAE21CAE4C172",
)
)

test("should be able to create address from fromBech32 with prefix band", () =>
expect("band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph"->fromBech32)->toEqual(
Address("3855e27cba57cea3c70bf4063a7e28f11bd149fe"),
)
)

test("should be able to create address from fromBech32 with prefix bandvaloper", () =>
expect("bandvaloper13zmknvkq2sj920spz90g4r9zjan8g58423y76e"->fromBech32)->toEqual(
Address("88b769b2c05424553e01115e8a8ca297667450f5"),
)
)

test("should be able to create address from fromBech32 with prefix bandvalconspub", () =>
expect("bandvalconspub1addwnpepq0grwz83v8g4s06fusnq5s4jkzxnhgvx67qr5g7v8tx39ur5m8tk7rg2nxj"->fromBech32)->toEqual(
Address("eb5ae9872103d03708f161d1583f49e4260a42b2b08d3ba186d7803a23cc3acd12f074d9d76f"),
)
)

test("should be able to convert self to hex", () =>
expect(Address("88b769b2c05424553e01115e8a8ca297667450f5")->toHex)->toEqual(
"88b769b2c05424553e01115e8a8ca297667450f5",
Expand Down Expand Up @@ -62,4 +80,14 @@ describe("Expect Address to work correctly", () => {
"bandvaloper13zmknvkq2sj920spz90g4r9zjan8g58423y76e",
)
)

test("isEqual should work correctly", () =>
expect({
let addr1 = "band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph"->fromBech32
let addr2 = "band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph"->fromBech32
isEqual(addr1,addr2)
})->toEqual(
true
)
)
})
66 changes: 28 additions & 38 deletions __tests__/BandChainJS_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ open Jest
open BandChainJS
open Expect

let mnemonic = "mule way gather advance quote endorse boat liquid kite mad cart"
let mnemonic = "test"

describe("Expect BandChainJS Client Module binding work correctly", () => {
testPromise("getReferenceData", async () => {
Expand All @@ -21,7 +21,7 @@ describe("Expect BandChainJS PrivateKey Module binding work correctly", () => {
test("fromMnemonic then toPubkey", () =>
expect(
mnemonic
->PrivateKey.fromMnemonic("test")
->PrivateKey.fromMnemonic("m/44'/494'/0'/0/0")
->PrivateKey.toPubkey
->PubKey.toAddress
->Address.toAccBech32,
Expand Down Expand Up @@ -50,14 +50,14 @@ describe("Expect BandChainJS PubKey Module binding work correctly", () => {

test("toBech32", () =>
expect(
mnemonic->PrivateKey.fromMnemonic("test")->PrivateKey.toPubkey->PubKey.toBech32("band"),
)->toEqual("band1addwnpepqvzkxlgphmkh4z0wg5lrpsrrgfl7hymwtfz5kzgmmdvwqk70hq67vyrctrw")
mnemonic->PrivateKey.fromMnemonic("m/44'/494'/0'/0/0")->PrivateKey.toPubkey->PubKey.toBech32("band"),
)->toEqual("band1addwnpepqt79xhl2m49qfpre5jf9td3q64y8r9cxwm26fmzaug2vsrfcwsg0v70ls97")
)

test("toAddress", () =>
expect(
mnemonic
->PrivateKey.fromMnemonic("test")
->PrivateKey.fromMnemonic("m/44'/494'/0'/0/0")
->PrivateKey.toPubkey
->PubKey.toAddress
->Address.toAccBech32,
Expand Down Expand Up @@ -107,17 +107,17 @@ describe("Expect BandChainJS Fee Module binding work correctly", () => {
test("setPayer getPayer", () =>
expect({
let fee = Fee.create()
fee->Fee.setPayer("band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph")
fee->Fee.setPayer("band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj")
fee->Fee.getPayer
})->toEqual("band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph")
})->toEqual("band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj")
)

test("setGranter getGranter", () =>
expect({
let fee = Fee.create()
fee->Fee.setGranter("band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph")
fee->Fee.setGranter("band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj")
fee->Fee.getGranter
})->toEqual("band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph")
})->toEqual("band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj")
)
})

Expand All @@ -129,14 +129,14 @@ describe("Expect BandChainJS Message Module binding work correctly", () => {
coin->Coin.setAmount("1000000")

Message.MsgSend.create(
"band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph",
"band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj",
"band120q5vvspxlczc8c72j7c3c4rafyndaelqccksu",
[coin],
)
->Message.MsgSend.toJSON
->Js.Json.stringifyAny
})->toEqual(
Some(`{"type":"cosmos-sdk/MsgSend","value":{"from_address":"band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph","to_address":"band120q5vvspxlczc8c72j7c3c4rafyndaelqccksu","amount":[{"denom":"uband","amount":"1000000"}]}}`),
Some(`{"type":"cosmos-sdk/MsgSend","value":{"from_address":"band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj","to_address":"band120q5vvspxlczc8c72j7c3c4rafyndaelqccksu","amount":[{"denom":"uband","amount":"1000000"}]}}`),
)
)

Expand All @@ -147,14 +147,14 @@ describe("Expect BandChainJS Message Module binding work correctly", () => {
coin->Coin.setAmount("1000000")

Message.MsgDelegate.create(
"band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph",
"band18aqvecak05emvl3hjff40swq0m6t9n4m42rcj8",
"band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj",
"bandvaloper18aqvecak05emvl3hjff40swq0m6t9n4meu8mhv",
coin,
)
->Message.MsgDelegate.toJSON
->Js.Json.stringifyAny
})->toEqual(
Some(`{"type":"cosmos-sdk/MsgDelegate","value":{"delegator_address":"band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph","validator_address":"band18aqvecak05emvl3hjff40swq0m6t9n4m42rcj8","amount":{"denom":"uband","amount":"1000000"}}}`),
Some(`{"type":"cosmos-sdk/MsgDelegate","value":{"delegator_address":"band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj","validator_address":"bandvaloper18aqvecak05emvl3hjff40swq0m6t9n4meu8mhv","amount":{"denom":"uband","amount":"1000000"}}}`),
)
)

Expand All @@ -165,14 +165,14 @@ describe("Expect BandChainJS Message Module binding work correctly", () => {
coin->Coin.setAmount("1000000")

Message.MsgUndelegate.create(
"band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph",
"band18aqvecak05emvl3hjff40swq0m6t9n4m42rcj8",
"band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj",
"bandvaloper18aqvecak05emvl3hjff40swq0m6t9n4meu8mhv",
coin,
)
->Message.MsgUndelegate.toJSON
->Js.Json.stringifyAny
})->toEqual(
Some(`{"type":"cosmos-sdk/MsgUndelegate","value":{"delegator_address":"band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph","validator_address":"band18aqvecak05emvl3hjff40swq0m6t9n4m42rcj8","amount":{"denom":"uband","amount":"1000000"}}}`),
Some(`{"type":"cosmos-sdk/MsgUndelegate","value":{"delegator_address":"band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj","validator_address":"bandvaloper18aqvecak05emvl3hjff40swq0m6t9n4meu8mhv","amount":{"denom":"uband","amount":"1000000"}}}`),
)
)

Expand All @@ -183,53 +183,44 @@ describe("Expect BandChainJS Message Module binding work correctly", () => {
coin->Coin.setAmount("1000000")

Message.MsgRedelegate.create(
"band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph",
"band18aqvecak05emvl3hjff40swq0m6t9n4m42rcj8",
"band1ntwzz6tlvpy52tf5urr7jz6lvk2402sksntxuw",
"band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj",
"bandvaloper18aqvecak05emvl3hjff40swq0m6t9n4meu8mhv",
"bandvaloper1kfj48adjsnrgu83lau6wc646q2uf65rf84tzus",
coin,
)
->Message.MsgRedelegate.toJSON
->Js.Json.stringifyAny
})->toEqual(
Some(`{"type":"cosmos-sdk/MsgBeginRedelegate","value":{"delegator_address":"band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph","validator_src_address":"band18aqvecak05emvl3hjff40swq0m6t9n4m42rcj8","validator_dst_address":"band1ntwzz6tlvpy52tf5urr7jz6lvk2402sksntxuw","amount":{"denom":"uband","amount":"1000000"}}}`),
Some(`{"type":"cosmos-sdk/MsgBeginRedelegate","value":{"delegator_address":"band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj","validator_src_address":"bandvaloper18aqvecak05emvl3hjff40swq0m6t9n4meu8mhv","validator_dst_address":"bandvaloper1kfj48adjsnrgu83lau6wc646q2uf65rf84tzus","amount":{"denom":"uband","amount":"1000000"}}}`),
)
)

test("MsgWithdrawReward", () =>
expect({
let coin = Coin.create()
coin->Coin.setDenom("uband")
coin->Coin.setAmount("1000000")

Message.MsgWithdrawReward.create(
"band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph",
"band18aqvecak05emvl3hjff40swq0m6t9n4m42rcj8",
"band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj",
"bandvaloper18aqvecak05emvl3hjff40swq0m6t9n4meu8mhv",
)
->Message.MsgWithdrawReward.toJSON
->Js.Json.stringifyAny
})->toEqual(
Some(`{"type":"cosmos-sdk/MsgWithdrawDelegationReward","value":{"delegator_address":"band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph","validator_address":"band18aqvecak05emvl3hjff40swq0m6t9n4m42rcj8"}}`),
Some(`{"type":"cosmos-sdk/MsgWithdrawDelegationReward","value":{"delegator_address":"band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj","validator_address":"bandvaloper18aqvecak05emvl3hjff40swq0m6t9n4meu8mhv"}}`),
)
)

test("MsgVote", () =>
expect({
let coin = Coin.create()
coin->Coin.setDenom("uband")
coin->Coin.setAmount("1000000")

Message.MsgVote.create(2, "band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph", 1)
Message.MsgVote.create(2, "band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj", 1)
->Message.MsgVote.toJSON
->Js.Json.stringifyAny
})->toEqual(
Some(`{"type":"cosmos-sdk/MsgVote","value":{"proposal_id":"2","voter":"band18p27yl962l8283ct7srr5l3g7ydazj07dqrwph","option":1}}`),
Some(`{"type":"cosmos-sdk/MsgVote","value":{"proposal_id":"2","voter":"band1dgstnw0m2cshvh4ymnlcxdj0wr3x797efzrexj","option":1}}`),
)
)

test("MsgRequest", () =>
expect({
let mnemo = "mule way gather advance quote endorse boat liquid kite mad cart"
let privKey = PrivateKey.fromMnemonic(mnemo, "test")
let privKey = PrivateKey.fromMnemonic(mnemonic, "m/44'/494'/0'/0/0")
let pub = privKey->PrivateKey.toPubkey
let address = pub->PubKey.toAddress->Address.toAccBech32

Expand Down Expand Up @@ -289,8 +280,7 @@ describe("Expect BandChainJS Message Module binding work correctly", () => {

describe("Expect BandChainJS Transaction Module binding work correctly", () => {
testPromise("create Address fromhex and call toHex", async () => {
let mnemo = "mule way gather advance quote endorse boat liquid kite mad cart"
let privKey = PrivateKey.fromMnemonic(mnemo, "test")
let privKey = PrivateKey.fromMnemonic(mnemonic, "m/44'/494'/0'/0/0")
let pub = privKey->PrivateKey.toPubkey
let address = pub->PubKey.toAddress->Address.toAccBech32

Expand Down
14 changes: 14 additions & 0 deletions __tests__/Ellipsis_test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
open Jest
open Ellipsis
open Expect

describe("Expect Ellipsis module to work correctly", () => {
test("text length > limit", () => {
expect(format(~text="EDCD7E7A10F564CCCC50B2E382E1924013CB01728EBAFEC1071D2B1D395F82CC", ~limit=25, ()))->toBe("EDCD7E7A10F564CCCC50B2E38...")
})

test("text length < limit", () => {
expect(format(~text="EDCD7E7A10F564CCCC50B2E382E1924013CB01728EBAFEC1071D2B1D395F82CC", ~limit=125, ()))->toBe("EDCD7E7A10F564CCCC50B2E382E1924013CB01728EBAFEC1071D2B1D395F82CC")
})
})

35 changes: 35 additions & 0 deletions __tests__/ExecutableParser_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,41 @@ open ExecutableParser
open Expect

describe("Expect Parser to work correctly", () => {
test("test pythonMatch function", () =>
expect(
`#!/usr/bin/env python3
import json
import urllib.request
import sys
COINS_URL = "https://api.coingecko.com/api/v3/coins/list"
PRICE_URL = "https://api.coingecko.com/api/v3/simple/price?ids={}&vs_currencies=usd"
def make_json_request(url):
return json.loads(urllib.request.urlopen(url).read())
def main(symbol):
coins = make_json_request(COINS_URL)
for coin in coins:
if coin["symbol"] == symbol.lower():
slug = coin["id"]
return make_json_request(PRICE_URL.format(slug))[slug]["usd"]
raise ValueError("unknown CoinGecko symbol: {}".format(symbol))
if __name__ == "__main__":
try:
print(main(*sys.argv[1:]))
except Exception as e:
print(str(e), file=sys.stderr)
sys.exit(1)
`
->pythonMatch
->Belt.Option.getExn,
)->toEqual(["symbol"])
)

test("test get 1 variable in python script", () =>
expect(
`#!/usr/bin/env python3
Expand Down
74 changes: 74 additions & 0 deletions __tests__/Format_test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
open Jest
open Format
open Expect

describe("Expect Format module to work correctly", () => {
test("withCommas", () => {
expect("10000000"->withCommas)->toBe("10,000,000")
})

test("withCommas with decimal digits", () => {
expect("10000000.1234"->withCommas)->toBe("10,000,000.1234")
})

test("fPretty value >= 1000000", () => {
expect(10000000.->fPretty)->toBe("10,000,000")
})

test("fPretty value > 100.", () => {
expect(1000.123->fPretty)->toBe("1,000.12")
})

test("fPretty value > 1.", () => {
expect(3.14285->fPretty)->toBe("3.1429")
})

test("fPretty value < 1", () => {
expect(0.123->fPretty)->toBe("0.123000")
})

test("fPretty with digit", () => {
expect(10000000.12345678->fPretty(~digits=6))->toBe("10,000,000.123457")
})

test("fCurrency value >= 1e9", () => {
expect(12300000000.->fCurrency)->toBe("12.30B")
})

test("fCurrency value >= 1e6", () => {
expect(12300000.->fCurrency)->toBe("12.30M")
})

test("fCurrency value >= 1e3", () => {
expect(12300.->fCurrency)->toBe("12.30K")
})

test("fCurrency else", () => {
expect(123.->fCurrency)->toBe("123.00")
})

test("fPercentChange positive", () => {
expect(99.99->fPercentChange)->toBe("+99.99%")
})

test("fPercentChange negative", () => {
expect(-99.99->fPercentChange)->toBe("-99.99%")
})

test("fPercent", () => {
expect(123456789.->fPercent)->toBe("123,456,789.00 %")
})

test("fPercent < 1", () => {
expect(0.12345678->fPercent)->toBe("0.123457 %")
})

test("fPercent with digits", () => {
expect(123.45678->fPercent(~digits=4))->toBe("123.4568 %")
})

test("iPretty", () => {
expect(10000000->iPretty)->toBe("10,000,000")
})
})

Loading

1 comment on commit 7bfb8b2

@vercel
Copy link

@vercel vercel bot commented on 7bfb8b2 May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.