chainlib

Generic blockchain access library and tooling
Info | Log | Files | Refs | README | LICENSE

test_src.py (2227B)


      1 # standard imports
      2 import unittest
      3 
      4 # local imports
      5 from chainlib.src import Src
      6 from chainlib.tx import (
      7         Tx,
      8         TxResult,
      9         )
     10 from chainlib.block import Block
     11 from chainlib.status import Status
     12 
     13 class TestBlock(Block):
     14 
     15     def load_src(self, dialect_filter=None):
     16         self.number = self.src['number']
     17 
     18 
     19 class TestTx(Tx):
     20 
     21     def load_src(self, dialect_filter=None):
     22         self.foo = self.src['foo']
     23 
     24 
     25 class TestTxResult(TxResult):
     26 
     27     def load_src(self, dialect_filter=None):
     28         self.bar = self.src['bar']
     29 
     30 
     31 class TestSrc(unittest.TestCase):
     32 
     33     def test_basic(self):
     34         v = {'foo': 'bar'}
     35         src = Src(v)
     36         self.assertEqual(src.src, v)
     37 
     38 
     39     def test_block_basic(self):
     40         v = {'number': 42}
     41         block = TestBlock(v)
     42         self.assertEqual(block.src, v)
     43         self.assertEqual(block.number, 42)
     44 
     45 
     46     def test_tx_basic(self):
     47         v = {'foo': 'bar'}
     48         tx = TestTx(v)
     49         self.assertEqual(tx.src, v)
     50         self.assertEqual(tx.foo, 'bar')
     51 
     52         v = {'bar': 'baz'}
     53         result = TestTxResult(v)
     54         self.assertEqual(result.src, v)
     55         self.assertEqual(result.status, Status.UNKNOWN)
     56         self.assertEqual(result.bar, 'baz')
     57 
     58 
     59     def test_apply(self):
     60         vt = {'foo': 'bar'}
     61         tx = TestTx(vt)
     62 
     63         vb = {'number': 42}
     64         block = TestBlock(vb)
     65 
     66         vr = {'bar': 'baz'}
     67         result = TestTxResult(vr)
     68 
     69         tx.apply_block(block)
     70 
     71         tx.apply_result(result)
     72 
     73         self.assertEqual(tx.src, vt)
     74         self.assertEqual(tx.status, Status.UNKNOWN)
     75         self.assertEqual(tx.status_name, Status.UNKNOWN.name)
     76         self.assertEqual(tx.foo, 'bar')
     77         self.assertEqual(tx.result.bar, 'baz')
     78 
     79         tx = TestTx(vt, block=block, result=result)
     80 
     81         self.assertEqual(tx.src, vt)
     82         self.assertEqual(tx.status, Status.UNKNOWN)
     83         self.assertEqual(tx.status_name, Status.UNKNOWN.name)
     84         self.assertEqual(tx.foo, 'bar')
     85         self.assertEqual(tx.result.bar, 'baz')
     86 
     87 
     88     def test_from(self):
     89         v = {'foo': 'bar'}
     90         tx = TestTx.from_src(v)
     91         self.assertEqual(tx.src, v)
     92         self.assertEqual(tx.foo, 'bar')
     93 
     94 
     95 if __name__ == '__main__':
     96     unittest.main()