8000 Added sample for using Cloud Spanner w/ Cloud Functions (#1681) · cevaris/python-docs-samples@c6f0f38 · GitHub
[go: up one dir, main page]

Skip to content

Commit c6f0f38

Browse files
author
chenyumic
authored
Added sample for using Cloud Spanner w/ Cloud Functions (GoogleCloudPlatform#1681)
1 parent a34f991 commit c6f0f38

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

functions/spanner/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
2+
3+
# Google Cloud Functions - Cloud Spanner sample
4+
5+
See:
6+
7+
* [Cloud Functions Cloud Spanner tutorial][tutorial]
8+
* [Cloud Functions Cloud Spanner source code][code]
9+
10+
[tutorial]: https://cloud.google.com/spanner/docs/use-cloud-functions
11+
[code]: main.py

functions/spanner/main.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START spanner_functions_quickstart]
16+
from google.cloud import spanner
17+
18+
instance_id = 'test-instance'
19+
database_id = 'example-db'
20+
21+
client = spanner.Client()
22+
23+
24+
def spanner_read_data(request):
25+
instance = client.instance(instance_id)
26+
database = instance.database(database_id)
27+
28+
query = 'SELECT * FROM Albums'
29+
30+
outputs = []
31+
with database.snapshot() as snapshot:
32+
results = snapshot.execute_sql(query)
33+
34+
for row in results:
35+
output = 'SingerId: {}, AlbumId: {}, AlbumTitle: {}'.format(*row)
36+
outputs.append(output)
37+
38+
return '\n'.join(outputs)
39+
# [END spanner_functions_quickstart]

functions/spanner/main_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from unittest.mock import MagicMock
16+
17+
from google.cloud import spanner
18+
19+
spanner.Client = MagicMock()
20+
21+
22+
def test_main():
23+
import main
24+
snapshot_mock = main.client.instance().database().snapshot().__enter__()
25+
snapshot_mock.execute_sql.return_value = [('SingerID', 'AlbumID', 'Album')]
26+
27+
response = main.spanner_read_data(None)
28+
29+
assert 'SingerID' in response
30+
assert 'AlbumID' in response
31+
assert 'Album' in response

functions/spanner/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-spanner==1.4.0

0 commit comments

Comments
 (0)
0