ts_misc.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.test_py_d.ts_misc: Miscellaneous test groups for the test.py test suite
  12. """
  13. import os,shutil
  14. from ..include.common import cfg
  15. from .ts_base import TestSuiteBase
  16. refdir = os.path.join('test','ref','ticker')
  17. class TestSuiteHelp(TestSuiteBase):
  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:.*LONG OPTIONS:')
  43. class TestSuiteScripts(TestSuiteBase):
  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-btc.json'),self.tmpdir)
  90. return 'ok'
  91. def ticker(self,args=[],expect_list=None,cached=True):
  92. t = self.spawn(
  93. f'mmnode-ticker',
  94. (['--cached-data'] if cached else []) + self.ticker_args + args )
  95. if expect_list:
  96. t.match_expect_list(expect_list)
  97. return t
  98. def ticker1(self):
  99. t = self.ticker(['--help'])
  100. t.expect('USAGE:')
  101. return t
  102. def ticker2(self):
  103. t = self.ticker(cached=False)
  104. if not cfg.skipping_deps:
  105. t.expect('Creating')
  106. t.expect('Creating')
  107. ret = t.expect(['proxy host could not be resolved','ProxyError'])
  108. t.req_exit_val = 3 if ret == 0 else 1
  109. return t
  110. def ticker3(self):
  111. return self.ticker(
  112. [],
  113. [
  114. 'USD BTC',
  115. 'BTC 23250.77 1.00000000 ETH 1659.66 0.07146397'
  116. ])
  117. def ticker4(self):
  118. return self.ticker(
  119. ['--wide','--add-columns=eurusd=x,inr-indian-rupee:79.5'],
  120. [
  121. r'EURUSD=X \(EUR/USD\) = 1.0642 USD ' +
  122. r'INR \(INDIAN RUPEE\) = 0.012579 USD',
  123. 'USD EURUSD=X INR BTC CHG_7d CHG_24h UPDATED',
  124. 'BITCOIN',
  125. r'ETHEREUM 1,659.66 1,559.5846 131,943.14 0.07146397 \+21.42 \+1.82',
  126. r'MONERO 158.97 149.3870 12,638.36 0.00684527 \+7.28 \+1.21 2022-08-02 18:25:59',
  127. r'INDIAN RUPEE 0.01 0.0118 1.00 0.00000054 -- --',
  128. ])
  129. def ticker5(self):
  130. shutil.copy2(os.path.join(refdir,'ticker-cfg.yaml'),self.nt_datadir)
  131. t = self.ticker(
  132. ['--wide','--adjust=-0.766'],
  133. [
  134. 'Adjusting prices by -0.77%',
  135. 'USD BTC CHG_7d CHG_24h UPDATED',
  136. r'LITECOIN 58.56 0.00252162 \+12.79 \+0.40 2022-08-02 18:25:59',
  137. r'MONERO 157.76 0.00679284 \+7.28 \+1.21'
  138. ])
  139. os.unlink(os.path.join(self.nt_datadir,'ticker-cfg.yaml'))
  140. return t
  141. def ticker6(self):
  142. t = self.ticker( ['--wide','--portfolio'], None )
  143. t.expect('No portfolio')
  144. t.req_exit_val = 1
  145. return t
  146. def ticker7(self): # demo
  147. shutil.copy2(os.path.join(refdir,'ticker-portfolio.yaml'),self.nt_datadir)
  148. t = self.ticker(
  149. ['--wide','--portfolio'],
  150. [
  151. 'USD BTC CHG_7d CHG_24h UPDATED',
  152. r'ETHEREUM 1,659.66 0.07146397 \+21.42 \+1.82 2022-08-02 18:25:59',
  153. 'CARDANO','ALGORAND',
  154. 'PORTFOLIO','BITCOIN','ETHEREUM','MONERO','CARDANO','ALGORAND','TOTAL'
  155. ])
  156. os.unlink(os.path.join(self.nt_datadir,'ticker-portfolio.yaml'))
  157. return t
  158. def ticker8(self):
  159. return self.ticker(
  160. ['--wide','--elapsed'],
  161. [
  162. 'USD BTC CHG_7d CHG_24h UPDATED',
  163. r'BITCOIN 23,250.77 1.00000000 \+11.15 \+0.89 10 minutes ago'
  164. ])
  165. def ticker9(self):
  166. shutil.copy2(
  167. os.path.join(refdir,'ticker-portfolio-bad.yaml'),
  168. os.path.join(self.nt_datadir,'ticker-portfolio.yaml') )
  169. t = self.ticker(
  170. ['--wide','--portfolio','--elapsed','--add-rows=fake-fakecoin:0.0123','--add-precision=2'],
  171. [
  172. 'USD BTC CHG_7d CHG_24h UPDATED',
  173. r'BITCOIN 23,250.7741 1.0000000000 \+11.15 \+0.89 10 minutes ago',
  174. r'FAKECOIN 81.3008 0.0034966927 -- -- --',
  175. r'\(no data for noc-nocoin\)',
  176. ])
  177. os.unlink(os.path.join(self.nt_datadir,'ticker-portfolio.yaml'))
  178. return t
  179. def ticker10(self):
  180. return self.ticker(
  181. ['XMR:17.234'],
  182. [
  183. r'XMR \(MONERO\) = 158.97 USD ' +
  184. 'Amount: 17.234 XMR',
  185. 'SPOT PRICE',
  186. 'BTC 0.11783441',
  187. 'XMR 17.23400000',
  188. 'GC=F',r'\^IXIC',
  189. ])
  190. def ticker11(self):
  191. return self.ticker(
  192. ['XMR:17.234:BTC'],
  193. [
  194. r'XMR \(MONERO\) = 158.97 USD ' +
  195. r'BTC \(BITCOIN\) = 23250.77 USD ' +
  196. 'Amount: 17.234 XMR',
  197. 'SPOT PRICE',
  198. 'XMR 17.23400000 BTC 0.11783441',
  199. ])
  200. def ticker12(self):
  201. return self.ticker(
  202. ['--adjust=1.23','--wide','XMR:17.234:BTC'],
  203. [
  204. r'XMR \(MONERO\) = 158.97 USD ' +
  205. r'BTC \(BITCOIN\) = 23,250.77 USD ' +
  206. 'Amount: 17.234 XMR',
  207. r'Adjusting prices by \+1.23%',
  208. 'SPOT PRICE ADJUSTED PRICE',
  209. 'MONERO 17.23400000 17.44597820 2022-08-02 18:25:59 ' +
  210. 'BITCOIN 0.11783441 0.11928377 2022-08-02 18:25:59',
  211. ])
  212. def ticker13(self):
  213. return self.ticker(
  214. ['-wE','-c','inr-indian-rupee:79.5','inr:200000:btc:0.1'],
  215. [
  216. 'Offer: 200,000 INR',
  217. 'Offered price differs from spot by -7.58%',
  218. 'SPOT PRICE OFFERED PRICE UPDATED',
  219. 'INDIAN RUPEE 200,000.00000000 184,843.65372424 -- ' +
  220. 'BITCOIN 0.10819955 0.10000000 10 minutes ago'
  221. ])
  222. def ticker14(self):
  223. shutil.copy2(os.path.join(refdir,'ticker-portfolio.yaml'),self.nt_datadir)
  224. t = self.ticker(
  225. ['--btc','--wide','--portfolio','--elapsed'],
  226. [
  227. 'PRICES',
  228. r'BITCOIN 23,368.86 \+6.05 -1.87 1 day 9 hours 2 minutes ago',
  229. 'PORTFOLIO',
  230. r'BITCOIN 28,850.44 \+6.05 -1.87 1.23456789'
  231. ])
  232. os.unlink(os.path.join(self.nt_datadir,'ticker-portfolio.yaml'))
  233. return t
  234. def ticker15(self):
  235. return self.ticker(
  236. ['--btc','--wide','--elapsed','-r','inr:79.5','btc:2:usd:45000'],
  237. [
  238. r'BTC \(BITCOIN\) = 23,368.86 USD',
  239. 'Offered price differs from spot by -3.72%',
  240. 'SPOT PRICE OFFERED PRICE UPDATED',
  241. 'BITCOIN 2.00000000 1.92563954 1 day 9 hours 2 minutes ago ' +
  242. 'US DOLLAR 46,737.71911598 45,000.00000000 --',
  243. ])
  244. def ticker16(self):
  245. return self.ticker(
  246. ['--wide','--elapsed','-c','eurusd=x,omr-omani-rial:2.59r'],
  247. [
  248. r'EURUSD=X \(EUR/USD\) = 1.0642 USD ' +
  249. r'OMR \(OMANI RIAL\) = 2.5900 USD',
  250. 'USD EURUSD=X OMR BTC CHG_7d CHG_24h UPDATED',
  251. r'BITCOIN 23,250.77 21,848.7527 8,977.1328 1.00000000 \+11.15 \+0.89 10 minutes ago',
  252. 'OMANI RIAL 2.59 2.4338 1.0000 0.00011139 -- -- --'
  253. ])
  254. def ticker17(self):
  255. # BGN pegged at 0.5113 EUR
  256. return self.ticker(
  257. ['--wide','--elapsed','-c','bgn-bulgarian-lev:0.5113r:eurusd=x'],
  258. [
  259. r'BGN \(BULGARIAN LEV\) = 0.54411 USD',
  260. 'USD BGN BTC CHG_7d CHG_24h UPDATED',
  261. 'BITCOIN 23,250.77 42,731.767 1.00000000',
  262. 'BULGARIAN LEV 0.54 1.000 0.00002340',
  263. ])