test_chain.py (2560B)
1 # standard imports 2 import unittest 3 import logging 4 5 # local imports 6 from chainlib.chain import ChainSpec 7 8 # test imports 9 from tests.base import TestBase 10 11 logging.basicConfig(level=logging.DEBUG) 12 logg = logging.getLogger() 13 logg.setLevel(logging.DEBUG) 14 15 16 class TestChain(TestBase): 17 18 def test_chain_spec_str(self): 19 s = ChainSpec('foo', 'bar', 3) 20 self.assertEqual('foo:bar:3', str(s)) 21 22 s = ChainSpec('foo', 'bar', 3, 'baz') 23 self.assertEqual('foo:bar:3:baz', str(s)) 24 25 26 s = ChainSpec('foo', 'bar', 3, 'baz', ['inky', 'pinky', 'blinky']) 27 self.assertEqual('foo:bar:3:baz:inky:pinky:blinky', str(s)) 28 29 30 def test_chain_spec(self): 31 s = ChainSpec.from_chain_str('foo:bar:3') 32 s = ChainSpec.from_chain_str('foo:bar:3:baz') 33 s = ChainSpec.from_chain_str('foo:bar:3:baz:inky:pinky:blinky') 34 35 with self.assertRaises(ValueError): 36 s = ChainSpec.from_chain_str('foo:bar:a') 37 s = ChainSpec.from_chain_str('foo:bar') 38 s = ChainSpec.from_chain_str('foo') 39 s = ChainSpec.from_chain_str('foo1:bar:3') 40 s = ChainSpec.from_chain_str('foo:bar2:3') 41 42 43 def test_chain_spec_dict(self): 44 ss = 'foo:bar:3:baz:inky:pinky:blinky' 45 c = ChainSpec.from_chain_str(ss) 46 d = c.asdict() 47 self.assertEqual(d['arch'], 'foo') 48 self.assertEqual(d['fork'], 'bar') 49 self.assertEqual(d['network_id'], 3) 50 self.assertEqual(d['common_name'], 'baz') 51 self.assertEqual(d['custom'], ['inky', 'pinky', 'blinky']) 52 cc = ChainSpec.from_dict(d) 53 self.assertEqual(ss, str(cc)) 54 55 d = c.asdict(use_custom=False) 56 cc = ChainSpec.from_dict(d) 57 self.assertEqual(str(cc), 'foo:bar:3:baz') 58 59 d = c.asdict(use_common_name=False) 60 cc = ChainSpec.from_dict(d) 61 self.assertEqual(str(cc), 'foo:bar:3') 62 63 def test_chain_spec_compare(self): 64 a = 'foo:bar:42:baz' 65 b = 'foo:bar:42:barbar' 66 c = 'foo:bar:42:baz:inky:pinky:blinky' 67 68 ca = ChainSpec.from_chain_str(a) 69 cb = ChainSpec.from_chain_str(b) 70 71 self.assertTrue(ca.is_same_as(cb)) 72 self.assertFalse(ca.is_same_as(cb, use_common_name=True)) 73 74 cc = ChainSpec.from_chain_str(c) 75 logg.debug('chain_spec_cmp ' + str(cc.o)) 76 self.assertTrue(ca.is_same_as(cc)) 77 self.assertTrue(ca.is_same_as(cc, use_common_name=True)) 78 self.assertFalse(ca.is_same_as(cc, use_common_name=True, use_custom=True)) 79 80 81 82 if __name__ == '__main__': 83 unittest.main()