Esempio n. 1
0
class TestFakeStock(unittest.TestCase):
    """Tests for fake Stock"""

    def setUp(self):
        """SetUp."""
        self.ticker = 'fake_ticker'
        self.stock = Stock(self.ticker)

    def test_get_ticker(self):
        """Test get_ticker."""
        self.assertEqual(self.stock.get_ticker(), self.ticker)

    def test_set_ticker(self):
        """Test set_ticker."""
        self.stock.set_ticker('new_ticker')
        self.assertEqual(self.stock.get_ticker(), 'new_ticker')

    def test_get_latest_price(self):
        """Test get_latest_price error."""
        response = self.stock.get_latest_price()
        # Checks for 'LastTradePriceOnly', 'LastTradeTime' field
        # Check if both fields are empty
        self.assertEqual(len(response[0].keys()), 2)
        for key in response[0].keys():
            self.assertTrue(key in ['LastTradePriceOnly', 'LastTradeTime'])
            self.assertFalse(response[0][key])

    def test_get_info(self):
        """Test get_info error."""
        keys = ['Ask', 'AverageDailyVolume', 'Bid', 'BookValue', 'Change',
                'Change_PercentChange', 'ChangeFromFiftydayMovingAverage',
                'ChangeFromTwoHundreddayMovingAverage',
                'ChangeFromYearHigh', 'ChangeFromYearLow',
                'ChangeinPercent', 'Currency', 'DaysHigh', 'DaysLow',
                'DaysRange', 'DividendPayDate', 'DividendShare',
                'DividendYield', 'EarningsShare', 'EBITDA',
                'EPSEstimateCurrentYear', 'EPSEstimateNextQuarter',
                'EPSEstimateNextYear', 'ExDividendDate',
                'FiftydayMovingAverage', 'LastTradeDate',
                'LastTradePriceOnly', 'LastTradeTime', 'LastTradeWithTime',
                'MarketCapitalization', 'Name', 'OneyrTargetPrice', 'Open',
                'PEGRatio', 'PERatio', 'PercebtChangeFromYearHigh',
                'PercentChange', 'PercentChangeFromFiftydayMovingAverage',
                'PercentChangeFromTwoHundreddayMovingAverage',
                'PercentChangeFromYearLow', 'PreviousClose', 'PriceBook',
                'PriceEPSEstimateCurrentYear', 'PriceEPSEstimateNextYear',
                'PriceSales', 'ShortRatio', 'StockExchange', 'Symbol',
                'TwoHundreddayMovingAverage', 'Volume', 'YearHigh',
                'YearLow', 'YearRange']

        response = self.stock.get_info()
        # Checks for 'LastTradePriceOnly', 'LastTradeTime' field
        self.assertEqual(len(response[0].keys()), len(keys))
        for key in response[0].keys():
            self.assertTrue(key in keys)
            # Check if all fields but Symbol are empty
            if key == 'Symbol':
                self.assertEqual(response[0][key], self.ticker)
            else:
                self.assertFalse(response[0][key])

    def test_save_historical(self):
        """Test save_historical error."""
        with self.assertRaises(error.RequestError):
            self.stock.save_historical('.')
class TestFakeStock(unittest.TestCase):
    """Tests for fake Stock"""
    def setUp(self):
        """SetUp."""
        self.ticker = 'fake_ticker'
        self.stock = Stock(self.ticker)

    def test_get_ticker(self):
        """Test get_ticker."""
        self.assertEqual(self.stock.get_ticker(), self.ticker)

    def test_set_ticker(self):
        """Test set_ticker."""
        self.stock.set_ticker('new_ticker')
        self.assertEqual(self.stock.get_ticker(), 'new_ticker')

    def test_get_latest_price(self):
        """Test get_latest_price error."""
        response = self.stock.get_latest_price()
        # Checks for 'LastTradePriceOnly', 'LastTradeTime' field
        # Check if both fields are empty
        self.assertEqual(len(response[0].keys()), 2)
        for key in response[0].keys():
            self.assertTrue(key in ['LastTradePriceOnly', 'LastTradeTime'])
            self.assertFalse(response[0][key])

    def test_get_info(self):
        """Test get_info error."""
        keys = [
            'Ask', 'AverageDailyVolume', 'Bid', 'BookValue', 'Change',
            'Change_PercentChange', 'ChangeFromFiftydayMovingAverage',
            'ChangeFromTwoHundreddayMovingAverage', 'ChangeFromYearHigh',
            'ChangeFromYearLow', 'ChangeinPercent', 'Currency', 'DaysHigh',
            'DaysLow', 'DaysRange', 'DividendPayDate', 'DividendShare',
            'DividendYield', 'EarningsShare', 'EBITDA',
            'EPSEstimateCurrentYear', 'EPSEstimateNextQuarter',
            'EPSEstimateNextYear', 'ExDividendDate', 'FiftydayMovingAverage',
            'LastTradeDate', 'LastTradePriceOnly', 'LastTradeTime',
            'LastTradeWithTime', 'MarketCapitalization', 'Name',
            'OneyrTargetPrice', 'Open', 'PEGRatio', 'PERatio',
            'PercebtChangeFromYearHigh', 'PercentChange',
            'PercentChangeFromFiftydayMovingAverage',
            'PercentChangeFromTwoHundreddayMovingAverage',
            'PercentChangeFromYearLow', 'PreviousClose', 'PriceBook',
            'PriceEPSEstimateCurrentYear', 'PriceEPSEstimateNextYear',
            'PriceSales', 'ShortRatio', 'StockExchange', 'Symbol',
            'TwoHundreddayMovingAverage', 'Volume', 'YearHigh', 'YearLow',
            'YearRange'
        ]

        response = self.stock.get_info()
        # Checks for 'LastTradePriceOnly', 'LastTradeTime' field
        self.assertEqual(len(response[0].keys()), len(keys))
        for key in response[0].keys():
            self.assertTrue(key in keys)
            # Check if all fields but Symbol are empty
            if key == 'Symbol':
                self.assertEqual(response[0][key], self.ticker)
            else:
                self.assertFalse(response[0][key])

    def test_save_historical(self):
        """Test save_historical error."""
        with self.assertRaises(error.RequestError):
            self.stock.save_historical('.')
Esempio n. 3
0
class TestRealStock(unittest.TestCase):
    """Tests for Stock."""

    def setUp(self):
        """SetUp."""
        self.ticker = 'AAPL'
        self.stock = Stock(self.ticker)

    def test_get_ticker(self):
        """Test get_ticker."""
        self.assertEqual(self.stock.get_ticker(), self.ticker)

    def test_set_ticker(self):
        """Test set_ticker."""
        self.stock.set_ticker('new_ticker')
        self.assertEqual(self.stock.get_ticker(), 'new_ticker')

    def test_get_latest_price(self):
        """Test get_latest_price success."""
        response = self.stock.get_latest_price()
        # Checks for 'LastTradePriceOnly', 'LastTradeTime' field
        self.assertEqual(len(response[0].keys()), 2)
        for key in response[0].keys():
            self.assertTrue(key in ['LastTradePriceOnly', 'LastTradeTime'])

    def test_get_info(self):
        """Test get_info success."""
        keys = ['Ask', 'AverageDailyVolume', 'Bid', 'BookValue', 'Change',
                'Change_PercentChange', 'ChangeFromFiftydayMovingAverage',
                'ChangeFromTwoHundreddayMovingAverage',
                'ChangeFromYearHigh', 'ChangeFromYearLow',
                'ChangeinPercent', 'Currency', 'DaysHigh', 'DaysLow',
                'DaysRange', 'DividendPayDate', 'DividendShare',
                'DividendYield', 'EarningsShare', 'EBITDA',
                'EPSEstimateCurrentYear', 'EPSEstimateNextQuarter',
                'EPSEstimateNextYear', 'ExDividendDate',
                'FiftydayMovingAverage', 'LastTradeDate',
                'LastTradePriceOnly', 'LastTradeTime', 'LastTradeWithTime',
                'MarketCapitalization', 'Name', 'OneyrTargetPrice', 'Open',
                'PEGRatio', 'PERatio', 'PercebtChangeFromYearHigh',
                'PercentChange', 'PercentChangeFromFiftydayMovingAverage',
                'PercentChangeFromTwoHundreddayMovingAverage',
                'PercentChangeFromYearLow', 'PreviousClose', 'PriceBook',
                'PriceEPSEstimateCurrentYear', 'PriceEPSEstimateNextYear',
                'PriceSales', 'ShortRatio', 'StockExchange', 'Symbol',
                'TwoHundreddayMovingAverage', 'Volume', 'YearHigh',
                'YearLow', 'YearRange']

        response = self.stock.get_info()
        # Checks for 'LastTradePriceOnly', 'LastTradeTime' field
        self.assertEqual(len(response[0].keys()), len(keys))
        for key in response[0].keys():
            self.assertTrue(key in keys)

    def test_save_historical(self):
        """Test save_historical success."""
        first_line = b'Date,Open,High,Low,Close,Volume,Adj Close\n'
        self.stock.save_historical('.')
        with open(self.ticker + '.csv', 'rb') as f:
            self.assertEqual(f.readline(), first_line)
        os.remove(self.ticker + '.csv')
class TestRealStock(unittest.TestCase):
    """Tests for Stock."""
    def setUp(self):
        """SetUp."""
        self.ticker = 'AAPL'
        self.stock = Stock(self.ticker)

    def test_get_ticker(self):
        """Test get_ticker."""
        self.assertEqual(self.stock.get_ticker(), self.ticker)

    def test_set_ticker(self):
        """Test set_ticker."""
        self.stock.set_ticker('new_ticker')
        self.assertEqual(self.stock.get_ticker(), 'new_ticker')

    def test_get_latest_price(self):
        """Test get_latest_price success."""
        response = self.stock.get_latest_price()
        # Checks for 'LastTradePriceOnly', 'LastTradeTime' field
        self.assertEqual(len(response[0].keys()), 2)
        for key in response[0].keys():
            self.assertTrue(key in ['LastTradePriceOnly', 'LastTradeTime'])

    def test_get_info(self):
        """Test get_info success."""
        keys = [
            'Ask', 'AverageDailyVolume', 'Bid', 'BookValue', 'Change',
            'Change_PercentChange', 'ChangeFromFiftydayMovingAverage',
            'ChangeFromTwoHundreddayMovingAverage', 'ChangeFromYearHigh',
            'ChangeFromYearLow', 'ChangeinPercent', 'Currency', 'DaysHigh',
            'DaysLow', 'DaysRange', 'DividendPayDate', 'DividendShare',
            'DividendYield', 'EarningsShare', 'EBITDA',
            'EPSEstimateCurrentYear', 'EPSEstimateNextQuarter',
            'EPSEstimateNextYear', 'ExDividendDate', 'FiftydayMovingAverage',
            'LastTradeDate', 'LastTradePriceOnly', 'LastTradeTime',
            'LastTradeWithTime', 'MarketCapitalization', 'Name',
            'OneyrTargetPrice', 'Open', 'PEGRatio', 'PERatio',
            'PercebtChangeFromYearHigh', 'PercentChange',
            'PercentChangeFromFiftydayMovingAverage',
            'PercentChangeFromTwoHundreddayMovingAverage',
            'PercentChangeFromYearLow', 'PreviousClose', 'PriceBook',
            'PriceEPSEstimateCurrentYear', 'PriceEPSEstimateNextYear',
            'PriceSales', 'ShortRatio', 'StockExchange', 'Symbol',
            'TwoHundreddayMovingAverage', 'Volume', 'YearHigh', 'YearLow',
            'YearRange'
        ]

        response = self.stock.get_info()
        # Checks for 'LastTradePriceOnly', 'LastTradeTime' field
        self.assertEqual(len(response[0].keys()), len(keys))
        for key in response[0].keys():
            self.assertTrue(key in keys)

    def test_save_historical(self):
        """Test save_historical success."""
        first_line = b'Date,Open,High,Low,Close,Volume,Adj Close\n'
        self.stock.save_historical('.')
        with open(self.ticker + '.csv', 'rb') as f:
            self.assertEqual(f.readline(), first_line)
        os.remove(self.ticker + '.csv')