From 04c80e0a43ea27c1ed66ef9eb8759c636dc63197 Mon Sep 17 00:00:00 2001 From: ajaaym <34161822+ajaaym@users.noreply.github.com> Date: Fri, 19 Oct 2018 18:51:54 -0400 Subject: [PATCH] Bigtable: checkAndMutateRow api implementation (#1333) * checkAndMutateRow api * fix doc * fix snippet test * change snippet test * address feedback * update note * update comment * apply note to truemutations * fix comment * fix snippet test --- Bigtable/src/DataClient.php | 96 ++++++++++- Bigtable/src/Mutations.php | 145 ++++++++++++++++ Bigtable/src/RowMutation.php | 57 ++----- Bigtable/tests/Snippet/DataClientTest.php | 30 ++++ .../DataClientCheckAndMutateRowTest.php | 139 ++++++++++++++++ Bigtable/tests/Unit/DataClientTest.php | 100 ++++++++++- Bigtable/tests/Unit/MutationsTest.php | 155 ++++++++++++++++++ Bigtable/tests/Unit/RowMutationTest.php | 10 +- 8 files changed, 676 insertions(+), 56 deletions(-) create mode 100644 Bigtable/src/Mutations.php create mode 100644 Bigtable/tests/System/DataClientCheckAndMutateRowTest.php create mode 100644 Bigtable/tests/Unit/MutationsTest.php diff --git a/Bigtable/src/DataClient.php b/Bigtable/src/DataClient.php index 4522ad465200..a825b4ca9b7c 100644 --- a/Bigtable/src/DataClient.php +++ b/Bigtable/src/DataClient.php @@ -22,6 +22,7 @@ use Google\Cloud\Bigtable\Exception\BigtableDataOperationException; use Google\Cloud\Bigtable\Filter\FilterInterface; use Google\Cloud\Bigtable\ReadModifyWriteRowRules; +use Google\Cloud\Bigtable\Mutations; use Google\Cloud\Bigtable\V2\BigtableClient as TableClient; use Google\Cloud\Bigtable\V2\Row; use Google\Cloud\Bigtable\V2\RowRange; @@ -144,7 +145,7 @@ public function mutateRows(array $rowMutations, array $options = []) { $entries = []; foreach ($rowMutations as $rowMutation) { - $entries[] = $rowMutation->getEntry(); + $entries[] = $rowMutation->toProto(); } $responseStream = $this->bigtableClient->mutateRows($this->tableName, $entries, $options + $this->options); $rowMutationsFailedResponse = []; @@ -436,4 +437,97 @@ public function sampleRowKeys(array $options = []) ]; } } + + /** + * Mutates the specified row atomically based on output of the filter. + * + * Example: + * ``` + * use Google\Cloud\Bigtable\Mutations; + * + * $mutations = (new Mutations)->upsert('family', 'qualifier', 'value'); + * $result = $dataClient->checkAndMutateRow('rk1', ['trueMutations' => $mutations]); + * ``` + * + * ``` + * // With predicate filter + * use Google\Cloud\Bigtable\Filter; + * use Google\Cloud\Bigtable\Mutations; + * + * $mutations = (new Mutations)->upsert('family', 'qualifier', 'value'); + * $predicateFilter = Filter::qualifier()->exactMatch('cq'); + * $options = ['predicateFilter' => $predicateFilter, 'trueMutations' => $mutations]; + * $result = $dataClient->checkAndMutateRow('rk1', $options); + * ``` + * + * @param string $rowKey The row key to mutate row conditionally. + * @param array $options [optional] { + * Configuration options. + * + * @type FilterInterface $predicateFilter The filter to be applied to the specified + * row. Depending on whether or not any results are yielded, either the + * trueMutations or falseMutations will be executed. If unset, checks that the + * row contains any values at all. Only a single condition can be set, however + * that filter can be {@see Google\Cloud\Bigtable\Filter::chain()} or + * {@see Google\Cloud\Bigtable\Filter::interleave()} which can wrap multiple other + * filters. + * WARNING: {@see Google\Cloud\Bigtable\Filter::condition()} is not supported. + * @type Mutations $trueMutations Mutations to be atomically applied when the predicate + * filter's condition yields at least one cell when applied to the row. Please note + * either `trueMutations` or `falseMutations` must be provided. + * @type Mutations $falseMutations Mutations to be atomically applied when the predicate + * filter's condition does not yield any cells when applied to the row. Please note + * either `trueMutations` or `falseMutations` must be provided. + * } + * + * @return bool Returns true if predicate filter yielded any output, false otherwise. + * @throws ApiException if the remote call fails or operation fails + * @throws \InvalidArgumentException if neither of $trueMutations or $falseMutations is set. + * if $predicateFilter is not instance of {@see Google\Cloud\Bigtable\Filter\FilterInterface}. + * if $trueMutations is set and is not instance of {@see Google\Cloud\Bigtable\Mutations}. + * if $falseMutations is set and is not instance of {@see Google\Cloud\Bigtable\Mutations}. + */ + public function checkAndMutateRow($rowKey, array $options = []) + { + $hasSetMutations = false; + + if (isset($options['predicateFilter'])) { + $this->convertToProto($options, 'predicateFilter', FilterInterface::class); + } + if (isset($options['trueMutations'])) { + $this->convertToProto($options, 'trueMutations', Mutations::class); + $hasSetMutations = true; + } + if (isset($options['falseMutations'])) { + $this->convertToProto($options, 'falseMutations', Mutations::class); + $hasSetMutations = true; + } + if (!$hasSetMutations) { + throw new \InvalidArgumentException('checkAndMutateRow must have either trueMutations or falseMutations.'); + } + + return $this->bigtableClient + ->checkAndMutateRow( + $this->tableName, + $rowKey, + $options + $this->options + ) + ->getPredicateMatched(); + } + + private function convertToProto(array &$options, $key, $expectedType) + { + if (!$options[$key] instanceof $expectedType) { + throw new \InvalidArgumentException( + sprintf( + 'Expected %s to be of type \'%s\', instead got \'%s\'.', + $key, + $expectedType, + gettype($options[$key]) + ) + ); + } + + $options[$key] = $options[$key]->toProto(); + } } diff --git a/Bigtable/src/Mutations.php b/Bigtable/src/Mutations.php new file mode 100644 index 000000000000..2d379347ef8c --- /dev/null +++ b/Bigtable/src/Mutations.php @@ -0,0 +1,145 @@ +setFamilyName($family) + ->setColumnQualifier($qualifier) + ->setValue($value); + if ($timeStamp === null) { + $mutationSetCell->setTimestampMicros( + // gives milli second + round($this->microtime() * 1000) + // multiply by 1000 to get micro + * 1000 + ); + } else { + $mutationSetCell->setTimestampMicros($timeStamp); + } + $this->mutations[] = (new Mutation)->setSetCell($mutationSetCell); + return $this; + } + + /** + * Creates delete from family mutation for a row. + * + * @param string $family Family name of the row. + * + * @return Mutations returns current Mutations object. + */ + public function deleteFromFamily($family) + { + $this->mutations[] = (new Mutation) + ->setDeleteFromFamily( + (new DeleteFromFamily)->setFamilyName($family) + ); + return $this; + } + + /** + * Creates delete from column mutation for a row. + * + * @param string $family Family name of the row. + * @param string $qualifier Column qualifier of the row. + * @param array $timeRange [optional] Array of values value, in microseconds to + * delete from column, keyed by `start` and `end` representing time range + * window. + * `start` **Defaults to** 0 + * `end` **Defaults to** infinity + * @return Mutations returns current Mutations object. + */ + public function deleteFromColumn($family, $qualifier, array $timeRange = []) + { + $deleteFromColumn = (new DeleteFromColumn) + ->setFamilyName($family) + ->setColumnQualifier($qualifier); + if (!empty($timeRange)) { + $timestampRange = new TimestampRange; + if (isset($timeRange['start'])) { + $timestampRange->setStartTimestampMicros($timeRange['start']); + } + if (isset($timeRange['end'])) { + $timestampRange->setEndTimestampMicros($timeRange['end']); + } + $deleteFromColumn->setTimeRange($timestampRange); + } + $this->mutations[] = (new Mutation)->setDeleteFromColumn($deleteFromColumn); + return $this; + } + + /** + * Creates delete row mutation for a row. + * + * @return Mutations returns current Mutations object. + */ + public function deleteRow() + { + $this->mutations[] = (new Mutation)->setDeleteFromRow(new DeleteFromRow); + return $this; + } + + /** + * Returns current unix timestamp with microseconds. This method exists for + * testing purposes. + * + * @return float + */ + protected function microtime() + { + return microtime(true); + } + + /** + * Returns protobuf representation of Mutations. + * + * @internal + * @access private + * @return array returns array of protobuf representation of Mutations. + */ + public function toProto() + { + return $this->mutations; + } +} diff --git a/Bigtable/src/RowMutation.php b/Bigtable/src/RowMutation.php index 56b6bee4bee5..07a9f6ae5466 100644 --- a/Bigtable/src/RowMutation.php +++ b/Bigtable/src/RowMutation.php @@ -17,13 +17,8 @@ namespace Google\Cloud\Bigtable; +use Google\Cloud\Bigtable\Mutations; use Google\Cloud\Bigtable\V2\MutateRowsRequest\Entry; -use Google\Cloud\Bigtable\V2\Mutation; -use Google\Cloud\Bigtable\V2\Mutation\DeleteFromColumn; -use Google\Cloud\Bigtable\V2\Mutation\DeleteFromFamily; -use Google\Cloud\Bigtable\V2\Mutation\DeleteFromRow; -use Google\Cloud\Bigtable\V2\Mutation\SetCell; -use Google\Cloud\Bigtable\V2\TimestampRange; /** * Represents a RowMutation to perform data operation on Bigtable table. @@ -31,20 +26,20 @@ */ class RowMutation { - /** * @var string */ private $rowKey; /** - * @var array Mutation + * @var Mutations */ - private $mutations = []; + private $mutations; public function __construct($rowKey, array $options = []) { $this->rowKey = $rowKey; + $this->mutations = new Mutations($options); } /** @@ -69,23 +64,7 @@ public function getRowKey() */ public function upsert($family, $qualifier, $value, $timeStamp = null) { - $mutation = new Mutation; - $mutationSetCell = new SetCell; - $mutationSetCell->setFamilyName($family) - ->setColumnQualifier($qualifier) - ->setValue($value); - if ($timeStamp === null) { - $mutationSetCell->setTimestampMicros( - // gives milli second - round(microtime(true) * 1000) - // multiply by 1000 to get micro - * 1000 - ); - } else { - $mutationSetCell->setTimestampMicros($timeStamp); - } - $mutation->setSetCell($mutationSetCell); - $this->mutations[] = $mutation; + $this->mutations->upsert($family, $qualifier, $value, $timeStamp); return $this; } @@ -98,11 +77,7 @@ public function upsert($family, $qualifier, $value, $timeStamp = null) */ public function deleteFromFamily($family) { - $mutation = new Mutation; - $deleteFromFamily = new DeleteFromFamily; - $deleteFromFamily->setFamilyName($family); - $mutation->setDeleteFromFamily($deleteFromFamily); - $this->mutations[] = $mutation; + $this->mutations->deleteFromFamily($family); return $this; } @@ -117,17 +92,7 @@ public function deleteFromFamily($family) */ public function deleteFromColumn($family, $qualifier, array $timeRange = []) { - $mutation = new Mutation; - $deleteFromColumn = new DeleteFromColumn; - $deleteFromColumn->setFamilyName($family)->setColumnQualifier($qualifier); - if (!empty($timeRange)) { - $timestampRange = new TimestampRange; - $timestampRange->setStartTimestampMicros($timeRange['start']); - $timestampRange->setEndTimestampMicros($timeRange['end']); - $deleteFromColumn->setTimeRange($timestampRange); - } - $mutation->setDeleteFromColumn($deleteFromColumn); - $this->mutations[] = $mutation; + $this->mutations->deleteFromColumn($family, $qualifier, $timeRange); return $this; } @@ -138,9 +103,7 @@ public function deleteFromColumn($family, $qualifier, array $timeRange = []) */ public function deleteRow() { - $mutation = new Mutation; - $mutation->setDeleteFromRow(new DeleteFromRow); - $this->mutations[] = $mutation; + $this->mutations->deleteRow(); return $this; } @@ -149,11 +112,11 @@ public function deleteRow() * * @return Entry Entry for Row. */ - public function getEntry() + public function toProto() { $mutateRowsRequestEntry = new Entry; $mutateRowsRequestEntry->setRowKey($this->rowKey); - $mutateRowsRequestEntry->setMutations($this->mutations); + $mutateRowsRequestEntry->setMutations($this->mutations->toProto()); return $mutateRowsRequestEntry; } } diff --git a/Bigtable/tests/Snippet/DataClientTest.php b/Bigtable/tests/Snippet/DataClientTest.php index cd96cd469ac0..21f4babe83e5 100644 --- a/Bigtable/tests/Snippet/DataClientTest.php +++ b/Bigtable/tests/Snippet/DataClientTest.php @@ -21,8 +21,11 @@ use Google\Cloud\Bigtable\DataClient; use Google\Cloud\Bigtable\DataUtil; use Google\Cloud\Bigtable\ReadModifyWriteRowRules; +use Google\Cloud\Bigtable\Filter; +use Google\Cloud\Bigtable\Mutations; use Google\Cloud\Bigtable\V2\BigtableClient as TableClient; use Google\Cloud\Bigtable\V2\Cell; +use Google\Cloud\Bigtable\V2\CheckAndMutateRowResponse; use Google\Cloud\Bigtable\V2\Column; use Google\Cloud\Bigtable\V2\Family; use Google\Cloud\Bigtable\V2\MutateRowsRequest\Entry as MutateRowsRequest_Entry; @@ -380,6 +383,33 @@ public function testSampleRowKeys() ); } + public function testCheckAndMutateRow() + { + $this->bigtableClient->checkAndMutateRow(self::TABLE_NAME, 'rk1', Argument::any()) + ->shouldBeCalled() + ->willReturn((new CheckAndMutateRowResponse)->setPredicateMatched(true)); + $snippet = $this->snippetFromMethod(DataClient::class, 'checkAndMutateRow'); + $snippet->addLocal('dataClient', $this->dataClient); + $res = $snippet->invoke('result'); + $this->assertTrue($res->returnVal()); + } + + public function testCheckAndMutateRowWithFilter() + { + $this->bigtableClient + ->checkAndMutateRow( + self::TABLE_NAME, + 'rk1', + Argument::any() + ) + ->shouldBeCalled() + ->willReturn((new CheckAndMutateRowResponse)->setPredicateMatched(true)); + $snippet = $this->snippetFromMethod(DataClient::class, 'checkAndMutateRow', 1); + $snippet->addLocal('dataClient', $this->dataClient); + $res = $snippet->invoke('result'); + $this->assertTrue($res->returnVal()); + } + private function setUpReadRowsResponse() { $readRowsResponse = new ReadRowsResponse; diff --git a/Bigtable/tests/System/DataClientCheckAndMutateRowTest.php b/Bigtable/tests/System/DataClientCheckAndMutateRowTest.php new file mode 100644 index 000000000000..fe4e0a3ef797 --- /dev/null +++ b/Bigtable/tests/System/DataClientCheckAndMutateRowTest.php @@ -0,0 +1,139 @@ + [ + 'cf1' => [ + 'cq1' => [ + 'value' => 'value1', + 'timeStamp' => 5000 + ] + ] + ], + ]; + self::$dataClient->upsert($insertRows); + } + + public function testCheckAndMutateRowWithEmptyFilter() + { + $mutations = (new Mutations)->upsert('cf1', 'cq1', 'value11', 6000); + $result = self::$dataClient->checkAndMutateRow( + 'rk1', + ['trueMutations' => $mutations] + ); + $this->assertTrue($result); + $row = self::$dataClient->readRow('rk1'); + $expectedRow = [ + 'cf1' => [ + 'cq1' => [ + [ + 'value' => 'value11', + 'timeStamp' => 6000, + 'labels' => '' + ], + [ + 'value' => 'value1', + 'timeStamp' => 5000, + 'labels' => '' + ] + ] + ] + ]; + $this->assertEquals($expectedRow, $row); + } + + public function testCheckAndMutateRowWithFilterYieldingCell() + { + $predicateFilter = Filter::family()->exactMatch('cf1'); + $mutations = (new Mutations)->upsert('cf1', 'cq1', 'value11', 6000); + $result = self::$dataClient->checkAndMutateRow( + 'rk1', + [ + 'predicateFilter' => $predicateFilter, + 'trueMutations' => $mutations + ] + ); + $this->assertTrue($result); + $row = self::$dataClient->readRow('rk1'); + $expectedRow = [ + 'cf1' => [ + 'cq1' => [ + [ + 'value' => 'value11', + 'timeStamp' => 6000, + 'labels' => '' + ], + [ + 'value' => 'value1', + 'timeStamp' => 5000, + 'labels' => '' + ] + ] + ] + ]; + $this->assertEquals($expectedRow, $row); + } + + public function testCheckAndMutateRowWithFilterNotYieldingCell() + { + $predicateFilter = Filter::qualifier()->exactMatch('cq10'); + $trueMutations = (new Mutations)->upsert('cf1', 'cq1', 'value12', 6000); + $falseMutations = (new Mutations)->upsert('cf1', 'cq1', 'value11', 6000); + $result = self::$dataClient->checkAndMutateRow( + 'rk1', + [ + 'predicateFilter' => $predicateFilter, + 'trueMutations' => $trueMutations, + 'falseMutations' => $falseMutations + ] + ); + $this->assertFalse($result); + $row = self::$dataClient->readRow('rk1'); + $expectedRow = [ + 'cf1' => [ + 'cq1' => [ + [ + 'value' => 'value11', + 'timeStamp' => 6000, + 'labels' => '' + ], + [ + 'value' => 'value1', + 'timeStamp' => 5000, + 'labels' => '' + ] + ] + ] + ]; + $this->assertEquals($expectedRow, $row); + } +} diff --git a/Bigtable/tests/Unit/DataClientTest.php b/Bigtable/tests/Unit/DataClientTest.php index 7f09c7eaa560..83797bd1f336 100644 --- a/Bigtable/tests/Unit/DataClientTest.php +++ b/Bigtable/tests/Unit/DataClientTest.php @@ -22,10 +22,13 @@ use Google\Cloud\Bigtable\ChunkFormatter; use Google\Cloud\Bigtable\DataClient; use Google\Cloud\Bigtable\Exception\BigtableDataOperationException; +use Google\Cloud\Bigtable\Filter; +use Google\Cloud\Bigtable\Mutations; use Google\Cloud\Bigtable\ReadModifyWriteRowRules; use Google\Cloud\Bigtable\RowMutation; use Google\Cloud\Bigtable\V2\BigtableClient as TableClient; use Google\Cloud\Bigtable\V2\Cell; +use Google\Cloud\Bigtable\V2\CheckAndMutateRowResponse; use Google\Cloud\Bigtable\V2\Column; use Google\Cloud\Bigtable\V2\Family; use Google\Cloud\Bigtable\V2\MutateRowsResponse; @@ -35,7 +38,6 @@ use Google\Cloud\Bigtable\V2\RowRange; use Google\Cloud\Bigtable\V2\RowSet; use Google\Cloud\Bigtable\V2\SampleRowKeysResponse; -use Google\Cloud\Bigtable\Filter; use Google\Rpc\Code; use Google\Rpc\Status; use PHPUnit\Framework\TestCase; @@ -77,12 +79,12 @@ public function setUp() $this->dataClient = new DataClient(self::INSTANCE_ID, self::TABLE_ID, $clientOptions); $rowMutation = new RowMutation('rk1'); $rowMutation->upsert('cf1', 'cq1', 'value1', self::TIMESTAMP); - $this->entries[] = $rowMutation->getEntry(); + $this->entries[] = $rowMutation->toProto(); $this->rowMutations[] = $rowMutation; $rowMutation = new RowMutation('rk2'); $rowMutation->upsert('cf2', 'cq2', 'value2', self::TIMESTAMP); - $this->entries[] = $rowMutation->getEntry(); + $this->entries[] = $rowMutation->toProto(); $this->rowMutations[] = $rowMutation; } @@ -672,6 +674,98 @@ public function testSampleRowKeys() $this->assertEquals($expectedRowKeys, $rowKeys); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage checkAndMutateRow must have either trueMutations or falseMutations. + */ + public function testCheckAndMutateRowShouldThrowWhenNoTrueOrFalseMutations() + { + $this->dataClient->checkAndMutateRow('rk1'); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage FilterInterface + */ + public function testCheckAndMutateRowShouldThrowWhenPredicateFilterIsNotFilter() + { + $this->dataClient->checkAndMutateRow('rk1', ['predicateFilter' => new \stdClass()]); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Mutations + */ + public function testCheckAndMutateRowShouldThrowWhenTrueMutationsNotMutations() + { + $this->dataClient->checkAndMutateRow('rk1', ['trueMutations' => new \stdClass()]); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Mutations + */ + public function testCheckAndMutateRowShouldThrowWhenFalseMutationsNotMutations() + { + $this->dataClient->checkAndMutateRow('rk1', ['falseMutations' => new \stdClass()]); + } + + public function testCheckAndMutateRowWithTrueMutations() + { + $mutations = (new Mutations)->upsert('family', 'qualifier', 'value'); + $expectedArgs = $this->options + [ + 'trueMutations' => $mutations->toProto() + ]; + $rowKey = 'rk1'; + $this->bigtableClient->checkAndMutateRow(self::TABLE_NAME, $rowKey, $expectedArgs) + ->shouldBeCalled() + ->willReturn( + (new CheckAndMutateRowResponse)->setPredicateMatched(true) + ); + $result = $this->dataClient->checkAndMutateRow($rowKey, ['trueMutations' => $mutations]); + $this->assertTrue($result); + } + + public function testCheckAndMutateRowWithFalseMutations() + { + $mutations = (new Mutations)->upsert('family', 'qualifier', 'value'); + $expectedArgs = $this->options + [ + 'falseMutations' => $mutations->toProto() + ]; + $rowKey = 'rk1'; + $this->bigtableClient->checkAndMutateRow(self::TABLE_NAME, $rowKey, $expectedArgs) + ->shouldBeCalled() + ->willReturn( + (new CheckAndMutateRowResponse)->setPredicateMatched(false) + ); + $result = $this->dataClient->checkAndMutateRow($rowKey, ['falseMutations' => $mutations]); + $this->assertFalse($result); + } + + public function testCheckAndMutateRowWithPredicateFilter() + { + $mutations = (new Mutations)->upsert('family', 'qualifier', 'value'); + $predicateFilter = Filter::family()->exactMatch('cf1'); + $expectedArgs = $this->options + [ + 'predicateFilter' => $predicateFilter->toProto(), + 'trueMutations' => $mutations->toProto() + ]; + $rowKey = 'rk1'; + $this->bigtableClient->checkAndMutateRow(self::TABLE_NAME, $rowKey, $expectedArgs) + ->shouldBeCalled() + ->willReturn( + (new CheckAndMutateRowResponse)->setPredicateMatched(false) + ); + $result = $this->dataClient->checkAndMutateRow( + $rowKey, + [ + 'predicateFilter' => $predicateFilter, + 'trueMutations' => $mutations + ] + ); + $this->assertFalse($result); + } + private function getMutateRowsResponse(array $status) { $mutateRowsResponses = []; diff --git a/Bigtable/tests/Unit/MutationsTest.php b/Bigtable/tests/Unit/MutationsTest.php new file mode 100644 index 000000000000..8211527b4edc --- /dev/null +++ b/Bigtable/tests/Unit/MutationsTest.php @@ -0,0 +1,155 @@ +mutations = new MutationsStub; + } + + public function testUpsert() + { + $return = $this->mutations->upsert( + self::COLUMN_FAMILY, + self::COLUMN_QUALIFIER, + self::VALUE + ); + $proto = $this->mutations->toProto(); + $expectedProto[] = (new Mutation) + ->setSetCell( + (new SetCell) + ->setFamilyName(self::COLUMN_FAMILY) + ->setColumnQualifier(self::COLUMN_QUALIFIER) + ->setValue(self::VALUE) + ->setTimestampMicros(315532800000000) + ); + $this->assertEquals($this->mutations, $return); + $this->assertEquals($expectedProto, $proto); + } + + public function testUpsertWithTimeRange() + { + $return = $this->mutations->upsert( + self::COLUMN_FAMILY, + self::COLUMN_QUALIFIER, + self::VALUE, + 1534183334215000 + ); + $proto = $this->mutations->toProto(); + $expectedProto[] = (new Mutation) + ->setSetCell( + (new SetCell) + ->setFamilyName(self::COLUMN_FAMILY) + ->setColumnQualifier(self::COLUMN_QUALIFIER) + ->setValue(self::VALUE) + ->setTimestampMicros(1534183334215000) + ); + $this->assertEquals($this->mutations, $return); + $this->assertEquals($expectedProto, $proto); + } + + public function testDeleteFromFamily() + { + $return = $this->mutations->deleteFromFamily(self::COLUMN_FAMILY); + $proto = $this->mutations->toProto(); + $expectedProto[] = (new Mutation) + ->setDeleteFromFamily( + (new DeleteFromFamily)->setFamilyName(self::COLUMN_FAMILY) + ); + $this->assertEquals($this->mutations, $return); + $this->assertEquals($expectedProto, $proto); + } + + public function testDeleteFromColumn() + { + $return = $this->mutations->deleteFromColumn(self::COLUMN_FAMILY, self::COLUMN_QUALIFIER); + $proto = $this->mutations->toProto(); + $expectedProto[] = (new Mutation) + ->setDeleteFromColumn( + (new DeleteFromColumn) + ->setFamilyName(self::COLUMN_FAMILY) + ->setColumnQualifier(self::COLUMN_QUALIFIER) + ); + $this->assertEquals($this->mutations, $return); + $this->assertEquals($expectedProto, $proto); + } + + public function testDeleteFromColumnWithTimeRange() + { + $return = $this->mutations->deleteFromColumn( + self::COLUMN_FAMILY, + self::COLUMN_QUALIFIER, + ['start' => 1, 'end' => 5] + ); + $proto = $this->mutations->toProto(); + $expectedProto[] = (new Mutation) + ->setDeleteFromColumn( + (new DeleteFromColumn) + ->setFamilyName(self::COLUMN_FAMILY) + ->setColumnQualifier(self::COLUMN_QUALIFIER) + ->setTimeRange( + (new TimestampRange) + ->setStartTimestampMicros(1) + ->setEndTimestampMicros(5) + ) + ); + $this->assertEquals($this->mutations, $return); + $this->assertEquals($expectedProto, $proto); + } + + public function testDeleteRow() + { + $return = $this->mutations->deleteRow(); + $proto = $this->mutations->toProto(); + $expectedProto[] = (new Mutation)->setDeleteFromRow(new DeleteFromRow); + $this->assertEquals($this->mutations, $return); + $this->assertEquals($expectedProto, $proto); + } +} + +//@codingStandardsIgnoreStart +class MutationsStub extends Mutations +{ + protected function microtime() + { + return MutationsTest::TIMESTAMP_VALUE; + } +} +//@codingStandardsIgnoreEnd diff --git a/Bigtable/tests/Unit/RowMutationTest.php b/Bigtable/tests/Unit/RowMutationTest.php index e06e5c2764a2..f12a472ee727 100644 --- a/Bigtable/tests/Unit/RowMutationTest.php +++ b/Bigtable/tests/Unit/RowMutationTest.php @@ -59,7 +59,7 @@ public function testUpsertWithTimeRange() 1534183334215000 ); - $entry = $this->rowMutation->getEntry(); + $entry = $this->rowMutation->toProto(); $mutationSetCell = new SetCell; $mutationSetCell->setFamilyName(self::COLUMN_FAMILY) ->setColumnQualifier(self::COLUMN_QUALIFIER) @@ -75,7 +75,7 @@ public function testUpsertWithTimeRange() public function testDeleteFromFamily() { $return = $this->rowMutation->deleteFromFamily(self::COLUMN_FAMILY); - $entry = $this->rowMutation->getEntry(); + $entry = $this->rowMutation->toProto(); $deleteFromFamily = new DeleteFromFamily; $deleteFromFamily->setFamilyName(self::COLUMN_FAMILY); $mutation = new Mutation; @@ -88,7 +88,7 @@ public function testDeleteFromFamily() public function testDeleteFromColumn() { $return = $this->rowMutation->deleteFromColumn(self::COLUMN_FAMILY, self::COLUMN_QUALIFIER); - $entry = $this->rowMutation->getEntry(); + $entry = $this->rowMutation->toProto(); $deleteFromColumn = new DeleteFromColumn; $deleteFromColumn->setFamilyName(self::COLUMN_FAMILY)->setColumnQualifier(self::COLUMN_QUALIFIER); $mutation = new Mutation; @@ -105,7 +105,7 @@ public function testDeleteFromColumnWithTimeRange() self::COLUMN_QUALIFIER, ['start' => 1, 'end' => 5] ); - $entry = $this->rowMutation->getEntry(); + $entry = $this->rowMutation->toProto(); $deleteFromColumn = new DeleteFromColumn; $deleteFromColumn->setFamilyName(self::COLUMN_FAMILY) ->setColumnQualifier(self::COLUMN_QUALIFIER); @@ -123,7 +123,7 @@ public function testDeleteFromColumnWithTimeRange() public function testDeleteRow() { $return = $this->rowMutation->deleteRow(); - $entry = $this->rowMutation->getEntry(); + $entry = $this->rowMutation->toProto(); $mutation = new Mutation; $mutation->setDeleteFromRow(new DeleteFromRow); $mutateRowsRequestEntry = $this->getMutateRowsRequestEntry($mutation);