Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Quickstart tests [(#569)](GoogleCloudPlatform/python-docs-samples#569)
Browse files Browse the repository at this point in the history
* Add tests for quickstarts
* Update secrets
  • Loading branch information
Jon Wayne Parrott authored and danoscarmike committed Jul 31, 2020
1 parent 2d599ed commit 8493bee
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
7 changes: 3 additions & 4 deletions samples/snippets/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ def run_quickstart():
translate_client = translate.Client(api_key)

# The text to translate
text = 'Hello, world!'
text = u'Hello, world!'
# The target language
target = 'ru'

# Translates some text into Russian
translation = translate_client.translate(text, target_language=target)

print('Text: {}'.format(text))
print('Translation: {}'.format(
translation['translatedText'].encode('utf-8')))
print(u'Text: {}'.format(text))
print(u'Translation: {}'.format(translation['translatedText']))
# [END translate_quickstart]


Expand Down
40 changes: 40 additions & 0 deletions samples/snippets/quickstart_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
# Copyright 2016 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.

from google.cloud import translate
import mock
import pytest

import quickstart


@pytest.fixture
def mock_client(cloud_config):
original_client_ctor = translate.Client

def new_client_ctor(api_key):
# Strip api_key argument and replace with our api key.
return original_client_ctor(cloud_config.api_key)

with mock.patch(
'google.cloud.translate.Client',
side_effect=new_client_ctor):
yield


def test_quickstart(mock_client, capsys):
quickstart.run_quickstart()
out, _ = capsys.readouterr()
assert u'Привет мир!' in out

0 comments on commit 8493bee

Please sign in to comment.