<s> import mock <EOL> from sqlalchemy import create_engine <EOL> from sqlalchemy . engine . reflection import Inspector <EOL> import unittest <EOL> from codalab . model . bundle_model import ( <EOL> BundleModel , <EOL> db_metadata , <EOL> ) <EOL> def metadata_to_dicts ( uuid , metadata ) : <EOL> return [ <EOL> { <EOL> '<STR_LIT>' : uuid , <EOL> '<STR_LIT>' : key , <EOL> '<STR_LIT>' : value , <EOL> } <EOL> for ( key , value ) in metadata . iteritems ( ) <EOL> ] <EOL> def canonicalize ( metadata_dicts ) : <EOL> '''<STR_LIT>''' <EOL> return sorted ( <EOL> sorted ( ( k , v ) for ( k , v ) in dict ( metadata_dict ) . iteritems ( ) if k != '<STR_LIT:id>' ) <EOL> for metadata_dict in metadata_dicts <EOL> ) <EOL> class MockDependency ( object ) : <EOL> _fields = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , row = None ) : <EOL> if row : <EOL> for ( field , value ) in self . _fields . iteritems ( ) : <EOL> self . _tester . assertEqual ( row [ field ] , value ) <EOL> for ( field , value ) in self . _fields . iteritems ( ) : <EOL> setattr ( self , field , value ) <EOL> def to_dict ( self ) : <EOL> return dict ( self . _fields ) <EOL> class MockBundle ( object ) : <EOL> _fields = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : [ MockDependency ( ) . to_dict ( ) ] <EOL> } <EOL> def __init__ ( self , row = None ) : <EOL> if row : <EOL> for ( field , value ) in self . _fields . iteritems ( ) : <EOL> if field == '<STR_LIT>' : <EOL> actual_value = canonicalize ( row [ field ] ) <EOL> expected_value = canonicalize ( <EOL> metadata_to_dicts ( self . _fields [ '<STR_LIT>' ] , self . _fields [ '<STR_LIT>' ] ) <EOL> ) <EOL> self . _tester . assertEqual ( actual_value , expected_value ) <EOL> elif field == '<STR_LIT>' : <EOL> [ MockDependency ( dep ) for dep in row [ field ] ] <EOL> else : <EOL> self . _tester . assertEqual ( row [ field ] , value ) <EOL> for ( field , value ) in self . _fields . iteritems ( ) : <EOL> setattr ( self , field , value ) <EOL> def validate ( self ) : <EOL> self . _validate_called = True <EOL> def to_dict ( self ) : <EOL> result = dict ( self . _fields ) <EOL> result [ '<STR_LIT>' ] = metadata_to_dicts ( result [ '<STR_LIT>' ] , result [ '<STR_LIT>' ] ) <EOL> return result <EOL> class BundleModelTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> MockBundle . _tester = self <EOL> MockDependency . _tester = self <EOL> self . engine = create_engine ( '<STR_LIT>' , strategy = '<STR_LIT>' ) <EOL> self . model = BundleModel ( self . engine , { } ) <EOL> self . model . create_tables ( ) <EOL> def tearDown ( self ) : <EOL> self . model = None <EOL> self . engine = None <EOL> def test_create_tables ( self ) : <EOL> inspector = Inspector . from_engine ( self . engine ) <EOL> tables = set ( inspector . get_table_names ( ) ) <EOL> for table in db_metadata . tables : <EOL> self . assertIn ( table , tables ) <EOL> def test_save_and_get_bundle ( self ) : <EOL> bundle = MockBundle ( ) <EOL> self . model . save_bundle ( bundle ) <EOL> self . assertTrue ( bundle . _validate_called ) <EOL> get_bundle_subclass_path = '<STR_LIT>' <EOL> with mock . patch ( get_bundle_subclass_path , lambda bundle_type : MockBundle ) : <EOL> retrieved_bundle = self . model . get_bundle ( bundle . uuid ) <EOL> self . assertTrue ( isinstance ( retrieved_bundle , MockBundle ) ) <EOL> self . assertTrue ( retrieved_bundle . _validate_called ) </s>
