def checkCreation (id): try: s = StockTracker () if not closeTo (s.getProfit (), 0.0): print ('Test %d failed: StockTracker created with non-default value.' % id) return 0 if not s.getQuantityOnHand () == 0: print ('Test %d failed: StockTracker created with non-default value.' % id) return 0 print ('Test %d passed.' % id) return 1 except: print ('Test %d failed: StockTracker created with unknown error.' % id) return 0
def sellInvalidQuantityTest (id, q): try: s = StockTracker () s.sell (q, 12.34) print ('Test %d failed: no error with non-positive selling quantity.' % id) return 0 except: if not closeTo (s.getProfit (), 0.0): print ('Test %d failed: selling error causes attribute change.' % id) return 0 if not s.getQuantityOnHand () == 0: print ('Test %d failed: selling error causes attribute change.' % id) return 0 print ('Test %d passed.' % id) return 1
def buyInvalidPriceTest (id, p): try: s = StockTracker () s.buy (12, p) print ('Test %d failed: no error with non-positive buying price.' % id) return 0 except: if not closeTo (s.getProfit (), 0.0): print ('Test %d failed: buying error causes attribute change.' % id) return 0 if not s.getQuantityOnHand () == 0: print ('Test %d failed: buying error causes attribute change.' % id) return 0 print ('Test %d passed.' % id) return 1
def checkCreation(id): try: s = StockTracker() if not closeTo(s.getProfit(), 0.0): print( 'Test %d failed: StockTracker created with non-default value.' % id) return 0 if not s.getQuantityOnHand() == 0: print( 'Test %d failed: StockTracker created with non-default value.' % id) return 0 print('Test %d passed.' % id) return 1 except: print('Test %d failed: StockTracker created with unknown error.' % id) return 0
def buyInvalidPriceTest(id, p): try: s = StockTracker() s.buy(12, p) print('Test %d failed: no error with non-positive buying price.' % id) return 0 except: if not closeTo(s.getProfit(), 0.0): print('Test %d failed: buying error causes attribute change.' % id) return 0 if not s.getQuantityOnHand() == 0: print('Test %d failed: buying error causes attribute change.' % id) return 0 print('Test %d passed.' % id) return 1
def sellInvalidQuantityTest(id, q): try: s = StockTracker() s.sell(q, 12.34) print('Test %d failed: no error with non-positive selling quantity.' % id) return 0 except: if not closeTo(s.getProfit(), 0.0): print('Test %d failed: selling error causes attribute change.' % id) return 0 if not s.getQuantityOnHand() == 0: print('Test %d failed: selling error causes attribute change.' % id) return 0 print('Test %d passed.' % id) return 1
def test(): score = checkCreation(1) score += buyInvalidQuantityTest(2, 0) score += buyInvalidQuantityTest(3, -10) score += buyInvalidPriceTest(4, 0) score += buyInvalidPriceTest(5, -10.0) score += sellInvalidQuantityTest(6, 0) score += sellInvalidQuantityTest(7, -10) score += sellInvalidPriceTest(8, 0) score += sellInvalidPriceTest(9, -10.0) st = StockTracker() for record in data: id, action, quantity, price, result, shares, gain = record #print( id, "buy" if action == 0 else "sell", quantity, price ) if action == 0: score += buyingTest(id, st, quantity, price, shares, gain) else: score += sellingTest(id, st, quantity, price, result, shares, gain) print('%d of 100 test cases passed - %.2f points.' % (score, score * 0.15))
def main(): '''main class to handle stock data''' print("Hello Stock World") stocks = StockTracker() stocks.add_ticker('AMD', True) #stocks.add_ticker('AAPL') #stocks.add_ticker('BABA', True) stocks.add_ticker('C', True) stocks.add_ticker('NVDA', True) # stocks.add_ticker('OLED') stocks.add_ticker('RIO', True) # stocks.add_ticker('GDXJ') stocks.add_ticker('IAG', True) # stocks.add_ticker('NFLX') # stocks.add_ticker('TSLA') # stocks.add_ticker('DIS') # stocks.add_ticker('FB') # stocks.add_ticker('AVGO') # stocks.add_ticker('GOOGL') # stocks.add_ticker('AAOI') # stocks.add_ticker('JNJ') # stocks.add_ticker('NBN') # stocks.add_ticker('CFG') # stocks.add_ticker('SBUX') #scrape stock data stocks.construct_url('nad1t1c1ohgv', stocks.company_list) # build stock data stocks.build_stock_data() # Write stock data to JSON file stocks.write_JSON() print("Goodbye Stock World")