Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update realtime bug #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions test/test_realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,39 @@

class RealtimeTest(unittest.TestCase):
def test_realtime_field(self):
retry = 5
for _ in range(retry):
stock = realtime.get_raw('2330')
if stock['rtcode'] == '0000' and stock['rtmessage'] != 'OK':
continue
break

self.assertCountEqual(
realtime.get_raw('2330').keys(),
stock.keys(),
twstock.mock.get_stock_info('2330').keys())

def test_realtime_get_raw(self):
self.assertIn('msgArray', realtime.get_raw('2330'))
retry = 5
for _ in range(retry):
stock = realtime.get_raw('2330')
if stock['rtcode'] == '0000' and stock['rtmessage'] != 'OK':
continue
self.assertIn('msgArray', realtime.get_raw('2330'))
break

def test_realtime_get_blank(self):
stock = realtime.get('')

self.assertFalse(stock['success'])
self.assertIn('rtmessage', stock)
self.assertIn('rtcode', stock)
self.assertEqual(stock['rtcode'], '0000')
self.assertNotEqual(stock['rtmessage'], 'OK')

def test_realtime_get_bad_id(self):
stock = realtime.get('9999')

self.assertFalse(stock['success'])
self.assertIn('rtmessage', stock)
self.assertIn('rtcode', stock)
self.assertEqual(stock['rtcode'], '5001')
self.assertEqual(stock['rtmessage'], 'Empty Query.')

stock = realtime.get(['9999', '8888'])

Expand Down
4 changes: 2 additions & 2 deletions twstock/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ def get_raw(stocks) -> dict:
return {'rtmessage': 'json decode error', 'rtcode': '5000'}


def get(stocks, retry=3):
def get(stocks, retry=10):
# Prepare data
data = get_raw(stocks) if not mock else twstock.mock.get(stocks)

# Set success
data['success'] = False

# JSONdecode error, could be too fast, retry
if data['rtcode'] == '5000':
if data['rtcode'] == '0000' and data['rtmessage'] != 'OK':
# XXX: Stupit retry, you will dead here
if retry:
return get(stocks, retry - 1)
Expand Down