Permalink
Browse files

fixed SELL command for taking in money amount not stock quantity, and…

… tested it thoroughly
  • Loading branch information...
1 parent 9df383c commit 687dd458ecb13db40d3687517d0731e96eb0e210 @alimjiwa alimjiwa committed Mar 21, 2012
Showing with 15 additions and 15 deletions.
  1. +6 −6 sps/transactions/commands.py
  2. +1 −1 sps/web/command_forms.py
  3. +8 −8 tests/test_commands.py
@@ -262,7 +262,7 @@ class SELLCommand(CommandHandler):
Sell the specified dollar amount of the stock currently held by the
specified user at the current price.
"""
- def run(self, username, stock_symbol, amount):
+ def run(self, username, stock_symbol, money_amount):
# see if user exists
session = get_session()
@@ -280,8 +280,8 @@ def run(self, username, stock_symbol, amount):
quoted_stock_value = quote_client.get_quote(stock_symbol, username)
# Work out quantity of stock to sell, fail if not enough for one stock
- amount = Money.from_string(amount)
- quantity_to_sell = amount_to_quantity(quoted_stock_value, amount)
+ money_amount = Money.from_string(money_amount)
+ quantity_to_sell = amount_to_quantity(quoted_stock_value, money_amount)
if quantity_to_sell == 0:
raise InsufficientFundsError()
@@ -295,9 +295,9 @@ def run(self, username, stock_symbol, amount):
username, len(records))
if len(records) != 1 or records[0].quantity < quantity_to_sell:
raise InsufficientStockError()
-
- price = quantity_to_sell*quoted_stock_value
-
+
+ price = quoted_stock_value * quantity_to_sell
+
# make transaction
self.trans = Transaction(username=user.username,
stock_symbol=stock_symbol, operation='SELL', committed=False,
View
@@ -72,7 +72,7 @@ class SELL():
inputs = (
_username_input,
_stock_symbol_input,
- _quantity_input,
+ _money_value_input,
)
class COMMIT_SELL():
View
@@ -159,7 +159,7 @@ def test_return_value(self):
""" Should return quoted stock value, quantity to be purchased, and
total price """
retval = self.command.run(username='rich_user', stock_symbol='ABAB',
- amount='2')
+ money_amount='50')
self.assertIsInstance(retval, xml.QuoteResponse)
self.assertEqual(retval.quantity, 2)
self.assertEqual(retval.price, Money(46, 90))
@@ -168,14 +168,14 @@ def test_return_value(self):
def test_too_little_stock_to_sell(self):
""" tests to see if returns error when requested to sell too much"""
self.assertRaises(commands.InsufficientStockError, self.command.run,
- username='rich_user', stock_symbol='ABAB', amount='100000')
+ username='rich_user', stock_symbol='ABAB', money_amount='100000')
self.assertRaises(commands.InsufficientStockError, self.command.run,
- username='poor_user', stock_symbol='ABAB', amount='100000')
+ username='poor_user', stock_symbol='ABAB', money_amount='100000')
def test_wrong_user_id(self):
""" tests to see if we have the wrong user id """
self.assertRaises(commands.UserNotFoundError, self.command.run,
- username='garbage', stock_symbol='ABAB', amount='5')
+ username='garbage', stock_symbol='ABAB', money_amount='50')
def test_multiple_sell_transaction(self):
""" Should return an error message if an uncommitted sell transaction
@@ -188,7 +188,7 @@ def test_multiple_sell_transaction(self):
)
self.assertRaises(commands.SellTransactionActiveError,
self.command.run, username='rich_user', stock_symbol='ABAB',
- amount='5')
+ money_amount='50')
def test_no_multiple_sell_transaction(self):
""" Should not return an error message if an committed sell transaction
@@ -200,17 +200,17 @@ def test_no_multiple_sell_transaction(self):
creation_time=datetime.now() - timedelta(seconds=30)),
)
self.command.run(username='rich_user', stock_symbol='ABAB',
- amount='5')
+ money_amount='50')
def test_postcondition_sell(self):
""" Uncommitted Transaction is created """
self.command.run(username='rich_user', stock_symbol='ABAB',
- amount='5')
+ money_amount='50')
transaction = self.session.query(Transaction).filter_by(
username='rich_user',
stock_symbol='ABAB',
committed=False,
- quantity=5,
+ quantity=2,
operation='SELL').one()
self.assertNotEqual(transaction, None)

0 comments on commit 687dd45

Please sign in to comment.