Exemplo n.º 1
0
class TestCache(unittest.TestCase):

    CACHE_PATH = '_local_test_cache'

    def setUp(self):
        cache = LocalFsCache(TestCache.CACHE_PATH)
        self.src = _MockSource()
        self.ds = CachedDatasource(self.src, cache)
        self.pcontract = PContract.from_string('000001.SH-1.DAY')

    def tearDown(self):
        shutil.rmtree(TestCache.CACHE_PATH)

    def test1(self):
        self.ds.get_bars(self.pcontract, '2010-3-1', '2010-6-1')
        self.assertTrue(len(self.src.log) > 0, '没有访问数据源!')
        rec = self.src.log[-1]
        self.assertTrue(dt_eq(rec.start, '2010-3-1') and
                        dt_eq(rec.end, '2010-6-1'),
                        '访问的时间范围不正确!')
        self.src.log = []

        self.ds.get_bars(self.pcontract, '2010-3-1', '2010-6-1')
        self.assertTrue(len(self.src.log) == 0, '访问缓存失败!')

        self.ds.get_bars(self.pcontract, '2010-2-15', '2010-6-1')
        self.assertTrue(len(self.src.log) > 0 and
                        dt_eq(self.src.log[-1].start, '2010-2-15') and
                        dt_eq(self.src.log[-1].end, '2010-2-28'),
                        '访问数据源的时间范围不正确!')
        self.src.log = []

        self.ds.get_bars(self.pcontract, '2010-3-1', '2010-7-1')
        self.assertTrue(len(self.src.log) > 0 and
                        dt_eq(self.src.log[-1].start, '2010-6-2') and
                        dt_eq(self.src.log[-1].end, '2010-7-1'),
                        '访问数据源的时间范围不正确!')
        self.src.log = []
Exemplo n.º 2
0
def CachedTuShareSource(cache_path):
    return CachedDatasource(TuShareSource(), LocalFsCache(cache_path))
Exemplo n.º 3
0
 def setUp(self):
     cache = LocalFsCache(TestCache.CACHE_PATH)
     self.src = _MockSource()
     self.ds = CachedDatasource(self.src, cache)
     self.pcontract = PContract.from_string('000001.SH-1.DAY')