示例#1
0
    def test_get_equity_data_after_multiple_dates_updates(self):
        ticker = 'AMZN'
        self.database.insert_securities([ticker])

        test_data = test_utils.load_test_data()
        dt_1 = datetime.datetime(2019, 8, 27)
        expected_data_1 = test_utils.get_test_data(test_data, ticker, dt_1)
        self.database.update_market_data(ticker, (dt_1, expected_data_1))

        dt_2 = datetime.datetime(2019, 8, 26)
        expected_data_2 = test_utils.get_test_data(test_data, ticker, dt_2)
        self.database.update_market_data(ticker, (dt_2, expected_data_2))

        actual_data_2 = self.database.get_equity_data(ticker, dt_2)
        self.assertEqual(expected_data_2, actual_data_2)

        actual_data_1 = self.database.get_equity_data(ticker, dt_1)
        self.assertEqual(expected_data_1, actual_data_1)

        data_series = self.database.get_equity_data_series(ticker)
        self.assertEqual(len(data_series), 2)

        self.assertEqual(dt_1, data_series[0][0])
        self.assertEqual(expected_data_1, data_series[0][1])

        self.assertEqual(dt_2, data_series[1][0])
        self.assertEqual(expected_data_2, data_series[1][1])
示例#2
0
    def test_get_equity_data_from_app(self, mock_scrape):
        # Josh has heard of this new app from
        # Carol and decides to open the app
        # and play with it.
        app = MarketData()
        app.run(database=self.database)

        # Carol told Josh that she has already
        # added a security into the app
        ticker = 'AMZN'
        dt = datetime.datetime(2019, 8, 27)
        app.add_security(ticker)

        # Josh proceeds to check what the security price
        # on the 31 July 2019 is of the stock Carol added
        # but accidentally enters the wrong ticker name
        with self.assertRaises(InvalidTickerError):
            data = app.get_equity_data('AMZNN', dt)

        # He then tries again but with the correct
        # ticker this time but with the wrong date..
        with self.assertRaises(InvalidDateError):
            data = app.get_equity_data(ticker, datetime.datetime(2017, 8, 25))

        # Third time lucky, he enters in the correct
        # ticker and date and gets the results!
        expected_data = test_utils.get_test_data(self.test_data, ticker, dt)
        mock_scrape.return_value = expected_data
        app.update_market_data(ticker, dt)

        data = app.get_equity_data(ticker, dt)

        # He then goes to his trusty source, Yahoo to
        # confirm that the security price is indeed correct.
        self.assertEqual(data, expected_data)
示例#3
0
    def test_get_equity_data_for_multiple_securities(self):
        self.database.insert_securities(['AMZN', 'GOOG'])
        dt = datetime.datetime(2019, 8, 27)

        test_data = test_utils.load_test_data()
        expected_data_1 = test_utils.get_test_data(test_data, 'AMZN', dt)
        self.database.update_market_data('AMZN', (dt, expected_data_1))

        expected_data_2 = test_utils.get_test_data(test_data, 'GOOG', dt)
        self.database.update_market_data('GOOG', (dt, expected_data_2))

        actual_data_2 = self.database.get_equity_data('GOOG', dt)
        self.assertEqual(expected_data_2, actual_data_2)

        actual_data_1 = self.database.get_equity_data('AMZN', dt)
        self.assertEqual(expected_data_1, actual_data_1)
示例#4
0
    def test_valid_and_non_valid_dates(self, mock_urlopen):
        test_data = test_utils.load_test_data()
        scraper = Scraper('yahoo')

        mock_urlopen = mock_urlopen.return_value.__enter__.return_value
        mock_urlopen.status = 200
        mock_urlopen.read.return_value = load_test_data()

        dt_1 = datetime.datetime(2019, 8, 26)
        dt_2 = datetime.datetime(2019, 8, 23)
        dt_3 = datetime.datetime(2019, 9, 2)
        dt_4 = datetime.datetime(2019, 8, 27)
        dt_5 = datetime.datetime(2019, 9, 4)
        dates = (dt_1, dt_2, dt_3, dt_4, dt_5)

        data, errors = scraper.scrape_eq_multiple_dates(self.ticker, dates)

        self.assertEqual(len(data), 3)
        self.assertEqual(len(errors), 2)

        # data checks
        for d in data:
            self.assertIsInstance(d[1], EquityData)

        for i, date in enumerate((dt_1, dt_2, dt_4)):
            self.assertEqual(data[i][0], date.date())
            expected_data = test_utils.get_test_data(test_data, self.ticker,
                                                     date)
            self.assertEqual(data[i][1], expected_data,
                             msg=f'res: {data[i][1]} != ex: {expected_data}')

        # error checks
        for i, date in enumerate((dt_3, dt_5)):
            self.assertIsInstance(errors[i], InvalidDateError)
            self.assertEqual(str(date.date()), str(errors[i]))
示例#5
0
    def test_can_get_equity_data_after_multiple_updates(self, mock_scrape):
        # Carol once again opens up the application and
        # adds a couple of securities into the app.
        app = MarketData()
        app.run(database=self.database)
        new_tickers = ['AMZN', 'GOOG']
        for ticker in new_tickers:
            app.add_security(ticker)

        # proceeds to update the data in the application
        # for multiple dates and securities
        for ticker in new_tickers:
            data = self.test_data[ticker]
            for date_string in data:
                dt = datetime.datetime.strptime(date_string, '%d-%b-%Y')
                equity_data = test_utils.get_test_data(self.test_data, ticker,
                                                       dt)
                mock_scrape.return_value = equity_data
                app.update_market_data(ticker, dt)

        # Satisfied that she has updated all she needs to
        # she closes the application down.
        app.close()

        # Josh, her ever inquistive friend checks in on
        # the prices of AMZN and GOOG.
        new_app = MarketData()
        new_app.run(database=self.database)

        # He first checks the prices for GOOG first
        dt = datetime.datetime(2019, 8, 27)
        expected_data = test_utils.get_test_data(self.test_data, 'GOOG', dt)
        actual_data = new_app.get_equity_data('GOOG', dt)
        self.assertEqual(expected_data, actual_data, 'GOOG: 27-Aug-2019')

        # He then checks the prices for AMZN on both dates
        expected_data = test_utils.get_test_data(self.test_data, 'AMZN', dt)
        actual_data = new_app.get_equity_data('AMZN', dt)
        self.assertEqual(expected_data, actual_data, 'AMZN: 27-Aug-2019')

        dt = datetime.datetime(2019, 8, 26)
        expected_data = test_utils.get_test_data(self.test_data, 'AMZN', dt)
        actual_data = new_app.get_equity_data('AMZN', dt)
        self.assertEqual(expected_data, actual_data, 'AMZN: 26-Aug-2019')

        # Satisfied with what he sees he closes the app
        new_app.close()
示例#6
0
    def update_with_test_data(self, params, mock_scraper):
        test_data = test_utils.load_test_data()

        expected_data = []
        for ticker, dt in params:
            data = test_utils.get_test_data(test_data, ticker, dt)
            mock_scraper.return_value = data
            self.app.update_market_data(ticker, dt)
            expected_data.append(data)

        return expected_data
示例#7
0
    def test_bulk_update_equity_data(self):
        ticker = 'AMZN'
        self.database.insert_securities([ticker])

        test_data = test_utils.load_test_data()
        dt_1 = datetime.datetime(2019, 8, 27)
        expected_data_1 = test_utils.get_test_data(test_data, ticker, dt_1)

        dt_2 = datetime.datetime(2019, 8, 26)
        expected_data_2 = test_utils.get_test_data(test_data, ticker, dt_2)

        data = ((dt_1, expected_data_1), (dt_2, expected_data_2))

        self.database.bulk_update_market_data(ticker, data)

        data_series = self.database.get_equity_data_series(ticker)
        self.assertEqual(len(data_series), 2)

        self.assertEqual(dt_1, data_series[0][0])
        self.assertEqual(expected_data_1, data_series[0][1])

        self.assertEqual(dt_2, data_series[1][0])
        self.assertEqual(expected_data_2, data_series[1][1])
示例#8
0
    def test_insert_duplicate_security_does_not_erase_existing_data(self):
        ticker = 'AMZN'
        self.database.insert_securities([ticker])
        test_data = test_utils.load_test_data()

        dt = datetime.datetime(2019, 8, 27)
        expected_data = test_utils.get_test_data(test_data, ticker, dt)

        self.database.update_market_data(ticker, (dt, expected_data))

        actual_data = self.database.get_equity_data(ticker, dt)
        self.assertEqual(expected_data, actual_data)

        self.database.insert_securities([ticker])

        actual_data = self.database.get_equity_data(ticker, dt)
        self.assertEqual(expected_data, actual_data)
示例#9
0
    def test_can_retreive_equity_data_on_app_reopen(self, mock_scrape):
        # Carol opens up the application and add
        # adds Amazon (AMZN) to the list of
        # securities she wants to start tracking
        # and then closes the app.
        app = MarketData()
        app.run(database=self.database)
        new_tickers = ['AMZN', 'TLS.AX']
        for ticker in new_tickers:
            app.add_security(ticker)
        app.close()

        # Jarvis opens up the application and
        # goes through the list of securities
        # and updates them for the latest
        # market data
        app2 = MarketData()
        app2.run(database=self.database)
        tickers = app2.get_securities_list()
        self.assertEqual(set(new_tickers), set(tickers))

        dt = datetime.datetime(2019, 8, 27)
        expected_data = test_utils.get_test_data(self.test_data, 'AMZN', dt)
        mock_scrape.return_value = expected_data
        for ticker in tickers:
            app2.update_market_data(ticker, dt)

        app2.close()

        # Josh now opens the app to checks today's closing
        # price of Amazon after he comes back from work
        app3 = MarketData()
        app3.run(database=self.database)
        actual_data = app3.get_equity_data('AMZN', dt)
        self.assertEqual(expected_data, actual_data)
        app3.close()
示例#10
0
    def test_bulk_update_market_data_and_get_equity_data(self, mock_scraper):
        self.app.add_security(self.ticker)

        date_list = [
            datetime.datetime(2019, 8, 27),
            datetime.datetime(2019, 8, 26)
        ]
        params = zip([self.ticker] * 2, date_list)
        test_data = test_utils.load_test_data()

        expected_data = []
        for ticker, dt in params:
            data = test_utils.get_test_data(test_data, ticker, dt)
            expected_data.append((dt, data))

        mock_scraper.return_value = expected_data, []
        errors = self.app.bulk_update_market_data(self.ticker, date_list)

        self.assertEqual(len(errors), 0)

        self.app.close()

        new_app = MarketData()
        new_app.run(database=self.database)

        self.check_equity_data(params, expected_data)

        # NOTE(steve): should this be in a separate test???
        data_series = new_app.get_equity_data_series(self.ticker)
        self.assertEqual(len(data_series), 2)

        for i in range(2):
            self.assertEqual(expected_data[i][0], data_series[i][0])
            self.assertEqual(expected_data[i][1], data_series[i][1])

        new_app.close()
示例#11
0
    def test_update_security_data_on_multiple_dates(self, mock_scraper):
        # Load test data
        dataset = test_utils.load_test_data()
        ticker = 'AMZN'
        dt1 = datetime.datetime(2019, 8, 23)
        expected_data_dt1 = test_utils.get_test_data(dataset, ticker, dt1)
        dt2 = datetime.datetime(2019, 8, 26)
        expected_data_dt2 = test_utils.get_test_data(dataset, ticker, dt2)
        dt3 = datetime.datetime(2019, 8, 27)
        expected_data_dt3 = test_utils.get_test_data(dataset, ticker, dt3)
        data_series = [(dt3, expected_data_dt3), (dt2, expected_data_dt2),
                       (dt1, expected_data_dt1)]

        # Create an existing database with data already in the database
        self.da.create_test_database()
        data = self.da.connect(self.database)
        data.insert_securities([ticker])

        data.update_market_data(ticker, (dt1, expected_data_dt1))
        data.close()

        sys.argv = ['./cli.py', self.database]
        self.expected_output = []
        self.expected_output.append(
            app.Messages.load_existing_database(self.database))
        self.expected_output.append(app.Messages.main_menu())
        self.expected_output.append(app.Messages.option_input())

        ret_data = [(dt2.date(), expected_data_dt2),
                    (dt3.date(), expected_data_dt3)]
        mock_scraper.return_value = ret_data, []

        self.user_input.append(app.MenuOptions.UPDATE_MARKET_DATA)
        self.expected_output.append(app.Messages.market_data_updated())
        self.expected_output.append(app.Messages.main_menu())
        self.expected_output.append(app.Messages.option_input())

        self.user_input.append(app.MenuOptions.VIEW_SECURITIES)
        self.expected_output.append(app.Messages.view_securities(['AMZN']))
        self.expected_output.append(app.Messages.option_input())

        self.user_input.append('1')
        self.expected_output.append(
            app.Messages.view_security_data(ticker, data_series))
        self.expected_output.append(app.Messages.any_key_to_return())

        self.user_input.append('')
        self.expected_output.append(app.Messages.view_securities(['AMZN']))
        self.expected_output.append(app.Messages.option_input())
        self.user_input.append('0')
        self.expected_output.append(app.Messages.main_menu())
        self.expected_output.append(app.Messages.option_input())
        self.user_input.append(app.MenuOptions.QUIT)
        self.expected_output.append(app.Messages.quit())

        app.main()

        check_output(self.actual_output, self.expected_output)

        # Check that scraper was called
        mock_scraper.assert_called_once()