Skip to content

Commit 6a30aff

Browse files
authored
Merge pull request #19 from rocklabs-io/unit-test
Unit test
2 parents 6c6a422 + f6a36db commit 6a30aff

File tree

10 files changed

+220
-2
lines changed

10 files changed

+220
-2
lines changed

.github/workflows/test.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: test
2+
on
3+
# Trigger the workflow on push or pull request,
4+
# but only for the main branch
5+
push:
6+
branches:
7+
- main
8+
pull_request:
9+
branches:
10+
- main
11+
12+
jobs:
13+
test:
14+
strategy:
15+
matrix:
16+
os: [ubuntu-latest, macos-latest]
17+
python-version: [3.6, 3.7, 3.8, 3.9]
18+
runs-on: ${{ matrix.os }}
19+
steps:
20+
- run: git config --global core.autocrlf input
21+
- uses: actions/checkout@v2
22+
with:
23+
fetch-depth: 0
24+
- uses: actions/setup-python@v2
25+
with:
26+
python-version: ${{ matrix.python-version }}
27+
- run: pip install --upgrade tox
28+
- run: tox

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ __pycache__
55
test.py
66

77
build/
8-
*.egg-info
8+
*.egg-info
9+
10+
.tox/

readme.md renamed to README.md

File renamed without changes.

pyproject.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[build-system]
2+
requires = [
3+
"setuptools",
4+
"wheel",
5+
]
6+
build-backend = "setuptools.build_meta"

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from setuptools import setup
44
from os import path
55
this_directory = path.abspath(path.dirname(__file__))
6-
with open(path.join(this_directory, 'readme.md'), 'r') as f:
6+
with open(path.join(this_directory, 'README.md'), 'r') as f:
77
long_description = f.read()
88

99
setup(

tests/test_agent.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from ic.agent import *
2+
from ic.identity import *
3+
from ic.client import *
4+
from ic.candid import Types, encode
5+
6+
class TestAgent:
7+
8+
def setup_class(self):
9+
client = Client()
10+
iden = Identity(privkey="833fe62409237b9d62ec77587520911e9a759cec1d19755b7da901b96dca3d42")
11+
self.agent = Agent(iden, client)
12+
13+
def test_query(self):
14+
# query token totalSupply
15+
ret = self.agent.query_raw("gvbup-jyaaa-aaaah-qcdwa-cai", "totalSupply", encode([]))
16+
assert ret[0]['value'] == 10000000000000000
17+
18+
# query token name
19+
ret = self.agent.query_raw("gvbup-jyaaa-aaaah-qcdwa-cai", "name", encode([]))
20+
assert ret[0]['value'] == 'XTC Test'
21+
22+
def test_update(self):
23+
ret = self.agent.update_raw(
24+
"gvbup-jyaaa-aaaah-qcdwa-cai",
25+
"transfer",
26+
encode([
27+
{'type': Types.Principal, 'value': 'aaaaa-aa'},
28+
{'type': Types.Nat, 'value': 10000000000}
29+
])
30+
)
31+
assert ret[0]['type'] == 'rec_0'

tests/test_candid.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
from ic.candid import *
2+
3+
# TODO
4+
class TestCandid:
5+
6+
def test_nat_encode(self):
7+
nat = NatClass()
8+
res = encode([{'type':nat, 'value':10000000000}])
9+
assert res.hex() == "4449444c00017d80c8afa025"
10+
11+
def test_nat_decode(self):
12+
data = bytes.fromhex("4449444c00017d80c8afa025")
13+
res = decode(data)
14+
assert len(res) == 1
15+
assert res[0]["type"] == 'nat'
16+
assert res[0]["value"] == 10000000000
17+
18+
def test_principal_encode(self):
19+
principal = PrincipalClass()
20+
res = encode([{'type': principal, 'value':'aaaaa-aa'}])
21+
assert res.hex() == "4449444c0001680100"
22+
23+
def test_principal_decode(self):
24+
data = bytes.fromhex("4449444c0001680100")
25+
res = decode(data)
26+
assert len(res) == 1
27+
assert res[0]["type"] == 'principal'
28+
assert res[0]["value"].to_str() == 'aaaaa-aa'
29+
30+
# data = b'DIDL\x00\x01q\x08XTC Test'
31+
# print('decode data: {}'.format(data))
32+
# out = decode(data)
33+
# print(out)
34+
35+
# data = b'DIDL\x00\x01}\xe2\x82\xac\xe2\x82\xac\xe2\x80'
36+
# print('decode data: {}'.format(data))
37+
# out = decode(data)
38+
# print(out)
39+
40+
def test_record_encode(self):
41+
record = Types.Record({'foo':Types.Text, 'bar': Types.Int})
42+
res = encode([{'type': record, 'value':{'foo': '💩', 'bar': 42}}])
43+
assert res.hex() == '4449444c016c02d3e3aa027c868eb7027101002a04f09f92a9'
44+
45+
def test_record_decode(self):
46+
data = bytes.fromhex('4449444c016c02d3e3aa027c868eb7027101002a04f09f92a9')
47+
res = decode(data)
48+
assert len(res) == 1
49+
assert res[0]["type"].startswith('rec')
50+
assert res[0]['value'] == {'_4895187': 42, '_5097222': '💩'}
51+
52+
# def test_tuple_encode(self):
53+
# tup = Types.Tuple(Types.Int, Types.Text)
54+
# res = encode([{'type': tup, 'value': [42, '💩']}])
55+
# assert res.hex() == '4449444c016c02007c017101002a04f09f92a9'
56+
57+
58+
# # variant
59+
# tup = Types.Variant({'ok': Types.Text, 'err': Types.Text})
60+
# res = encode([{'type': tup, 'value': {'ok': 'good'} }])
61+
# print('expected:', '4449444c016b03017e9cc20171e58eb4027101000104676f6f64')
62+
# print('current:', res.hex())
63+
# print(decode(res, tup))
64+
65+
# # tuple(variant)
66+
# tup = Types.Tuple(Types.Variant({'ok': Types.Text, 'err': Types.Text}))
67+
# res = encode([{'type': tup, 'value': [{'ok': 'good'}] }])
68+
# print('expected:', '4449444c026b029cc20171e58eb402716c01000001010004676f6f64')
69+
# print('current:', res.hex())
70+
# print(decode(res, tup))
71+
72+
# # Vec
73+
# vec = Types.Vec(Types.Nat64)
74+
# param = [0, 1, 2, 3]
75+
# res = encode([{'type': vec, 'value': param}])
76+
# print('expected:', '4449444c016d7c01000400010203')
77+
# print('current :', res.hex())
78+
# print('decode Vec:', decode(res, vec))
79+
80+
# # Principle
81+
# Prin = Types.Principal
82+
# param = 'expmt-gtxsw-inftj-ttabj-qhp5s-nozup-n3bbo-k7zvn-dg4he-knac3-lae'
83+
# res = encode([{'type': Prin, 'value': param}])
84+
# print('current :', res.hex())
85+
# print('decode Principal:', decode(res))
86+
87+
# # Opt principal
88+
# Prin = Types.Opt(Types.Principal)
89+
# param = ['expmt-gtxsw-inftj-ttabj-qhp5s-nozup-n3bbo-k7zvn-dg4he-knac3-lae']
90+
# res = encode([{'type': Prin, 'value': param}])
91+
# print('current :', res.hex())
92+
# print('decode Principal:', decode(res, Prin))
93+
94+
# # NULL
95+
# Prin = Types.Null
96+
# param = None
97+
# res = encode([{'type': Prin, 'value': param}])
98+
# print('current :', res.hex())
99+
# print('decode Null:', decode(res, Prin))

tests/test_identity.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from ic.identity import *
2+
3+
class TestIdentity:
4+
5+
def test_ed25519_privatekey(self):
6+
iden = Identity(privkey="833fe62409237b9d62ec77587520911e9a759cec1d19755b7da901b96dca3d42")
7+
assert iden.key_type == 'ed25519'
8+
assert iden.pubkey == 'ec172b93ad5e563bf4932c70e1245034c35467ef2efd4d64ebf819683467e2bf'
9+
10+
def test_secp256k1_privatekey(self):
11+
pass
12+
13+
def test_ed25519_frompem(self):
14+
pem = """
15+
-----BEGIN PRIVATE KEY-----
16+
MFMCAQEwBQYDK2VwBCIEIGQqNAZlORmn1k4QrYz1FvO4fOQowS3GXQMqRKDzmx9P
17+
oSMDIQCrO5iGM5hnLWrHavywoXekAoXPpYRuB0Dr6DjZF6FZkg==
18+
-----END PRIVATE KEY-----"""
19+
iden = Identity.from_pem(pem)
20+
assert iden.key_type == 'ed25519'
21+
assert iden.privkey == '642a3406653919a7d64e10ad8cf516f3b87ce428c12dc65d032a44a0f39b1f4f'
22+
assert iden.pubkey == 'ab3b98863398672d6ac76afcb0a177a40285cfa5846e0740ebe838d917a15992'
23+
24+
def test_secp256k1_frompem(self):
25+
pass

tests/test_principal.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from ic.principal import Principal
2+
3+
class TestPrincipal:
4+
5+
def test_default(self):
6+
p = Principal()
7+
assert p.to_str() == 'aaaaa-aa'
8+
9+
def test_anonymous(self):
10+
p = Principal.anonymous();
11+
assert p.to_str() == '2vxsx-fae'
12+
13+
def test_pubkey(self):
14+
p = Principal.self_authenticating("ec172b93ad5e563bf4932c70e1245034c35467ef2efd4d64ebf819683467e2bf") # create a principal from public key
15+
assert p.to_str() == 'zbtml-m23rk-szoaa-5x6p5-co5in-yylx6-463xu-zbjp4-4oizn-cqaij-tae'
16+
17+
def test_fromstr(self):
18+
p = Principal.from_str("zbtml-m23rk-szoaa-5x6p5-co5in-yylx6-463xu-zbjp4-4oizn-cqaij-tae")
19+
assert p.to_str() == 'zbtml-m23rk-szoaa-5x6p5-co5in-yylx6-463xu-zbjp4-4oizn-cqaij-tae'

tox.ini

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[tox]
2+
envlist = py
3+
isolated_build = true
4+
5+
[testenv]
6+
changedir = tests
7+
deps = pytest
8+
commands = pytest {posargs}

0 commit comments

Comments
 (0)