コード例 #1
0
    def test_get_quotes(self, expired_mock, remote_mock, cache_mock):
        """get_quotes should retrieve quotes from cache if not expired"""
        inv = Investment("T1")
        quotes = [
            {
                "quote_date": date(year=2016, month=1, day=5),
                "close": 100
            },
            {
                "quote_date": date(year=2016, month=1, day=4),
                "close": 101
            },
            {
                "quote_date": date(year=2016, month=1, day=3),
                "close": 102
            },
            {
                "quote_date": date(year=2016, month=1, day=2),
                "close": 103
            },
            {
                "quote_date": date(year=2016, month=1, day=1),
                "close": 104
            },
        ]

        cache_mock.return_value = {"fetch_time": 1234, "quotes": quotes[:3]}
        expired_mock.return_value = False

        self.assertEquals(inv.get_quotes(), quotes[:3][::-1])

        expired_mock.return_value = True
        remote_mock.return_value = {"fetch_time": 12345, "quotes": quotes}

        self.assertEquals(inv.get_quotes(), quotes[::-1])
コード例 #2
0
    def test_get_quotes(self, cache_mock, remote_mock):
        """get_quotes should raise an exception if ticker is not valid"""
        inv = Investment("T1")

        cache_mock.return_value = None
        remote_mock.return_value = None

        with self.assertRaises(InvalidUsage):
            inv.get_quotes()
コード例 #3
0
    def test_get_quotes(self, cache_mock, remote_mock, expired_mock):
        """get_quotes should fetch quotes if expired"""
        inv = Investment("T1")

        cache_mock.return_value = {"fetch_time": 1234, "quotes": [1, 2, 3]}
        expired_mock.return_value = True
        remote_mock.return_value = {"fetch_time": 12345, "quotes": []}
        expired_mock.return_value = False

        self.assertEquals(inv.get_quotes(), [])
コード例 #4
0
    def test_get_quotes_fills_holes(self, expired_mock, remote_mock,
                                    cache_mock):
        """get_quotes should fill in missing entries in quotes"""
        inv = Investment("T1")
        quotes = [
            {
                "quote_date": date(year=2016, month=1, day=4),
                "close": 100
            },
            {
                "quote_date": date(year=2016, month=1, day=1),
                "close": 104
            },
        ]
        quotes_with_filled_holes = [
            {
                "quote_date": date(year=2016, month=1, day=1),
                "close": 104
            },
            {
                "quote_date": date(year=2016, month=1, day=2),
                "close": 104
            },
            {
                "quote_date": date(year=2016, month=1, day=3),
                "close": 104
            },
            {
                "quote_date": date(year=2016, month=1, day=4),
                "close": 100
            },
        ]

        cache_mock.return_value = {"fetch_time": 1234, "quotes": quotes}
        expired_mock.return_value = False

        self.assertEquals(inv.get_quotes(), quotes_with_filled_holes)