Exemple #1
0
 def test_get_fp(self):
     with TemporaryDirectory() as td:
         filepath = os.path.join(td, 'test')
         with open(filepath, 'w') as f:
             f.write('\0')
         table = pddb.PandasTable(filepath)
         fp = table._get_fp()
         fp.close()
         assert fp.mode == 'rb'
Exemple #2
0
    def test_panel(self):
        """
        Overview test that panel bundle saving works
        """
        with TemporaryDirectory() as td:
            b.save_panel(panel, td)

            test = b.load_panel(td)
            tm.assert_panel_equal(panel, test)
Exemple #3
0
    def test_init_df(self):
        with TemporaryDirectory() as td:
            filepath = os.path.join(td, 'test')
            df = pd.DataFrame({'test':range(10)})
            table = pddb.PandasTable(filepath)
            table.init_df(df)

            assert df is table._df

            data = tm.TestStringIO(pickle.dumps(df))

            table = pddb.PandasTable(filepath)
            table._init_df(data)
            assert tm.assert_almost_equal(df, table._df)
    def test_tuple_frame_key(self):
        """
            Moved OBT to default to directory format. Test the the meta is working
        """
        # I added this awhile back to remind myself to add this. It is
        # broken. skipping this test for now
        return
        with TemporaryDirectory() as td:
            store = tb.OBTFile(td + '/test', 'w', 'symbol', type='directory')
            store[('AAPL', 5902)] = df
            store.close()

            # reload
            store = tb.OBTFile(td + '/test')
Exemple #5
0
    def test_save(self):
        with TemporaryDirectory() as td:
            filepath = os.path.join(td, 'test')

            df = pd.DataFrame({'test':range(10)})
            table = pddb.PandasTable(filepath)
            table._init_df(df)

            data = tm.TestStringIO()
            table.save(data)

            data.seek(0)
            new_table = pickle.loads(data.read())
            assert tm.assert_almost_equal(new_table._df, df)
            data.free()
    def test_directory_meta(self):
        """
            Moved OBT to default to directory format. Test the the meta is working
        """
        with TemporaryDirectory() as td:
            store = tb.OBTFile(td + '/test', 'w', 'symbol', type='directory')
            store['AAPL'] = df
            store.handle.meta('testtest', 123)
            store.obt.meta('testtest', 456)
            store.close()

            # reload
            store = tb.OBTFile(td + '/test')
            assert store.handle.meta('testtest') == 123
            assert store.obt.meta('testtest') == 456
    def test_directory_meta(self):
        """
            Moved HDFFile to directoryl. Test that meta is workihng
        """
        with TemporaryDirectory() as td:
            store = tb.HDFFile(td + '/test', 'w', type='directory')
            store['AAPL'] = df
            store.handle.meta('testtest', 123)
            store.table.meta('testtest', 456)
            store.close()

            # reload
            store = tb.HDFFile(td + '/test')
            assert store.handle.meta('testtest') == 123
            assert store.table.meta('testtest') == 456
Exemple #8
0
    def test_bundle_io(self):
        """
            Test saving bundle io. 
        """
        N = 10000
        data = {}
        data['AAPL'] = tm.fake_ohlc(N)
        data['AMD'] = tm.fake_ohlc(N).tail(100)
        data['INTC'] = tm.fake_ohlc(N).head(100)
        cp = ColumnPanel(data)

        with TemporaryDirectory() as td:
            path = td + 'TEST.columnpanel'
            cp.bundle_save(path)

            loaded = cp.bundle_load(path)
            tm.assert_columnpanel_equal(cp, loaded)
Exemple #9
0
    def test_meta(self):
        key1 = MetaKey('dale')
        key2 = MetaKey('bob')

        with TemporaryDirectory() as td:
            mfc = fc.MetaFileCache(td)
            mfc[key1] = Value('daledata')
            mfc[key2] = Value('bobdata')
            mfc[key1] = Value('daledata2')

            # grabbing with key still works
            assert mfc[key1].string == 'daledata2'

            # this one should load keys from index file
            mfc2 = fc.MetaFileCache(td)
            for a, b in zip(list(mfc2.keys()), list(mfc.keys())):
                assert a.key == b.key
Exemple #10
0
    def test_save(self):
        with TemporaryDirectory() as td:
            filepath = os.path.join(td, 'test')

            df = pd.DataFrame({'test':list(range(10))})
            table = pddb.PandasTable(filepath)
            table._init_df(df)

            # save to string
            data = tm.TestStringIO()
            table.save(data)

            # PandasTable saves as df
            data.seek(0)
            new_table = pickle.loads(data.read())
            assert tm.assert_almost_equal(new_table.values, df.values)
            data.free()

            # save to disk
            table.save()
            pandas_table = pddb.PandasTable(filepath)
            assert tm.assert_almost_equal(pandas_table._df.values, df.values)
Exemple #11
0
    def test_append_table(self):
        with TemporaryDirectory() as td:
            store = tb.OBTFile(td + '/test.h5', 'w', 'symbol')
            store['AAPL'] = df

            # Pytable table
            table = store.obt.table.obj
            colnames = table.colnames
            # index name, columns, frame_key
            assert colnames == [
                'timestamp', 'open', 'high', 'low', 'close', 'vol',
                'other_dates', 'symbol'
            ]

            temp = store.ix['AAPL']
            tm.assert_frame_equal(temp,
                                  df,
                                  "dataframe returned from HDF is different",
                                  check_names=False)

            # this shoudl throw error
            bad_df = df.reindex(
                columns=['high', 'low', 'open', 'close', 'other_dates', 'vol'])
            try:
                store['BAD'] = bad_df
            except tb.MismatchColumnsError as e:
                pass  # success
            else:
                assert False, 'this should return an error, columns in wrong order'

            # fails when obt doesn't check column order. stored wrong
            temp = store.ix['BAD']
            if len(temp):
                # if properly throwing erros, we should never reach here
                tm.assert_frame_equal(
                    temp, df, "dataframe returned from HDF is different")
import os.path

from unittest import TestCase

from trtools.io.cacher import cacher
from trtools.util.tempdir import TemporaryDirectory

td = TemporaryDirectory()

class TestClass(object):
    def __init__(self):
        self.count = 0

    @cacher(td.name + '/test/test_meth', method=True)
    def test_meth(self):
        self.count += 1
        return self.count

    def test_meth2(self):
        self.count += 1
        return self.count

class TestCaching(TestCase):

    def __init__(self, *args, **kwargs):
        TestCase.__init__(self, *args, **kwargs)

    def runTest(self):
        pass

    def setUp(self):
Exemple #13
0
 def test_create(self):
     with TemporaryDirectory() as td:
         filepath = os.path.join(td, 'test')
         df = pd.DataFrame({'test':range(10)})
         table = pddb.PandasTable(filepath)
         table._init_df(df)