Example #1
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]))
Example #2
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])
Example #3
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
Example #4
0
    def test_get_latest_equity_data_for_security(self, mock_scraper):
        self.app.add_security(self.ticker)

        test_data = test_utils.load_test_data()

        dt_1 = datetime.datetime(2019, 8, 27)
        dt_2 = datetime.datetime(2019, 8, 26)
        dt_3 = datetime.datetime(2019, 8, 23)
        dt = [dt_2, dt_1, dt_3]  # not updated in order!
        params = zip([self.ticker] * 3, dt)
        expected_data = self.update_with_test_data(params, mock_scraper)

        dt, actual_data = self.app.get_latest_equity_data(self.ticker)
        self.assertEqual(dt_1, dt)
        self.assertEqual(expected_data[1], actual_data)
Example #5
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)
Example #6
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)
Example #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])
Example #8
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()
Example #9
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()
Example #10
0
 def setUp(self):
     self.test_data = test_utils.load_test_data()
     self.da = data_adapter.get_adapter(self.data_adapter_source)
     self.database = MarketData.Database(self.da.test_database,
                                         self.data_adapter_source)
     self.da.create_test_database()