-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_helpers.py
85 lines (67 loc) · 2.93 KB
/
test_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import json
from datetime import datetime
from unittest.mock import patch
import pytest
import unittest.mock
from degiro_connector.trading.models.trading_pb2 import (
Order
)
from helpers import get_instrument_and_order_params, OrderParams
def test_get_instrument_and_order_params_no_order(monkeypatch):
def mock_json_load(*args, **kwargs):
return {
"instruments": {},
"buy_amounts": {},
"account_currency": "USD",
"conversion_currency": "USD",
}
monkeypatch.setattr("json.load", mock_json_load)
result = get_instrument_and_order_params()
assert isinstance(result, OrderParams)
assert result.execute_order is False
def test_get_instrument_and_order_params_same_currency():
def mock_json_load(*args, **kwargs):
return {
"instruments": {"1": "123456"},
"buy_amounts": {"1": "100"},
"account_currency": "USD",
"conversion_currency": "USD",
}
with unittest.mock.patch("helpers.current_datetime") as mock_current_datetime:
mock_current_datetime.return_value = datetime(2023, 4, 1)
with unittest.mock.patch("json.load", mock_json_load):
result = get_instrument_and_order_params()
expected_result = OrderParams("123456", Order.OrderType.LIMIT, 100.0, True)
assert result == expected_result
@patch("helpers.convert")
def test_get_instrument_and_order_params_different_currency(mock_convert):
def mock_json_load(*args, **kwargs):
return {
"instruments": {"1": "123456"},
"buy_amounts": {"1": "100"},
"account_currency": "EUR",
"conversion_currency": "USD",
}
with unittest.mock.patch("helpers.current_datetime") as mock_current_datetime:
mock_current_datetime.return_value = datetime(2023, 4, 1)
with unittest.mock.patch("json.load", mock_json_load):
mock_convert.return_value = json.dumps({"converted": True, "amount": "85.0"})
result = get_instrument_and_order_params()
expected_result = OrderParams("123456", Order.OrderType.LIMIT, 85.0, True)
assert result == expected_result
def test_get_instrument_and_order_params_conversion_failed():
def mock_json_load(*args, **kwargs):
return {
"instruments": {"1": "123456"},
"buy_amounts": {"1": "100"},
"account_currency": "EUR",
"conversion_currency": "USD",
}
def mock_convert(*args, **kwargs):
return json.dumps({"converted": False})
with unittest.mock.patch("helpers.current_datetime") as mock_current_datetime:
mock_current_datetime.return_value = datetime(2023, 4, 1)
with unittest.mock.patch("json.load", mock_json_load):
with unittest.mock.patch("helpers.convert", mock_convert):
result = get_instrument_and_order_params()
assert result is None