Skip to content

Commit 82de3f7

Browse files
authored
chore: fix system tests in BigQuery and Language (#8775)
1 parent 53e704b commit 82de3f7

2 files changed

Lines changed: 76 additions & 145 deletions

File tree

BigQuery/tests/System/LoadDataAndQueryTest.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
use Google\Cloud\Core\Exception\BadRequestException;
2626
use Google\ApiCore\ApiException;
2727
use Google\Cloud\Core\ExponentialBackoff;
28-
use Google\Cloud\BigQuery\Reservation\V1\ReservationServiceClient;
28+
use Google\Cloud\BigQuery\Reservation\V1\Client\ReservationServiceClient;
29+
use Google\Cloud\BigQuery\Reservation\V1\CreateReservationRequest;
30+
use Google\Cloud\BigQuery\Reservation\V1\DeleteReservationRequest;
31+
use Google\Cloud\BigQuery\Reservation\V1\GetReservationRequest;
2932
use Google\Cloud\BigQuery\Reservation\V1\Reservation;
3033
use GuzzleHttp\Psr7\Utils;
3134

@@ -986,15 +989,17 @@ public function testLoadJobWithReservationAndTimeout()
986989

987990
// Create reservation if it does not exist
988991
$reservationAdminClient = new ReservationServiceClient();
992+
$getRequest = GetReservationRequest::build($fullReservationPath);
989993
try {
990-
$reservationAdminClient->getReservation($fullReservationPath);
994+
$reservationAdminClient->getReservation($getRequest);
991995
} catch (ApiException $e) {
992996
if ($e->getStatus() === 'NOT_FOUND') {
993-
$reservationAdminClient->createReservation(
994-
'projects/' . $projectId . '/locations/US',
995-
['reservation' => new Reservation(['slot_capacity' => 50, 'ignore_idle_slots' => true]),
996-
'reservationId' => $reservationId]
997-
);
997+
$createRequest = new CreateReservationRequest([
998+
'parent' => 'projects/' . $projectId . '/locations/US',
999+
'reservation' => new Reservation(['slot_capacity' => 50, 'ignore_idle_slots' => true]),
1000+
'reservation_id' => $reservationId
1001+
]);
1002+
$reservationAdminClient->createReservation($createRequest);
9981003
}
9991004
}
10001005

@@ -1005,7 +1010,7 @@ public function testLoadJobWithReservationAndTimeout()
10051010
->jobTimeoutMs($timeout);
10061011

10071012
$maxRetry = 3;
1008-
$retry = 1;
1013+
$retry = 3;
10091014
$success = false;
10101015

10111016
do {
@@ -1021,7 +1026,7 @@ public function testLoadJobWithReservationAndTimeout()
10211026

10221027
// Cancel job
10231028
$job->cancel();
1024-
1029+
10251030
// Exit retry loop
10261031
$success = true;
10271032
break;
@@ -1038,11 +1043,12 @@ public function testLoadJobWithReservationAndTimeout()
10381043
}
10391044
} while ($retry <= $maxRetry);
10401045

1041-
// Clean up - delete reservation
1042-
$reservationAdminClient->deleteReservation($fullReservationPath);
1043-
10441046
// Assert that load job with reservation could be started and verified
10451047
$this->assertTrue($success, 'Load job with reservation could not be started after maximum retries.');
1048+
1049+
// Clean up - delete reservation
1050+
$deleteRequest = DeleteReservationRequest::build($fullReservationPath);
1051+
$reservationAdminClient->deleteReservation($deleteRequest);
10461052
}
10471053

10481054
public function invalidJsonProvider()

Language/tests/System/AnalyzeTest.php

Lines changed: 58 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,50 @@
1717

1818
namespace Google\Cloud\Language\Tests\System;
1919

20+
use Google\Cloud\Language\V2\AnalyzeEntitiesRequest;
21+
use Google\Cloud\Language\V2\AnalyzeSentimentRequest;
22+
use Google\Cloud\Language\V2\ClassifyTextRequest;
23+
use Google\Cloud\Language\V2\Document;
24+
use Google\Cloud\Language\V2\EncodingType;
25+
use Google\Cloud\Language\V2\Entity;
26+
2027
/**
2128
* @group language
2229
*/
2330
class AnalyzeTest extends LanguageTestCase
2431
{
25-
/**
26-
* @dataProvider analyzeSyntaxProvider
27-
*/
28-
public function testAnalyzeSyntax($text, $expectedValues)
29-
{
30-
$result = self::$client->analyzeSyntax($text);
31-
$info = $result->info();
32-
33-
foreach ($expectedValues as $key => $expected) {
34-
$this->assertEquals($expected, $info[$key]);
35-
}
36-
}
37-
38-
public function analyzeSyntaxProvider()
39-
{
40-
return [
41-
[
42-
'Do you know the way to San Jose?',
43-
[
44-
'sentences' => [
45-
[
46-
'text' => [
47-
'content' => 'Do you know the way to San Jose?',
48-
'beginOffset' => 0,
49-
]
50-
]
51-
],
52-
'entities' => [],
53-
'language' => 'en'
54-
]
55-
]
56-
];
57-
}
58-
5932
/**
6033
* @dataProvider analyzeSentimentProvider
6134
*/
6235
public function testAnalyzeSentiment($text, $expectedValues)
6336
{
64-
$result = self::$client->analyzeSentiment($text);
65-
$info = $result->info();
66-
67-
foreach ($expectedValues as $key => $expected) {
68-
$this->assertEqualsWithDelta($expected, $info[$key], 0.2);
37+
$request = (new AnalyzeSentimentRequest())
38+
->setDocument(new Document([
39+
'content' => $text,
40+
'type' => Document\Type::PLAIN_TEXT
41+
]))
42+
->setEncodingType(EncodingType::UTF8);
43+
$result = self::$client->analyzeSentiment($request);
44+
45+
$this->assertEqualsWithDelta($expectedValues['language'], $result->getLanguageCode(), 0.2);
46+
47+
$sentences = $result->getSentences();
48+
$this->assertCount(count($expectedValues['sentences']), $sentences);
49+
50+
foreach ($expectedValues['sentences'] as $i => $expectedSentence) {
51+
$sentence = $sentences[$i];
52+
$this->assertEquals($expectedSentence['text']['content'], $sentence->getText()->getContent());
53+
$this->assertEquals($expectedSentence['text']['beginOffset'], $sentence->getText()->getBeginOffset());
54+
$this->assertEqualsWithDelta(
55+
$expectedSentence['sentiment']['magnitude'],
56+
$sentence->getSentiment()->getMagnitude(),
57+
0.2
58+
);
59+
$this->assertEqualsWithDelta(
60+
$expectedSentence['sentiment']['score'],
61+
$sentence->getSentiment()->getScore(),
62+
0.2
63+
);
6964
}
7065
}
7166

@@ -98,75 +93,29 @@ public function analyzeSentimentProvider()
9893
*/
9994
public function testAnalyzeEntities($text, $expectedEntities)
10095
{
101-
$result = self::$client->analyzeEntities($text);
102-
$info = $result->info();
96+
$request = (new AnalyzeEntitiesRequest())
97+
->setDocument(new Document([
98+
'content' => $text,
99+
'type' => Document\Type::PLAIN_TEXT,
100+
]));
103101

104-
foreach ($expectedEntities as $expectedEntity) {
105-
$exists = false;
106-
foreach ($info['entities'] as $entity) {
107-
if ($entity['name'] == $expectedEntity['name']) {
108-
$exists = true;
109-
$this->assertEquals($entity['type'], $expectedEntity['type']);
110-
break;
111-
}
112-
}
113-
$this->assertTrue($exists);
114-
}
115-
}
116-
117-
public function analyzeEntitiesProvider()
118-
{
119-
return [
120-
[
121-
'Do you know the way to San Jose?',
122-
[
123-
[
124-
'name' => 'San Jose',
125-
'type' => 'LOCATION',
126-
],
127-
[
128-
'name' => 'way',
129-
'type' => 'OTHER',
130-
],
131-
]
132-
]
133-
];
134-
}
135-
136-
/**
137-
* @dataProvider analyzeEntitySentimentProvider
138-
*/
139-
public function testAnalyzeEntitySentiment($text, $expectedEntities)
140-
{
141-
$result = self::$client->analyzeEntitySentiment($text);
142-
$info = $result->info();
102+
$result = self::$client->analyzeEntities($request);
103+
$entities = $result->getEntities();
143104

144105
foreach ($expectedEntities as $expectedEntity) {
145106
$exists = false;
146-
foreach ($info['entities'] as $entity) {
147-
if ($entity['name'] == $expectedEntity['name']) {
107+
foreach ($entities as $entity) {
108+
if ($entity->getName() == $expectedEntity['name']) {
148109
$exists = true;
149-
$this->assertEquals($entity['type'], $expectedEntity['type']);
150-
$this->assertEqualsWithDelta(
151-
$entity['sentiment']['score'],
152-
$expectedEntity['sentiment']['score'],
153-
0.2
154-
);
155-
156-
$this->assertEqualsWithDelta(
157-
$entity['sentiment']['magnitude'],
158-
$expectedEntity['sentiment']['magnitude'],
159-
0.2
160-
);
161-
110+
$this->assertEquals(Entity\Type::value($expectedEntity['type']), $entity->getType());
162111
break;
163112
}
164113
}
165114
$this->assertTrue($exists);
166115
}
167116
}
168117

169-
public function analyzeEntitySentimentProvider()
118+
public function analyzeEntitiesProvider()
170119
{
171120
return [
172121
[
@@ -175,39 +124,10 @@ public function analyzeEntitySentimentProvider()
175124
[
176125
'name' => 'San Jose',
177126
'type' => 'LOCATION',
178-
'sentiment' => [
179-
'magnitude' => 0,
180-
'score' => 0,
181-
],
182127
],
183128
[
184129
'name' => 'way',
185130
'type' => 'OTHER',
186-
'sentiment' => [
187-
'magnitude' => 0,
188-
'score' => 0,
189-
],
190-
],
191-
]
192-
],
193-
[
194-
"The road to San Jose is great!",
195-
[
196-
[
197-
'name' => 'San Jose',
198-
'type' => 'LOCATION',
199-
'sentiment' => [
200-
'magnitude' => 0.8,
201-
'score' => 0.8,
202-
],
203-
],
204-
[
205-
'name' => 'road',
206-
'type' => 'LOCATION',
207-
'sentiment' => [
208-
'magnitude' => 0.8,
209-
'score' => 0.8,
210-
],
211131
],
212132
]
213133
]
@@ -216,15 +136,20 @@ public function analyzeEntitySentimentProvider()
216136

217137
public function testClassifyText()
218138
{
219-
$result = self::$client->classifyText(
220-
'Rafael Montero Shines in Mets’ Victory Over the Reds.Montero, who ' .
139+
$text = 'Rafael Montero Shines in Mets’ Victory Over the Reds.Montero, who ' .
221140
'was demoted at midseason, took a one-hitter into the ninth inning ' .
222141
'as the Mets continued to dominate Cincinnati with a win at Great ' .
223-
'American Ball Park.'
224-
);
225-
$category = $result->categories()[0];
226-
227-
$this->assertEquals('/Sports/Team Sports/Baseball', $category['name']);
228-
$this->assertGreaterThan(.9, $category['confidence']);
142+
'American Ball Park.';
143+
$request = (new ClassifyTextRequest())
144+
->setDocument(new Document([
145+
'content' => $text,
146+
'type' => Document\Type::PLAIN_TEXT,
147+
]));
148+
$result = self::$client->classifyText($request);
149+
$categories = $result->getCategories();
150+
$category = $categories[0];
151+
152+
$this->assertEquals('/Sports/Team Sports/Baseball', $category->getName());
153+
$this->assertGreaterThan(.9, $category->getConfidence());
229154
}
230155
}

0 commit comments

Comments
 (0)