Skip to content

Commit

Permalink
Unittests for Streamer object
Browse files Browse the repository at this point in the history
- created a testsuite directory: tests
- added tests to setup.py
  • Loading branch information
hootnot committed Jan 22, 2016
1 parent bcafd4c commit ce55f54
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
url='http://developer.oanda.com/',
license='MIT',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
test_suite="tests",
include_package_data=True,
zip_safe=False,
install_requires=[
Expand Down
Empty file added tests/__init__.py
Empty file.
1 change: 1 addition & 0 deletions tests/account.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9999999
85 changes: 85 additions & 0 deletions tests/stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import unittest
import oandapy
import sys

access_token = None
account = None


class Stream(oandapy.Streamer):
"""
class to parse the OANDA stream
tick records are processed into candles of different timeframes
candles that are ready are processed by the plugin manager
"""
def __init__(self, count=10, *args, **kwargs):
super(Stream, self).__init__(*args, **kwargs)
self.count = count
self.reccnt = 0
self.hbcnt = 0

# in case we want heartbeats by default: override start()
# def start(self, ignore_heartbeat=False, **params):
# super(Stream, self).start(ignore_heartbeat=ignore_heartbeat, **params)

def on_success(self, data):
print data, "\n"
if "heartbeat" in data:
self.hbcnt += 1
else:
self.reccnt += 1

if self.reccnt + self.hbcnt == self.count:
self.disconnect()

def on_error(self, data):
self.disconnect()


class TestRates(unittest.TestCase):
def setUp(self):
global access_token
global account
with open("tests/token.txt") as T:
access_token = T.read().strip()
with open("tests/account.txt") as T:
account = T.read().strip()
if account == "9999999":
warning = """
***************************************************
*** PLEASE PROVIDE YOUR account AND token IN ****
*** account.txt AND token.txt TO RUN THE TESTS ****
***************************************************
"""
print warning

def test_Rates(self):
""" get records from stream and including heartbeats,
#recs received should equal #recs requested + # hb recs
"""
count = 100
count = 10
instruments = ["EUR_USD", "US30_USD", "DE30_EUR"]
r = Stream(access_token=access_token, environment="practice",
count=count)
r.start(accountId=account, ignore_heartbeat=False,
instruments=",".join(instruments))
self.assertEqual(count, r.reccnt + r.hbcnt)

def test_RatesNoHeartBeats(self):
""" get records from stream and ignore heartbeats,
#recs received should equal #recs requested
"""
count = 100
instruments = ["EUR_USD", "US30_USD", "DE30_EUR"]
r = Stream(access_token=access_token, environment="practice",
count=count)
r.start(accountId=account, ignore_heartbeat=True,
instruments=",".join(instruments))

self.assertEqual(count, r.reccnt)


if __name__ == "__main__":

unittest.main()
1 change: 1 addition & 0 deletions tests/token.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaaa9999bbbb8888cccc7777dddd6666-eeee5555ffff4444aaaa3333bbbb2222

0 comments on commit ce55f54

Please sign in to comment.