@@ -349,82 +349,100 @@ def test_patch_w_user_project_w_explicit_client(self):
349349 _target_object = derived ,
350350 )
351351
352- def test_update (self ):
353- connection = _Connection ({ "foo" : "Foo" })
354- client = _Client ( connection )
355- derived = self ._derivedClass ("/ path" )()
352+ def test_update_w_defaults (self ):
353+ path = "/path"
354+ api_response = { "foo" : "Foo" }
355+ derived = self ._derivedClass (path )()
356356 # Make sure changes is non-empty, so we can observe a change.
357- BAR = object ()
358- BAZ = object ()
359- derived ._properties = {"bar" : BAR , "baz" : BAZ }
357+ bar = object ()
358+ baz = object ()
359+ expected_data = derived ._properties = {"bar" : bar , "baz" : baz }
360360 derived ._changes = set (["bar" ]) # Update sends 'baz' anyway.
361- derived .update (client = client , timeout = 42 )
362- self .assertEqual (derived ._properties , {"foo" : "Foo" })
363- kw = connection ._requested
364- self .assertEqual (len (kw ), 1 )
365- self .assertEqual (kw [0 ]["method" ], "PUT" )
366- self .assertEqual (kw [0 ]["path" ], "/path" )
367- self .assertEqual (kw [0 ]["query_params" ], {"projection" : "full" })
368- self .assertEqual (kw [0 ]["data" ], {"bar" : BAR , "baz" : BAZ })
369- self .assertEqual (kw [0 ]["timeout" ], 42 )
370- self .assertEqual (kw [0 ]["retry" ], DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED )
371- # Make sure changes get reset by patch().
361+ client = derived .client = mock .Mock (spec = ["_put_resource" ])
362+ client ._put_resource .return_value = api_response
363+
364+ derived .update ()
365+
366+ self .assertEqual (derived ._properties , api_response )
367+ # Make sure changes get reset by update().
372368 self .assertEqual (derived ._changes , set ())
373369
374- def test_update_with_metageneration_not_match (self ):
375- GENERATION_NUMBER = 6
370+ expected_query_params = {"projection" : "full" }
371+ client ._put_resource .assert_called_once_with (
372+ path ,
373+ expected_data ,
374+ query_params = expected_query_params ,
375+ timeout = self ._get_default_timeout (),
376+ retry = DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED ,
377+ _target_object = derived ,
378+ )
376379
377- connection = _Connection ({"foo" : "Foo" })
378- client = _Client (connection )
379- derived = self ._derivedClass ("/path" )()
380+ def test_update_with_metageneration_not_match_w_timeout_w_retry (self ):
381+ path = "/path"
382+ generation_number = 6
383+ api_response = {"foo" : "Foo" }
384+ derived = self ._derivedClass (path )()
380385 # Make sure changes is non-empty, so we can observe a change.
381- BAR = object ()
382- BAZ = object ()
383- derived ._properties = {"bar" : BAR , "baz" : BAZ }
386+ bar = object ()
387+ baz = object ()
388+ expected_data = derived ._properties = {"bar" : bar , "baz" : baz }
384389 derived ._changes = set (["bar" ]) # Update sends 'baz' anyway.
390+ client = derived .client = mock .Mock (spec = ["_put_resource" ])
391+ client ._put_resource .return_value = api_response
392+ timeout = 42
393+
385394 derived .update (
386- client = client , timeout = 42 , if_metageneration_not_match = GENERATION_NUMBER
395+ if_metageneration_not_match = generation_number , timeout = timeout ,
387396 )
397+
388398 self .assertEqual (derived ._properties , {"foo" : "Foo" })
389- kw = connection ._requested
390- self .assertEqual (len (kw ), 1 )
391- self .assertEqual (kw [0 ]["method" ], "PUT" )
392- self .assertEqual (kw [0 ]["path" ], "/path" )
393- self .assertEqual (
394- kw [0 ]["query_params" ],
395- {"projection" : "full" , "ifMetagenerationNotMatch" : GENERATION_NUMBER },
396- )
397- self .assertEqual (kw [0 ]["data" ], {"bar" : BAR , "baz" : BAZ })
398- self .assertEqual (kw [0 ]["timeout" ], 42 )
399- self .assertEqual (kw [0 ]["retry" ], DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED )
400399 # Make sure changes get reset by patch().
401400 self .assertEqual (derived ._changes , set ())
402401
403- def test_update_w_user_project (self ):
402+ expected_query_params = {
403+ "projection" : "full" ,
404+ "ifMetagenerationNotMatch" : generation_number ,
405+ }
406+ client ._put_resource .assert_called_once_with (
407+ path ,
408+ expected_data ,
409+ query_params = expected_query_params ,
410+ timeout = timeout ,
411+ retry = DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED ,
412+ _target_object = derived ,
413+ )
414+
415+ def test_update_w_user_project_w_retry_w_explicit_client (self ):
404416 user_project = "user-project-123"
405- connection = _Connection ({ "foo" : "Foo" })
406- client = _Client ( connection )
407- derived = self ._derivedClass ("/ path" , user_project )()
417+ path = "/path"
418+ api_response = { "foo" : "Foo" }
419+ derived = self ._derivedClass (path , user_project )()
408420 # Make sure changes is non-empty, so we can observe a change.
409- BAR = object ()
410- BAZ = object ()
411- derived ._properties = {"bar" : BAR , "baz" : BAZ }
421+ bar = object ()
422+ baz = object ()
423+ expected_data = derived ._properties = {"bar" : bar , "baz" : baz }
412424 derived ._changes = set (["bar" ]) # Update sends 'baz' anyway.
413- derived .update (client = client )
414- self .assertEqual (derived ._properties , {"foo" : "Foo" })
415- kw = connection ._requested
416- self .assertEqual (len (kw ), 1 )
417- self .assertEqual (kw [0 ]["method" ], "PUT" )
418- self .assertEqual (kw [0 ]["path" ], "/path" )
419- self .assertEqual (
420- kw [0 ]["query_params" ], {"projection" : "full" , "userProject" : user_project }
421- )
422- self .assertEqual (kw [0 ]["data" ], {"bar" : BAR , "baz" : BAZ })
423- self .assertEqual (kw [0 ]["timeout" ], self ._get_default_timeout ())
424- self .assertEqual (kw [0 ]["retry" ], DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED )
425+ client = mock .Mock (spec = ["_put_resource" ])
426+ client ._put_resource .return_value = api_response
427+ retry = mock .Mock (spec = [])
428+
429+ derived .update (client = client , retry = retry )
425430 # Make sure changes get reset by patch().
426431 self .assertEqual (derived ._changes , set ())
427432
433+ expected_query_params = {
434+ "projection" : "full" ,
435+ "userProject" : user_project ,
436+ }
437+ client ._put_resource .assert_called_once_with (
438+ path ,
439+ expected_data ,
440+ query_params = expected_query_params ,
441+ timeout = self ._get_default_timeout (),
442+ retry = retry ,
443+ _target_object = derived ,
444+ )
445+
428446
429447class Test__scalar_property (unittest .TestCase ):
430448 def _call_fut (self , fieldName ):
@@ -575,17 +593,6 @@ def test_hostname_and_scheme(self):
575593 self .assertEqual (self ._call_fut (host = HOST , scheme = SCHEME ), EXPECTED_URL )
576594
577595
578- class _Connection (object ):
579- def __init__ (self , * responses ):
580- self ._responses = responses
581- self ._requested = []
582-
583- def api_request (self , ** kw ):
584- self ._requested .append (kw )
585- response , self ._responses = self ._responses [0 ], self ._responses [1 :]
586- return response
587-
588-
589596class _MD5Hash (object ):
590597 def __init__ (self , digest_val ):
591598 self .digest_val = digest_val
@@ -617,8 +624,3 @@ def __init__(self):
617624 def b64encode (self , value ):
618625 self ._called_b64encode .append (value )
619626 return value
620-
621-
622- class _Client (object ):
623- def __init__ (self , connection ):
624- self ._connection = connection
0 commit comments