ut_tx.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.ut_tx: TX unit test for the MMGen suite
  4. """
  5. import re
  6. from mmgen.common import *
  7. from mmgen.tx import MMGenTX
  8. from mmgen.txfile import MMGenTxFile
  9. from mmgen.rpc import rpc_init
  10. from mmgen.daemon import CoinDaemon
  11. from mmgen.protocol import init_proto
  12. class unit_tests:
  13. def tx(self,name,ut):
  14. qmsg(' Testing transaction objects')
  15. d = CoinDaemon('btc',test_suite=True)
  16. d.start()
  17. async def do():
  18. proto = init_proto('btc')
  19. tx = MMGenTX.New(proto=proto)
  20. tx.rpc = await rpc_init(proto=proto)
  21. run_session(do())
  22. d.stop()
  23. qmsg(' OK')
  24. return True
  25. def txfile(self,name,ut):
  26. qmsg(' Testing TX file operations')
  27. fns = ( # TODO: add altcoin TX files
  28. '0B8D5A[15.31789,14,tl=1320969600].rawtx',
  29. '0C7115[15.86255,14,tl=1320969600].testnet.rawtx',
  30. '460D4D-BCH[10.19764,tl=1320969600].rawtx',
  31. '25EFA3[2.34].testnet.rawtx',
  32. )
  33. for fn in fns:
  34. vmsg(f' parsing: {fn}')
  35. fpath = os.path.join('test','ref',fn)
  36. tx = MMGenTX.Unsigned(filename=fpath,quiet_open=True)
  37. f = MMGenTxFile(tx)
  38. fn_gen = f.make_filename()
  39. if g.debug_utf8:
  40. fn_gen = fn_gen.replace('-α','')
  41. assert fn_gen == fn, f'{fn_gen} != {fn}'
  42. text = f.format()
  43. # New in version 3.3: Support for the unicode legacy literal (u'value') was
  44. # reintroduced to simplify the maintenance of dual Python 2.x and 3.x codebases.
  45. # See PEP 414 for more information.
  46. chk = re.subn(r"\bu(['\"])",r"\1",open(fpath).read())[0] # remove Python2 'u' string prefixes from ref files
  47. diff = get_ndiff(chk,text)
  48. #print('\n'.join(diff))
  49. nLines = len([i for i in diff if i.startswith('-')])
  50. assert nLines in (0,1), f'{nLines} lines differ: only checksum line may differ'
  51. #break # FIXME - test BCH, testnet
  52. qmsg(' OK')
  53. return True