class ClientTest(unittest.TestCase):
    def setUp(self):
        logging.basicConfig(level=logging.DEBUG)
        config = ConfigParser()
        dir_path = os.path.dirname(os.path.realpath(__file__))
        config.read('{}/../config/config.ini'.format(dir_path))
        self.client = Client(logging.getLogger(),
                             host=config.get('ShiPanE', 'host'),
                             key=config.get('ShiPanE', 'key'))
        self.client.start_clients()

    def test_get_account(self):
        try:
            self.client.get_account()
        except HTTPError as e:
            self.fail()

    def test_get_positions(self):
        try:
            data = self.client.get_positions()
            sub_accounts = data['sub_accounts']
            self.assertGreater(sub_accounts['总资产']['人民币'], 0)
            positions = data['positions']
            self.assertIsNotNone(positions['证券代码'][0])
        except HTTPError as e:
            self.fail()

    def test_buy_stock(self):
        try:
            order = self.client.buy(symbol='000001', price=8.11, amount=100)
            self.assertIsNotNone(order['id'])
        except HTTPError as e:
            result = e.response.json()
            self.assertIsNotNone(result['message'])

    def test_sell_stock(self):
        try:
            order = self.client.sell(symbol='000001', price=9.51, amount=100)
            self.assertIsNotNone(order['id'])
        except HTTPError as e:
            result = e.response.json()
            self.assertIsNotNone(result['message'])

    def test_cancel_all(self):
        try:
            self.client.cancel_all()
        except HTTPError as e:
            self.fail()

    def test_query(self):
        try:
            df = self.client.query(None, '查询>资金股份')
            self.assertIsNotNone(df['证券代码'][0])
        except HTTPError as e:
            self.fail()

    def test_purchase_new_stocks(self):
        pass
示例#2
0
class ClientTest(unittest.TestCase):
    def setUp(self):
        config = ConfigParser()
        dir_path = os.path.dirname(os.path.realpath(__file__))
        config.read('{}/../config/config.ini'.format(dir_path))
        self.client = Client(host=config.get('ShiPanE', 'host'))

    def test_get_account(self):
        response = self.client.get_account()
        print(inspect.stack()[0][3] + ' - ' + response.text)
        self.assertEqual(response.status_code, 200)

    def test_get_positions(self):
        response = self.client.get_positions()
        print(inspect.stack()[0][3] + ' - ' + response.text)
        self.assertEqual(response.status_code, 200)

    def test_buy_stock(self):
        response = self.client.buy(symbol='000001', price=8.11, amount=100)
        print(inspect.stack()[0][3] + ' - ' + response.text)
        json = response.json()
        if response.status_code == 200:
            self.assertTrue(json['id'])
        elif response.status_code == 400:
            self.assertTrue(json['message'])
        else:
            self.fail()

    def test_sell_stock(self):
        response = self.client.sell(symbol='000001', price=9.51, amount=100)
        print(inspect.stack()[0][3] + ' - ' + response.text)
        json = response.json()
        if response.status_code == 200:
            self.assertTrue(json['id'])
        elif response.status_code == 400:
            self.assertTrue(json['message'])
        else:
            self.fail()

    def test_cancel_all(self):
        response = self.client.cancel_all()
        print(inspect.stack()[0][3] + ' - ' + response.text)
        self.assertEqual(response.status_code, 200)

    def test_query(self):
        response = self.client.query(None, '查询>资金股份')
        print(inspect.stack()[0][3] + ' - ' + response.text)
        self.assertEqual(response.status_code, 200)