# Path: databot/db/serializers.py
# def serrow(key, value, compression=None, **kwargs):
#     value = servalue(key, value, compression)
#     compression = None if compression is None else int(compression.value)
#     return dict(kwargs, key=serkey(key), value=value, compression=compression)


import datetime
import pytest
from databot.db.serializers import serrow

def exclude(data, keys):
    return {k: v for k, v in data.items() if k not in keys}


@pytest.fixture
def bot(db):
    bot = db.Bot().main(argv=['-v0', 'run'])
    bot.define('pipe 1')
    bot.define('pipe 2')
    return bot


def test_append(bot):
    t1 = bot.pipe('pipe 1')
    t1.append('foo', 'bar').append('a', 'b')
    data = [(row['key'], row['value']) for row in bot.engine.execute(t1.table.select())]
    assert data == [
        ('bd4339d24800a1815bb1b7382320cab96bba0cde', b'\x92\xa3foo\xa3bar'),
        ('9eddb6860ecf7699015ffff67e3b375820a48cad', b'\x92\xa1a\xa1b'),
    ]


def test_clean(bot):
    t1 = bot.pipe('pipe 1')
    t2 = bot.pipe('pipe 2')

    day = datetime.timedelta(days=1)
    now = datetime.datetime(2015, 6, 1, 1, 1, 0)
    bot.engine.execute(t1.table.insert(), serrow('1', 'a', created=now - 1 * day))

# Path: databot/db/serializers.py
# def serrow(key, value, compression=None, **kwargs):
#     value = servalue(key, value, compression)
#     compression = None if compression is None else int(compression.value)
#     return dict(kwargs, key=serkey(key), value=value, compression=compression)


# Path: tests/test_storage.py
import datetime
import pytest
from databot.db.serializers import serrow


def exclude(data, keys):
    return {k: v for k, v in data.items() if k not in keys}


@pytest.fixture
def bot(db):
    bot = db.Bot().main(argv=['-v0', 'run'])
    bot.define('pipe 1')
    bot.define('pipe 2')
    return bot


def test_append(bot):
    t1 = bot.pipe('pipe 1')
    t1.append('foo', 'bar').append('a', 'b')
    data = [(row['key'], row['value']) for row in bot.engine.execute(t1.table.select())]
    assert data == [
        ('bd4339d24800a1815bb1b7382320cab96bba0cde', b'\x92\xa3foo\xa3bar'),
        ('9eddb6860ecf7699015ffff67e3b375820a48cad', b'\x92\xa1a\xa1b'),
    ]


def test_clean(bot):
    t1 = bot.pipe('pipe 1')
    t2 = bot.pipe('pipe 2')

    day = datetime.timedelta(days=1)
    now = datetime.datetime(2015, 6, 1, 1, 1, 0)
    bot.engine.execute(t1.table.insert(), serrow('1', 'a', created=now - 1 * day))

    bot.engine.execute(t1.table.insert(), serrow('2', 'b', created=now - 2 * day))