Skip to content

Commit

Permalink
Added rigorous tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinali1 committed May 24, 2015
1 parent c4766f5 commit 450b936
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 21 deletions.
5 changes: 1 addition & 4 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
Current or previous core committers

Quincy Alexander

Contributors (in alphabetical order)

* Your name could stand here :)
Kevin Ali
23 changes: 15 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ Forex, by Valuehorizon
.. image:: https://codeclimate.com/github/Valuehorizon/valuehorizon-forex/badges/gpa.svg
:target: https://codeclimate.com/github/Valuehorizon/valuehorizon-forex

A Django-based Foreign Exchange data toolkit. Part of the Valuehorizon application ecosystem.
A Django-based Foreign Exchange data toolkit. It provides time-series functionality
with built-in statistical plugins such as volatility and returns. You can also write
your own statistical plugins.
It also includes documentation, test coverage and a good amount of sample data to play around with.
This app is a part of the Valuehorizon application ecosystem.

Contributors
============
Expand All @@ -18,7 +22,7 @@ Contributors

Dependencies
=============
``forex`` supports `Django`_ (>=1.7) or later and requires and `Pandas`_ (>= 0.12.0).
``forex`` supports `Django`_ (>=1.8.1) or later and requires and `Pandas`_ (>= 0.12.0).
**Note** because of problems with the ``requires`` directive of setuptools
you probably need to install ``numpy`` in your virtualenv before you install
this package or if you want to run the test suite ::
Expand Down Expand Up @@ -49,17 +53,20 @@ Start by creating a new ``virtualenv`` for your project ::

mkvirtualenv myproject

Next install ``numpy`` and ``pandas`` and optionally ``scipy`` ::
Next install ``numpy`` and ``pandas`` ::

pip install numpy
pip install pandas

You may want to consult the `scipy documentation`_ for more information
on installing the ``Scipy`` stack.

.. _scipy documentation: http://www.scipy.org/install.html

Finally, install the development version of ``forex`` from ``github`` using ``pip``::
pip install https://github.com/Valuehorizon/forex/tarball/master


Commercial Support
==================

This project is sponsored by Valuehorizon_. If you require assistance on
your project(s), please contact us: support@valuehorizon.com.

.. _Valuehorizon: http://www.valuehorizon.com
4 changes: 3 additions & 1 deletion forex/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def generate_dataframe(self, start_date=None, end_date=None):

required_dates = date_range(start_date,end_date)
df = df.reindex(required_dates)
df = df.fillna(method='ffill')

return df

Expand Down Expand Up @@ -108,6 +109,7 @@ def __unicode__(self):
return u'%s, %s' % (unicode(self.currency),
unicode(self.date),)

@property
def mid_price(self):
"""
Compute the mid point between bid and ask
Expand All @@ -131,7 +133,7 @@ def bid_price_us(self):
Calculate the bid_price in USD. This is the inverse
of the bid price.
"""
if self.ask_price != 0:
if self.bid_price != 0:
return 1 / Decimal(str(self.bid_price))
else:
raise ZeroDivisionError('Bid price is zero')
Expand Down
75 changes: 68 additions & 7 deletions forex/tests/models_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,41 @@ def test_model(self):
class CurrencyModelTests(TestCase):
def setUp(self):
Currency.objects.create(name="Test Dollar", symbol="TEST")

def test_if_saved(self):
test = Currency.objects.get(symbol="TEST")
test.save()
test_curr1 = Currency.objects.get(symbol="TEST")

CurrencyPrices.objects.create(currency=test_curr1,
date=date(2015,1,1),
ask_price = 3,
bid_price = 4)

CurrencyPrices.objects.create(currency=test_curr1,
date=date(2015,1,3),
ask_price = 5,
bid_price = 6)

CurrencyPrices.objects.create(currency=test_curr1,
date=date(2015,1,5),
ask_price = 7,
bid_price = 8)

def test_dataframe_generation_base(self):
test_curr1 = Currency.objects.get(symbol="TEST")
df = test_curr1.generate_dataframe()
self.assertEqual(len(df.columns), 4)
self.assertTrue('ASK' in df.columns)
self.assertTrue('BID' in df.columns)
self.assertTrue('CHANGE' in df.columns)
self.assertTrue('MID' in df.columns)

def test_dataframe_generation_mid(self):
test_curr1 = Currency.objects.get(symbol="TEST")
df = test_curr1.generate_dataframe()
self.assertEqual(df.ix[0]['MID'], (df.ix[0]['ASK'] + df.ix[0]['BID']) / 2.0)

def test_dataframe_generation_fill(self):
test_curr1 = Currency.objects.get(symbol="TEST")
df = test_curr1.generate_dataframe()
self.assertEqual(df.ix[1]['ASK'], 3)


class CurrencyPriceModelTests(TestCase):
Expand All @@ -42,19 +73,49 @@ def test_mid_price(self):
price.ask_price = 3
price.bid_price = 4
price.save()
self.assertEqual(price.mid_price(), Decimal('3.5'))
self.assertEqual(price.mid_price, Decimal('3.5'))

def test_mid_price_negative(self):
test_curr1 = Currency.objects.get(symbol="TEST")
price = CurrencyPrices.objects.get(currency=test_curr1, date=date(2015,1,1))
price.ask_price = -1
price.bid_price = 4
price.save()
midprice = price.mid_price()
midprice = price.mid_price
self.assertEqual(midprice, Decimal('1.5'))

price.ask_price = 2
price.bid_price = -8
price.save()
self.assertEqual(price.mid_price(), Decimal('-3'))
self.assertEqual(price.mid_price, Decimal('-3'))


def test_ask_us(self):
test_curr1 = Currency.objects.get(symbol="TEST")
price = CurrencyPrices.objects.get(currency=test_curr1, date=date(2015,1,1))

price.ask_price = Decimal('3')
price.save()
self.assertEqual(price.ask_price_us, 1/Decimal('3'))

price.ask_price = 0
try:
test = price.ask_price_us
raise AssertionError("Price should not be zero")
except ZeroDivisionError:
pass

def test_bid_us(self):
test_curr1 = Currency.objects.get(symbol="TEST")
price = CurrencyPrices.objects.get(currency=test_curr1, date=date(2015,1,1))

price.bid_price = Decimal('3')
price.save()
self.assertEqual(price.bid_price_us, 1/Decimal('3'))

price.bid_price = 0
try:
test = price.bid_price_us
raise AssertionError("Price should not be zero")
except ZeroDivisionError:
pass
2 changes: 1 addition & 1 deletion forex/tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

INTERNAL_APPS = [
'forex',
'forex.tests.test_app',
#'forex.tests.test_app',
]

INSTALLED_APPS = EXTERNAL_APPS + INTERNAL_APPS
Expand Down

0 comments on commit 450b936

Please sign in to comment.