# Path: databot/db/windowedquery.py
# def windowed_query(engine, query, column, windowsize=1000):
#     """"Break a query into windows on a given column."""
#     name = engine.name if isinstance(engine, Engine) else engine.engine.name
#     if name == 'postgresql':
#         for whereclause in column_windows(engine, column, windowsize):
#             yield from list(engine.execute(query.where(whereclause).order_by(column)))
#     else:
#         for offset in offset_windows(engine, query, column, windowsize):
#             yield from list(engine.execute(query.order_by(column).offset(offset).limit(windowsize)))
# 
# Path: databot/db/serializers.py
# def dumps(value):
#     """Convert primitive value received from database to Python object."""
#     return msgpack.dumps(value, use_bin_type=True)


import pytest
from databot.db.windowedquery import windowed_query
from databot.db.serializers import dumps

def keys(result):
    return [int(row['key']) for row in result]


def populate(engine, table, keys):
    engine.execute(table.insert(), [row(key) for key in keys])


@pytest.fixture
def table(db):
    table = db.models.get_data_table('t1')
    db.meta.create_all()
    return table


def test_windowed_query(db, table):
    populate(db.engine, table, [1, 2, 3, 4])
    query = windowed_query(db.engine, table.select(), table.c.id)
    assert keys(query) == [1, 2, 3, 4]


def test_small_windowsize(db, table):
    populate(db.engine, table, [1, 2, 3, 4, 5])
    query = windowed_query(db.engine, table.select(), table.c.id, windowsize=2)
    assert keys(query) == [1, 2, 3, 4, 5]


def test_even_windowsize(db, table):
    populate(db.engine, table, [1, 2, 3, 4, 5, 6])

# Path: databot/db/windowedquery.py
# def windowed_query(engine, query, column, windowsize=1000):
#     """"Break a query into windows on a given column."""
#     name = engine.name if isinstance(engine, Engine) else engine.engine.name
#     if name == 'postgresql':
#         for whereclause in column_windows(engine, column, windowsize):
#             yield from list(engine.execute(query.where(whereclause).order_by(column)))
#     else:
#         for offset in offset_windows(engine, query, column, windowsize):
#             yield from list(engine.execute(query.order_by(column).offset(offset).limit(windowsize)))
# 
# Path: databot/db/serializers.py
# def dumps(value):
#     """Convert primitive value received from database to Python object."""
#     return msgpack.dumps(value, use_bin_type=True)


# Path: tests/test_windowed_query.py
import pytest
from databot.db.windowedquery import windowed_query
from databot.db.serializers import dumps


def keys(result):
    return [int(row['key']) for row in result]


def populate(engine, table, keys):
    engine.execute(table.insert(), [row(key) for key in keys])


@pytest.fixture
def table(db):
    table = db.models.get_data_table('t1')
    db.meta.create_all()
    return table


def test_windowed_query(db, table):
    populate(db.engine, table, [1, 2, 3, 4])
    query = windowed_query(db.engine, table.select(), table.c.id)
    assert keys(query) == [1, 2, 3, 4]


def test_small_windowsize(db, table):
    populate(db.engine, table, [1, 2, 3, 4, 5])
    query = windowed_query(db.engine, table.select(), table.c.id, windowsize=2)
    assert keys(query) == [1, 2, 3, 4, 5]


def test_even_windowsize(db, table):
    populate(db.engine, table, [1, 2, 3, 4, 5, 6])

    query = windowed_query(db.engine, table.select(), table.c.id, windowsize=2)