<s> """<STR_LIT>""" <EOL> import unittest <EOL> import datetime <EOL> import time <EOL> import os <EOL> import random <EOL> import shutil <EOL> import dbutil <EOL> import agdc . cube_util as cube_util <EOL> class TestGetDatacubeRoot ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> MODULE = '<STR_LIT>' <EOL> SUITE = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . save_dcr = os . environ . get ( '<STR_LIT>' , None ) <EOL> def test_datacube_root_from_env ( self ) : <EOL> """<STR_LIT>""" <EOL> test_dir = '<STR_LIT>' <EOL> os . environ [ '<STR_LIT>' ] = test_dir <EOL> datacube_root = cube_util . get_datacube_root ( ) <EOL> self . assertEqual ( test_dir , datacube_root ) <EOL> def test_datacube_root_from_inspect ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in os . environ : <EOL> del os . environ [ '<STR_LIT>' ] <EOL> datacube_root = cube_util . get_datacube_root ( ) <EOL> module_path = os . path . join ( datacube_root , '<STR_LIT>' ) <EOL> if not os . path . isfile ( module_path ) : <EOL> self . fail ( "<STR_LIT>" + <EOL> "<STR_LIT:U+0020>" + module_path + "<STR_LIT:\n>" ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . save_dcr is None : <EOL> if '<STR_LIT>' in os . environ : <EOL> del os . environ [ '<STR_LIT>' ] <EOL> else : <EOL> os . environ [ '<STR_LIT>' ] = self . save_dcr <EOL> class TestParseDate ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> MODULE = '<STR_LIT>' <EOL> SUITE = '<STR_LIT>' <EOL> def test_parse_date_format1 ( self ) : <EOL> """<STR_LIT>""" <EOL> date = cube_util . parse_date_from_string ( '<STR_LIT>' ) <EOL> self . assertEqual ( date , datetime . date ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT> ) ) <EOL> def test_parse_date_format2 ( self ) : <EOL> """<STR_LIT>""" <EOL> date = cube_util . parse_date_from_string ( '<STR_LIT>' ) <EOL> self . assertEqual ( date , datetime . date ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT> ) ) <EOL> def test_parse_date_format3 ( self ) : <EOL> """<STR_LIT>""" <EOL> date = cube_util . parse_date_from_string ( '<STR_LIT>' ) <EOL> self . assertEqual ( date , datetime . date ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT> ) ) <EOL> def test_parse_date_bad_format ( self ) : <EOL> """<STR_LIT>""" <EOL> date = cube_util . parse_date_from_string ( '<STR_LIT>' ) <EOL> self . assertEqual ( date , None ) <EOL> class TestCreateDirectory ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> MODULE = '<STR_LIT>' <EOL> SUITE = '<STR_LIT>' <EOL> OUTPUT_DIR = dbutil . output_directory ( MODULE , SUITE ) <EOL> def setUp ( self ) : <EOL> self . single_dir_path = os . path . join ( self . OUTPUT_DIR , '<STR_LIT>' ) <EOL> self . multi_dir_top_path = os . path . join ( self . OUTPUT_DIR , '<STR_LIT>' ) <EOL> self . multi_dir_full_path = os . path . join ( self . multi_dir_top_path , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> try : <EOL> os . remove ( self . single_dir_path ) <EOL> except OSError : <EOL> pass <EOL> shutil . rmtree ( self . single_dir_path , ignore_errors = True ) <EOL> shutil . rmtree ( self . multi_dir_top_path , ignore_errors = True ) <EOL> def test_create_one ( self ) : <EOL> """<STR_LIT>""" <EOL> cube_util . create_directory ( self . single_dir_path ) <EOL> self . assertTrue ( os . path . isdir ( self . single_dir_path ) , <EOL> "<STR_LIT>" % self . single_dir_path ) <EOL> def test_create_multi_simple ( self ) : <EOL> """<STR_LIT>""" <EOL> cube_util . create_directory ( self . multi_dir_full_path ) <EOL> self . assertTrue ( os . path . isdir ( self . multi_dir_full_path ) , <EOL> "<STR_LIT>" % self . multi_dir_full_path ) <EOL> def test_create_multi_complex ( self ) : <EOL> """<STR_LIT>""" <EOL> cube_util . create_directory ( self . multi_dir_top_path ) <EOL> self . assertTrue ( os . path . isdir ( self . multi_dir_top_path ) , <EOL> "<STR_LIT>" % self . multi_dir_top_path ) <EOL> cube_util . create_directory ( self . multi_dir_full_path ) <EOL> self . assertTrue ( os . path . isdir ( self . multi_dir_full_path ) , <EOL> "<STR_LIT>" % self . multi_dir_full_path ) <EOL> cube_util . create_directory ( self . multi_dir_full_path ) <EOL> def test_create_dir_error ( self ) : <EOL> """<STR_LIT>""" <EOL> f = open ( self . single_dir_path , '<STR_LIT:w>' ) <EOL> f . close ( ) <EOL> self . assertRaises ( cube_util . DatasetError , <EOL> cube_util . create_directory , <EOL> self . single_dir_path <EOL> ) <EOL> class TestSynchronize ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_synchronize ( self ) : <EOL> """<STR_LIT>""" <EOL> sync_time = time . time ( ) + <NUM_LIT> <EOL> cube_util . synchronize ( sync_time ) <EOL> finish_time = time . time ( ) <EOL> self . assertAlmostEqual ( sync_time , finish_time , delta = <NUM_LIT:0.1> , <EOL> msg = "<STR_LIT>" ) <EOL> class TestStopwatch ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> DELTA = <NUM_LIT> <EOL> def setUp ( self ) : <EOL> self . sw = cube_util . Stopwatch ( ) <EOL> @ staticmethod <EOL> def waste_time ( ) : <EOL> """<STR_LIT>""" <EOL> cube_util . synchronize ( time . time ( ) + random . gauss ( <NUM_LIT:1.0> , <NUM_LIT> ) ) <EOL> def check_times ( self , estimated_time , estimated_cpu ) : <EOL> """<STR_LIT>""" <EOL> ( elapsed_time , cpu_time ) = self . sw . read ( ) <EOL> self . assertAlmostEqual ( estimated_time , elapsed_time , delta = self . DELTA , <EOL> msg = "<STR_LIT>" ) <EOL> self . assertAlmostEqual ( estimated_cpu , cpu_time , delta = self . DELTA , <EOL> msg = "<STR_LIT>" ) <EOL> def test_simple ( self ) : <EOL> """<STR_LIT>""" <EOL> t = time . time ( ) <EOL> cpu = time . clock ( ) <EOL> self . sw . start ( ) <EOL> self . waste_time ( ) <EOL> t = time . time ( ) - t <EOL> cpu = time . clock ( ) - cpu <EOL> self . sw . stop ( ) <EOL> self . waste_time ( ) <EOL> self . check_times ( t , cpu ) <EOL> def test_restart ( self ) : <EOL> """<STR_LIT>""" <EOL> t1 = time . time ( ) <EOL> cpu1 = time . clock ( ) <EOL> self . sw . start ( ) <EOL> self . waste_time ( ) <EOL> t2 = time . time ( ) <EOL> cpu2 = time . clock ( ) <EOL> self . sw . stop ( ) <EOL> self . waste_time ( ) <EOL> t3 = time . time ( ) <EOL> cpu3 = time . clock ( ) <EOL> self . sw . start ( ) <EOL> self . waste_time ( ) <EOL> t4 = time . time ( ) <EOL> cpu4 = time . clock ( ) <EOL> self . sw . stop ( ) <EOL> estimated_time = ( t2 - t1 ) + ( t4 - t3 ) <EOL> estimated_cpu = ( cpu2 - cpu1 ) + ( cpu4 - cpu3 ) <EOL> self . check_times ( estimated_time , estimated_cpu ) <EOL> def test_reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . sw . start ( ) <EOL> self . waste_time ( ) <EOL> self . sw . reset ( ) <EOL> t1 = time . time ( ) <EOL> cpu1 = time . clock ( ) <EOL> self . sw . start ( ) <EOL> self . waste_time ( ) <EOL> t2 = time . time ( ) <EOL> cpu2 = time . clock ( ) <EOL> self . sw . stop ( ) <EOL> self . check_times ( t2 - t1 , cpu2 - cpu1 ) <EOL> def the_suite ( ) : <EOL> """<STR_LIT>""" <EOL> test_classes = [ TestParseDate , <EOL> TestGetDatacubeRoot , <EOL> TestCreateDirectory , <EOL> TestSynchronize , <EOL> TestStopwatch <EOL> ] <EOL> suite_list = map ( unittest . defaultTestLoader . loadTestsFromTestCase , <EOL> test_classes ) <EOL> suite = unittest . TestSuite ( suite_list ) <EOL> return suite <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . TextTestRunner ( verbosity = <NUM_LIT:2> ) . run ( the_suite ( ) ) </s>
