@@ -24,34 +24,46 @@ def _getTargetClass(self):
2424 def _makeOne (self , * args , ** kw ):
2525 return self ._getTargetClass ()(* args , ** kw )
2626
27- def _derivedClass (self , connection = None , path = None ):
27+ def _derivedClass (self , path = None ):
2828
2929 class Derived (self ._getTargetClass ()):
3030
31- @property
32- def connection (self ):
33- return connection
34-
3531 @property
3632 def path (self ):
3733 return path
3834
3935 return Derived
4036
41- def test_connection_is_abstract (self ):
42- mixin = self . _makeOne ()
43- self . assertRaises ( NotImplementedError , lambda : mixin . connection )
37+ def _monkey (self , connection ):
38+ from gcloud . storage . _testing import _monkey_defaults
39+ return _monkey_defaults ( connection = connection )
4440
4541 def test_path_is_abstract (self ):
4642 mixin = self ._makeOne ()
4743 self .assertRaises (NotImplementedError , lambda : mixin .path )
4844
49- def test_reload (self ):
45+ def test_reload_w_implicit_connection (self ):
46+ connection = _Connection ({'foo' : 'Foo' })
47+ derived = self ._derivedClass ('/path' )()
48+ # Make sure changes is not a set, so we can observe a change.
49+ derived ._changes = object ()
50+ with self ._monkey (connection ):
51+ derived .reload ()
52+ self .assertEqual (derived ._properties , {'foo' : 'Foo' })
53+ kw = connection ._requested
54+ self .assertEqual (len (kw ), 1 )
55+ self .assertEqual (kw [0 ]['method' ], 'GET' )
56+ self .assertEqual (kw [0 ]['path' ], '/path' )
57+ self .assertEqual (kw [0 ]['query_params' ], {'projection' : 'noAcl' })
58+ # Make sure changes get reset by reload.
59+ self .assertEqual (derived ._changes , set ())
60+
61+ def test_reload_w_explicit_connection (self ):
5062 connection = _Connection ({'foo' : 'Foo' })
51- derived = self ._derivedClass (connection , '/path' )()
63+ derived = self ._derivedClass ('/path' )()
5264 # Make sure changes is not a set, so we can observe a change.
5365 derived ._changes = object ()
54- derived .reload ()
66+ derived .reload (connection )
5567 self .assertEqual (derived ._properties , {'foo' : 'Foo' })
5668 kw = connection ._requested
5769 self .assertEqual (len (kw ), 1 )
@@ -66,15 +78,36 @@ def test__patch_property(self):
6678 derived ._patch_property ('foo' , 'Foo' )
6779 self .assertEqual (derived ._properties , {'foo' : 'Foo' })
6880
69- def test_patch (self ):
81+ def test_patch_w_implicit_connection (self ):
82+ connection = _Connection ({'foo' : 'Foo' })
83+ derived = self ._derivedClass ('/path' )()
84+ # Make sure changes is non-empty, so we can observe a change.
85+ BAR = object ()
86+ BAZ = object ()
87+ derived ._properties = {'bar' : BAR , 'baz' : BAZ }
88+ derived ._changes = set (['bar' ]) # Ignore baz.
89+ with self ._monkey (connection ):
90+ derived .patch ()
91+ self .assertEqual (derived ._properties , {'foo' : 'Foo' })
92+ kw = connection ._requested
93+ self .assertEqual (len (kw ), 1 )
94+ self .assertEqual (kw [0 ]['method' ], 'PATCH' )
95+ self .assertEqual (kw [0 ]['path' ], '/path' )
96+ self .assertEqual (kw [0 ]['query_params' ], {'projection' : 'full' })
97+ # Since changes does not include `baz`, we don't see it sent.
98+ self .assertEqual (kw [0 ]['data' ], {'bar' : BAR })
99+ # Make sure changes get reset by patch().
100+ self .assertEqual (derived ._changes , set ())
101+
102+ def test_patch_w_explicit_connection (self ):
70103 connection = _Connection ({'foo' : 'Foo' })
71- derived = self ._derivedClass (connection , '/path' )()
104+ derived = self ._derivedClass ('/path' )()
72105 # Make sure changes is non-empty, so we can observe a change.
73106 BAR = object ()
74107 BAZ = object ()
75108 derived ._properties = {'bar' : BAR , 'baz' : BAZ }
76109 derived ._changes = set (['bar' ]) # Ignore baz.
77- derived .patch ()
110+ derived .patch (connection )
78111 self .assertEqual (derived ._properties , {'foo' : 'Foo' })
79112 kw = connection ._requested
80113 self .assertEqual (len (kw ), 1 )
0 commit comments