@@ -357,6 +357,194 @@ def test_delete_cells_with_string_columns(self):
357357 )
358358 self .assertEqual (row ._pb_mutations , [expected_pb1 , expected_pb2 ])
359359
360+ def test_commit (self ):
361+ from google .protobuf import empty_pb2
362+ from gcloud .bigtable ._generated import bigtable_data_pb2 as data_pb2
363+ from gcloud .bigtable ._generated import (
364+ bigtable_service_messages_pb2 as messages_pb2 )
365+ from gcloud .bigtable ._testing import _FakeStub
366+
367+ row_key = b'row_key'
368+ table_name = 'projects/more-stuff'
369+ column_family_id = u'column_family_id'
370+ column = b'column'
371+ timeout_seconds = 711
372+ client = _Client (timeout_seconds = timeout_seconds )
373+ table = _Table (table_name , client = client )
374+ row = self ._makeOne (row_key , table )
375+
376+ # Create request_pb
377+ value = b'bytes-value'
378+ mutation = data_pb2 .Mutation (
379+ set_cell = data_pb2 .Mutation .SetCell (
380+ family_name = column_family_id ,
381+ column_qualifier = column ,
382+ timestamp_micros = - 1 , # Default value.
383+ value = value ,
384+ ),
385+ )
386+ request_pb = messages_pb2 .MutateRowRequest (
387+ table_name = table_name ,
388+ row_key = row_key ,
389+ mutations = [mutation ],
390+ )
391+
392+ # Create response_pb
393+ response_pb = empty_pb2 .Empty ()
394+
395+ # Patch the stub used by the API method.
396+ client ._data_stub = stub = _FakeStub (response_pb )
397+
398+ # Create expected_result.
399+ expected_result = None # commit() has no return value when no filter.
400+
401+ # Perform the method and check the result.
402+ row .set_cell (column_family_id , column , value )
403+ result = row .commit ()
404+ self .assertEqual (result , expected_result )
405+ self .assertEqual (stub .method_calls , [(
406+ 'MutateRow' ,
407+ (request_pb , timeout_seconds ),
408+ {},
409+ )])
410+ self .assertEqual (row ._pb_mutations , [])
411+ self .assertEqual (row ._true_pb_mutations , None )
412+ self .assertEqual (row ._false_pb_mutations , None )
413+
414+ def test_commit_too_many_mutations (self ):
415+ from gcloud ._testing import _Monkey
416+ from gcloud .bigtable import row as MUT
417+
418+ row_key = b'row_key'
419+ table = object ()
420+ row = self ._makeOne (row_key , table )
421+ row ._pb_mutations = [1 , 2 , 3 ]
422+ num_mutations = len (row ._pb_mutations )
423+ with _Monkey (MUT , _MAX_MUTATIONS = num_mutations - 1 ):
424+ with self .assertRaises (ValueError ):
425+ row .commit ()
426+
427+ def test_commit_no_mutations (self ):
428+ from gcloud .bigtable ._testing import _FakeStub
429+
430+ row_key = b'row_key'
431+ client = _Client ()
432+ table = _Table (None , client = client )
433+ row = self ._makeOne (row_key , table )
434+ self .assertEqual (row ._pb_mutations , [])
435+
436+ # Patch the stub used by the API method.
437+ client ._data_stub = stub = _FakeStub ()
438+
439+ # Perform the method and check the result.
440+ result = row .commit ()
441+ self .assertEqual (result , None )
442+ # Make sure no request was sent.
443+ self .assertEqual (stub .method_calls , [])
444+
445+ def test_commit_with_filter (self ):
446+ from gcloud .bigtable ._generated import bigtable_data_pb2 as data_pb2
447+ from gcloud .bigtable ._generated import (
448+ bigtable_service_messages_pb2 as messages_pb2 )
449+ from gcloud .bigtable ._testing import _FakeStub
450+ from gcloud .bigtable .row import RowSampleFilter
451+
452+ row_key = b'row_key'
453+ table_name = 'projects/more-stuff'
454+ column_family_id = u'column_family_id'
455+ column = b'column'
456+ timeout_seconds = 262
457+ client = _Client (timeout_seconds = timeout_seconds )
458+ table = _Table (table_name , client = client )
459+ row_filter = RowSampleFilter (0.33 )
460+ row = self ._makeOne (row_key , table , filter_ = row_filter )
461+
462+ # Create request_pb
463+ value1 = b'bytes-value'
464+ mutation1 = data_pb2 .Mutation (
465+ set_cell = data_pb2 .Mutation .SetCell (
466+ family_name = column_family_id ,
467+ column_qualifier = column ,
468+ timestamp_micros = - 1 , # Default value.
469+ value = value1 ,
470+ ),
471+ )
472+ value2 = b'other-bytes'
473+ mutation2 = data_pb2 .Mutation (
474+ set_cell = data_pb2 .Mutation .SetCell (
475+ family_name = column_family_id ,
476+ column_qualifier = column ,
477+ timestamp_micros = - 1 , # Default value.
478+ value = value2 ,
479+ ),
480+ )
481+ request_pb = messages_pb2 .CheckAndMutateRowRequest (
482+ table_name = table_name ,
483+ row_key = row_key ,
484+ predicate_filter = row_filter .to_pb (),
485+ true_mutations = [mutation1 ],
486+ false_mutations = [mutation2 ],
487+ )
488+
489+ # Create response_pb
490+ predicate_matched = True
491+ response_pb = messages_pb2 .CheckAndMutateRowResponse (
492+ predicate_matched = predicate_matched )
493+
494+ # Patch the stub used by the API method.
495+ client ._data_stub = stub = _FakeStub (response_pb )
496+
497+ # Create expected_result.
498+ expected_result = predicate_matched
499+
500+ # Perform the method and check the result.
501+ row .set_cell (column_family_id , column , value1 , state = True )
502+ row .set_cell (column_family_id , column , value2 , state = False )
503+ result = row .commit ()
504+ self .assertEqual (result , expected_result )
505+ self .assertEqual (stub .method_calls , [(
506+ 'CheckAndMutateRow' ,
507+ (request_pb , timeout_seconds ),
508+ {},
509+ )])
510+ self .assertEqual (row ._pb_mutations , None )
511+ self .assertEqual (row ._true_pb_mutations , [])
512+ self .assertEqual (row ._false_pb_mutations , [])
513+
514+ def test_commit_with_filter_too_many_mutations (self ):
515+ from gcloud ._testing import _Monkey
516+ from gcloud .bigtable import row as MUT
517+
518+ row_key = b'row_key'
519+ table = object ()
520+ filter_ = object ()
521+ row = self ._makeOne (row_key , table , filter_ = filter_ )
522+ row ._true_pb_mutations = [1 , 2 , 3 ]
523+ num_mutations = len (row ._true_pb_mutations )
524+ with _Monkey (MUT , _MAX_MUTATIONS = num_mutations - 1 ):
525+ with self .assertRaises (ValueError ):
526+ row .commit ()
527+
528+ def test_commit_with_filter_no_mutations (self ):
529+ from gcloud .bigtable ._testing import _FakeStub
530+
531+ row_key = b'row_key'
532+ client = _Client ()
533+ table = _Table (None , client = client )
534+ filter_ = object ()
535+ row = self ._makeOne (row_key , table , filter_ = filter_ )
536+ self .assertEqual (row ._true_pb_mutations , [])
537+ self .assertEqual (row ._false_pb_mutations , [])
538+
539+ # Patch the stub used by the API method.
540+ client ._data_stub = stub = _FakeStub ()
541+
542+ # Perform the method and check the result.
543+ result = row .commit ()
544+ self .assertEqual (result , None )
545+ # Make sure no request was sent.
546+ self .assertEqual (stub .method_calls , [])
547+
360548
361549class Test_BoolFilter (unittest2 .TestCase ):
362550
@@ -1345,3 +1533,24 @@ def test_to_pb_false_only(self):
13451533 ),
13461534 )
13471535 self .assertEqual (filter_pb , expected_pb )
1536+
1537+
1538+ class _Client (object ):
1539+
1540+ data_stub = None
1541+
1542+ def __init__ (self , timeout_seconds = None ):
1543+ self .timeout_seconds = timeout_seconds
1544+
1545+
1546+ class _Cluster (object ):
1547+
1548+ def __init__ (self , client = None ):
1549+ self ._client = client
1550+
1551+
1552+ class _Table (object ):
1553+
1554+ def __init__ (self , name , client = None ):
1555+ self .name = name
1556+ self ._cluster = _Cluster (client )
0 commit comments