<s> import datetime <EOL> import calendar <EOL> import mock <EOL> import unittest2 <EOL> from st2common . fields import ComplexDateTimeField <EOL> from st2common . util import date as date_utils <EOL> class ComplexDateTimeFieldTestCase ( unittest2 . TestCase ) : <EOL> def test_what_comes_in_goes_out ( self ) : <EOL> field = ComplexDateTimeField ( ) <EOL> date = date_utils . get_datetime_utc_now ( ) <EOL> us = field . _datetime_to_microseconds_since_epoch ( date ) <EOL> result = field . _microseconds_since_epoch_to_datetime ( us ) <EOL> self . assertEqual ( date , result ) <EOL> def test_round_trip_conversion ( self ) : <EOL> datetime_values = [ <EOL> datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:15> , <NUM_LIT:0> , <NUM_LIT:0> ) . replace ( microsecond = <NUM_LIT> ) , <EOL> datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:15> , <NUM_LIT:0> , <NUM_LIT:0> ) . replace ( microsecond = <NUM_LIT:0> ) , <EOL> datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:15> , <NUM_LIT:0> , <NUM_LIT:0> ) . replace ( microsecond = <NUM_LIT> ) <EOL> ] <EOL> datetime_values = [ <EOL> date_utils . add_utc_tz ( datetime_values [ <NUM_LIT:0> ] ) , <EOL> date_utils . add_utc_tz ( datetime_values [ <NUM_LIT:1> ] ) , <EOL> date_utils . add_utc_tz ( datetime_values [ <NUM_LIT:2> ] ) <EOL> ] <EOL> microsecond_values = [ ] <EOL> for value in datetime_values : <EOL> seconds = calendar . timegm ( value . timetuple ( ) ) <EOL> microseconds_reminder = value . time ( ) . microsecond <EOL> result = int ( seconds * <NUM_LIT> ) + microseconds_reminder <EOL> microsecond_values . append ( result ) <EOL> field = ComplexDateTimeField ( ) <EOL> for index , value in enumerate ( datetime_values ) : <EOL> actual_value = field . _datetime_to_microseconds_since_epoch ( value = value ) <EOL> expected_value = microsecond_values [ index ] <EOL> expected_microseconds = value . time ( ) . microsecond <EOL> self . assertEqual ( actual_value , expected_value ) <EOL> self . assertTrue ( str ( actual_value ) . endswith ( str ( expected_microseconds ) ) ) <EOL> for index , value in enumerate ( microsecond_values ) : <EOL> actual_value = field . _microseconds_since_epoch_to_datetime ( data = value ) <EOL> expected_value = datetime_values [ index ] <EOL> self . assertEqual ( actual_value , expected_value ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_get_ ( self , mock_get ) : <EOL> field = ComplexDateTimeField ( ) <EOL> mock_get . return_value = None <EOL> self . assertEqual ( field . __get__ ( instance = None , owner = None ) , None ) <EOL> mock_get . return_value = date_utils . get_datetime_utc_now ( ) <EOL> self . assertEqual ( field . __get__ ( instance = None , owner = None ) , mock_get . return_value ) <EOL> dt = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:15> , <NUM_LIT:0> , <NUM_LIT:0> ) . replace ( microsecond = <NUM_LIT> ) <EOL> dt = date_utils . add_utc_tz ( dt ) <EOL> us = field . _datetime_to_microseconds_since_epoch ( value = dt ) <EOL> mock_get . return_value = us <EOL> self . assertEqual ( field . __get__ ( instance = None , owner = None ) , dt ) </s>
