Exemple #1
0
def test_input_min_date():
    with testing.postgresql.Postgresql() as psql:
        engine = sqlalchemy.create_engine(psql.url())
        engine.execute("create table events (entity_id int, date date, outcome bool)")
        for event in events_data:
            engine.execute("insert into events values (%s, %s, %s::bool)", event)

        engine.execute("create table states (entity_id int, date date)")
        for state in state_data:
            engine.execute("insert into states values (%s, %s)", state)

        agg = Aggregate(
            "outcome::int",
            ["sum", "avg"],
            {
                "coltype": "aggregate",
                "avg": {"type": "mean"},
                "sum": {"type": "constant", "value": 3},
                "max": {"type": "zero"},
            },
        )
        st = SpacetimeAggregation(
            aggregates=[agg],
            from_obj="events",
            groups=["entity_id"],
            intervals=["all"],
            dates=["2016-01-01"],
            state_table="states",
            state_group="entity_id",
            date_column='"date"',
            input_min_date="2015-11-10",
        )

        st.execute(engine.connect())

        r = engine.execute("select * from events_entity_id order by entity_id")
        rows = [x for x in r]

        assert rows[0]["entity_id"] == 1
        assert rows[0]["date"] == date(2016, 1, 1)
        assert rows[0]["events_entity_id_all_outcome::int_sum"] == 1
        assert rows[0]["events_entity_id_all_outcome::int_avg"] == 1
        assert rows[1]["entity_id"] == 4
        assert rows[1]["date"] == date(2016, 1, 1)
        assert rows[1]["events_entity_id_all_outcome::int_sum"] == 0
        assert rows[1]["events_entity_id_all_outcome::int_avg"] == 0

        assert len(rows) == 2

        st = SpacetimeAggregation(
            aggregates=[agg],
            from_obj="events",
            groups=["entity_id"],
            intervals=["1y", "all"],
            dates=["2016-01-01", "2015-01-01"],
            state_table="states",
            state_group="entity_id",
            date_column='"date"',
            input_min_date="2014-11-10",
        )
        with pytest.raises(ValueError):
            st.validate(engine.connect())
        with pytest.raises(ValueError):
            st.execute(engine.connect())
Exemple #2
0
def test_input_min_date():
    with testing.postgresql.Postgresql() as psql:
        engine = sqlalchemy.create_engine(psql.url())
        engine.execute(
            'create table events (entity_id int, date date, outcome bool)'
        )
        for event in events_data:
            engine.execute(
                'insert into events values (%s, %s, %s::bool)',
                event
            )

        engine.execute(
            'create table states (entity_id int, date date)'
        )
        for state in state_data:
            engine.execute(
                'insert into states values (%s, %s)',
                state
            )

        agg = Aggregate('outcome::int', ['sum','avg'], {
            "coltype": "aggregate",
            "avg": {"type": "mean"},
            "sum": {"type": "constant", "value": 3},
            "max": {"type": "zero"}
        })
        st = SpacetimeAggregation(
            aggregates = [agg],
            from_obj = 'events',
            groups = ['entity_id'],
            intervals = ['all'],
            dates = ['2016-01-01'],
            state_table = 'states',
            state_group = 'entity_id',
            date_column = '"date"',
            input_min_date = '2015-11-10'
        )

        st.execute(engine.connect())

        r = engine.execute('select * from events_entity_id order by entity_id')
        rows = [x for x in r]

        assert rows[0]['entity_id'] == 1
        assert rows[0]['date'] == date(2016, 1, 1)
        assert rows[0]['events_entity_id_all_outcome::int_sum'] == 1
        assert rows[0]['events_entity_id_all_outcome::int_avg'] == 1
        assert rows[1]['entity_id'] == 4
        assert rows[1]['date'] == date(2016, 1, 1)
        assert rows[1]['events_entity_id_all_outcome::int_sum'] == 0
        assert rows[1]['events_entity_id_all_outcome::int_avg'] == 0

        assert len(rows) == 2

        st = SpacetimeAggregation(
            aggregates = [agg],
            from_obj = 'events',
            groups = ['entity_id'],
            intervals = ['1y', 'all'],
            dates = ['2016-01-01', '2015-01-01'],
            state_table = 'states',
            state_group = 'entity_id',
            date_column = '"date"',
            input_min_date = '2014-11-10'
        )
        with pytest.raises(ValueError):
            st.validate(engine.connect())
        with pytest.raises(ValueError):
            st.execute(engine.connect())