Пример #1
0
def test_db_tableinfo():
    ti = db.TableInfo("TblLogOpr_")
    assert ti.name == 'TblLogOpr_'
    assert ti == 'TblLogOpr_'
    assert 'TblLogOpr_' == ti
    assert ti + '20151121' == 'TblLogOpr_20151121'
    assert ti + u'20151121' == 'TblLogOpr_20151121'
    with pytest.raises(NotImplementedError):
        assert ti + 3
    assert 'asdf_' + ti == 'asdf_TblLogOpr_'
    assert str(ti) == 'TblLogOpr_'
    assert ti.replace('_', '[_]') == 'TblLogOpr[_]'

    ti += '20151121'
    ti.icols = ['cId', 'cDateReg', 'cMajorType']
    with db.Connector(dcfg) as con:
        ti.build_columns(con)
        assert len(ti.columns) == 3
        assert len(ti.types) == 3

    ti = db.TableInfo("TblMissionPlayLogOpr_20151122")
    ti.ecols = ['cWorldNo', 'cSvrNo']
    with db.Connector(dcfg) as con:
        ti.build_columns(con)
        assert len(ti.columns) == 26
        assert len(ti.types) == 26
Пример #2
0
def test_dump_by_dates():
    with db.Connector(dcfg) as con:
        dates = db.collect_dates(con, False)
        assert dates == [
            datetime(2015, 11, 21),
            datetime(2015, 11, 22),
            datetime(2015, 11, 23),
            datetime(2015, 11, 24)
        ]

        tables = db.daily_tables_from_dates(con, dates)
        assert tables == \
            [
                [
                    u'TblHackLogOpr_20151121',
                    u'TblLogOpr_20151121',
                    u'TblMissionPlayLogOpr_20151121',
                ],
                [
                    u'TblHackLogOpr_20151122',
                    u'TblLogOpr_20151122',
                    u'TblMissionPlayLogOpr_20151122',
                ],
                [
                    u'TblHackLogOpr_20151123',
                    u'TblLogOpr_20151123',
                    u'TblMissionPlayLogOpr_20151123',
                ],
                [
                    u'TblHackLogOpr_20151124',
                    u'TblLogOpr_20151124',
                    u'TblMissionPlayLogOpr_20151124',
                ],
            ]
Пример #3
0
def test_dump_db():
    dump.clean_info(dcfg)
    # no dump result check
    with db.Connector(dcfg) as con:
        dtbc = db.daily_tables_by_change(dcfg, con)
        assert dtbc == \
            [
                [
                    u'TblHackLogOpr_20151121',
                    u'TblLogOpr_20151121',
                    u'TblMissionPlayLogOpr_20151121',
                ],
                [
                    u'TblHackLogOpr_20151122',
                    u'TblLogOpr_20151122',
                    u'TblMissionPlayLogOpr_20151122',
                ],
                [
                    u'TblHackLogOpr_20151123',
                    u'TblLogOpr_20151123',
                    u'TblMissionPlayLogOpr_20151123',
                ],
            ]
        daily_tables = db.daily_tables_from_dates(con, db.collect_dates(con))
        for tables in daily_tables:
            # 2 max fetch for speed
            dumped = db.dump_tables(dcfg, con, tables, 2)
            db.write_table_info(dcfg, dumped)

        # check dump result
        folder = dcfg['folder']
        files = os.listdir(folder)
        assert TABLE_INFO_FILE in files

        with open(os.path.join(folder, TABLE_INFO_FILE)) as f:
            tables = []
            for line in f:
                tables.append(line.split(':')[0])
            assert len(tables) == 9

        # check dumped files
        logopr = [
            fi for fi in files if ('_wdfwd_' not in fi and 'TblLogOpr' in fi
                                   and not fi.endswith('.swp'))
        ][0]
        dlm = dcfg['field_delimiter'].encode('utf8')

        # and its contents
        with open(os.path.join(folder, logopr), 'r') as f:
            # check header (cols)
            header = f.readline().strip()
            assert len(header.split(dlm)) == 3
            cAccId = f.readline().strip().split(dlm)[0]
            assert "17" == cAccId

        # modify table then detect change
        with db.TemporaryRemoveFirstRow(con, 'TblHackLogOpr_20151122'):
            changed_tables = db.daily_tables_by_change(dcfg, con)
            assert changed_tables == [['TblHackLogOpr_20151122']]
Пример #4
0
def check_dump_db_and_sync(dcfg, max_fetch=None):
    from wdfwd.dump import db
    logging.info("check_dump_db_and_sync")
    dumped = []
    # check changed daily tables
    with db.Connector(dcfg) as con:
        if con.no_daily_table:
            _dump_table_updates_n_sync(dcfg, con, dumped, max_fetch)
        else:
            _dump_daily_table_n_sync(dcfg, con, dumped, max_fetch)
    return dumped
Пример #5
0
def test_dump_daily_table_and_sync():
    dump.clean_info(dcfg)
    dumped = dump.check_dump_db_and_sync(dcfg, 2)
    assert len(dumped) > 0

    # delete a row and check dump info
    with db.Connector(dcfg) as con:
        with db.TemporaryRemoveFirstRow(con, 'TblHackLogOpr_20151122'):
            ct = db.daily_tables_by_change(dcfg, con)
            ct = [str(t) for t in ct[0]]
            assert ct == ['TblHackLogOpr_20151122']
Пример #6
0
def test_dump_table_updates_and_sync():
    dcfg2 = write_eloa_cfg(dcfg)
    dump.clean_info(dcfg2)
    dumped = dump.check_dump_db_and_sync(dcfg2, 2)
    assert len(dumped) == 4

    # delete a row and check dump info
    with db.Connector(dcfg2) as con:
        with db.TemporaryRemoveFirstRow(con, 'ChatingLog_TBL'):
            ut = [t for t in db.updated_day_tables(dcfg2, con, '2015-11-23')]
            ut = [str(t) for t in ut]
            assert ut == ['ChatingLog_TBL']
Пример #7
0
def test_db_temp_del():
    with db.Connector(dcfg) as con:
        table = "TblHackLogOpr_20151122"

        def row_cnt():
            db.execute(con, "SELECT COUNT(*) FROM %s" % table)
            return con.cursor.fetchone()[0]

        prev_cnt = row_cnt()
        with db.TemporaryRemoveFirstRow(con, table):
            new_cnt = row_cnt()

        assert prev_cnt - new_cnt == 1
        rol_cnt = row_cnt()
        assert prev_cnt == rol_cnt
Пример #8
0
def test_dump_by_names():
    with db.Connector(dcfg) as con:
        tables = db.tables_by_names(con, False)
        assert tables == \
            [
                u'TblHackLogOpr_20151122', u'TblHackLogOpr_20151123',
                u'TblLogOpr_20151121', u'TblLogOpr_20151122',
                u'TblLogOpr_20151123',
                u'TblMissionPlayLogOpr_20151122',
                u'TblMissionPlayLogOpr_20151123',
                u'TblMissionPlayLogOpr_20151124',
            ]
        tables = db.tables_by_names(con)
        assert tables == \
            [
                u'TblHackLogOpr_20151122',
                u'TblLogOpr_20151121', u'TblLogOpr_20151122',
                u'TblMissionPlayLogOpr_20151122',
                u'TblMissionPlayLogOpr_20151123',
            ]
Пример #9
0
def test_db_basic():
    with db.Connector(dcfg) as con:
        assert con.cursor is not None

        # fetch all tables with matching name
        tbnames = dbc['table']['names']
        subtables = db.table_array(con, tbnames[0])
        assert len(subtables) == 2

        # parse table date
        date = db.get_table_date(con, subtables[0])
        assert date == datetime(2015, 11, 22)

        ftable = subtables[0]
        ti = db.TableInfo(ftable)
        ti.build_columns(con)
        # select rows with fetch size
        gen = db.table_rows(con, ti)
        assert len(gen.next()) == dbc['fetchsize']

        # fast fetch table row count
        assert db.get_table_rowcnt(con, ti) == 11790
Пример #10
0
def test_db_data_dates():
    dcfg2 = write_eloa_cfg(dcfg)
    with db.Connector(dcfg2) as con:
        dates = db.get_data_dates(con, False)
        assert dates == ['2015-11-23', '2015-11-24', '2015-11-25']
Пример #11
0
def test_db_connect():
    with db.Connector(dcfg) as con:
        print con.conn, con.cursor
Пример #12
0
def test_db_table_rowcnt_by_date():
    dcfg2 = write_eloa_cfg(dcfg)
    with db.Connector(dcfg2) as con:
        n_rows = db.table_rowcnt_by_date(con, 'CharStateLog_TBL', '2015-11-23')
        assert n_rows == 375476