<s> import binascii <EOL> import testtools <EOL> from kmip . core import enums <EOL> from kmip . pie . objects import ManagedObject , OpaqueObject <EOL> from kmip . pie import sqltypes <EOL> from sqlalchemy import create_engine <EOL> from sqlalchemy . orm import sessionmaker <EOL> class TestOpaqueObject ( testtools . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestOpaqueObject , self ) . setUp ( ) <EOL> self . bytes_a = ( <EOL> b'<STR_LIT>' ) <EOL> self . bytes_b = ( <EOL> b'<STR_LIT>' ) <EOL> self . engine = create_engine ( '<STR_LIT>' , echo = True ) <EOL> sqltypes . Base . metadata . create_all ( self . engine ) <EOL> def tearDown ( self ) : <EOL> super ( TestOpaqueObject , self ) . tearDown ( ) <EOL> def test_init ( self ) : <EOL> """<STR_LIT>""" <EOL> obj = OpaqueObject ( <EOL> self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> self . assertEqual ( obj . value , self . bytes_a ) <EOL> self . assertEqual ( obj . opaque_type , enums . OpaqueDataType . NONE ) <EOL> self . assertEqual ( obj . names , [ '<STR_LIT>' ] ) <EOL> def test_init_with_args ( self ) : <EOL> """<STR_LIT>""" <EOL> obj = OpaqueObject ( <EOL> self . bytes_a , <EOL> enums . OpaqueDataType . NONE , <EOL> name = '<STR_LIT>' ) <EOL> self . assertEqual ( obj . value , self . bytes_a ) <EOL> self . assertEqual ( obj . opaque_type , enums . OpaqueDataType . NONE ) <EOL> self . assertEqual ( obj . names , [ '<STR_LIT>' ] ) <EOL> def test_get_object_type ( self ) : <EOL> """<STR_LIT>""" <EOL> expected = enums . ObjectType . OPAQUE_DATA <EOL> obj = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> observed = obj . object_type <EOL> self . assertEqual ( expected , observed ) <EOL> def test_validate_on_invalid_value ( self ) : <EOL> """<STR_LIT>""" <EOL> args = ( <NUM_LIT:0> , enums . OpaqueDataType . NONE ) <EOL> self . assertRaises ( TypeError , OpaqueObject , * args ) <EOL> def test_validate_on_invalid_data_type ( self ) : <EOL> """<STR_LIT>""" <EOL> args = ( self . bytes_a , '<STR_LIT>' ) <EOL> self . assertRaises ( TypeError , OpaqueObject , * args ) <EOL> def test_validate_on_invalid_name ( self ) : <EOL> """<STR_LIT>""" <EOL> args = ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> kwargs = { '<STR_LIT:name>' : <NUM_LIT:0> } <EOL> self . assertRaises ( TypeError , OpaqueObject , * args , ** kwargs ) <EOL> def test_repr ( self ) : <EOL> """<STR_LIT>""" <EOL> obj = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> args = "<STR_LIT>" . format ( <EOL> binascii . hexlify ( self . bytes_a ) , enums . OpaqueDataType . NONE ) <EOL> expected = "<STR_LIT>" . format ( args ) <EOL> observed = repr ( obj ) <EOL> self . assertEqual ( expected , observed ) <EOL> def test_str ( self ) : <EOL> """<STR_LIT>""" <EOL> obj = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> expected = str ( binascii . hexlify ( self . bytes_a ) ) <EOL> observed = str ( obj ) <EOL> self . assertEqual ( expected , observed ) <EOL> def test_equal_on_equal ( self ) : <EOL> """<STR_LIT>""" <EOL> a = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> b = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> self . assertTrue ( a == b ) <EOL> self . assertTrue ( b == a ) <EOL> def test_equal_on_not_equal_value ( self ) : <EOL> """<STR_LIT>""" <EOL> a = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> b = OpaqueObject ( self . bytes_b , enums . OpaqueDataType . NONE ) <EOL> self . assertFalse ( a == b ) <EOL> self . assertFalse ( b == a ) <EOL> def test_equal_on_not_equal_data_type ( self ) : <EOL> """<STR_LIT>""" <EOL> a = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> b = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> b . opaque_type = "<STR_LIT>" <EOL> self . assertFalse ( a == b ) <EOL> self . assertFalse ( b == a ) <EOL> def test_equal_on_type_mismatch ( self ) : <EOL> """<STR_LIT>""" <EOL> a = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> b = "<STR_LIT>" <EOL> self . assertFalse ( a == b ) <EOL> self . assertFalse ( b == a ) <EOL> def test_not_equal_on_equal ( self ) : <EOL> """<STR_LIT>""" <EOL> a = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> b = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> self . assertFalse ( a != b ) <EOL> self . assertFalse ( b != a ) <EOL> def test_not_equal_on_not_equal_value ( self ) : <EOL> """<STR_LIT>""" <EOL> a = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> b = OpaqueObject ( self . bytes_b , enums . OpaqueDataType . NONE ) <EOL> self . assertTrue ( a != b ) <EOL> self . assertTrue ( b != a ) <EOL> def test_not_equal_on_not_equal_data_type ( self ) : <EOL> """<STR_LIT>""" <EOL> a = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> b = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> b . opaque_type = "<STR_LIT>" <EOL> self . assertTrue ( a != b ) <EOL> self . assertTrue ( b != a ) <EOL> def test_not_equal_on_type_mismatch ( self ) : <EOL> """<STR_LIT>""" <EOL> a = OpaqueObject ( self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> b = "<STR_LIT>" <EOL> self . assertTrue ( a != b ) <EOL> self . assertTrue ( b != a ) <EOL> def test_save ( self ) : <EOL> """<STR_LIT>""" <EOL> obj = OpaqueObject ( <EOL> self . bytes_a , enums . OpaqueDataType . NONE ) <EOL> Session = sessionmaker ( bind = self . engine ) <EOL> session = Session ( ) <EOL> session . add ( obj ) <EOL> session . commit ( ) <EOL> def test_get ( self ) : <EOL> """<STR_LIT>""" <EOL> test_name = '<STR_LIT>' <EOL> obj = OpaqueObject ( <EOL> self . bytes_a , enums . OpaqueDataType . NONE , name = test_name ) <EOL> Session = sessionmaker ( bind = self . engine ) <EOL> session = Session ( ) <EOL> session . add ( obj ) <EOL> session . commit ( ) <EOL> session = Session ( ) <EOL> get_obj = session . query ( OpaqueObject ) . filter ( <EOL> ManagedObject . unique_identifier == obj . unique_identifier <EOL> ) . one ( ) <EOL> session . commit ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( get_obj . names ) ) <EOL> self . assertEqual ( [ test_name ] , get_obj . names ) <EOL> self . assertEqual ( self . bytes_a , get_obj . value ) <EOL> self . assertEqual ( enums . ObjectType . OPAQUE_DATA , get_obj . object_type ) <EOL> self . assertEqual ( enums . OpaqueDataType . NONE , get_obj . opaque_type ) <EOL> def test_add_multiple_names ( self ) : <EOL> """<STR_LIT>""" <EOL> expected_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> obj = OpaqueObject ( <EOL> self . bytes_a , enums . OpaqueDataType . NONE , name = expected_names [ <NUM_LIT:0> ] ) <EOL> obj . names . append ( expected_names [ <NUM_LIT:1> ] ) <EOL> obj . names . append ( expected_names [ <NUM_LIT:2> ] ) <EOL> self . assertEquals ( <NUM_LIT:3> , obj . name_index ) <EOL> expected_mo_names = list ( ) <EOL> for i , name in enumerate ( expected_names ) : <EOL> expected_mo_names . append ( sqltypes . ManagedObjectName ( name , i ) ) <EOL> self . assertEquals ( expected_mo_names , obj . _names ) <EOL> Session = sessionmaker ( bind = self . engine ) <EOL> session = Session ( ) <EOL> session . add ( obj ) <EOL> session . commit ( ) <EOL> session = Session ( ) <EOL> get_obj = session . query ( OpaqueObject ) . filter ( <EOL> ManagedObject . unique_identifier == obj . unique_identifier <EOL> ) . one ( ) <EOL> session . commit ( ) <EOL> self . assertEquals ( expected_mo_names , get_obj . _names ) <EOL> def test_remove_name ( self ) : <EOL> """<STR_LIT>""" <EOL> names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> remove_index = <NUM_LIT:1> <EOL> obj = OpaqueObject ( <EOL> self . bytes_a , enums . OpaqueDataType . NONE , name = names [ <NUM_LIT:0> ] ) <EOL> obj . names . append ( names [ <NUM_LIT:1> ] ) <EOL> obj . names . append ( names [ <NUM_LIT:2> ] ) <EOL> obj . names . pop ( remove_index ) <EOL> self . assertEquals ( <NUM_LIT:3> , obj . name_index ) <EOL> expected_names = list ( ) <EOL> expected_mo_names = list ( ) <EOL> for i , name in enumerate ( names ) : <EOL> if i != remove_index : <EOL> expected_names . append ( name ) <EOL> expected_mo_names . append ( sqltypes . ManagedObjectName ( name , i ) ) <EOL> self . assertEquals ( expected_names , obj . names ) <EOL> self . assertEquals ( expected_mo_names , obj . _names ) <EOL> Session = sessionmaker ( bind = self . engine ) <EOL> session = Session ( ) <EOL> session . add ( obj ) <EOL> session . commit ( ) <EOL> session = Session ( ) <EOL> get_obj = session . query ( OpaqueObject ) . filter ( <EOL> ManagedObject . unique_identifier == obj . unique_identifier <EOL> ) . one ( ) <EOL> session . commit ( ) <EOL> self . assertEquals ( expected_names , get_obj . names ) <EOL> self . assertEquals ( expected_mo_names , get_obj . _names ) <EOL> def test_remove_and_add_name ( self ) : <EOL> """<STR_LIT>""" <EOL> names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> obj = OpaqueObject ( <EOL> self . bytes_a , enums . OpaqueDataType . NONE , name = names [ <NUM_LIT:0> ] ) <EOL> obj . names . append ( names [ <NUM_LIT:1> ] ) <EOL> obj . names . append ( names [ <NUM_LIT:2> ] ) <EOL> obj . names . pop ( ) <EOL> obj . names . pop ( ) <EOL> obj . names . append ( '<STR_LIT>' ) <EOL> self . assertEquals ( <NUM_LIT:4> , obj . name_index ) <EOL> expected_names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> expected_mo_names = list ( ) <EOL> expected_mo_names . append ( sqltypes . ManagedObjectName ( expected_names [ <NUM_LIT:0> ] , <EOL> <NUM_LIT:0> ) ) <EOL> expected_mo_names . append ( sqltypes . ManagedObjectName ( expected_names [ <NUM_LIT:1> ] , <EOL> <NUM_LIT:3> ) ) <EOL> self . assertEquals ( expected_names , obj . names ) <EOL> self . assertEquals ( expected_mo_names , obj . _names ) <EOL> Session = sessionmaker ( bind = self . engine ) <EOL> session = Session ( ) <EOL> session . add ( obj ) <EOL> session . commit ( ) <EOL> session = Session ( ) <EOL> get_obj = session . query ( OpaqueObject ) . filter ( <EOL> ManagedObject . unique_identifier == obj . unique_identifier <EOL> ) . one ( ) <EOL> session . commit ( ) <EOL> self . assertEquals ( expected_names , get_obj . names ) <EOL> self . assertEquals ( expected_mo_names , get_obj . _names ) <EOL> def test_update_with_add_name ( self ) : <EOL> """<STR_LIT>""" <EOL> first_name = '<STR_LIT>' <EOL> obj = OpaqueObject ( <EOL> self . bytes_a , enums . OpaqueDataType . NONE , name = first_name ) <EOL> Session = sessionmaker ( bind = self . engine ) <EOL> session = Session ( ) <EOL> session . add ( obj ) <EOL> session . commit ( ) <EOL> added_name = '<STR_LIT>' <EOL> expected_names = [ first_name , added_name ] <EOL> expected_mo_names = list ( ) <EOL> for i , name in enumerate ( expected_names ) : <EOL> expected_mo_names . append ( sqltypes . ManagedObjectName ( name , i ) ) <EOL> session = Session ( ) <EOL> update_obj = session . query ( OpaqueObject ) . filter ( <EOL> ManagedObject . unique_identifier == obj . unique_identifier <EOL> ) . one ( ) <EOL> update_obj . names . append ( added_name ) <EOL> session . commit ( ) <EOL> session = Session ( ) <EOL> get_obj = session . query ( OpaqueObject ) . filter ( <EOL> ManagedObject . unique_identifier == obj . unique_identifier <EOL> ) . one ( ) <EOL> session . commit ( ) <EOL> self . assertEquals ( expected_names , get_obj . names ) <EOL> self . assertEquals ( expected_mo_names , get_obj . _names ) <EOL> def test_update_with_remove_name ( self ) : <EOL> """<STR_LIT>""" <EOL> names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> remove_index = <NUM_LIT:1> <EOL> obj = OpaqueObject ( <EOL> self . bytes_a , enums . OpaqueDataType . NONE , name = names [ <NUM_LIT:0> ] ) <EOL> obj . names . append ( names [ <NUM_LIT:1> ] ) <EOL> obj . names . append ( names [ <NUM_LIT:2> ] ) <EOL> Session = sessionmaker ( bind = self . engine ) <EOL> session = Session ( ) <EOL> session . add ( obj ) <EOL> session . commit ( ) <EOL> expected_names = list ( ) <EOL> expected_mo_names = list ( ) <EOL> for i , name in enumerate ( names ) : <EOL> if i != remove_index : <EOL> expected_names . append ( name ) <EOL> expected_mo_names . append ( sqltypes . ManagedObjectName ( name , i ) ) <EOL> session = Session ( ) <EOL> update_obj = session . query ( OpaqueObject ) . filter ( <EOL> ManagedObject . unique_identifier == obj . unique_identifier <EOL> ) . one ( ) <EOL> update_obj . names . pop ( remove_index ) <EOL> session . commit ( ) <EOL> session = Session ( ) <EOL> get_obj = session . query ( OpaqueObject ) . filter ( <EOL> ManagedObject . unique_identifier == obj . unique_identifier <EOL> ) . one ( ) <EOL> session . commit ( ) <EOL> self . assertEquals ( expected_names , get_obj . names ) <EOL> self . assertEquals ( expected_mo_names , get_obj . _names ) <EOL> def test_update_with_remove_and_add_name ( self ) : <EOL> """<STR_LIT>""" <EOL> names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> obj = OpaqueObject ( <EOL> self . bytes_a , enums . OpaqueDataType . NONE , name = names [ <NUM_LIT:0> ] ) <EOL> obj . names . append ( names [ <NUM_LIT:1> ] ) <EOL> obj . names . append ( names [ <NUM_LIT:2> ] ) <EOL> Session = sessionmaker ( bind = self . engine ) <EOL> session = Session ( ) <EOL> session . add ( obj ) <EOL> session . commit ( ) <EOL> session = Session ( ) <EOL> update_obj = session . query ( OpaqueObject ) . filter ( <EOL> ManagedObject . unique_identifier == obj . unique_identifier <EOL> ) . one ( ) <EOL> update_obj . names . pop ( ) <EOL> update_obj . names . pop ( ) <EOL> update_obj . names . append ( '<STR_LIT>' ) <EOL> session . commit ( ) <EOL> expected_names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> expected_mo_names = list ( ) <EOL> expected_mo_names . append ( sqltypes . ManagedObjectName ( expected_names [ <NUM_LIT:0> ] , <EOL> <NUM_LIT:0> ) ) <EOL> expected_mo_names . append ( sqltypes . ManagedObjectName ( expected_names [ <NUM_LIT:1> ] , <EOL> <NUM_LIT:3> ) ) <EOL> session = Session ( ) <EOL> get_obj = session . query ( OpaqueObject ) . filter ( <EOL> ManagedObject . unique_identifier == obj . unique_identifier <EOL> ) . one ( ) <EOL> session . commit ( ) <EOL> self . assertEquals ( expected_names , get_obj . names ) <EOL> self . assertEquals ( expected_mo_names , get_obj . _names ) </s>
