Esempio n. 1
0
class TestInstrumentsAPI(unittest.TestCase):
    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_get_instruments_pass(self):
        with mock.patch.object(
            Client, '_Client__call',
            return_value={"message": "good one"}
        ):
            assert self.client.get_instruments()

    def test_get_prices(self):
        with mock.patch.object(
            Client, '_Client__call',
            return_value={"message": "good one"}
        ):
            assert self.client.get_prices(instruments="EUR_GBP", stream=False)

    def test_get_instrument_history(self):
        with mock.patch.object(
            Client, '_Client__call',
            return_value=[{}]
        ):
            assert self.client.get_instrument_history('EUR_GBP')
Esempio n. 2
0
 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"
         )
Esempio n. 3
0
 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)
Esempio n. 4
0
class TestTradeAPI(unittest.TestCase):
    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_get_trades(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_trades()

    def test_get_trade(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_trade(1)
            assert self.client.get_trade(trade_id=1)

    def test_update_trade(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.update_trade(1)
            assert self.client.update_trade(1, 0)
            assert self.client.update_trade(1, 1)
            assert self.client.update_trade(1, None, 0)
            assert self.client.update_trade(1, None, 1)
            assert self.client.update_trade(1, None, None, 0)
            assert self.client.update_trade(1, None, None, 1)

    def test_close_trade(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.close_trade(1)
            assert self.client.close_trade(trade_id=1)
Esempio n. 5
0
 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"
         )
Esempio n. 6
0
class TestPositionAPI(unittest.TestCase):
    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_get_positions(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_positions()

    def test_get_position(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_position('AUD_USD')
            assert self.client.get_position(instrument='AUD_USD')

    def test_close_position(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.close_position('AUD_USD')
            assert self.client.close_position(instrument='AUD_USD')
Esempio n. 7
0
    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)
Esempio n. 8
0
class TestAccountAPI(unittest.TestCase):
    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_create_account(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.create_account()

    def test_create_account_with_currency(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.create_account('AUD')
            assert self.client.create_account(currency='AUD')

    def test_get_accounts(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_accounts()

    def test_get_accounts_with_username(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_accounts('bob')
            assert self.client.get_accounts(username='******')
Esempio n. 9
0
class TestPositionAPI(unittest.TestCase):
    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_get_positions(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_positions()

    def test_get_position(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_position('AUD_USD')
            assert self.client.get_position(instrument='AUD_USD')

    def test_close_position(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.close_position('AUD_USD')
            assert self.client.close_position(instrument='AUD_USD')
Esempio n. 10
0
class TestInstrumentsAPI(unittest.TestCase):
    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_get_instruments_pass(self):
        with mock.patch.object(Client,
                               '_Client__call',
                               return_value={"message": "good one"}):
            assert self.client.get_instruments()

    def test_get_prices(self):
        with mock.patch.object(Client,
                               '_Client__call',
                               return_value={"message": "good one"}):
            assert self.client.get_prices(instruments="EUR_GBP", stream=False)

    def test_get_instrument_history(self):
        with mock.patch.object(Client, '_Client__call', return_value=[{}]):
            assert self.client.get_instrument_history('EUR_GBP')
Esempio n. 11
0
class TestOrdersAPI(unittest.TestCase):
    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_credentials_pass(self):
        with mock.patch.object(
            Client, '_Client__call',
            return_value={"message": "good one"}
        ):
            assert self.client.get_credentials()

    def test_credentials_fail(self):
        with mock.patch.object(Client, '_Client__call', return_value=()):
            assert not self.client.get_credentials()

        e = RequestException("I fail")
        with mock.patch.object(Client, '_Client__call', side_effect=e):
            assert not self.client.get_credentials()

    def test_get_orders(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_orders()

    def test_get_order(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_order(1)

    def test_create_order(self):
        order = Order(instrument="GBP_EUR", units=1, side="buy", type="market")
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.create_order(order)

    def test_update_order(self):
        order = Order(instrument="GBP_EUR", units=1, side="buy", type="market")
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.update_order(1, order)

    def test_close_order(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.close_order(1)
Esempio n. 12
0
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()
        )
Esempio n. 13
0
class TestOrdersAPI(unittest.TestCase):
    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_credentials_pass(self):
        with mock.patch.object(Client,
                               '_Client__call',
                               return_value={"message": "good one"}):
            assert self.client.get_credentials()

    def test_credentials_fail(self):
        with mock.patch.object(Client, '_Client__call', return_value=()):
            assert not self.client.get_credentials()

        e = RequestException("I fail")
        with mock.patch.object(Client, '_Client__call', side_effect=e):
            assert not self.client.get_credentials()

    def test_get_orders(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_orders()

    def test_get_order(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_order(1)

    def test_create_order(self):
        order = Order(instrument="GBP_EUR", units=1, side="buy", type="market")
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.create_order(order)

    def test_update_order(self):
        order = Order(instrument="GBP_EUR", units=1, side="buy", type="market")
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.update_order(1, order)

    def test_close_order(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.close_order(1)
Esempio n. 14
0
    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")
Esempio n. 15
0
class TestAccountAPI(unittest.TestCase):
    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_create_account(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.create_account()

    def test_create_account_with_currency(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.create_account('AUD')
            assert self.client.create_account(currency='AUD')

    def test_get_accounts(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_accounts()

    def test_get_accounts_with_username(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_accounts('bob')
            assert self.client.get_accounts(username='******')
Esempio n. 16
0
class TestTransactionAPI(unittest.TestCase):
    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_get_transactions(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_transactions()

    def test_get_transaction(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_transaction(1)

    @requests_mock.Mocker()
    def test_request_transaction_history(self, m):
        location = 'http://example.com/transactions.json.gz'
        m.get(
            requests_mock.ANY,
            headers={'Location': location}, status_code=202
        )
        self.assertEqual(location, self.client.request_transaction_history())

    @requests_mock.Mocker()
    def test_get_transaction_history(self, m):
        # Mock zip file content
        content = BytesIO()
        with ZipFile(content, 'w') as zip:
            zip.writestr('transactions.json', json.dumps({'ok': True}))
        content.seek(0)

        # Mock requests, one HEAD to check if it's there, one GET once it is
        location = 'http://example.com/transactions.json.gz'
        m.head(location, status_code=200)
        m.get(location, body=content, status_code=200)

        method = 'request_transaction_history'
        with mock.patch.object(Client, method, return_value=location):
            transactions = self.client.get_transaction_history()
            assert transactions['ok']
            assert m.call_count == 2

    @requests_mock.Mocker()
    def test_get_transaction_history_slow(self, m):
        """Ensures that get_transaction_history retries requests."""
        # Mock zip file content
        content = BytesIO()
        with ZipFile(content, 'w') as zip:
            zip.writestr('transactions.json', json.dumps({'ok': True}))
        content.seek(0)

        # Mock requests, one HEAD to check if it's there, one GET once it is
        location = 'http://example.com/transactions.json.gz'
        m.head(location, [{'status_code': 404}, {'status_code': 200}])
        m.get(location, body=content, status_code=200)

        method = 'request_transaction_history'
        with mock.patch.object(Client, method, return_value=location):
            transactions = self.client.get_transaction_history()
            assert transactions['ok']
            assert m.call_count == 3

    @requests_mock.Mocker()
    def test_get_transaction_history_gives_up(self, m):
        """Ensures that get_transaction_history eventually gives up."""
        # Mock requests, one HEAD to check if it's there, one GET once it is
        location = 'http://example.com/transactions.json.gz'
        m.head(location, [{'status_code': 404}, {'status_code': 404}])

        method = 'request_transaction_history'
        with mock.patch.object(Client, method, return_value=location):
            transactions = self.client.get_transaction_history(.3)
            assert not transactions
            # Possible timing issue, may be one or the other
            assert m.call_count == 2 or m.call_count == 3

    @requests_mock.Mocker()
    def test_get_transaction_history_handles_bad_files(self, m):
        """Ensures that get_transaction_history gracefully handles bad files.
        """
        # Mock requests, one HEAD to check if it's there, one GET once it is
        location = 'http://example.com/transactions.json.gz'
        m.head(location, status_code=200)
        m.get(location, text='invalid', status_code=200)

        method = 'request_transaction_history'
        with mock.patch.object(Client, method, return_value=location):
            transactions = self.client.get_transaction_history()
            assert not transactions
Esempio n. 17
0
 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")
Esempio n. 18
0
 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")
Esempio n. 19
0
class TestTransactionAPI(unittest.TestCase):
    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_get_transactions(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_transactions()

    def test_get_transaction(self):
        with mock.patch.object(Client, '_Client__call', return_value=True):
            assert self.client.get_transaction(1)

    @requests_mock.Mocker()
    def test_request_transaction_history(self, m):
        location = 'http://example.com/transactions.json.gz'
        m.get(requests_mock.ANY,
              headers={'Location': location},
              status_code=202)
        self.assertEqual(location, self.client.request_transaction_history())

    @requests_mock.Mocker()
    def test_get_transaction_history(self, m):
        # Mock zip file content
        content = BytesIO()
        with ZipFile(content, 'w') as zip:
            zip.writestr('transactions.json', json.dumps({'ok': True}))
        content.seek(0)

        # Mock requests, one HEAD to check if it's there, one GET once it is
        location = 'http://example.com/transactions.json.gz'
        m.head(location, status_code=200)
        m.get(location, body=content, status_code=200)

        method = 'request_transaction_history'
        with mock.patch.object(Client, method, return_value=location):
            transactions = self.client.get_transaction_history()
            assert transactions['ok']
            assert m.call_count == 2

    @requests_mock.Mocker()
    def test_get_transaction_history_slow(self, m):
        """Ensures that get_transaction_history retries requests."""
        # Mock zip file content
        content = BytesIO()
        with ZipFile(content, 'w') as zip:
            zip.writestr('transactions.json', json.dumps({'ok': True}))
        content.seek(0)

        # Mock requests, one HEAD to check if it's there, one GET once it is
        location = 'http://example.com/transactions.json.gz'
        m.head(location, [{'status_code': 404}, {'status_code': 200}])
        m.get(location, body=content, status_code=200)

        method = 'request_transaction_history'
        with mock.patch.object(Client, method, return_value=location):
            transactions = self.client.get_transaction_history()
            assert transactions['ok']
            assert m.call_count == 3

    @requests_mock.Mocker()
    def test_get_transaction_history_gives_up(self, m):
        """Ensures that get_transaction_history eventually gives up."""
        # Mock requests, one HEAD to check if it's there, one GET once it is
        location = 'http://example.com/transactions.json.gz'
        m.head(location, [{'status_code': 404}, {'status_code': 404}])

        method = 'request_transaction_history'
        with mock.patch.object(Client, method, return_value=location):
            transactions = self.client.get_transaction_history(.3)
            assert not transactions
            # Possible timing issue, may be one or the other
            assert m.call_count == 2 or m.call_count == 3

    @requests_mock.Mocker()
    def test_get_transaction_history_handles_bad_files(self, m):
        """Ensures that get_transaction_history gracefully handles bad files.
        """
        # Mock requests, one HEAD to check if it's there, one GET once it is
        location = 'http://example.com/transactions.json.gz'
        m.head(location, status_code=200)
        m.get(location, text='invalid', status_code=200)

        method = 'request_transaction_history'
        with mock.patch.object(Client, method, return_value=location):
            transactions = self.client.get_transaction_history()
            assert not transactions
Esempio n. 20
0
 def test_connect_fail(self):
     with self.assertRaises(BadRequest):
         Client(SANDBOX, 999999999)