Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new "quickstart" samples. #174

Merged
merged 22 commits into from
Oct 10, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
331c184
Add Pub/Sub quickstart sample.
jmdobry Sep 27, 2016
78c821b
adds tests for quickstarts, adds logging quickstart, and fixes review…
bshaffer Sep 30, 2016
16982ac
updates logging quickstart to log entry
bshaffer Sep 30, 2016
8196545
Merge branch 'master' into quickstarts
bshaffer Oct 4, 2016
0158ed9
moves logging to api subdirectory
bshaffer Oct 4, 2016
09e2cb1
moves dataset->delete to teardown
bshaffer Oct 4, 2016
9340a8b
adds composer autoloading to quickstarts
bshaffer Oct 4, 2016
ac14b65
updates datastore quickstart to use query instead of entity
bshaffer Oct 4, 2016
27e54e5
updates datastore sample to use lookup
bshaffer Oct 5, 2016
fd64f8b
removes project ID from logging, skips tests if ADC is not set
bshaffer Oct 5, 2016
124566c
moves topic deletion to teardown
bshaffer Oct 5, 2016
59f9085
fixes cs
bshaffer Oct 5, 2016
21eaaa6
adds project ID and tests to pubsub sample
bshaffer Oct 5, 2016
d187637
changes datastore entity back to Task
bshaffer Oct 5, 2016
8850d29
Added missing print statements. Added Language, Speech, and Vision qu…
jmdobry Oct 6, 2016
dc00f27
Address comments.
jmdobry Oct 6, 2016
b7bda05
Change datastore quickstart sample to create a task.
jmdobry Oct 6, 2016
a772cd1
Add missing print statment.
jmdobry Oct 7, 2016
ff6a9c4
adds tests for language/speech/vision quickstarts
bshaffer Oct 7, 2016
eda129e
updates all quickstarts to use google-cloud 0.10
bshaffer Oct 7, 2016
4a2c81f
adds projectId back to bigquery, storage, logging, and datastore quic…
bshaffer Oct 8, 2016
bc4e224
Merge branch 'master' into quickstarts
bshaffer Oct 10, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions bigquery/quickstart/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2016 Google Inc.

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

http://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.
-->
<phpunit bootstrap="test/bootstrap.php">
<testsuites>
<testsuite name="Google BigQuery Quickstart Tests">
<directory>test</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<filter>
<whitelist>
<file>quickstart.php</file>
</whitelist>
</filter>
</phpunit>
10 changes: 3 additions & 7 deletions bigquery/quickstart/quickstart.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@
# Imports the Google Cloud client library
use Google\Cloud\BigQuery\BigQueryClient;

# Your Google Cloud Platform project ID
$projectId = 'YOUR_PROJECT_ID';

# Instantiates a client
$bigqueryClient = new BigQueryClient([
'projectId' => $projectId
]);
$bigquery = new BigQueryClient();

# The name for the new dataset
$datasetName = 'my_new_dataset';

# Creates the new dataset
$dataset = $bigqueryClient->createDataset($datasetName);
$dataset = $bigquery->createDataset($datasetName);
# [END bigquery_quickstart]
return $dataset;
3 changes: 3 additions & 0 deletions bigquery/quickstart/test/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';
39 changes: 39 additions & 0 deletions bigquery/quickstart/test/quickstartTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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
*
* http://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.
*/
class quickstartTest extends PHPUnit_Framework_TestCase
{
public function testQuickstart()
{
$datasetId = 'my_new_dataset_' . time();
$file = sys_get_temp_dir() . '/quickstart_quickstart.php';
$contents = file_get_contents(__DIR__ . '/../quickstart.php');
$contents = str_replace(
['my_new_dataset', '__DIR__'],
[$datasetId, sprintf('"%s/.."', __DIR__)],
$contents
);
file_put_contents($file, $contents);

// Invoke quickstart.php
$dataset = include $file;

// Make sure it looks correct
$this->assertInstanceOf('Google\Cloud\BigQuery\Dataset', $dataset);
$this->assertEquals($datasetId, $dataset->id());
$dataset->delete();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it more appropriate to call delete() in tearDown or something?

}
}
31 changes: 31 additions & 0 deletions datastore/quickstart/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2016 Google Inc.

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

http://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.
-->
<phpunit bootstrap="test/bootstrap.php">
<testsuites>
<testsuite name="Google Datastore Quickstart Tests">
<directory>test</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<filter>
<whitelist>
<file>quickstart.php</file>
</whitelist>
</filter>
</phpunit>
12 changes: 4 additions & 8 deletions datastore/quickstart/quickstart.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@
# Imports the Google Cloud client library
use Google\Cloud\Datastore\DatastoreClient;

# Your Google Cloud Platform project ID
$projectId = 'YOUR_PROJECT_ID';

# Instantiates a client
$datastoreClient = new DatastoreClient([
'projectId' => $projectId
]);
$datastore = new DatastoreClient();

# The kind of the entity to retrieve
$kind = 'Task';
# The id of the entity to retrieve
$id = 1234567890;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit controversial to manually assign the numeric id (since you need to allocateId to avoid the collision with auto assigned id).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we call "lookup" instead of "upsert" here, and maybe use a name instead of ID, would that be more appropriate?

$kind = 'Person';
$name = 'Bob';
$key = $datastore->key($kind, $name);
$entity = $datastore->lookup($key);

# The Datastore key for the entity
$taskKey = $datastoreClient->key($kind, $id);
$taskKey = $datastore->key($kind, $id);

# Retrieves the entity
$dataset = $datastoreClient->lookup($taskKey);
$entity = $datastore->entity($taskKey);
Copy link
Contributor

@tmatsuo tmatsuo Sep 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't retrieve the entity, it just creates a local Entity object. No API calls are made.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right. What do you think we should do for this test?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd create the key with named id and upsert the entity.
But I think we should match with the other language samples.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other language samples do what this sample does. But I agree this is not a good sample. @jmdobry thoughts on changing this to an upsert?

# [END datastore_quickstart]
return $entity;
3 changes: 3 additions & 0 deletions datastore/quickstart/test/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';
41 changes: 41 additions & 0 deletions datastore/quickstart/test/quickstartTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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
*
* http://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.
*/
class quickstartTest extends PHPUnit_Framework_TestCase
{
public function testQuickstart()
{
$file = sys_get_temp_dir() . '/datastore_quickstart.php';
$contents = file_get_contents(__DIR__ . '/../quickstart.php');
$contents = str_replace(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to replace anything

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__DIR__ needs replaced for the autoloading to work

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the __DIR__ is the only thing you need to replace, you don't need to copy the file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh, good point

['__DIR__'],
[sprintf('"%s/.."', __DIR__)],
$contents
);
file_put_contents($file, $contents);

// Invoke quickstart.php
$entity = include $file;

// Make sure it looks correct
$this->assertInstanceOf('Google\Cloud\Datastore\Entity', $entity);

$key = $entity->key();
$path = $key->path();
$this->assertEquals('Task', $path[0]['kind']);
$this->assertEquals(1234567890, $path[0]['id']);
}
}
6 changes: 6 additions & 0 deletions logging/quickstart/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": {
"php": ">=5.4",
"google/cloud": "0.9"
}
}
Loading