chainlib/tests/test_chain.py

84 lines
2.5 KiB
Python
Raw Permalink Normal View History

# standard imports
2021-02-18 09:29:52 +01:00
import unittest
import logging
2021-02-18 09:29:52 +01:00
# local imports
2021-02-18 09:29:52 +01:00
from chainlib.chain import ChainSpec
# test imports
2021-02-18 09:29:52 +01:00
from tests.base import TestBase
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
logg.setLevel(logging.DEBUG)
2021-02-18 09:29:52 +01:00
class TestChain(TestBase):
2021-10-18 13:25:00 +02:00
def test_chain_spec_str(self):
s = ChainSpec('foo', 'bar', 3)
self.assertEqual('foo:bar:3', str(s))
2021-10-18 13:25:00 +02:00
s = ChainSpec('foo', 'bar', 3, 'baz')
self.assertEqual('foo:bar:3:baz', str(s))
s = ChainSpec('foo', 'bar', 3, 'baz', ['inky', 'pinky', 'blinky'])
self.assertEqual('foo:bar:3:baz:inky:pinky:blinky', str(s))
2021-02-18 09:29:52 +01:00
def test_chain_spec(self):
2021-02-18 09:29:52 +01:00
s = ChainSpec.from_chain_str('foo:bar:3')
s = ChainSpec.from_chain_str('foo:bar:3:baz')
s = ChainSpec.from_chain_str('foo:bar:3:baz:inky:pinky:blinky')
2021-02-18 09:29:52 +01:00
with self.assertRaises(ValueError):
s = ChainSpec.from_chain_str('foo:bar:a')
s = ChainSpec.from_chain_str('foo:bar')
s = ChainSpec.from_chain_str('foo')
s = ChainSpec.from_chain_str('foo1:bar:3')
s = ChainSpec.from_chain_str('foo:bar2:3')
2021-02-18 09:29:52 +01:00
2021-10-18 13:25:00 +02:00
def test_chain_spec_dict(self):
ss = 'foo:bar:3:baz:inky:pinky:blinky'
c = ChainSpec.from_chain_str(ss)
2021-10-18 13:25:00 +02:00
d = c.asdict()
self.assertEqual(d['arch'], 'foo')
self.assertEqual(d['fork'], 'bar')
self.assertEqual(d['network_id'], 3)
self.assertEqual(d['common_name'], 'baz')
self.assertEqual(d['custom'], ['inky', 'pinky', 'blinky'])
2021-10-18 13:25:00 +02:00
cc = ChainSpec.from_dict(d)
self.assertEqual(ss, str(cc))
d = c.asdict(use_custom=False)
cc = ChainSpec.from_dict(d)
self.assertEqual(str(cc), 'foo:bar:3:baz')
d = c.asdict(use_common_name=False)
cc = ChainSpec.from_dict(d)
self.assertEqual(str(cc), 'foo:bar:3')
def test_chain_spec_compare(self):
a = 'foo:bar:42:baz'
b = 'foo:bar:42:barbar'
c = 'foo:bar:42:baz:inky:pinky:blinky'
ca = ChainSpec.from_chain_str(a)
cb = ChainSpec.from_chain_str(b)
self.assertTrue(ca.is_same_as(cb))
self.assertFalse(ca.is_same_as(cb, use_common_name=True))
cc = ChainSpec.from_chain_str(c)
logg.debug('chain_spec_cmp ' + str(cc.o))
self.assertTrue(ca.is_same_as(cc))
self.assertTrue(ca.is_same_as(cc, use_common_name=True))
self.assertFalse(ca.is_same_as(cc, use_common_name=True, use_custom=True))
2021-10-18 13:25:00 +02:00
2021-02-18 09:29:52 +01:00
if __name__ == '__main__':
unittest.main()