<s> import unittest2 <EOL> class TestKey ( unittest2 . TestCase ) : <EOL> _DEFAULT_PROJECT = '<STR_LIT>' <EOL> def _getTargetClass ( self ) : <EOL> from gcloud . datastore . key import Key <EOL> return Key <EOL> def _makeOne ( self , * args , ** kwargs ) : <EOL> return self . _getTargetClass ( ) ( * args , ** kwargs ) <EOL> def test_ctor_empty ( self ) : <EOL> self . assertRaises ( ValueError , self . _makeOne ) <EOL> def test_ctor_no_project ( self ) : <EOL> klass = self . _getTargetClass ( ) <EOL> self . assertRaises ( ValueError , klass , '<STR_LIT>' ) <EOL> def test_ctor_w_explicit_project_empty_path ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> self . assertRaises ( ValueError , self . _makeOne , project = _PROJECT ) <EOL> def test_ctor_parent ( self ) : <EOL> _PARENT_KIND = '<STR_LIT>' <EOL> _PARENT_ID = <NUM_LIT> <EOL> _PARENT_PROJECT = '<STR_LIT>' <EOL> _PARENT_NAMESPACE = '<STR_LIT>' <EOL> _CHILD_KIND = '<STR_LIT>' <EOL> _CHILD_ID = <NUM_LIT> <EOL> _PATH = [ <EOL> { '<STR_LIT>' : _PARENT_KIND , '<STR_LIT:id>' : _PARENT_ID } , <EOL> { '<STR_LIT>' : _CHILD_KIND , '<STR_LIT:id>' : _CHILD_ID } , <EOL> ] <EOL> parent_key = self . _makeOne ( _PARENT_KIND , _PARENT_ID , <EOL> project = _PARENT_PROJECT , <EOL> namespace = _PARENT_NAMESPACE ) <EOL> key = self . _makeOne ( _CHILD_KIND , _CHILD_ID , parent = parent_key ) <EOL> self . assertEqual ( key . project , parent_key . project ) <EOL> self . assertEqual ( key . namespace , parent_key . namespace ) <EOL> self . assertEqual ( key . kind , _CHILD_KIND ) <EOL> self . assertEqual ( key . path , _PATH ) <EOL> self . assertTrue ( key . parent is parent_key ) <EOL> def test_ctor_partial_parent ( self ) : <EOL> parent_key = self . _makeOne ( '<STR_LIT>' , project = self . _DEFAULT_PROJECT ) <EOL> with self . assertRaises ( ValueError ) : <EOL> self . _makeOne ( '<STR_LIT>' , <NUM_LIT> , parent = parent_key ) <EOL> def test_ctor_parent_bad_type ( self ) : <EOL> with self . assertRaises ( AttributeError ) : <EOL> self . _makeOne ( '<STR_LIT>' , <NUM_LIT> , parent = ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> project = self . _DEFAULT_PROJECT ) <EOL> def test_ctor_parent_bad_namespace ( self ) : <EOL> parent_key = self . _makeOne ( '<STR_LIT>' , <NUM_LIT> , namespace = '<STR_LIT>' , <EOL> project = self . _DEFAULT_PROJECT ) <EOL> with self . assertRaises ( ValueError ) : <EOL> self . _makeOne ( '<STR_LIT>' , <NUM_LIT> , namespace = '<STR_LIT>' , parent = parent_key , <EOL> project = self . _DEFAULT_PROJECT ) <EOL> def test_ctor_parent_bad_project ( self ) : <EOL> parent_key = self . _makeOne ( '<STR_LIT>' , <NUM_LIT> , project = '<STR_LIT>' ) <EOL> with self . assertRaises ( ValueError ) : <EOL> self . _makeOne ( '<STR_LIT>' , <NUM_LIT> , parent = parent_key , <EOL> project = '<STR_LIT>' ) <EOL> def test_ctor_parent_empty_path ( self ) : <EOL> parent_key = self . _makeOne ( '<STR_LIT>' , <NUM_LIT> , <EOL> project = self . _DEFAULT_PROJECT ) <EOL> with self . assertRaises ( ValueError ) : <EOL> self . _makeOne ( parent = parent_key ) <EOL> def test_ctor_explicit ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _NAMESPACE = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _ID = <NUM_LIT> <EOL> _PATH = [ { '<STR_LIT>' : _KIND , '<STR_LIT:id>' : _ID } ] <EOL> key = self . _makeOne ( _KIND , _ID , namespace = _NAMESPACE , <EOL> project = _PROJECT ) <EOL> self . assertEqual ( key . project , _PROJECT ) <EOL> self . assertEqual ( key . namespace , _NAMESPACE ) <EOL> self . assertEqual ( key . kind , _KIND ) <EOL> self . assertEqual ( key . path , _PATH ) <EOL> def test_ctor_bad_kind ( self ) : <EOL> self . assertRaises ( ValueError , self . _makeOne , object ( ) , <EOL> project = self . _DEFAULT_PROJECT ) <EOL> def test_ctor_bad_id_or_name ( self ) : <EOL> self . assertRaises ( ValueError , self . _makeOne , '<STR_LIT>' , object ( ) , <EOL> project = self . _DEFAULT_PROJECT ) <EOL> self . assertRaises ( ValueError , self . _makeOne , '<STR_LIT>' , None , <EOL> project = self . _DEFAULT_PROJECT ) <EOL> self . assertRaises ( ValueError , self . _makeOne , '<STR_LIT>' , <NUM_LIT:10> , '<STR_LIT>' , None , <EOL> project = self . _DEFAULT_PROJECT ) <EOL> def test__clone ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _NAMESPACE = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _ID = <NUM_LIT> <EOL> _PATH = [ { '<STR_LIT>' : _KIND , '<STR_LIT:id>' : _ID } ] <EOL> key = self . _makeOne ( _KIND , _ID , namespace = _NAMESPACE , <EOL> project = _PROJECT ) <EOL> clone = key . _clone ( ) <EOL> self . assertEqual ( clone . project , _PROJECT ) <EOL> self . assertEqual ( clone . namespace , _NAMESPACE ) <EOL> self . assertEqual ( clone . kind , _KIND ) <EOL> self . assertEqual ( clone . path , _PATH ) <EOL> def test__clone_with_parent ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _NAMESPACE = '<STR_LIT>' <EOL> _KIND1 = '<STR_LIT>' <EOL> _KIND2 = '<STR_LIT>' <EOL> _ID1 = <NUM_LIT> <EOL> _ID2 = <NUM_LIT> <EOL> _PATH = [ { '<STR_LIT>' : _KIND1 , '<STR_LIT:id>' : _ID1 } , { '<STR_LIT>' : _KIND2 , '<STR_LIT:id>' : _ID2 } ] <EOL> parent = self . _makeOne ( _KIND1 , _ID1 , namespace = _NAMESPACE , <EOL> project = _PROJECT ) <EOL> key = self . _makeOne ( _KIND2 , _ID2 , parent = parent ) <EOL> self . assertTrue ( key . parent is parent ) <EOL> clone = key . _clone ( ) <EOL> self . assertTrue ( clone . parent is key . parent ) <EOL> self . assertEqual ( clone . project , _PROJECT ) <EOL> self . assertEqual ( clone . namespace , _NAMESPACE ) <EOL> self . assertEqual ( clone . path , _PATH ) <EOL> def test___eq_____ne___w_non_key ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _NAME = '<STR_LIT>' <EOL> key = self . _makeOne ( _KIND , _NAME , project = _PROJECT ) <EOL> self . assertFalse ( key == object ( ) ) <EOL> self . assertTrue ( key != object ( ) ) <EOL> def test___eq_____ne___two_incomplete_keys_same_kind ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> key1 = self . _makeOne ( _KIND , project = _PROJECT ) <EOL> key2 = self . _makeOne ( _KIND , project = _PROJECT ) <EOL> self . assertFalse ( key1 == key2 ) <EOL> self . assertTrue ( key1 != key2 ) <EOL> def test___eq_____ne___incomplete_key_w_complete_key_same_kind ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _ID = <NUM_LIT> <EOL> key1 = self . _makeOne ( _KIND , project = _PROJECT ) <EOL> key2 = self . _makeOne ( _KIND , _ID , project = _PROJECT ) <EOL> self . assertFalse ( key1 == key2 ) <EOL> self . assertTrue ( key1 != key2 ) <EOL> def test___eq_____ne___complete_key_w_incomplete_key_same_kind ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _ID = <NUM_LIT> <EOL> key1 = self . _makeOne ( _KIND , _ID , project = _PROJECT ) <EOL> key2 = self . _makeOne ( _KIND , project = _PROJECT ) <EOL> self . assertFalse ( key1 == key2 ) <EOL> self . assertTrue ( key1 != key2 ) <EOL> def test___eq_____ne___same_kind_different_ids ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _ID1 = <NUM_LIT> <EOL> _ID2 = <NUM_LIT> <EOL> key1 = self . _makeOne ( _KIND , _ID1 , project = _PROJECT ) <EOL> key2 = self . _makeOne ( _KIND , _ID2 , project = _PROJECT ) <EOL> self . assertFalse ( key1 == key2 ) <EOL> self . assertTrue ( key1 != key2 ) <EOL> def test___eq_____ne___same_kind_and_id ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _ID = <NUM_LIT> <EOL> key1 = self . _makeOne ( _KIND , _ID , project = _PROJECT ) <EOL> key2 = self . _makeOne ( _KIND , _ID , project = _PROJECT ) <EOL> self . assertTrue ( key1 == key2 ) <EOL> self . assertFalse ( key1 != key2 ) <EOL> def test___eq_____ne___same_kind_and_id_different_project ( self ) : <EOL> _PROJECT1 = '<STR_LIT>' <EOL> _PROJECT2 = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _ID = <NUM_LIT> <EOL> key1 = self . _makeOne ( _KIND , _ID , project = _PROJECT1 ) <EOL> key2 = self . _makeOne ( _KIND , _ID , project = _PROJECT2 ) <EOL> self . assertFalse ( key1 == key2 ) <EOL> self . assertTrue ( key1 != key2 ) <EOL> def test___eq_____ne___same_kind_and_id_different_namespace ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _NAMESPACE1 = '<STR_LIT>' <EOL> _NAMESPACE2 = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _ID = <NUM_LIT> <EOL> key1 = self . _makeOne ( _KIND , _ID , project = _PROJECT , <EOL> namespace = _NAMESPACE1 ) <EOL> key2 = self . _makeOne ( _KIND , _ID , project = _PROJECT , <EOL> namespace = _NAMESPACE2 ) <EOL> self . assertFalse ( key1 == key2 ) <EOL> self . assertTrue ( key1 != key2 ) <EOL> def test___eq_____ne___same_kind_different_names ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _NAME1 = '<STR_LIT>' <EOL> _NAME2 = '<STR_LIT>' <EOL> key1 = self . _makeOne ( _KIND , _NAME1 , project = _PROJECT ) <EOL> key2 = self . _makeOne ( _KIND , _NAME2 , project = _PROJECT ) <EOL> self . assertFalse ( key1 == key2 ) <EOL> self . assertTrue ( key1 != key2 ) <EOL> def test___eq_____ne___same_kind_and_name ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _NAME = '<STR_LIT>' <EOL> key1 = self . _makeOne ( _KIND , _NAME , project = _PROJECT ) <EOL> key2 = self . _makeOne ( _KIND , _NAME , project = _PROJECT ) <EOL> self . assertTrue ( key1 == key2 ) <EOL> self . assertFalse ( key1 != key2 ) <EOL> def test___eq_____ne___same_kind_and_name_different_project ( self ) : <EOL> _PROJECT1 = '<STR_LIT>' <EOL> _PROJECT2 = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _NAME = '<STR_LIT>' <EOL> key1 = self . _makeOne ( _KIND , _NAME , project = _PROJECT1 ) <EOL> key2 = self . _makeOne ( _KIND , _NAME , project = _PROJECT2 ) <EOL> self . assertFalse ( key1 == key2 ) <EOL> self . assertTrue ( key1 != key2 ) <EOL> def test___eq_____ne___same_kind_and_name_different_namespace ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _NAMESPACE1 = '<STR_LIT>' <EOL> _NAMESPACE2 = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _NAME = '<STR_LIT>' <EOL> key1 = self . _makeOne ( _KIND , _NAME , project = _PROJECT , <EOL> namespace = _NAMESPACE1 ) <EOL> key2 = self . _makeOne ( _KIND , _NAME , project = _PROJECT , <EOL> namespace = _NAMESPACE2 ) <EOL> self . assertFalse ( key1 == key2 ) <EOL> self . assertTrue ( key1 != key2 ) <EOL> def test___hash___incomplete ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> key = self . _makeOne ( _KIND , project = _PROJECT ) <EOL> self . assertNotEqual ( hash ( key ) , <EOL> hash ( _KIND ) + hash ( _PROJECT ) + hash ( None ) ) <EOL> def test___hash___completed_w_id ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _ID = <NUM_LIT> <EOL> key = self . _makeOne ( _KIND , _ID , project = _PROJECT ) <EOL> self . assertNotEqual ( hash ( key ) , <EOL> hash ( _KIND ) + hash ( _ID ) + <EOL> hash ( _PROJECT ) + hash ( None ) ) <EOL> def test___hash___completed_w_name ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> _KIND = '<STR_LIT>' <EOL> _NAME = '<STR_LIT>' <EOL> key = self . _makeOne ( _KIND , _NAME , project = _PROJECT ) <EOL> self . assertNotEqual ( hash ( key ) , <EOL> hash ( _KIND ) + hash ( _NAME ) + <EOL> hash ( _PROJECT ) + hash ( None ) ) <EOL> def test_completed_key_on_partial_w_id ( self ) : <EOL> key = self . _makeOne ( '<STR_LIT>' , project = self . _DEFAULT_PROJECT ) <EOL> _ID = <NUM_LIT> <EOL> new_key = key . completed_key ( _ID ) <EOL> self . assertFalse ( key is new_key ) <EOL> self . assertEqual ( new_key . id , _ID ) <EOL> self . assertEqual ( new_key . name , None ) <EOL> def test_completed_key_on_partial_w_name ( self ) : <EOL> key = self . _makeOne ( '<STR_LIT>' , project = self . _DEFAULT_PROJECT ) <EOL> _NAME = '<STR_LIT>' <EOL> new_key = key . completed_key ( _NAME ) <EOL> self . assertFalse ( key is new_key ) <EOL> self . assertEqual ( new_key . id , None ) <EOL> self . assertEqual ( new_key . name , _NAME ) <EOL> def test_completed_key_on_partial_w_invalid ( self ) : <EOL> key = self . _makeOne ( '<STR_LIT>' , project = self . _DEFAULT_PROJECT ) <EOL> self . assertRaises ( ValueError , key . completed_key , object ( ) ) <EOL> def test_completed_key_on_complete ( self ) : <EOL> key = self . _makeOne ( '<STR_LIT>' , <NUM_LIT> , project = self . _DEFAULT_PROJECT ) <EOL> self . assertRaises ( ValueError , key . completed_key , <NUM_LIT> ) <EOL> def test_to_protobuf_defaults ( self ) : <EOL> from gcloud . datastore . _generated import entity_pb2 <EOL> _KIND = '<STR_LIT>' <EOL> key = self . _makeOne ( _KIND , project = self . _DEFAULT_PROJECT ) <EOL> pb = key . to_protobuf ( ) <EOL> self . assertTrue ( isinstance ( pb , entity_pb2 . Key ) ) <EOL> self . assertEqual ( pb . partition_id . project_id , self . _DEFAULT_PROJECT ) <EOL> self . assertEqual ( pb . partition_id . namespace_id , '<STR_LIT>' ) <EOL> elem , = list ( pb . path ) <EOL> self . assertEqual ( elem . kind , _KIND ) <EOL> self . assertEqual ( elem . name , '<STR_LIT>' ) <EOL> self . assertEqual ( elem . id , <NUM_LIT:0> ) <EOL> def test_to_protobuf_w_explicit_project ( self ) : <EOL> _PROJECT = '<STR_LIT>' <EOL> key = self . _makeOne ( '<STR_LIT>' , project = _PROJECT ) <EOL> pb = key . to_protobuf ( ) <EOL> self . assertEqual ( pb . partition_id . project_id , _PROJECT ) <EOL> def test_to_protobuf_w_explicit_namespace ( self ) : <EOL> _NAMESPACE = '<STR_LIT>' <EOL> key = self . _makeOne ( '<STR_LIT>' , namespace = _NAMESPACE , <EOL> project = self . _DEFAULT_PROJECT ) <EOL> pb = key . to_protobuf ( ) <EOL> self . assertEqual ( pb . partition_id . namespace_id , _NAMESPACE ) <EOL> def test_to_protobuf_w_explicit_path ( self ) : <EOL> _PARENT = '<STR_LIT>' <EOL> _CHILD = '<STR_LIT>' <EOL> _ID = <NUM_LIT> <EOL> _NAME = '<STR_LIT>' <EOL> key = self . _makeOne ( _PARENT , _NAME , _CHILD , _ID , <EOL> project = self . _DEFAULT_PROJECT ) <EOL> pb = key . to_protobuf ( ) <EOL> elems = list ( pb . path ) <EOL> self . assertEqual ( len ( elems ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( elems [ <NUM_LIT:0> ] . kind , _PARENT ) <EOL> self . assertEqual ( elems [ <NUM_LIT:0> ] . name , _NAME ) <EOL> self . assertEqual ( elems [ <NUM_LIT:1> ] . kind , _CHILD ) <EOL> self . assertEqual ( elems [ <NUM_LIT:1> ] . id , _ID ) <EOL> def test_to_protobuf_w_no_kind ( self ) : <EOL> key = self . _makeOne ( '<STR_LIT>' , project = self . _DEFAULT_PROJECT ) <EOL> key . _path [ - <NUM_LIT:1> ] . pop ( '<STR_LIT>' ) <EOL> pb = key . to_protobuf ( ) <EOL> self . assertEqual ( pb . path [ <NUM_LIT:0> ] . kind , '<STR_LIT>' ) <EOL> def test_is_partial_no_name_or_id ( self ) : <EOL> key = self . _makeOne ( '<STR_LIT>' , project = self . _DEFAULT_PROJECT ) <EOL> self . assertTrue ( key . is_partial ) <EOL> def test_is_partial_w_id ( self ) : <EOL> _ID = <NUM_LIT> <EOL> key = self . _makeOne ( '<STR_LIT>' , _ID , project = self . _DEFAULT_PROJECT ) <EOL> self . assertFalse ( key . is_partial ) <EOL> def test_is_partial_w_name ( self ) : <EOL> _NAME = '<STR_LIT>' <EOL> key = self . _makeOne ( '<STR_LIT>' , _NAME , project = self . _DEFAULT_PROJECT ) <EOL> self . assertFalse ( key . is_partial ) <EOL> def test_id_or_name_no_name_or_id ( self ) : <EOL> key = self . _makeOne ( '<STR_LIT>' , project = self . _DEFAULT_PROJECT ) <EOL> self . assertEqual ( key . id_or_name , None ) <EOL> def test_id_or_name_no_name_or_id_child ( self ) : <EOL> key = self . _makeOne ( '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' , <EOL> project = self . _DEFAULT_PROJECT ) <EOL> self . assertEqual ( key . id_or_name , None ) <EOL> def test_id_or_name_w_id_only ( self ) : <EOL> _ID = <NUM_LIT> <EOL> key = self . _makeOne ( '<STR_LIT>' , _ID , project = self . _DEFAULT_PROJECT ) <EOL> self . assertEqual ( key . id_or_name , _ID ) <EOL> def test_id_or_name_w_name_only ( self ) : <EOL> _NAME = '<STR_LIT>' <EOL> key = self . _makeOne ( '<STR_LIT>' , _NAME , project = self . _DEFAULT_PROJECT ) <EOL> self . assertEqual ( key . id_or_name , _NAME ) <EOL> def test_parent_default ( self ) : <EOL> key = self . _makeOne ( '<STR_LIT>' , project = self . _DEFAULT_PROJECT ) <EOL> self . assertEqual ( key . parent , None ) <EOL> def test_parent_explicit_top_level ( self ) : <EOL> key = self . _makeOne ( '<STR_LIT>' , <NUM_LIT> , project = self . _DEFAULT_PROJECT ) <EOL> self . assertEqual ( key . parent , None ) <EOL> def test_parent_explicit_nested ( self ) : <EOL> _PARENT_KIND = '<STR_LIT>' <EOL> _PARENT_ID = <NUM_LIT> <EOL> _PARENT_PATH = [ { '<STR_LIT>' : _PARENT_KIND , '<STR_LIT:id>' : _PARENT_ID } ] <EOL> key = self . _makeOne ( _PARENT_KIND , _PARENT_ID , '<STR_LIT>' , <EOL> project = self . _DEFAULT_PROJECT ) <EOL> self . assertEqual ( key . parent . path , _PARENT_PATH ) <EOL> def test_parent_multiple_calls ( self ) : <EOL> _PARENT_KIND = '<STR_LIT>' <EOL> _PARENT_ID = <NUM_LIT> <EOL> _PARENT_PATH = [ { '<STR_LIT>' : _PARENT_KIND , '<STR_LIT:id>' : _PARENT_ID } ] <EOL> key = self . _makeOne ( _PARENT_KIND , _PARENT_ID , '<STR_LIT>' , <EOL> project = self . _DEFAULT_PROJECT ) <EOL> parent = key . parent <EOL> self . assertEqual ( parent . path , _PARENT_PATH ) <EOL> new_parent = key . parent <EOL> self . assertTrue ( parent is new_parent ) </s>
