<s> from sqlalchemy import * <EOL> from sqlalchemy . types import TypeEngine <EOL> from sqlalchemy . sql . expression import ClauseElement , ColumnClause , FunctionElement , Select , BindParameter <EOL> from sqlalchemy . schema import DDLElement , CreateColumn , CreateTable <EOL> from sqlalchemy . ext . compiler import compiles , deregister <EOL> from sqlalchemy import exc <EOL> from sqlalchemy . sql import table , column , visitors <EOL> from sqlalchemy . testing import assert_raises_message <EOL> from sqlalchemy . testing import fixtures , AssertsCompiledSQL <EOL> class UserDefinedTest ( fixtures . TestBase , AssertsCompiledSQL ) : <EOL> __dialect__ = '<STR_LIT:default>' <EOL> def test_column ( self ) : <EOL> class MyThingy ( ColumnClause ) : <EOL> def __init__ ( self , arg = None ) : <EOL> super ( MyThingy , self ) . __init__ ( arg or '<STR_LIT>' ) <EOL> @ compiles ( MyThingy ) <EOL> def visit_thingy ( thingy , compiler , ** kw ) : <EOL> return "<STR_LIT>" % thingy . name <EOL> self . assert_compile ( <EOL> select ( [ column ( '<STR_LIT:foo>' ) , MyThingy ( ) ] ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> self . assert_compile ( <EOL> select ( [ MyThingy ( '<STR_LIT:x>' ) , MyThingy ( '<STR_LIT:y>' ) ] ) . where ( MyThingy ( ) == <NUM_LIT:5> ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_create_column_skip ( self ) : <EOL> @ compiles ( CreateColumn ) <EOL> def skip_xmin ( element , compiler , ** kw ) : <EOL> if element . element . name == '<STR_LIT>' : <EOL> return None <EOL> else : <EOL> return compiler . visit_create_column ( element , ** kw ) <EOL> t = Table ( '<STR_LIT:t>' , MetaData ( ) , Column ( '<STR_LIT:a>' , Integer ) , <EOL> Column ( '<STR_LIT>' , Integer ) , <EOL> Column ( '<STR_LIT:c>' , Integer ) ) <EOL> self . assert_compile ( <EOL> CreateTable ( t ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_types ( self ) : <EOL> class MyType ( TypeEngine ) : <EOL> pass <EOL> @ compiles ( MyType , '<STR_LIT>' ) <EOL> def visit_type ( type , compiler , ** kw ) : <EOL> return "<STR_LIT>" <EOL> @ compiles ( MyType , '<STR_LIT>' ) <EOL> def visit_type ( type , compiler , ** kw ) : <EOL> return "<STR_LIT>" <EOL> from sqlalchemy . dialects . sqlite import base as sqlite <EOL> from sqlalchemy . dialects . postgresql import base as postgresql <EOL> self . assert_compile ( <EOL> MyType ( ) , <EOL> "<STR_LIT>" , <EOL> dialect = sqlite . dialect ( ) <EOL> ) <EOL> self . assert_compile ( <EOL> MyType ( ) , <EOL> "<STR_LIT>" , <EOL> dialect = postgresql . dialect ( ) <EOL> ) <EOL> def test_stateful ( self ) : <EOL> class MyThingy ( ColumnClause ) : <EOL> def __init__ ( self ) : <EOL> super ( MyThingy , self ) . __init__ ( '<STR_LIT>' ) <EOL> @ compiles ( MyThingy ) <EOL> def visit_thingy ( thingy , compiler , ** kw ) : <EOL> if not hasattr ( compiler , '<STR_LIT>' ) : <EOL> compiler . counter = <NUM_LIT:0> <EOL> compiler . counter += <NUM_LIT:1> <EOL> return str ( compiler . counter ) <EOL> self . assert_compile ( <EOL> select ( [ column ( '<STR_LIT:foo>' ) , MyThingy ( ) ] ) . order_by ( desc ( MyThingy ( ) ) ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> self . assert_compile ( <EOL> select ( [ MyThingy ( ) , MyThingy ( ) ] ) . where ( MyThingy ( ) == <NUM_LIT:5> ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_callout_to_compiler ( self ) : <EOL> class InsertFromSelect ( ClauseElement ) : <EOL> def __init__ ( self , table , select ) : <EOL> self . table = table <EOL> self . select = select <EOL> @ compiles ( InsertFromSelect ) <EOL> def visit_insert_from_select ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" % ( <EOL> compiler . process ( element . table , asfrom = True ) , <EOL> compiler . process ( element . select ) <EOL> ) <EOL> t1 = table ( "<STR_LIT>" , column ( '<STR_LIT:x>' ) , column ( '<STR_LIT:y>' ) , column ( '<STR_LIT:z>' ) ) <EOL> self . assert_compile ( <EOL> InsertFromSelect ( <EOL> t1 , <EOL> select ( [ t1 ] ) . where ( t1 . c . x > <NUM_LIT:5> ) <EOL> ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> def test_no_default_message ( self ) : <EOL> class MyThingy ( ColumnClause ) : <EOL> pass <EOL> @ compiles ( MyThingy , "<STR_LIT>" ) <EOL> def visit_thingy ( thingy , compiler , ** kw ) : <EOL> return "<STR_LIT>" <EOL> assert_raises_message ( <EOL> exc . CompileError , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> str , MyThingy ( '<STR_LIT:x>' ) <EOL> ) <EOL> def test_annotations ( self ) : <EOL> """<STR_LIT>""" <EOL> t1 = table ( '<STR_LIT>' , column ( '<STR_LIT>' ) , column ( '<STR_LIT>' ) ) <EOL> dispatch = Select . _compiler_dispatch <EOL> try : <EOL> @ compiles ( Select ) <EOL> def compile ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" <EOL> s1 = select ( [ t1 ] ) <EOL> self . assert_compile ( <EOL> s1 , "<STR_LIT>" <EOL> ) <EOL> self . assert_compile ( <EOL> s1 . _annotate ( { } ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> finally : <EOL> Select . _compiler_dispatch = dispatch <EOL> if hasattr ( Select , '<STR_LIT>' ) : <EOL> del Select . _compiler_dispatcher <EOL> def test_dialect_specific ( self ) : <EOL> class AddThingy ( DDLElement ) : <EOL> __visit_name__ = '<STR_LIT>' <EOL> class DropThingy ( DDLElement ) : <EOL> __visit_name__ = '<STR_LIT>' <EOL> @ compiles ( AddThingy , '<STR_LIT>' ) <EOL> def visit_add_thingy ( thingy , compiler , ** kw ) : <EOL> return "<STR_LIT>" <EOL> @ compiles ( AddThingy ) <EOL> def visit_add_thingy ( thingy , compiler , ** kw ) : <EOL> return "<STR_LIT>" <EOL> @ compiles ( DropThingy ) <EOL> def visit_drop_thingy ( thingy , compiler , ** kw ) : <EOL> return "<STR_LIT>" <EOL> self . assert_compile ( AddThingy ( ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> self . assert_compile ( DropThingy ( ) , <EOL> "<STR_LIT>" <EOL> ) <EOL> from sqlalchemy . dialects . sqlite import base <EOL> self . assert_compile ( AddThingy ( ) , <EOL> "<STR_LIT>" , <EOL> dialect = base . dialect ( ) <EOL> ) <EOL> self . assert_compile ( DropThingy ( ) , <EOL> "<STR_LIT>" , <EOL> dialect = base . dialect ( ) <EOL> ) <EOL> @ compiles ( DropThingy , '<STR_LIT>' ) <EOL> def visit_drop_thingy ( thingy , compiler , ** kw ) : <EOL> return "<STR_LIT>" <EOL> self . assert_compile ( DropThingy ( ) , <EOL> "<STR_LIT>" , <EOL> dialect = base . dialect ( ) <EOL> ) <EOL> self . assert_compile ( DropThingy ( ) , <EOL> "<STR_LIT>" , <EOL> ) <EOL> def test_functions ( self ) : <EOL> from sqlalchemy . dialects import postgresql <EOL> class MyUtcFunction ( FunctionElement ) : <EOL> pass <EOL> @ compiles ( MyUtcFunction ) <EOL> def visit_myfunc ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" <EOL> @ compiles ( MyUtcFunction , '<STR_LIT>' ) <EOL> def visit_myfunc ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" <EOL> self . assert_compile ( <EOL> MyUtcFunction ( ) , <EOL> "<STR_LIT>" , <EOL> use_default_dialect = True <EOL> ) <EOL> self . assert_compile ( <EOL> MyUtcFunction ( ) , <EOL> "<STR_LIT>" , <EOL> dialect = postgresql . dialect ( ) <EOL> ) <EOL> def test_function_calls_base ( self ) : <EOL> from sqlalchemy . dialects import mssql <EOL> class greatest ( FunctionElement ) : <EOL> type = Numeric ( ) <EOL> name = '<STR_LIT>' <EOL> @ compiles ( greatest ) <EOL> def default_greatest ( element , compiler , ** kw ) : <EOL> return compiler . visit_function ( element ) <EOL> @ compiles ( greatest , '<STR_LIT>' ) <EOL> def case_greatest ( element , compiler , ** kw ) : <EOL> arg1 , arg2 = list ( element . clauses ) <EOL> return "<STR_LIT>" % ( <EOL> compiler . process ( arg1 ) , <EOL> compiler . process ( arg2 ) , <EOL> compiler . process ( arg1 ) , <EOL> compiler . process ( arg2 ) , <EOL> ) <EOL> self . assert_compile ( <EOL> greatest ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , <EOL> '<STR_LIT>' , <EOL> use_default_dialect = True <EOL> ) <EOL> self . assert_compile ( <EOL> greatest ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> dialect = mssql . dialect ( ) <EOL> ) <EOL> def test_subclasses_one ( self ) : <EOL> class Base ( FunctionElement ) : <EOL> name = '<STR_LIT>' <EOL> class Sub1 ( Base ) : <EOL> name = '<STR_LIT>' <EOL> class Sub2 ( Base ) : <EOL> name = '<STR_LIT>' <EOL> @ compiles ( Base ) <EOL> def visit_base ( element , compiler , ** kw ) : <EOL> return element . name <EOL> @ compiles ( Sub1 ) <EOL> def visit_base ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" + element . name <EOL> self . assert_compile ( <EOL> select ( [ Sub1 ( ) , Sub2 ( ) ] ) , <EOL> '<STR_LIT>' , <EOL> use_default_dialect = True <EOL> ) <EOL> def test_subclasses_two ( self ) : <EOL> class Base ( FunctionElement ) : <EOL> name = '<STR_LIT>' <EOL> class Sub1 ( Base ) : <EOL> name = '<STR_LIT>' <EOL> @ compiles ( Base ) <EOL> def visit_base ( element , compiler , ** kw ) : <EOL> return element . name <EOL> class Sub2 ( Base ) : <EOL> name = '<STR_LIT>' <EOL> class SubSub1 ( Sub1 ) : <EOL> name = '<STR_LIT>' <EOL> self . assert_compile ( <EOL> select ( [ Sub1 ( ) , Sub2 ( ) , SubSub1 ( ) ] ) , <EOL> '<STR_LIT>' , <EOL> use_default_dialect = True <EOL> ) <EOL> @ compiles ( Sub1 ) <EOL> def visit_base ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" + element . name <EOL> self . assert_compile ( <EOL> select ( [ Sub1 ( ) , Sub2 ( ) , SubSub1 ( ) ] ) , <EOL> '<STR_LIT>' , <EOL> use_default_dialect = True <EOL> ) <EOL> class DefaultOnExistingTest ( fixtures . TestBase , AssertsCompiledSQL ) : <EOL> """<STR_LIT>""" <EOL> __dialect__ = '<STR_LIT:default>' <EOL> def teardown ( self ) : <EOL> for cls in ( Select , BindParameter ) : <EOL> deregister ( cls ) <EOL> def test_select ( self ) : <EOL> t1 = table ( '<STR_LIT>' , column ( '<STR_LIT>' ) , column ( '<STR_LIT>' ) ) <EOL> @ compiles ( Select , '<STR_LIT>' ) <EOL> def compile ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" <EOL> s1 = select ( [ t1 ] ) <EOL> self . assert_compile ( <EOL> s1 , "<STR_LIT>" , <EOL> ) <EOL> from sqlalchemy . dialects . sqlite import base as sqlite <EOL> self . assert_compile ( <EOL> s1 , "<STR_LIT>" , <EOL> dialect = sqlite . dialect ( ) <EOL> ) <EOL> def test_binds_in_select ( self ) : <EOL> t = table ( '<STR_LIT:t>' , <EOL> column ( '<STR_LIT:a>' ) , <EOL> column ( '<STR_LIT:b>' ) , <EOL> column ( '<STR_LIT:c>' ) <EOL> ) <EOL> @ compiles ( BindParameter ) <EOL> def gen_bind ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" % compiler . visit_bindparam ( element , ** kw ) <EOL> self . assert_compile ( <EOL> t . select ( ) . where ( t . c . c == <NUM_LIT:5> ) , <EOL> "<STR_LIT>" , <EOL> use_default_dialect = True <EOL> ) <EOL> def test_binds_in_dml ( self ) : <EOL> t = table ( '<STR_LIT:t>' , <EOL> column ( '<STR_LIT:a>' ) , <EOL> column ( '<STR_LIT:b>' ) , <EOL> column ( '<STR_LIT:c>' ) <EOL> ) <EOL> @ compiles ( BindParameter ) <EOL> def gen_bind ( element , compiler , ** kw ) : <EOL> return "<STR_LIT>" % compiler . visit_bindparam ( element , ** kw ) <EOL> self . assert_compile ( <EOL> t . insert ( ) , <EOL> "<STR_LIT>" , <EOL> { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } , <EOL> use_default_dialect = True <EOL> ) </s>
