Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: TeamGamma/directedstudies
base: f359e6c307
...
head fork: TeamGamma/directedstudies
compare: fbcbd7e398
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
View
31 sps/transactions/commands.py
@@ -262,9 +262,8 @@ 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):
- amount = int(amount)
-
+ def run(self, username, stock_symbol, money_amount):
+
# see if user exists
session = get_session()
user = session.query(User).filter_by(username=username).first()
@@ -276,6 +275,17 @@ def run(self, username, stock_symbol, amount):
user=user, operation='SELL', committed=False).count() > 0:
raise SellTransactionActiveError()
+ #set up client to get quote
+ quote_client = get_quote_client()
+ quoted_stock_value = quote_client.get_quote(stock_symbol, username)
+
+ # Work out quantity of stock to sell, fail if not enough for one stock
+ 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()
+
+
# see if the user owns the requested stock and has enough for request
records = session.query(StockPurchase).filter_by(
username=user.username, stock_symbol=stock_symbol).all()
@@ -283,18 +293,15 @@ def run(self, username, stock_symbol, amount):
if(len(records) > 1):
raise UnknownCommandError('Multiple StockPurchase for user %s: %d',
username, len(records))
- if len(records) != 1 or records[0].quantity < amount:
+ if len(records) != 1 or records[0].quantity < quantity_to_sell:
raise InsufficientStockError()
-
- #set up client to get quote
- quote_client = get_quote_client()
- quoted_stock_value = quote_client.get_quote(stock_symbol, username)
- price = quoted_stock_value * amount
-
+
+ price = quoted_stock_value * quantity_to_sell
+
# make transaction
self.trans = Transaction(username=user.username,
stock_symbol=stock_symbol, operation='SELL', committed=False,
- quantity=amount, stock_value=quoted_stock_value)
+ quantity=quantity_to_sell, stock_value=quoted_stock_value)
# commit transaction after all actions for atomicity
session.add(self.trans)
@@ -302,7 +309,7 @@ def run(self, username, stock_symbol, amount):
xml.log_transaction('SELL', self.trans, status_message='success')
- return xml.QuoteResponse(quantity=amount, price=price)
+ return xml.QuoteResponse(quantity=quantity_to_sell, price=price)
class COMMIT_SELLCommand(CommandHandler):
View
4 sps/web/app.py
@@ -112,8 +112,8 @@ def checkentry(username, action, money_value, stock_quantity, stock_symbol, file
elif action == 'SELL':
try:
- if len(stock_symbol) != 0 and float(stock_quantity) > 0 and len(username)!=0:
- return "SELL," + username + ',' + stock_symbol + ',' + stock_quantity
+ if len(stock_symbol) != 0 and float(money_value) > 0 and len(username)!=0:
+ return "SELL," + username + ',' + stock_symbol + ',' + money_value
else:
return False
except ValueError:
View
2  sps/web/command_forms.py
@@ -72,7 +72,7 @@ class SELL():
inputs = (
_username_input,
_stock_symbol_input,
- _quantity_input,
+ _money_value_input,
)
class COMMIT_SELL():
View
16 tests/test_commands.py
@@ -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)

No commit comments for this range

Something went wrong with that request. Please try again.