<s> """<STR_LIT>""" <EOL> from datetime import datetime <EOL> from pytz import timezone <EOL> from pytz import UTC <EOL> from tztest . tztest . models import Timestamp <EOL> from django . conf import settings <EOL> DJANGO_TZ = timezone ( settings . TIME_ZONE ) <EOL> class SaveMethod ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tz , style ) : <EOL> assert style in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . tz = tz <EOL> self . style = style <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' . format ( self . style , self . tz . zone ) <EOL> def save ( self , dt ) : <EOL> dt = self . tz . normalize ( dt ) <EOL> if self . style == '<STR_LIT>' : <EOL> dt = dt . replace ( tzinfo = None ) <EOL> return Timestamp . objects . create ( timestamp = dt ) <EOL> class LoadMethod ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tz , conversion ) : <EOL> assert conversion in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . tz = tz <EOL> self . conversion = conversion <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' . format ( self . conversion , self . tz . zone ) <EOL> def load ( self , timestamp ) : <EOL> """<STR_LIT>""" <EOL> select = { <EOL> '<STR_LIT>' : <EOL> "<STR_LIT>" . format ( self . tz . zone ) , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } <EOL> loaded_attr = ( '<STR_LIT>' if self . conversion == '<STR_LIT>' <EOL> else '<STR_LIT>' ) <EOL> qs = Timestamp . objects . extra ( select = select ) <EOL> timestamp = qs . get ( pk = timestamp . pk ) <EOL> stored_datetime = UTC . localize ( timestamp . timestamp_stored ) <EOL> loaded_datetime = self . tz . localize ( getattr ( timestamp , loaded_attr ) ) <EOL> return stored_datetime , loaded_datetime <EOL> class TestResult ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , saved_dt , stored_dt , loaded_dt ) : <EOL> self . stored_correctly = saved_dt == stored_dt <EOL> self . loaded_correctly = saved_dt == loaded_dt <EOL> self . stored_error = stored_dt - saved_dt <EOL> self . loaded_error = loaded_dt - saved_dt <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' . format ( <EOL> '<STR_LIT:OK>' if self . stored_correctly else self . stored_error , <EOL> '<STR_LIT:OK>' if self . loaded_correctly else self . loaded_error ) <EOL> class Test ( object ) : <EOL> """<STR_LIT>""" <EOL> next_test_number = <NUM_LIT:1> <EOL> def __init__ ( self , save_method , load_method ) : <EOL> self . test_number , Test . next_test_number = ( <EOL> self . next_test_number , self . next_test_number + <NUM_LIT:1> ) <EOL> self . save_method = save_method <EOL> self . load_method = load_method <EOL> def __unicode__ ( self ) : <EOL> return unicode ( self . test_number ) <EOL> def make_roundtrip ( self , saved_dt ) : <EOL> return self . load_method . load ( self . save_method . save ( saved_dt ) ) <EOL> def run_test ( self , saved_dt ) : <EOL> return TestResult ( saved_dt , * self . make_roundtrip ( saved_dt ) ) <EOL> class TestTable ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . headers = [ '<STR_LIT:#>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . rows = [ ] <EOL> def add_datetime ( self , dt ) : <EOL> """<STR_LIT>""" <EOL> self . headers . append ( DJANGO_TZ . normalize ( dt ) ) <EOL> for row in self . rows : <EOL> row . append ( row [ <NUM_LIT:0> ] . run_test ( dt ) ) <EOL> def add_test ( self , test ) : <EOL> """<STR_LIT>""" <EOL> row = [ test , test . save_method , test . load_method ] <EOL> self . rows . append ( row ) <EOL> for dt in self . headers [ <NUM_LIT:3> : ] : <EOL> row . append ( test . run_test ( dt ) ) <EOL> def __unicode__ ( self ) : <EOL> lines = [ ] <EOL> def add_row ( row ) : <EOL> cols = [ <EOL> u'<STR_LIT>' . format ( row [ <NUM_LIT:0> ] ) , <EOL> u'<STR_LIT>' . format ( row [ <NUM_LIT:1> ] ) , <EOL> u'<STR_LIT>' . format ( row [ <NUM_LIT:2> ] ) , <EOL> ] <EOL> cols . extend ( u'<STR_LIT>' . format ( unicode ( val ) ) for val in row [ <NUM_LIT:3> : ] ) <EOL> lines . append ( u'<STR_LIT:|>' . join ( cols ) ) <EOL> add_row ( self . headers ) <EOL> lines . append ( '<STR_LIT:->' * len ( lines [ <NUM_LIT:0> ] ) ) <EOL> map ( add_row , self . rows ) <EOL> return u'<STR_LIT:\n>' . join ( lines ) <EOL> datetimes = [ <EOL> datetime ( <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:8> , <NUM_LIT:30> , <NUM_LIT:0> , tzinfo = UTC ) , <EOL> datetime ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT:7> , <NUM_LIT:2> , <NUM_LIT:30> , <NUM_LIT:0> , tzinfo = UTC ) , <EOL> ] <EOL> tests = [ <EOL> Test ( SaveMethod ( DJANGO_TZ , '<STR_LIT>' ) , LoadMethod ( DJANGO_TZ , '<STR_LIT>' ) ) , <EOL> Test ( SaveMethod ( DJANGO_TZ , '<STR_LIT>' ) , LoadMethod ( DJANGO_TZ , '<STR_LIT>' ) ) , <EOL> Test ( SaveMethod ( DJANGO_TZ , '<STR_LIT>' ) , LoadMethod ( DJANGO_TZ , '<STR_LIT>' ) ) , <EOL> Test ( SaveMethod ( DJANGO_TZ , '<STR_LIT>' ) , LoadMethod ( DJANGO_TZ , '<STR_LIT>' ) ) , <EOL> Test ( SaveMethod ( DJANGO_TZ , '<STR_LIT>' ) , LoadMethod ( UTC , '<STR_LIT>' ) ) , <EOL> Test ( SaveMethod ( UTC , '<STR_LIT>' ) , LoadMethod ( DJANGO_TZ , '<STR_LIT>' ) ) , <EOL> Test ( SaveMethod ( UTC , '<STR_LIT>' ) , LoadMethod ( UTC , '<STR_LIT>' ) ) , <EOL> Test ( SaveMethod ( UTC , '<STR_LIT>' ) , LoadMethod ( UTC , '<STR_LIT>' ) ) , <EOL> Test ( SaveMethod ( DJANGO_TZ , '<STR_LIT>' ) , LoadMethod ( UTC , '<STR_LIT>' ) ) , <EOL> ] <EOL> def generate_test_table ( ) : <EOL> table = TestTable ( ) <EOL> for dt in datetimes : <EOL> table . add_datetime ( dt ) <EOL> for test in tests : <EOL> table . add_test ( test ) <EOL> return table </s>
