def test_triggers(self, schema=None):
        c = self.cube
        query = c.query.axis(c.d['time'].l['month'],
                c.d['store'].l['region'],
                c.d['product'].l['All'])
        builder = AggBuilder(query)
        builder.build(with_trigger=True, schema=schema)
        old_total_qty = c.query.axis().execute()["Quantity"]
        old_total_fact_count = c.query.axis().execute()["FACT_COUNT"]
        # Test with a value already in the agg table
        c.table.insert({
            'store_id': 1,
            'product_id': 2,
            'date': '2009-01-12',
            'qty': 200,
            'price': 1000}).execute()
        oldaggs = c.aggregates
        c.aggregates = []
        result = c.query.axis().execute()
        c.aggregates = oldaggs
        newresult = c.query.axis().execute()

        assert 'facts_table' not in str(c.query._as_sql())
        assert newresult == result
        assert newresult["Quantity"] == old_total_qty + 200
        assert newresult["FACT_COUNT"] == old_total_fact_count + 200
        # Test with a new value (insert statement)
        c.table.insert({
            'store_id': 1,
            'product_id': 2,
            'date': '2020-01-12',
            'qty': 200,
            'price': 1000}).execute()
        oldaggs = c.aggregates
        c.aggregates = []
        result = c.query.axis().execute()
        c.aggregates = oldaggs
        newresult = c.query.axis().execute()
        assert 'facts_table' not in str(c.query._as_sql())
        assert newresult == result
        assert newresult["Quantity"] == old_total_qty + 400
        res = c.query.axis(c.d['time'].l['year']).filter(
                c.d['time'].l['year'].member_by_label('2020')).execute()
        assert len(res) == 1
        assert res.by_label()['2020'].Quantity == 200
        assert res.by_label()['2020'].FACT_COUNT == 200
 def test_builder(self):
     c = self.cube
     query = c.query.axis(c.d['time'].l['month'],
             c.d['store'].l['region'])
     facts_table_result = query.execute()
     other_query = c.query.axis(c.d['time'].l['year'])
     facts_table_other_result = other_query.execute()
     builder = AggBuilder(query)
     builder.build()
     agg_expected_name = 'agg_time_month_store_region'
     assert ([agg.selectable.name for agg in c.aggregates] ==
             [agg_expected_name])
     sql_query = str(query._as_sql())
     assert agg_expected_name in sql_query
     assert c.table.name not in sql_query
     assert query.execute() == facts_table_result
     sql_query = str(other_query._as_sql())
     assert agg_expected_name in sql_query
     assert c.table.name not in sql_query
     assert other_query.execute() == facts_table_other_result