def testFromId(self): apple = Position(ticker="AAPL", shares=100, account_id=3) apple.save() apple_id = apple.id apple2 = Position.from_id(apple_id) self.assertEqual( apple2.ticker, "AAPL", "Data should be loaded from database with .from_id()") orange = Position.from_id(100000000000) self.assertIsNone(orange, ".from_id() returns None for nonexistent row")
def testSaveUpdate(self): apple = Position(ticker="AAPL", shares=100, account_id=3) apple.save() apple_id = apple.id apple2 = Position.from_id(apple_id) apple2.shares = 150 apple2.save() self.assertEqual(apple2.id, apple_id, "update should not change ID number") apple3 = Position.from_id(apple_id) self.assertEqual(apple3.ticker, "AAPL", "save update should keep same ticker if not changed") self.assertEqual(apple3.shares, 150, "save updates should increase share amt by 50") self.assertEqual( apple3.account_id, 3, "save should keep the same account_id if not changed")