def setUp(self): with mock.patch.object(Client, 'get_credentials', return_value=True): self.client = Client( ("http://mydomain.com", "http://mystreamingdomain.com"), "my_account", "my_token" )
def test_custom_json_options(self, m): with mock.patch.object(Client, 'get_credentials', return_value=True): c = Client(("http://mydomain.com", "http://mystreamingdomain.com"), "my_account", "my_token") c.json_options['parse_float'] = Decimal m.get(requests_mock.ANY, text=json.dumps({'float': 1.01})) r = c._Client__call('http://www.example.com/') assert isinstance(r['float'], Decimal)
def test_call_stream_pass(self, m): """Ensure that successful HTTP streaming response codes pass.""" with mock.patch.object(Client, 'get_credentials', return_value=True): c = Client(("http://mydomain.com", "http://mystreamingdomain.com"), "my_account", "my_token") c.session = requests.Session() for status_code in [200, 201, 202, 301, 302]: m.get(requests_mock.ANY, status_code=status_code) c._Client__call_stream(uri="http://example.com", params={"test": "test"}, method="get") m.post(requests_mock.ANY, status_code=status_code) c._Client__call_stream(uri="http://example.com", params={"test": "test"}, method="post")
class IntegrationTestCase(unittest.TestCase): # Keep this as it will be share between all tests cases, prevent to over # use as this literaly creates new users (I expext the use to be wipeout) client = Client(SANDBOX) user = client.create_account(currency="GBP") client.account_id = user['accountId'] def build_order(self, immediate=False): """ Build an order to be used with create_order. Building an order is commonly required in the integration tests, so this makes it easy. Parameters ---------- immediate: bool Whether to place an order that will be met immediately or not; this is achieved by examining current prices and bidding well below for non-immediate or by placing a market order for immediate. Returns an Order """ if immediate: return Order( instrument="GBP_USD", units=1, side="buy", type="market" ) expiry = datetime.utcnow() + timedelta(minutes=1) prices = self.client.get_prices("GBP_USD", False) price = prices['prices'][0] at = round(price['bid'] * 0.9, 5) # order must not be met straight away, otherwise we can't get it back return Order( instrument="GBP_USD", units=1, side="buy", type="limit", price=at, expiry=expiry.isoformat() )
def test_call_stream_fail(self, m): """Ensure that failure HTTP streaming response codes fail.""" with mock.patch.object(Client, 'get_credentials', return_value=True): c = Client(("http://mydomain.com", "http://mystreamingdomain.com"), "my_account", "my_token") c.session = requests.Session() status_codes = [400, 401, 403, 404, 500] caught = 0 for status_code in status_codes: m.get(requests_mock.ANY, text=json.dumps({'message': 'test'}), status_code=400) try: c._Client__call_stream(uri="http://example.com", params={"test": "test"}, method="get") except BadRequest: caught += 1 self.assertEqual(len(status_codes), caught)
def test_connect_fail(self): with mock.patch.object(Client, 'get_credentials', return_value=False): with self.assertRaises(BadCredentials): Client(("http://mydomain.com", "http://mystreamingdomain.com"), "my_account", "my_token")
def test_connect_pass(self): with mock.patch.object(Client, 'get_credentials', return_value=True): Client(("http://mydomain.com", "http://mystreamingdomain.com"), "my_account", "my_token")
def test_connect_fail(self): with self.assertRaises(BadRequest): Client(SANDBOX, 999999999)