Skip to content

Commit

Permalink
_process_prices added to Available and tests fixed
Browse files Browse the repository at this point in the history
closes #145
  • Loading branch information
liampauling committed Dec 31, 2017
1 parent a66a453 commit 4843908
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
14 changes: 13 additions & 1 deletion betfairlightweight/resources/streamingresources.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,25 @@ def __init__(self, prices, deletion_select, reverse=False):
:param int deletion_select: Used to decide if update should delete cache
:param bool reverse: Used for sorting
"""
self.prices = prices or []
self.prices = self._process_prices(prices)
self.deletion_select = deletion_select
self.reverse = reverse

self.serialise = []
self.sort()

@staticmethod
def _process_prices(prices):
# 145 prevents empty prices
_prices = prices or []
output_prices = []
for price in _prices:
if price[-1] == 0 and price[-2] == 0:
continue
else:
output_prices.append(price)
return output_prices

def sort(self):
self.prices.sort(reverse=self.reverse)
self.serialise = [
Expand Down
23 changes: 15 additions & 8 deletions tests/unit/test_streamingresources.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@
class TestAvailable(unittest.TestCase):

def setUp(self):
self.prices = [[1, 1.02, 34.45], [0, 1.01, 12]]
self.prices = [[0, 1.01, 12], [1, 1.02, 34.45]]
self.available = Available(self.prices, 2)

def test_init(self):
assert self.available.prices == self.prices
assert self.available.deletion_select == 2
assert self.available.reverse is False

def test__process_prices(self):
assert self.available._process_prices(None) == []
assert self.available._process_prices([[1, 2, 3]]) == [[1, 2, 3]]
assert self.available._process_prices([[1, 2, 3], [4, 0, 6], [7, 8, 0]]) == [[1, 2, 3], [4, 0, 6], [7, 8, 0]]
assert self.available._process_prices([[1, 2, 3], [4, 0, 6], [7, 0, 0]]) == [[1, 2, 3], [4, 0, 6]]
assert self.available._process_prices([[1, 2, 3], [4, 0, 0], [7, 8, 0]]) == [[1, 2, 3], [7, 8, 0]]

def test_sort(self):
self.available.sort()
assert self.available.prices == self.prices
Expand All @@ -52,15 +59,15 @@ def test_update_available_new_update(self):

available = Available(current, 1)
available.update(book_update)
assert current == expected
assert available.prices == expected

book_update = [[30, 6.9], [1.01, 12]]
current = [[27, 0.95], [13, 28.01], [1.02, 1157.21]]
expected = [[1.01, 12], [1.02, 1157.21], [13, 28.01], [27, 0.95], [30, 6.9]]

available = Available(current, 1)
available.update(book_update)
assert current == expected
assert available.prices == expected

# [position, price, size]
book_update = [[0, 36, 0.57]]
Expand All @@ -79,7 +86,7 @@ def test_update_available_new_replace(self):

available = Available(current, 1)
available.update(book_update)
assert current == expected
assert available.prices == expected

# [position, price, size]
book_update = [[0, 36, 0.57]]
Expand All @@ -88,7 +95,7 @@ def test_update_available_new_replace(self):

available = Available(current, 2)
available.update(book_update)
assert current == expected
assert available.prices == expected

# tests handling of betfair bug, http://forum.bdp.betfair.com/showthread.php?t=3351
book_update = [[2, 0, 0], [1, 1.01, 9835.74], [0, 1.02, 1126.22]]
Expand All @@ -97,7 +104,7 @@ def test_update_available_new_replace(self):

available = Available(current, 2)
available.update(book_update)
assert current == expected
assert available.prices == expected

def test_update_available_new_remove(self):
book_update = [[27, 0]]
Expand All @@ -106,7 +113,7 @@ def test_update_available_new_remove(self):

available = Available(current, 1)
available.update(book_update)
assert current == expected
assert available.prices == expected

# [position, price, size]
book_update = [[0, 36, 0], [1, 38, 0], [0, 38, 3.57]]
Expand All @@ -115,7 +122,7 @@ def test_update_available_new_remove(self):

available = Available(current, 2)
available.update(book_update)
assert current == expected
assert available.prices == expected


class TestMarketDefinition(unittest.TestCase):
Expand Down

0 comments on commit 4843908

Please sign in to comment.