ct_misc.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line cryptocurrency wallet
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-node-tools
  9. # https://gitlab.com/mmgen/mmgen-node-tools
  10. """
  11. test.cmdtest_d.ct_misc: Miscellaneous test groups for the cmdtest.py test suite
  12. """
  13. import os,shutil
  14. from ..include.common import cfg
  15. from .ct_base import CmdTestBase
  16. refdir = os.path.join('test','ref','ticker')
  17. class CmdTestHelp(CmdTestBase):
  18. 'help, info and usage screens'
  19. networks = ('btc','ltc','bch')
  20. tmpdir_nums = []
  21. passthru_opts = ('daemon_data_dir','rpc_port','coin','testnet')
  22. cmd_group = (
  23. ('version', (1,'version message',[])),
  24. ('helpscreens', (1,'help screens', [])),
  25. ('longhelpscreens', (1,'help screens (--longhelp)',[])),
  26. )
  27. color = True
  28. def version(self):
  29. t = self.spawn(f'mmnode-netrate',['--version'])
  30. t.expect('MMNODE-NETRATE version')
  31. return t
  32. def helpscreens(self,arg='--help',scripts=(),expect='USAGE:.*OPTIONS:'):
  33. scripts = list(scripts) or [s for s in os.listdir('cmds') if s.startswith('mmnode-')]
  34. for s in sorted(scripts):
  35. t = self.spawn(s,[arg],extra_desc=f'({s})')
  36. t.expect(expect,regex=True)
  37. t.read()
  38. t.ok()
  39. t.skip_ok = True
  40. return t
  41. def longhelpscreens(self):
  42. return self.helpscreens(arg='--longhelp',expect='USAGE:.*GLOBAL OPTIONS:')
  43. class CmdTestScripts(CmdTestBase):
  44. 'scripts not requiring a coin daemon'
  45. networks = ('btc',)
  46. tmpdir_nums = [2]
  47. passthru_opts = ()
  48. color = True
  49. cmd_group_in = (
  50. ('subgroup.ticker_setup', []),
  51. ('subgroup.ticker', ['ticker_setup']),
  52. )
  53. cmd_subgroups = {
  54. 'ticker_setup': (
  55. "setup for 'ticker' subgroup",
  56. ('ticker_setup', 'ticker setup'),
  57. ),
  58. 'ticker': (
  59. "'mmnode-ticker' script",
  60. ('ticker1', 'ticker [--help)'),
  61. ('ticker2', 'ticker (bad proxy)'),
  62. ('ticker3', 'ticker [--cached-data]'),
  63. ('ticker4', 'ticker [--cached-data --wide]'),
  64. ('ticker5', 'ticker [--cached-data --wide --adjust=-0.766] (usr cfg file)'),
  65. ('ticker6', 'ticker [--cached-data --wide --portfolio] (missing portfolio)'),
  66. ('ticker7', 'ticker [--cached-data --wide --portfolio]'),
  67. ('ticker8', 'ticker [--cached-data --wide --elapsed]'),
  68. ('ticker9', 'ticker [--cached-data --wide --portfolio --elapsed --add-rows=fake-fakecoin:0.0123 --add-precision=2]'),
  69. ('ticker10', 'ticker [--cached-data xmr:17.234]'),
  70. ('ticker11', 'ticker [--cached-data xmr:17.234:btc]'),
  71. ('ticker12', 'ticker [--cached-data --adjust=1.23 xmr:17.234:btc]'),
  72. ('ticker13', 'ticker [--cached-data --wide --elapsed -c inr-indian-rupee:79.5 inr:200000:btc:0.1]'),
  73. ('ticker14', 'ticker [--cached-data --wide --btc]'),
  74. ('ticker15', 'ticker [--cached-data --wide --btc btc:2:usd:45000]'),
  75. ('ticker16', 'ticker [--cached-data --wide --elapsed -c eur,omr-omani-rial:2.59r'),
  76. ('ticker17', 'ticker [--cached-data --wide --elapsed -c bgn-bulgarian-lev:0.5113r:eur'),
  77. )
  78. }
  79. @property
  80. def ticker_args(self):
  81. return [ f'--cachedir={self.tmpdir}', '--proxy=http://asdfzxcv:32459' ]
  82. @property
  83. def nt_datadir(self):
  84. return os.path.join( cfg.data_dir_root, 'node_tools' )
  85. def ticker_setup(self):
  86. self.spawn('',msg_only=True)
  87. shutil.copy2(os.path.join(refdir,'ticker.json'),self.tmpdir)
  88. shutil.copy2(os.path.join(refdir,'ticker-finance.json'),self.tmpdir)
  89. shutil.copy2(os.path.join(refdir,'ticker-finance-history.json'),self.tmpdir)
  90. shutil.copy2(os.path.join(refdir,'ticker-btc.json'),self.tmpdir)
  91. return 'ok'
  92. def ticker(self, args=[], expect_list=None, cached=True, exit_val=None):
  93. t = self.spawn(
  94. f'mmnode-ticker',
  95. (['--cached-data'] if cached else []) + self.ticker_args + args,
  96. exit_val = exit_val)
  97. if expect_list:
  98. t.match_expect_list(expect_list)
  99. return t
  100. def ticker1(self):
  101. t = self.ticker(['--help'])
  102. t.expect('USAGE:')
  103. return t
  104. def ticker2(self):
  105. t = self.ticker(cached=False)
  106. if not cfg.skipping_deps:
  107. t.expect('Creating')
  108. t.expect('Creating')
  109. ret = t.expect(['proxy host could not be resolved', 'ProxyError'])
  110. t.exit_val = 1 if ret else 3
  111. return t
  112. def ticker3(self):
  113. return self.ticker(
  114. [],
  115. [
  116. 'USD BTC',
  117. 'BTC 23250.77 1.00000000 ETH 1659.66 0.07138094'
  118. ])
  119. def ticker4(self):
  120. return self.ticker(
  121. ['--widest','--add-columns=eurusd=x,inr-indian-rupee:79.5'],
  122. [
  123. r'EURUSD=X \(EUR/USD\) = 1.0642 USD ' +
  124. r'INR \(INDIAN RUPEE\) = 0.012579 USD',
  125. 'USD EURUSD=X INR BTC CHG_1y CHG_30d CHG_7d CHG_24h UPDATED',
  126. 'BITCOIN',
  127. r'ETHEREUM 1,659.66 1,559.5846 131,943.14 0.07138094 \+36.41 \+29.99 \+21.42 \+1.82',
  128. r'MONERO 158.97 149.3870 12,638.36 0.00683732 \+12.38 \+10.19 \+7.28 \+1.21 2022-08-02 18:25:59',
  129. r'S&P 500 4,320.06 4,059.5604 343,444.77 0.18580285 -1.71 \+12.93 \+9.05 -0.23',
  130. r'INDIAN RUPEE 0.01 0.0118 1.00 0.00000054 -- -- -- --',
  131. ])
  132. def ticker5(self):
  133. shutil.copy2(os.path.join(refdir,'ticker-cfg.yaml'),self.nt_datadir)
  134. t = self.ticker(
  135. ['--wide','--adjust=-0.766'],
  136. [
  137. 'Adjusting prices by -0.77%',
  138. 'USD BTC CHG_7d CHG_24h UPDATED',
  139. r'LITECOIN 58.56 0.00251869 \+12.79 \+0.40 2022-08-02 18:25:59',
  140. r'MONERO 157.76 0.00678495 \+7.28 \+1.21'
  141. ])
  142. os.unlink(os.path.join(self.nt_datadir,'ticker-cfg.yaml'))
  143. return t
  144. def ticker6(self):
  145. t = self.ticker(['--wide','--portfolio'], None, exit_val=1)
  146. t.expect('No portfolio')
  147. return t
  148. def ticker7(self): # demo
  149. shutil.copy2(os.path.join(refdir,'ticker-portfolio.yaml'),self.nt_datadir)
  150. t = self.ticker(
  151. ['--wide','--portfolio'],
  152. [
  153. 'USD BTC CHG_7d CHG_24h UPDATED',
  154. r'ETHEREUM 1,659.66 0.07138094 \+21.42 \+1.82 2022-08-02 18:25:59',
  155. 'CARDANO','ALGORAND',
  156. 'PORTFOLIO','BITCOIN','ETHEREUM','MONERO','CARDANO','ALGORAND','TOTAL'
  157. ])
  158. os.unlink(os.path.join(self.nt_datadir,'ticker-portfolio.yaml'))
  159. return t
  160. def ticker8(self):
  161. return self.ticker(
  162. ['--wide','--elapsed'],
  163. [
  164. 'USD BTC CHG_7d CHG_24h UPDATED',
  165. r'BITCOIN 23,250.77 1.00000000 \+11.15 \+0.89 10 minutes ago'
  166. ])
  167. def ticker9(self):
  168. shutil.copy2(
  169. os.path.join(refdir,'ticker-portfolio-bad.yaml'),
  170. os.path.join(self.nt_datadir,'ticker-portfolio.yaml') )
  171. t = self.ticker(
  172. ['--wide','--portfolio','--elapsed','--add-rows=fake-fakecoin:0.0123','--add-precision=2'],
  173. [
  174. 'USD BTC CHG_7d CHG_24h UPDATED',
  175. r'BITCOIN 23,250.7741 1.0000000000 \+11.15 \+0.89 10 minutes ago',
  176. r'FAKECOIN 81.3008 0.0034966927 -- -- --',
  177. r'\(no data for noc-nocoin\)',
  178. ])
  179. os.unlink(os.path.join(self.nt_datadir,'ticker-portfolio.yaml'))
  180. return t
  181. def ticker10(self):
  182. return self.ticker(
  183. ['XMR:17.234'],
  184. [
  185. r'XMR \(MONERO\) = 158.97 USD ' +
  186. 'Amount: 17.234 XMR',
  187. 'SPOT PRICE',
  188. 'BTC 0.11783441',
  189. 'XMR 17.23400000',
  190. 'GC=F',r'\^IXIC',
  191. ])
  192. def ticker11(self):
  193. return self.ticker(
  194. ['XMR:17.234:BTC'],
  195. [
  196. r'XMR \(MONERO\) = 158.97 USD ' +
  197. r'BTC \(BITCOIN\) = 23250.77 USD ' +
  198. 'Amount: 17.234 XMR',
  199. 'SPOT PRICE',
  200. 'XMR 17.23400000 BTC 0.11783441',
  201. ])
  202. def ticker12(self):
  203. return self.ticker(
  204. ['--adjust=1.23','--wide','XMR:17.234:BTC'],
  205. [
  206. r'XMR \(MONERO\) = 158.97 USD ' +
  207. r'BTC \(BITCOIN\) = 23,250.77 USD ' +
  208. 'Amount: 17.234 XMR',
  209. r'Adjusting prices by \+1.23%',
  210. 'SPOT PRICE ADJUSTED PRICE',
  211. 'MONERO 17.23400000 17.44597820 2022-08-02 18:25:59 ' +
  212. 'BITCOIN 0.11783441 0.11928377 2022-08-02 18:25:59',
  213. ])
  214. def ticker13(self):
  215. return self.ticker(
  216. ['-wE','-c','inr-indian-rupee:79.5','inr:200000:btc:0.1'],
  217. [
  218. 'Offer: 200,000 INR',
  219. 'Offered price differs from spot by -7.58%',
  220. 'SPOT PRICE OFFERED PRICE UPDATED',
  221. 'INDIAN RUPEE 200,000.00000000 184,843.65372424 -- ' +
  222. 'BITCOIN 0.10819955 0.10000000 10 minutes ago'
  223. ])
  224. def ticker14(self):
  225. shutil.copy2(os.path.join(refdir,'ticker-portfolio.yaml'),self.nt_datadir)
  226. t = self.ticker(
  227. ['--btc','--wide','--portfolio','--elapsed'],
  228. [
  229. 'PRICES',
  230. r'BITCOIN 23,368.86 \+6.05 -1.87 1 day 9 hours 2 minutes ago',
  231. 'PORTFOLIO',
  232. r'BITCOIN 28,850.44 \+6.05 -1.87 1.23456789'
  233. ])
  234. os.unlink(os.path.join(self.nt_datadir,'ticker-portfolio.yaml'))
  235. return t
  236. def ticker15(self):
  237. return self.ticker(
  238. ['--btc','--wide','--elapsed','-r','inr:79.5','btc:2:usd:45000'],
  239. [
  240. r'BTC \(BITCOIN\) = 23,368.86 USD',
  241. 'Offered price differs from spot by -3.72%',
  242. 'SPOT PRICE OFFERED PRICE UPDATED',
  243. 'BITCOIN 2.00000000 1.92563954 1 day 9 hours 2 minutes ago ' +
  244. 'US DOLLAR 46,737.71911598 45,000.00000000 --',
  245. ])
  246. def ticker16(self):
  247. return self.ticker(
  248. ['--wide','--elapsed','-c','eurusd=x,omr-omani-rial:2.59r'],
  249. [
  250. r'EURUSD=X \(EUR/USD\) = 1.0642 USD ' +
  251. r'OMR \(OMANI RIAL\) = 2.5900 USD',
  252. 'USD EURUSD=X OMR BTC CHG_7d CHG_24h UPDATED',
  253. r'BITCOIN 23,250.77 21,848.7527 8,977.1328 1.00000000 \+11.15 \+0.89 10 minutes ago',
  254. 'OMANI RIAL 2.59 2.4338 1.0000 0.00011139 -- -- --'
  255. ])
  256. def ticker17(self):
  257. # BGN pegged at 0.5113 EUR
  258. return self.ticker(
  259. ['--wide','--elapsed','-c','bgn-bulgarian-lev:0.5113r:eurusd=x'],
  260. [
  261. r'BGN \(BULGARIAN LEV\) = 0.54411 USD',
  262. 'USD BGN BTC CHG_7d CHG_24h UPDATED',
  263. 'BITCOIN 23,250.77 42,731.767 1.00000000',
  264. 'BULGARIAN LEV 0.54 1.000 0.00002340',
  265. ])