<s> import datetime <EOL> from fleming import unix_time , floor <EOL> from querybuilder . fields import SimpleField , LagDifferenceField , LeadDifferenceField , AllTime , SumField , Week <EOL> from querybuilder . query import Query , QueryWindow <EOL> from querybuilder . tests . models import Order <EOL> from querybuilder . tests . query_tests import QueryTestCase <EOL> class FieldTest ( QueryTestCase ) : <EOL> def test_cast ( self ) : <EOL> """<STR_LIT>""" <EOL> query = Query ( ) . from_table ( <EOL> table = Order , <EOL> fields = [ SimpleField ( field = '<STR_LIT>' , cast = '<STR_LIT>' ) ] <EOL> ) . order_by ( '<STR_LIT>' ) . limit ( <NUM_LIT:1> ) <EOL> expected_query = '<STR_LIT>' <EOL> self . assertEqual ( expected_query , query . get_sql ( ) ) <EOL> rows = query . select ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( rows ) ) <EOL> self . assertEqual ( type ( rows [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) , int ) <EOL> def test_get_alias ( self ) : <EOL> """<STR_LIT>""" <EOL> field = SimpleField ( field = '<STR_LIT>' ) <EOL> query = Query ( ) . from_table ( table = Order , fields = [ field ] ) <EOL> expected_query = '<STR_LIT>' <EOL> self . assertEqual ( expected_query , query . get_sql ( ) ) <EOL> field . auto_alias = '<STR_LIT>' <EOL> query = Query ( ) . from_table ( table = Order , fields = [ field ] ) <EOL> expected_query = '<STR_LIT>' <EOL> self . assertEqual ( expected_query , query . get_sql ( ) ) <EOL> field . alias = '<STR_LIT>' <EOL> query = Query ( ) . from_table ( table = Order , fields = [ field ] ) <EOL> expected_query = '<STR_LIT>' <EOL> self . assertEqual ( expected_query , query . get_sql ( ) ) <EOL> query = Query ( ) . from_table ( <EOL> table = Order , <EOL> fields = [ field ] , <EOL> prefix_fields = True , <EOL> field_prefix = '<STR_LIT>' , <EOL> ) <EOL> expected_query = '<STR_LIT>' <EOL> self . assertEqual ( expected_query , query . get_sql ( ) ) <EOL> field . alias = None <EOL> field . auto_alias = None <EOL> query = Query ( ) . from_table ( <EOL> table = Order , <EOL> fields = [ field ] , <EOL> prefix_fields = True , <EOL> field_prefix = '<STR_LIT>' , <EOL> ) <EOL> expected_query = '<STR_LIT>' <EOL> self . assertEqual ( expected_query , query . get_sql ( ) ) <EOL> def lead_lag_difference_test ( self ) : <EOL> query = Query ( ) . from_table ( <EOL> table = Order , <EOL> fields = [ <EOL> '<STR_LIT>' , <EOL> LagDifferenceField ( '<STR_LIT>' , over = QueryWindow ( ) . order_by ( '<STR_LIT>' ) ) , <EOL> LeadDifferenceField ( '<STR_LIT>' , over = QueryWindow ( ) . order_by ( '<STR_LIT>' ) ) , <EOL> ] <EOL> ) <EOL> expected_query = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( expected_query , query . get_sql ( ) ) <EOL> rows = query . select ( ) <EOL> self . assertEqual ( <NUM_LIT:4> , len ( rows ) ) <EOL> self . assertEqual ( None , rows [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( <NUM_LIT> , rows [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( - <NUM_LIT> , rows [ <NUM_LIT:3> ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( None , rows [ <NUM_LIT:3> ] [ '<STR_LIT>' ] ) <EOL> def date_part_field_test ( self ) : <EOL> """<STR_LIT>""" <EOL> query = Query ( ) . from_table ( <EOL> table = Order , <EOL> fields = [ <EOL> AllTime ( '<STR_LIT:time>' ) , <EOL> SumField ( '<STR_LIT>' ) <EOL> ] <EOL> ) <EOL> expected_query = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( expected_query , query . get_sql ( ) ) <EOL> rows = query . select ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( rows ) ) <EOL> self . assertEqual ( <NUM_LIT> , rows [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( <NUM_LIT:0> , rows [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> def test_week_grouping ( self ) : <EOL> """<STR_LIT>""" <EOL> query = Query ( ) . from_table ( <EOL> table = Order , <EOL> fields = [ <EOL> Week ( '<STR_LIT:time>' , auto = True ) , <EOL> SumField ( '<STR_LIT>' ) <EOL> ] <EOL> ) <EOL> expected_query = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> self . assertEqual ( expected_query , query . get_sql ( ) ) <EOL> rows = query . select ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( rows ) ) <EOL> week_dt = datetime . datetime ( <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> ) <EOL> week_unix_time = unix_time ( floor ( week_dt , week = <NUM_LIT:1> ) ) <EOL> self . assertEqual ( <NUM_LIT> , rows [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( week_unix_time , rows [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( <NUM_LIT> , rows [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( <NUM_LIT> , rows [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) </s>
