def test_store_copy_from(conn, dataset):
    partition_size = 86400

    with closing(conn.cursor()) as cursor:
        trendstore = TrendStore(dataset.datasource, dataset.entitytype,
                dataset.granularity, partition_size, "table").create(cursor)

    conn.commit()

    timestamp = dataset.datasource.tzinfo.localize(
            datetime.datetime(2013, 4, 25, 9, 45))

    trends = ["a", "b", "c"]

    def make_row(index):
        return (1234 + index, [1, 2, 3 + index])

    rows = map(make_row, range(100))

    datapackage = DataPackage(dataset.granularity, timestamp, trends, rows)

    transaction = trendstore.store(datapackage)
    transaction.run(conn)

    transaction = trendstore.store(datapackage)
    transaction.run(conn)
def test_store_alter_column(conn, dataset):
    partition_size = 86400
    trendstore = TrendStore(dataset.datasource, dataset.entitytype, dataset.granularity,
            partition_size, "table")

    with closing(conn.cursor()) as cursor:
        trendstore.create(cursor)

    conn.commit()

    timestamp = dataset.datasource.tzinfo.localize(
            datetime.datetime(2013, 4, 25, 11, 00))

    trends = ["a", "b", "c"]

    rows = [
        (1234, [1, 2, 3]),
        (2345, [4, 5, 6])]

    datapackage = DataPackage(dataset.granularity, timestamp, trends, rows)

    transaction = trendstore.store(datapackage)
    transaction.run(conn)

    table = trendstore.partition(timestamp).table()

    condition = And(
        Eq(Column("entity_id"), 2345),
        Eq(Column("timestamp"), timestamp))

    query = table.select(Column("c")).where_(condition)

    with closing(conn.cursor()) as cursor:
        query.execute(cursor)

        c, = cursor.fetchone()

    eq_(c, 6)

    trends = ["a", "b", "c"]

    rows = [
        (2345, [4, 5, "2013-04-25 11:00:00"])]

    datapackage = DataPackage(dataset.granularity, timestamp, trends, rows)

    transaction = trendstore.store(datapackage)
    transaction.run(conn)

    with closing(conn.cursor()) as cursor:
        query.execute(cursor)

        c, = cursor.fetchone()

    eq_(c, datetime.datetime(2013, 4, 25, 11, 0, 0))