def test_parse_valid_positions(self): ''' Test that parse_positions on a list_as_string returns a list of InvestmentPosition objects. ''' self.assertEqual(InvestmentPositions.parse_positions("[1, 10, 100, 1000]"), [InvestmentPositions(position, int(1000 / position)) for position in [1, 10, 100, 1000]] )
def test_parse_positions_valid_repeated_vals(self): ''' Test that parse_positions accepts repeated valid positions and returns list of InvestmentPosition objects. ''' self.assertEqual(InvestmentPositions.parse_positions("[1, 10, 10, 100, 1000, 1]"), [InvestmentPositions(position, int(1000 / position)) for position in [1, 10, 10, 100, 1000, 1]] )
def test_parse_positions_valid_repeated_vals(self): ''' Test that parse_positions accepts repeated valid positions and returns list of InvestmentPosition objects. ''' self.assertEqual( InvestmentPositions.parse_positions("[1, 10, 10, 100, 1000, 1]"), [ InvestmentPositions(position, int(1000 / position)) for position in [1, 10, 10, 100, 1000, 1] ])
def test_parse_valid_positions(self): ''' Test that parse_positions on a list_as_string returns a list of InvestmentPosition objects. ''' self.assertEqual( InvestmentPositions.parse_positions("[1, 10, 100, 1000]"), [ InvestmentPositions(position, int(1000 / position)) for position in [1, 10, 100, 1000] ])
def prompt_positions(): ''' Prompt user and read in keyboard input of investment positions Raises InvalidListError, InvalidPositionError, ValueError for incorrect inputs ''' while True: try: userinput = input("Enter the positions as a list with one space after each comma.") return InvestmentPositions.parse_positions(userinput) except (InvalidListError, InvalidPositionError, ValueError) as e: print(e)
def test_parse_positions_invalid_format(self): ''' Test parse_positions raises InvalidListError if list_as_string is not in "list" format. ''' with self.assertRaises(InvalidListError): InvestmentPositions.parse_positions("1, 10, 100, 1000")
def test_parse_invalid_positions(self): ''' Invalid position value (5) raises InvalidPositionError ''' with self.assertRaises(InvalidPositionError): InvestmentPositions.parse_positions("[1, 5, 100, 1000]")