<s> import os . path <EOL> import unittest <EOL> import tempfile <EOL> import shutil <EOL> import sys <EOL> import zipfile <EOL> from resync . dump import Dump , DumpError <EOL> from resync . resource_list import ResourceList <EOL> from resync . change_list import ChangeList <EOL> from resync . resource import Resource <EOL> from resync . resource_dump_manifest import ResourceDumpManifest <EOL> from resync . change_dump_manifest import ChangeDumpManifest <EOL> class TestDump ( unittest . TestCase ) : <EOL> _tmpdir = None <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> cls . _tmpdir = tempfile . mkdtemp ( ) <EOL> if ( not os . path . isdir ( cls . _tmpdir ) ) : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> @ classmethod <EOL> def tearDownClass ( cls ) : <EOL> if ( not os . path . isdir ( cls . _tmpdir ) ) : <EOL> raise Exception ( "<STR_LIT>" % ( tmpdir ) ) <EOL> shutil . rmtree ( cls . _tmpdir ) <EOL> @ property <EOL> def tmpdir ( self ) : <EOL> if ( not self . _tmpdir and sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:7> ) ) : <EOL> self . setUpClass ( ) <EOL> return ( self . _tmpdir ) <EOL> def test00_dump_zip_resource_list ( self ) : <EOL> rl = ResourceDumpManifest ( ) <EOL> rl . add ( Resource ( '<STR_LIT>' , length = <NUM_LIT:7> , path = '<STR_LIT>' ) ) <EOL> rl . add ( Resource ( '<STR_LIT>' , length = <NUM_LIT> , path = '<STR_LIT>' ) ) <EOL> d = Dump ( ) <EOL> zipf = os . path . join ( self . tmpdir , "<STR_LIT>" ) <EOL> d . write_zip ( resources = rl , dumpfile = zipf ) <EOL> self . assertTrue ( os . path . exists ( zipf ) ) <EOL> self . assertTrue ( zipfile . is_zipfile ( zipf ) ) <EOL> zo = zipfile . ZipFile ( zipf , '<STR_LIT:r>' ) <EOL> self . assertEqual ( len ( zo . namelist ( ) ) , <NUM_LIT:3> ) <EOL> zo . close ( ) <EOL> os . unlink ( zipf ) <EOL> def test01_dump_zip_change_list ( self ) : <EOL> cl = ChangeDumpManifest ( ) <EOL> cl . add ( Resource ( '<STR_LIT>' , length = <NUM_LIT:7> , path = '<STR_LIT>' , change = "<STR_LIT>" ) ) <EOL> cl . add ( Resource ( '<STR_LIT>' , length = <NUM_LIT> , path = '<STR_LIT>' , change = "<STR_LIT>" ) ) <EOL> d = Dump ( ) <EOL> zipf = os . path . join ( self . tmpdir , "<STR_LIT>" ) <EOL> d . write_zip ( cl , zipf ) <EOL> self . assertTrue ( os . path . exists ( zipf ) ) <EOL> self . assertTrue ( zipfile . is_zipfile ( zipf ) ) <EOL> zo = zipfile . ZipFile ( zipf , '<STR_LIT:r>' ) <EOL> self . assertEqual ( len ( zo . namelist ( ) ) , <NUM_LIT:3> ) <EOL> zo . close ( ) <EOL> os . unlink ( zipf ) <EOL> def test02_dump_check_files ( self ) : <EOL> cl = ChangeList ( ) <EOL> cl . add ( Resource ( '<STR_LIT>' , length = <NUM_LIT:7> , path = '<STR_LIT>' , change = "<STR_LIT>" ) ) <EOL> cl . add ( Resource ( '<STR_LIT>' , length = <NUM_LIT> , path = '<STR_LIT>' , change = "<STR_LIT>" ) ) <EOL> d = Dump ( resources = cl ) <EOL> self . assertTrue ( d . check_files ( ) ) <EOL> self . assertEqual ( d . total_size , <NUM_LIT> ) <EOL> def test03_dump_multi_file_max_size ( self ) : <EOL> rl = ResourceList ( ) <EOL> for letter in map ( chr , range ( ord ( '<STR_LIT:a>' ) , ord ( '<STR_LIT:l>' ) + <NUM_LIT:1> ) ) : <EOL> uri = '<STR_LIT>' % ( letter ) <EOL> fname = '<STR_LIT>' % ( letter ) <EOL> rl . add ( Resource ( uri , path = fname ) ) <EOL> self . assertEqual ( len ( rl ) , <NUM_LIT:12> ) <EOL> d2 = Dump ( rl ) <EOL> tmpbase = os . path . join ( self . tmpdir , '<STR_LIT>' ) <EOL> d2 . max_files = <NUM_LIT:4> <EOL> n = d2 . write ( tmpbase ) <EOL> self . assertEqual ( n , <NUM_LIT:3> , '<STR_LIT>' ) <EOL> self . assertTrue ( os . path . isfile ( tmpbase + '<STR_LIT>' ) ) <EOL> self . assertTrue ( os . path . isfile ( tmpbase + '<STR_LIT>' ) ) <EOL> self . assertTrue ( os . path . isfile ( tmpbase + '<STR_LIT>' ) ) <EOL> zipf = tmpbase + '<STR_LIT>' <EOL> zo = zipfile . ZipFile ( zipf , '<STR_LIT:r>' ) <EOL> self . assertEqual ( zo . namelist ( ) , [ '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' ] ) <EOL> self . assertEqual ( zo . getinfo ( '<STR_LIT:a>' ) . file_size , <NUM_LIT:9> ) <EOL> self . assertEqual ( zo . getinfo ( '<STR_LIT:b>' ) . file_size , <NUM_LIT> ) <EOL> self . assertEqual ( zo . getinfo ( '<STR_LIT:c>' ) . file_size , <NUM_LIT:32> ) <EOL> self . assertEqual ( zo . getinfo ( '<STR_LIT:d>' ) . file_size , <NUM_LIT> ) <EOL> zo . close ( ) <EOL> os . unlink ( zipf ) <EOL> zipf = tmpbase + '<STR_LIT>' <EOL> zo = zipfile . ZipFile ( zipf , '<STR_LIT:r>' ) <EOL> self . assertEqual ( zo . namelist ( ) , [ '<STR_LIT>' , '<STR_LIT:e>' , '<STR_LIT:f>' , '<STR_LIT:g>' , '<STR_LIT:h>' ] ) <EOL> zo . close ( ) <EOL> os . unlink ( zipf ) <EOL> zipf = tmpbase + '<STR_LIT>' <EOL> zo = zipfile . ZipFile ( zipf , '<STR_LIT:r>' ) <EOL> self . assertEqual ( zo . namelist ( ) , [ '<STR_LIT>' , '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT:k>' , '<STR_LIT:l>' ] ) <EOL> zo . close ( ) <EOL> os . unlink ( zipf ) <EOL> def test04_dump_multi_file_max_size ( self ) : <EOL> rl = ResourceList ( ) <EOL> for letter in map ( chr , range ( ord ( '<STR_LIT:a>' ) , ord ( '<STR_LIT:l>' ) + <NUM_LIT:1> ) ) : <EOL> uri = '<STR_LIT>' % ( letter ) <EOL> fname = '<STR_LIT>' % ( letter ) <EOL> rl . add ( Resource ( uri , path = fname ) ) <EOL> self . assertEqual ( len ( rl ) , <NUM_LIT:12> ) <EOL> d2 = Dump ( rl ) <EOL> tmpbase = os . path . join ( self . tmpdir , '<STR_LIT>' ) <EOL> d2 . max_size = <NUM_LIT> <EOL> n = d2 . write ( tmpbase ) <EOL> self . assertEqual ( n , <NUM_LIT:2> , '<STR_LIT>' ) <EOL> self . assertTrue ( os . path . isfile ( tmpbase + '<STR_LIT>' ) ) <EOL> self . assertTrue ( os . path . isfile ( tmpbase + '<STR_LIT>' ) ) <EOL> zipf = tmpbase + '<STR_LIT>' <EOL> zo = zipfile . ZipFile ( zipf , '<STR_LIT:r>' ) <EOL> self . assertEqual ( zo . namelist ( ) , [ '<STR_LIT>' , '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' , '<STR_LIT:e>' , '<STR_LIT:f>' ] ) <EOL> self . assertEqual ( zo . getinfo ( '<STR_LIT:a>' ) . file_size , <NUM_LIT:9> ) <EOL> self . assertEqual ( zo . getinfo ( '<STR_LIT:b>' ) . file_size , <NUM_LIT> ) <EOL> self . assertEqual ( zo . getinfo ( '<STR_LIT:c>' ) . file_size , <NUM_LIT:32> ) <EOL> self . assertEqual ( zo . getinfo ( '<STR_LIT:d>' ) . file_size , <NUM_LIT> ) <EOL> self . assertEqual ( zo . getinfo ( '<STR_LIT:e>' ) . file_size , <NUM_LIT:20> ) <EOL> self . assertEqual ( zo . getinfo ( '<STR_LIT:f>' ) . file_size , <NUM_LIT> ) <EOL> zo . close ( ) <EOL> os . unlink ( zipf ) <EOL> zipf = tmpbase + '<STR_LIT>' <EOL> zo = zipfile . ZipFile ( zipf , '<STR_LIT:r>' ) <EOL> self . assertEqual ( zo . namelist ( ) , [ '<STR_LIT>' , '<STR_LIT:g>' , '<STR_LIT:h>' , '<STR_LIT:i>' , '<STR_LIT>' , '<STR_LIT:k>' , '<STR_LIT:l>' ] ) <EOL> zo . close ( ) <EOL> os . unlink ( zipf ) <EOL> def test10_no_path ( self ) : <EOL> rl = ResourceList ( ) <EOL> rl . add ( Resource ( '<STR_LIT>' , length = <NUM_LIT:7> , path = '<STR_LIT>' ) ) <EOL> rl . add ( Resource ( '<STR_LIT>' , length = <NUM_LIT> ) ) <EOL> d = Dump ( rl ) <EOL> self . assertRaises ( DumpError , d . check_files ) <EOL> def test11_bad_size ( self ) : <EOL> rl = ResourceList ( ) <EOL> rl . add ( Resource ( '<STR_LIT>' , length = <NUM_LIT> , path = '<STR_LIT>' ) ) <EOL> d = Dump ( rl ) <EOL> self . assertTrue ( d . check_files ( check_length = False ) ) <EOL> self . assertRaises ( DumpError , d . check_files ) <EOL> def test10_no_path ( self ) : <EOL> rl = ResourceList ( ) <EOL> rl . add ( Resource ( '<STR_LIT>' , length = <NUM_LIT:7> , path = '<STR_LIT>' ) ) <EOL> rl . add ( Resource ( '<STR_LIT>' , length = <NUM_LIT> ) ) <EOL> d = Dump ( rl ) <EOL> self . assertRaises ( DumpError , d . check_files ) <EOL> def test11_bad_size ( self ) : <EOL> rl = ResourceList ( ) <EOL> rl . add ( Resource ( '<STR_LIT>' , length = <NUM_LIT> , path = '<STR_LIT>' ) ) <EOL> d = Dump ( rl ) <EOL> self . assertTrue ( d . check_files ( check_length = False ) ) <EOL> self . assertRaises ( DumpError , d . check_files ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> suite = unittest . defaultTestLoader . loadTestsFromTestCase ( TestDump ) <EOL> unittest . TextTestRunner ( verbosity = <NUM_LIT:2> ) . run ( suite ) </s>
