Skip to content

Commit

Permalink
Remove cloud config fixture [(googleapis#887)](GoogleCloudPlatform/py…
Browse files Browse the repository at this point in the history
…thon-docs-samples#887)

* Remove cloud config fixture

* Fix client secrets

* Fix bigtable instance
  • Loading branch information
Jon Wayne Parrott authored and dpebot committed Apr 4, 2017
1 parent e3e62a6 commit 9df9890
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 48 deletions.
12 changes: 8 additions & 4 deletions samples/samples/quickstart_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from google.cloud import spanner
import google.cloud.exceptions
import google.cloud.spanner.client
Expand All @@ -20,13 +22,15 @@

import quickstart

SPANNER_INSTANCE = os.environ['SPANNER_INSTANCE']


@pytest.fixture
def patch_instance(cloud_config):
def patch_instance():
original_instance = google.cloud.spanner.client.Client.instance

def new_instance(self, unused_instance_name):
return original_instance(self, cloud_config.spanner_instance)
return original_instance(self, SPANNER_INSTANCE)

instance_patch = mock.patch(
'google.cloud.spanner.client.Client.instance',
Expand All @@ -38,9 +42,9 @@ def new_instance(self, unused_instance_name):


@pytest.fixture
def example_database(cloud_config):
def example_database():
spanner_client = spanner.Client()
instance = spanner_client.instance(cloud_config.spanner_instance)
instance = spanner_client.instance(SPANNER_INSTANCE)
database = instance.database('my-database-id')

if not database.exists():
Expand Down
79 changes: 35 additions & 44 deletions samples/samples/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import random
import string

Expand All @@ -21,144 +22,135 @@

import snippets

SPANNER_INSTANCE = os.environ['SPANNER_INSTANCE']


@pytest.fixture(scope='module')
def spanner_instance(cloud_config):
def spanner_instance():
spanner_client = spanner.Client()
return spanner_client.instance(cloud_config.spanner_instance)
return spanner_client.instance(SPANNER_INSTANCE)


def unique_database_id():
return 'test-db-{}'.format(''.join(random.choice(
string.ascii_lowercase + string.digits) for _ in range(5)))


def test_create_database(cloud_config, spanner_instance):
def test_create_database(spanner_instance):
database_id = unique_database_id()
print(cloud_config.spanner_instance, database_id)
snippets.create_database(
cloud_config.spanner_instance, database_id)
print(SPANNER_INSTANCE, database_id)
snippets.create_database(SPANNER_INSTANCE, database_id)

database = spanner_instance.database(database_id)
database.reload() # Will only succeed if the database exists.
database.drop()


@pytest.fixture(scope='module')
def temporary_database(cloud_config, spanner_instance):
def temporary_database(spanner_instance):
database_id = unique_database_id()
snippets.create_database(cloud_config.spanner_instance, database_id)
snippets.insert_data(
cloud_config.spanner_instance, database_id)
snippets.create_database(SPANNER_INSTANCE, database_id)
snippets.insert_data(SPANNER_INSTANCE, database_id)
database = spanner_instance.database(database_id)
database.reload()
yield database
database.drop()


def test_query_data(cloud_config, temporary_database, capsys):
snippets.query_data(
cloud_config.spanner_instance, temporary_database.database_id)
def test_query_data(temporary_database, capsys):
snippets.query_data(SPANNER_INSTANCE, temporary_database.database_id)

out, _ = capsys.readouterr()

assert 'Total Junk' in out


def test_read_data(cloud_config, temporary_database, capsys):
snippets.read_data(
cloud_config.spanner_instance, temporary_database.database_id)
def test_read_data(temporary_database, capsys):
snippets.read_data(SPANNER_INSTANCE, temporary_database.database_id)

out, _ = capsys.readouterr()

assert 'Total Junk' in out


@pytest.fixture(scope='module')
def temporary_database_with_column(cloud_config, temporary_database):
snippets.add_column(
cloud_config.spanner_instance, temporary_database.database_id)
def temporary_database_with_column(temporary_database):
snippets.add_column(SPANNER_INSTANCE, temporary_database.database_id)
yield temporary_database


def test_update_data(cloud_config, temporary_database_with_column):
def test_update_data(temporary_database_with_column):
snippets.update_data(
cloud_config.spanner_instance,
SPANNER_INSTANCE,
temporary_database_with_column.database_id)


def test_query_data_with_new_column(
cloud_config, temporary_database_with_column, capsys):
def test_query_data_with_new_column(temporary_database_with_column, capsys):
snippets.query_data_with_new_column(
cloud_config.spanner_instance,
SPANNER_INSTANCE,
temporary_database_with_column.database_id)

out, _ = capsys.readouterr()
assert 'MarketingBudget' in out


@pytest.fixture(scope='module')
def temporary_database_with_indexes(
cloud_config, temporary_database_with_column):
def temporary_database_with_indexes(temporary_database_with_column):
snippets.add_index(
cloud_config.spanner_instance,
SPANNER_INSTANCE,
temporary_database_with_column.database_id)
snippets.add_storing_index(
cloud_config.spanner_instance,
SPANNER_INSTANCE,
temporary_database_with_column.database_id)

yield temporary_database_with_column


@pytest.mark.slow
def test_query_data_with_index(
cloud_config, temporary_database_with_indexes, capsys):
def test_query_data_with_index(temporary_database_with_indexes, capsys):
@eventually_consistent.call
def _():
snippets.query_data_with_index(
cloud_config.spanner_instance,
SPANNER_INSTANCE,
temporary_database_with_indexes.database_id)

out, _ = capsys.readouterr()
assert 'Go, Go, Go' in out


@pytest.mark.slow
def test_read_data_with_index(
cloud_config, temporary_database_with_indexes, capsys):
def test_read_data_with_index(temporary_database_with_indexes, capsys):
@eventually_consistent.call
def _():
snippets.read_data_with_index(
cloud_config.spanner_instance,
SPANNER_INSTANCE,
temporary_database_with_indexes.database_id)

out, _ = capsys.readouterr()
assert 'Go, Go, Go' in out


@pytest.mark.slow
def test_read_data_with_storing_index(
cloud_config, temporary_database_with_indexes, capsys):
def test_read_data_with_storing_index(temporary_database_with_indexes, capsys):
@eventually_consistent.call
def _():
snippets.read_data_with_storing_index(
cloud_config.spanner_instance,
SPANNER_INSTANCE,
temporary_database_with_indexes.database_id)

out, _ = capsys.readouterr()
assert 'Go, Go, Go' in out


@pytest.mark.slow
def test_read_write_transaction(
cloud_config, temporary_database_with_column, capsys):
def test_read_write_transaction(temporary_database_with_column, capsys):
@eventually_consistent.call
def _():
snippets.update_data(
cloud_config.spanner_instance,
SPANNER_INSTANCE,
temporary_database_with_column.database_id)
snippets.read_write_transaction(
cloud_config.spanner_instance,
SPANNER_INSTANCE,
temporary_database_with_column.database_id)

out, _ = capsys.readouterr()
Expand All @@ -167,12 +159,11 @@ def _():


@pytest.mark.slow
def test_read_only_transaction(
cloud_config, temporary_database, capsys):
def test_read_only_transaction(temporary_database, capsys):
@eventually_consistent.call
def _():
snippets.read_only_transaction(
cloud_config.spanner_instance,
SPANNER_INSTANCE,
temporary_database.database_id)

out, _ = capsys.readouterr()
Expand Down

0 comments on commit 9df9890

Please sign in to comment.