class WebMockTestCase(unittest.TestCase):
import time
+ def market_args(self, debug=False, quiet=False):
+ return type('Args', (object,), { "debug": debug, "quiet": quiet })()
+
def setUp(self):
super(WebMockTestCase, self).setUp()
self.wm = requests_mock.Mocker()
self.ccxt = mock.Mock(spec=market.ccxt.poloniexE)
def test_values(self):
- m = market.Market(self.ccxt)
+ m = market.Market(self.ccxt, self.market_args())
self.assertEqual(self.ccxt, m.ccxt)
self.assertFalse(m.debug)
self.assertEqual(m, m.balances.market)
self.assertEqual(m, m.ccxt._market)
- m = market.Market(self.ccxt, debug=True)
+ m = market.Market(self.ccxt, self.market_args(debug=True))
self.assertTrue(m.debug)
- m = market.Market(self.ccxt, debug=False)
+ m = market.Market(self.ccxt, self.market_args(debug=False))
self.assertFalse(m.debug)
+ with mock.patch("market.ReportStore") as report_store:
+ with self.subTest(quiet=False):
+ m = market.Market(self.ccxt, self.market_args(quiet=False))
+ report_store.assert_called_with(m, verbose_print=True)
+ with self.subTest(quiet=True):
+ m = market.Market(self.ccxt, self.market_args(quiet=True))
+ report_store.assert_called_with(m, verbose_print=False)
+
@mock.patch("market.ccxt")
def test_from_config(self, ccxt):
with mock.patch("market.ReportStore"):
ccxt.poloniexE.return_value = self.ccxt
self.ccxt.session.request.return_value = "response"
- m = market.Market.from_config({"key": "key", "secred": "secret"})
+ m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args())
self.assertEqual(self.ccxt, m.ccxt)
m.report.log_http_request.assert_called_with('GET', 'URL', 'data',
'headers', 'response')
- m = market.Market.from_config({"key": "key", "secred": "secret"}, debug=True)
+ m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args(debug=True))
self.assertEqual(True, m.debug)
def test_get_tickers(self):
market.NotSupported
]
- m = market.Market(self.ccxt)
+ m = market.Market(self.ccxt, self.market_args())
self.assertEqual("tickers", m.get_tickers())
self.assertEqual("tickers", m.get_tickers())
self.ccxt.fetch_tickers.assert_called_once()
"ETH/ETC": { "bid": 1, "ask": 3 },
"XVG/ETH": { "bid": 10, "ask": 40 },
}
- m = market.Market(self.ccxt)
+ m = market.Market(self.ccxt, self.market_args())
ticker = m.get_ticker("ETH", "ETC")
self.assertEqual(1, ticker["bid"])
market.ExchangeError("foo"),
]
- m = market.Market(self.ccxt)
+ m = market.Market(self.ccxt, self.market_args())
ticker = m.get_ticker("ETH", "ETC")
self.ccxt.fetch_ticker.assert_called_with("ETH/ETC")
self.assertIsNone(ticker)
def test_fetch_fees(self):
- m = market.Market(self.ccxt)
+ m = market.Market(self.ccxt, self.market_args())
self.ccxt.fetch_fees.return_value = "Foo"
self.assertEqual("Foo", m.fetch_fees())
self.ccxt.fetch_fees.assert_called_once()
get_ticker.side_effect = _get_ticker
with mock.patch("market.ReportStore"):
- m = market.Market(self.ccxt)
+ m = market.Market(self.ccxt, self.market_args())
self.ccxt.fetch_all_balances.return_value = {
"USDT": {
"exchange_free": D("10000.0"),
(False, 12), (True, 12)]:
with self.subTest(sleep=sleep, debug=debug), \
mock.patch("market.ReportStore"):
- m = market.Market(self.ccxt, debug=debug)
+ m = market.Market(self.ccxt, self.market_args(debug=debug))
order_mock1 = mock.Mock()
order_mock2 = mock.Mock()
for debug in [True, False]:
with self.subTest(debug=debug),\
mock.patch("market.ReportStore"):
- m = market.Market(self.ccxt, debug=debug)
+ m = market.Market(self.ccxt, self.market_args(debug=debug))
value_from = portfolio.Amount("BTC", "1.0")
value_from.linked_to = portfolio.Amount("ETH", "10.0")
def test_store_report(self):
file_open = mock.mock_open()
- m = market.Market(self.ccxt, user_id=1)
+ m = market.Market(self.ccxt, self.market_args(), user_id=1)
with self.subTest(file=None),\
mock.patch.object(m, "report") as report,\
mock.patch("market.open", file_open):
report.reset_mock()
file_open = mock.mock_open()
- m = market.Market(self.ccxt, report_path="present", user_id=1)
+ m = market.Market(self.ccxt, self.market_args(), report_path="present", user_id=1)
with self.subTest(file="present"),\
mock.patch("market.open", file_open),\
mock.patch.object(m, "report") as report,\
mock.patch.object(market, "datetime") as time_mock:
time_mock.now.return_value = datetime.datetime(2018, 2, 25)
+ report.print_logs = [[time_mock.now(), "Foo"], [time_mock.now(), "Bar"]]
report.to_json.return_value = "json_content"
m.store_report()
file_open.assert_any_call("present/2018-02-25T00:00:00_1.json", "w")
- file_open().write.assert_called_once_with("json_content")
+ file_open.assert_any_call("present/2018-02-25T00:00:00_1.log", "w")
+ file_open().write.assert_any_call("json_content")
+ file_open().write.assert_any_call("Foo\nBar")
m.report.to_json.assert_called_once_with()
report.merge.assert_called_with(store.Portfolio.report)
report.reset_mock()
- m = market.Market(self.ccxt, report_path="error", user_id=1)
+ m = market.Market(self.ccxt, self.market_args(), report_path="error", user_id=1)
with self.subTest(file="error"),\
mock.patch("market.open") as file_open,\
mock.patch.object(m, "report") as report,\
self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;")
def test_print_orders(self):
- m = market.Market(self.ccxt)
+ m = market.Market(self.ccxt, self.market_args())
with mock.patch.object(m.report, "log_stage") as log_stage,\
mock.patch.object(m.balances, "fetch_balances") as fetch_balances,\
mock.patch.object(m, "prepare_trades") as prepare_trades,\
prepare_orders.assert_called_with(compute_value="average")
def test_print_balances(self):
- m = market.Market(self.ccxt)
+ m = market.Market(self.ccxt, self.market_args())
with mock.patch.object(m.balances, "in_currency") as in_currency,\
mock.patch.object(m.report, "log_stage") as log_stage,\
@mock.patch("market.ReportStore.log_error")
@mock.patch("market.Market.store_report")
def test_process(self, store_report, log_error, process):
- m = market.Market(self.ccxt)
+ m = market.Market(self.ccxt, self.market_args())
with self.subTest(before=False, after=False):
m.process(None)
self.assertEqual(3, len(report_store1.logs))
self.assertEqual(["1", "2", "3"], list(map(lambda x: x["stage"], report_store1.logs)))
+ self.assertEqual(6, len(report_store1.print_logs))
def test_print_log(self):
report_store = market.ReportStore(self.m)
with self.subTest(verbose=True),\
+ mock.patch.object(store, "datetime") as time_mock,\
mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+ time_mock.now.return_value = datetime.datetime(2018, 2, 25, 2, 20, 10)
report_store.set_verbose(True)
report_store.print_log("Coucou")
report_store.print_log(portfolio.Amount("BTC", 1))
- self.assertEqual(stdout_mock.getvalue(), "Coucou\n1.00000000 BTC\n")
+ self.assertEqual(stdout_mock.getvalue(), "2018-02-25 02:20:10: Coucou\n2018-02-25 02:20:10: 1.00000000 BTC\n")
with self.subTest(verbose=False),\
mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
main.process("config", 1, "report_path", args_mock)
market_mock.from_config.assert_has_calls([
- mock.call("config", debug="debug", user_id=1, report_path="report_path"),
+ mock.call("config", args_mock, user_id=1, report_path="report_path"),
mock.call().process("action", before="before", after="after"),
])
def test_method_arguments(self):
ccxt = mock.Mock(spec=market.ccxt.poloniexE)
- m = market.Market(ccxt)
+ m = market.Market(ccxt, self.market_args())
processor = market.Processor(m)