Skip to content

Commit

Permalink
chore: fix BC breaking paged list method (#5096)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Feb 7, 2022
1 parent a2287b6 commit e6d84de
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Firestore/owlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,15 @@
rf'{lro_method}\(',
rf'{lro_method}LRO(',
)
# fix backwards-compatibility issues with paginated methods
pagnated_method = 'partitionQuery'
s.replace(
"src/V1/Gapic/FirestoreGapicClient.php",
rf'{paginated_method}\(',
rf'{paginated_method}Paginated(',
)
s.replace(
"tests/Unit/V1/FirestoreClientTest.php",
rf'{paginated_method}\(',
rf'{paginated_method}Paginated(',
)
10 changes: 10 additions & 0 deletions Firestore/src/V1/FirestoreClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
*/
class FirestoreClient extends FirestoreGapicClient
{
/**
* @see FirestoreClient::partitionQueryPaginated
* @return \Google\Cloud\Firestore\V1\PartitionQueryResponse
* @deprecated use partitionQueryPaginated instead
*/
// public function partitionQuery(array $optionalArgs = [])
// {
// return $this->partitionQueryPaginated($optionalArgs)->getPage()->getResponseObject();
// }

/**
* Formats a string containing the fully-qualified path to represent
* a any_path resource.
Expand Down
133 changes: 133 additions & 0 deletions Firestore/tests/Unit/V1/FirestoreClientBCTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
* GENERATED CODE WARNING
* This file was automatically generated - do not edit!
*/

namespace Google\Cloud\Firestore\Tests\Unit\V1;

use Google\ApiCore\ApiException;

use Google\ApiCore\CredentialsWrapper;

use Google\ApiCore\Testing\GeneratedTest;
use Google\ApiCore\Testing\MockTransport;

use Google\Cloud\Firestore\V1\FirestoreClient;
use Google\Cloud\Firestore\V1\PartitionQueryResponse;
use Google\Rpc\Code;
use stdClass;

/**
* @group firestore
*
* @group gapic
*/
class FirestoreClientBCTest extends GeneratedTest
{
/**
* @return TransportInterface
*/
private function createTransport($deserialize = null)
{
return new MockTransport($deserialize);
}

/**
* @return CredentialsWrapper
*/
private function createCredentials()
{
return $this->getMockBuilder(CredentialsWrapper::class)->disableOriginalConstructor()->getMock();
}

/**
* @return FirestoreClient
*/
private function createClient(array $options = [])
{
$options += [
'credentials' => $this->createCredentials(),
];
return new FirestoreClient($options);
}

/**
* @test
*/
public function partitionQueryTest()
{
$transport = $this->createTransport();
$client = $this->createClient(['transport' => $transport]);

$this->assertTrue($transport->isExhausted());

// Mock response
$nextPageToken = 'nextPageToken-1530815211';
$expectedResponse = new PartitionQueryResponse();
$expectedResponse->setNextPageToken($nextPageToken);
$transport->addResponse($expectedResponse);

$response = $client->partitionQuery();
$this->assertEquals($expectedResponse, $response);
$actualRequests = $transport->popReceivedCalls();
$this->assertSame(1, count($actualRequests));
$actualFuncCall = $actualRequests[0]->getFuncCall();
$actualRequestObject = $actualRequests[0]->getRequestObject();
$this->assertSame('/google.firestore.v1.Firestore/PartitionQuery', $actualFuncCall);

$this->assertTrue($transport->isExhausted());
}

/**
* @test
*/
public function partitionQueryExceptionTest()
{
$transport = $this->createTransport();
$client = $this->createClient(['transport' => $transport]);

$this->assertTrue($transport->isExhausted());

$status = new stdClass();
$status->code = Code::DATA_LOSS;
$status->details = 'internal error';

$expectedExceptionMessage = json_encode([
'message' => 'internal error',
'code' => Code::DATA_LOSS,
'status' => 'DATA_LOSS',
'details' => [],
], JSON_PRETTY_PRINT);
$transport->addResponse(null, $status);

try {
$client->partitionQuery();
// If the $client method call did not throw, fail the test
$this->fail('Expected an ApiException, but no exception was thrown.');
} catch (ApiException $ex) {
$this->assertEquals($status->code, $ex->getCode());
$this->assertEquals($expectedExceptionMessage, $ex->getMessage());
}

// Call popReceivedCalls to ensure the stub is exhausted
$transport->popReceivedCalls();
$this->assertTrue($transport->isExhausted());
}
}

0 comments on commit e6d84de

Please sign in to comment.