ut_BlocksInfo.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.nt_BlocksInfo: BlocksInfo unit test for the MMGen Node Tools suite
  4. """
  5. from mmgen_node_tools.BlocksInfo import BlocksInfo
  6. from ..include.common import vmsg
  7. tip = 50000
  8. range_vecs = (
  9. # First Last FromTip nBlocks Step First Last BlockList
  10. ( (), (), (tip, tip, None) ),
  11. ( (199,2,37), (), (None, None, [199,2,37]) ),
  12. ( '0', (0, 0, None, None, None), (0, 0, None) ),
  13. ( '0-0', (0, 0, None, None, None), (0, 0, None) ),
  14. (f'-{tip}', (0, 0, tip, None, None), (0, 0, None) ),
  15. ( '0-10', (0, 10, None, None, None), (0, 10, None) ),
  16. ( '0+10', (0, 9, None, 10, None), (0, 9, None) ),
  17. ( '0+10+2', (0, 9, None, 10, 2 ), (0, 9, [0,2,4,6,8]) ),
  18. ( '1', (1, 1, None, None, None), (1, 1, None) ),
  19. ( '1-1', (1, 1, None, None, None), (1, 1, None) ),
  20. ( '1-10', (1, 10, None, None, None), (1, 10, None) ),
  21. ( '1+10', (1, 10, None, 10, None), (1, 10, None) ),
  22. ( '1+10+2', (1, 10, None, 10, 2 ), (1, 10, [1,3,5,7,9]) ),
  23. ( '+1', (tip, tip, None, 1, None), (tip, tip, None) ),
  24. ( '+10', (tip-9, tip, None, 10, None), (tip-9, tip, None) ),
  25. ( '-1', (tip-1, tip-1, 1, None, None), (tip-1, tip-1, None) ),
  26. ( '-1+1', (tip-1, tip-1, 1, 1, None), (tip-1, tip-1, None) ),
  27. ( '-1+2', (tip-1, tip, 1, 2, None), (tip-1, tip, None) ),
  28. ( '-10', (tip-10, tip-10, 10, None, None), (tip-10, tip-10, None) ),
  29. ( '-10+11', (tip-10, tip, 10, 11, None), (tip-10, tip, None) ),
  30. ( '-10+11+2', (tip-10, tip, 10, 11, 2 ), (tip-10, tip, list(range(tip-10,tip+1,2))) ),
  31. ( 'cUr', (tip, tip, None, None, None), (tip, tip, None) ),
  32. ( 'cur-cUR', (tip, tip, None, None, None), (tip, tip, None) ),
  33. ( '0-cur', (0, tip, None, None, None), (0, tip, None) ),
  34. (f'{tip-1}-cur', (tip-1, tip, None, None, None), (tip-1, tip, None) ),
  35. ( '0-cur+3000', (0, tip, None, None, 3000 ), (0, tip, list(range(0,tip+1,3000))) ),
  36. ( '+1440+144', (tip-1439, tip, None, 1440, 144 ), (tip-1439, tip, list(range(tip-1439,tip+1,144))) ),
  37. ( '+144*10+12*12', (tip-1439, tip, None, 1440, 144 ), (tip-1439, tip, list(range(tip-1439,tip+1,144))) ),
  38. )
  39. full_set = ['aa','bbb','ccc_P2','ddddd','eeeeee','ffffffff','gg']
  40. dfl_set = ['aa','ddddd','ffffffff']
  41. fields_vecs = (
  42. ( 'Ccc_P2', ['ccc_P2'] ),
  43. ( '+CCC_P2', ['aa','ccc_P2','ddddd','ffffffff'] ),
  44. ( '+Aa', dfl_set ),
  45. ( '+ddDDD,FffffffF', dfl_set ),
  46. ( '+bBb', ['aa','bbb','ddddd','ffffffff'] ),
  47. ( '-ddddd', ['aa','ffffffff'] ),
  48. ( '-DDDDD,fFffffff', ['aa'] ),
  49. ( '-ffffffff,AA,DdDdD', [] ),
  50. ( '+aa,gG,ccC_P2', ['aa','ccc_P2','ddddd','ffffffff','gg'] ),
  51. ( '+BBB,gg-dDddD,fFffffff', ['aa','bbb','gg'] ),
  52. ( '-dDddD,fFffffff+BBB,gg', ['aa','bbb','gg'] ),
  53. ( 'aLL-Ccc_P2', [e for e in full_set if e != 'ccc_P2'] ),
  54. ( 'All-dDddd,aa', [e for e in full_set if e not in ('aa','ddddd')] ),
  55. )
  56. class dummyRPC:
  57. blockcount = tip
  58. def info(self,arg):
  59. return True
  60. class proto:
  61. class coin_amt:
  62. satoshi = 0.00000001
  63. class dummyCfg:
  64. fields = None
  65. stats = None
  66. miner_info = None
  67. header_info = None
  68. full_stats = None
  69. coin = 'BTC'
  70. class unit_tests:
  71. def parse_field(self,name,ut):
  72. vmsg('{:28} => {}'.format('FULL SET:',full_set))
  73. vmsg('{:28} => {}'.format('DFL SET: ',dfl_set))
  74. b = BlocksInfo
  75. for opt,chk in fields_vecs:
  76. ret = b.parse_cslist(opt,full_set,dfl_set,'field')
  77. vmsg(f'{opt:28} => {ret}')
  78. assert ret == chk, f'{ret} != {chk}'
  79. return True
  80. def parse_rangespec(self,name,ut):
  81. b = BlocksInfo(dummyCfg(),None,dummyRPC())
  82. def test(spec,chk,foo):
  83. ret = b.parse_rangespec(spec)
  84. vmsg(f'{spec:13} => {BlocksInfo.range_data(*chk)}')
  85. assert ret == chk, f'{ret} != {chk}'
  86. for vec in range_vecs:
  87. if vec[1]:
  88. test(*vec)
  89. return True
  90. def parse_cmd_args(self,name,ut):
  91. def test(spec,foo,chk):
  92. b = BlocksInfo(
  93. dummyCfg(),
  94. spec if type(spec) == tuple else [spec],
  95. dummyRPC() )
  96. ret = (b.first,b.last,b.block_list)
  97. vmsg('{:13} => {}'.format(
  98. (repr(spec) if type(spec) == tuple else spec),
  99. chk ))
  100. assert ret == chk, f'{ret} != {chk}'
  101. for vec in range_vecs:
  102. test(*vec)
  103. return True