def test_script_source(): source = SQLScriptSource(""" {% set product = PostgresRelation(["schema", "name", "table"]) %} CREATE TABLE {{product}} AS SELECT * FROM some_table """) params = Params._from_dict({'product': 'this should be ignored'}) assert source.render(params)
def test_doesnt_warn_if_relations_match(sql, split_source): product = SQLiteRelation((None, 'my_table', 'table')) source = SQLScriptSource(sql, split_source=split_source) with pytest.warns(None) as record: source.render({'product': product}) assert not record
def test_warns_if_no_create_statement_found(): product = SQLiteRelation((None, 'my_table', 'table')) source = SQLScriptSource(""" -- {{product}} without CREATE statement SELECT * FROM my_table GROUP BY some_column """) with pytest.warns(UserWarning) as record: source.render({'product': product}) assert len(record) == 1 msg = ('It appears that your script will not create any tables/views ' 'but the product parameter is ' "SQLiteRelation(('my_table', 'table'))") assert record[0].message.args[0] == msg
def test_cannot_initialize_sql_script_with_literals(): with pytest.raises(SourceInitializationError) as excinfo: SQLScriptSource('SELECT * FROM my_table') expected = ( "Error initializing SQLScriptSource('SELECT * FROM my_table'): " "The {{product}} placeholder is required. " "Example: 'CREATE TABLE {{product}} AS (SELECT * FROM ...)'") assert expected == str(excinfo.value)
def test_warns_if_inferred_relations_do_not_match_product(): product = SQLiteRelation((None, 'my_table', 'table')) source = SQLScriptSource(""" -- using {{product}} in the wrong place CREATE TABLE some_table AS SELECT * FROM another_table """) with pytest.warns(UserWarning) as record: source.render({'product': product}) assert len(record) == 1 msg = ('It appears that your script will create relations ' '{ParsedSQLRelation((\'some_table\', \'table\'))}, ' 'which doesn\'t match ' 'products: {SQLiteRelation((\'my_table\', \'table\'))}. ' 'Make sure schema, ' 'name and kind (table or view) match') assert record[0].message.args[0] == msg
def test_warns_if_number_of_relations_does_not_match_number_of_products(): product = [ SQLiteRelation((None, 'my_table', 'table')), SQLiteRelation((None, 'another_table', 'table')) ] source = SQLScriptSource(""" CREATE TABLE {{product[0]}} AS SELECT * FROM my_table GROUP BY some_column """) with pytest.warns(UserWarning) as record: source.render({'product': product}) assert len(record) == 1 msg = ('It appears that your script will create 1 relation(s) ' 'but you declared 2 product(s): ' "[SQLiteRelation(('my_table', 'table')), " "SQLiteRelation(('another_table', 'table'))]") assert record[0].message.args[0] == msg
def test_cannot_initialize_sql_script_with_literals(): with pytest.raises(SourceInitializationError): SQLScriptSource('SELECT * FROM my_table')
def _init_source(source, kwargs): return SQLScriptSource(source, **kwargs)
def _init_source(self, source): return SQLScriptSource(source)