@@ -10,29 +10,121 @@ def _getTargetClass(self):
1010 def _makeOne (self , * args , ** kw ):
1111 return self ._getTargetClass ()(* args , ** kw )
1212
13- def test_abstract_properties (self ):
14- metadata_object = self ._makeOne ()
15- self .assertRaises (NotImplementedError ,
16- lambda : metadata_object .connection )
17- self .assertRaises (NotImplementedError ,
18- lambda : metadata_object .path )
13+ def _derivedClass (self , connection = None , path = None , ** custom_fields ):
1914
20- def test_get_metadata_w_custom_field (self ):
2115 class Derived (self ._getTargetClass ()):
22- CUSTOM_METADATA_FIELDS = { 'foo' : 'get_foo' }
16+ CUSTOM_METADATA_FIELDS = custom_fields
2317
2418 @property
25- def connection (self ): # pragma: NO COVER
26- return None
19+ def connection (self ):
20+ return connection
2721
2822 @property
29- def path (self ): # pragma: NO COVER
30- return None
23+ def path (self ):
24+ return path
25+
26+ return Derived
27+
28+ def test_connetction_is_abstract (self ):
29+ mixin = self ._makeOne ()
30+ self .assertRaises (NotImplementedError , lambda : mixin .connection )
31+
32+ def test_path_is_abstract (self ):
33+ mixin = self ._makeOne ()
34+ self .assertRaises (NotImplementedError , lambda : mixin .path )
35+
36+ def test_has_metadata_not_loaded (self ):
37+ mixin = self ._makeOne ()
38+ self .assertEqual (mixin .has_metadata ('nonesuch' ), False )
39+
40+ def test_has_metadata_loaded_no_field (self ):
41+ mixin = self ._makeOne (metadata = {'foo' : 'Foo' })
42+ self .assertEqual (mixin .has_metadata (), True )
43+
44+ def test_has_metadata_loaded_miss (self ):
45+ mixin = self ._makeOne (metadata = {'foo' : 'Foo' })
46+ self .assertEqual (mixin .has_metadata ('nonesuch' ), False )
47+
48+ def test_has_metadata_loaded_hit (self ):
49+ mixin = self ._makeOne (metadata = {'extant' : False })
50+ self .assertEqual (mixin .has_metadata ('extant' ), True )
51+
52+ def test_reload_metadata (self ):
53+ connection = _Connection ({'foo' : 'Foo' })
54+ derived = self ._derivedClass (connection , '/path' )()
55+ derived .reload_metadata ()
56+ self .assertEqual (derived .metadata , {'foo' : 'Foo' })
57+ kw = connection ._requested
58+ self .assertEqual (len (kw ), 1 )
59+ self .assertEqual (kw [0 ]['method' ], 'GET' )
60+ self .assertEqual (kw [0 ]['path' ], '/path' )
61+ self .assertEqual (kw [0 ]['query_params' ], {'projection' : 'noAcl' })
62+
63+ def test_get_metadata_eager_no_field (self ):
64+ derived = self ._derivedClass ()(metadata = {'extant' : False })
65+ self .assertEqual (derived .get_metadata (), {'extant' : False })
66+
67+ def test_get_metadata_eager_hit (self ):
68+ derived = self ._derivedClass ()(metadata = {'foo' : 'Foo' })
69+ self .assertEqual (derived .get_metadata ('foo' ), 'Foo' )
3170
32- derived = Derived ()
71+ def test_get_metadata_lazy_hit (self ):
72+ connection = _Connection ({'foo' : 'Foo' })
73+ derived = self ._derivedClass (connection , '/path' )()
74+ self .assertEqual (derived .get_metadata ('foo' ), 'Foo' )
75+ kw = connection ._requested
76+ self .assertEqual (len (kw ), 1 )
77+ self .assertEqual (kw [0 ]['method' ], 'GET' )
78+ self .assertEqual (kw [0 ]['path' ], '/path' )
79+ self .assertEqual (kw [0 ]['query_params' ], {'projection' : 'noAcl' })
80+
81+ def test_get_metadata_w_custom_field (self ):
82+ derived = self ._derivedClass (foo = 'get_foo' )()
3383 try :
3484 derived .get_metadata ('foo' )
3585 except KeyError as e :
3686 self .assertTrue ('get_foo' in str (e ))
3787 else : # pragma: NO COVER
3888 self .assert_ ('KeyError not raised' )
89+
90+ def test_patch_metadata (self ):
91+ connection = _Connection ({'foo' : 'Foo' })
92+ derived = self ._derivedClass (connection , '/path' )()
93+ self .assertTrue (derived .patch_metadata ({'foo' : 'Foo' }) is derived )
94+ kw = connection ._requested
95+ self .assertEqual (len (kw ), 1 )
96+ self .assertEqual (kw [0 ]['method' ], 'PATCH' )
97+ self .assertEqual (kw [0 ]['path' ], '/path' )
98+ self .assertEqual (kw [0 ]['data' ], {'foo' : 'Foo' })
99+ self .assertEqual (kw [0 ]['query_params' ], {'projection' : 'full' })
100+
101+ def test_get_acl_not_yet_loaded (self ):
102+ class ACL (object ):
103+ loaded = False
104+
105+ def reload (self ):
106+ self .loaded = True
107+
108+ mixin = self ._makeOne ()
109+ acl = mixin .acl = ACL ()
110+ self .assertTrue (mixin .get_acl () is acl )
111+ self .assertTrue (acl .loaded )
112+
113+ def test_get_acl_already_loaded (self ):
114+ class ACL (object ):
115+ loaded = True
116+ mixin = self ._makeOne ()
117+ acl = mixin .acl = ACL ()
118+ self .assertTrue (mixin .get_acl () is acl ) # no 'reload'
119+
120+
121+ class _Connection (object ):
122+
123+ def __init__ (self , * responses ):
124+ self ._responses = responses
125+ self ._requested = []
126+
127+ def api_request (self , ** kw ):
128+ self ._requested .append (kw )
129+ response , self ._responses = self ._responses [0 ], self ._responses [1 :]
130+ return response
0 commit comments