Skip to content

Commit

Permalink
update docs, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tswast committed Feb 1, 2021
1 parent 3f9cc92 commit 40a60e3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
6 changes: 3 additions & 3 deletions google/cloud/spanner_v1/database.py
Expand Up @@ -47,12 +47,12 @@
)
from google.cloud.spanner_admin_database_v1 import CreateDatabaseRequest
from google.cloud.spanner_admin_database_v1 import UpdateDatabaseDdlRequest
from google.cloud.spanner_v1 import ExecuteSqlRequest
from google.cloud.spanner_v1.table import Table
from google.cloud.spanner_v1.proto.transaction_pb2 import (
from google.cloud.spanner_v1 import (
ExecuteSqlRequest,
TransactionSelector,
TransactionOptions,
)
from google.cloud.spanner_v1.table import Table

# pylint: enable=ungrouped-imports

Expand Down
29 changes: 25 additions & 4 deletions google/cloud/spanner_v1/table.py
Expand Up @@ -19,18 +19,39 @@

class Table(object):
"""Representation of a Cloud Spanner Table.
:type table_id: str
:param table_id: The ID of the table.
:type instance: :class:`~google.cloud.spanner_v1.database.Database`
:param instance: The database that owns the table.
"""

def __init__(self, table_id, database):
self.table_id = table_id
self._table_id = table_id
self._database = database

def get_schema(self):
@property
def table_id(self):
"""The ID of the table used in SQL.
:rtype: str
:returns: The table ID.
"""
List of google.cloud.spanner_v1.types.Field
return self._table_id

def get_schema(self):
"""Get the schema of this table.
:rtype: list of :class:`~google.cloud.spanner_v1.types.Field`
:returns: The table schema.
"""
with self._database.snapshot() as snapshot:
query = _GET_SCHEMA_TEMPLATE.format(self.table_id)
results = snapshot.execute_sql(query)
# _ = list(results)
# Start iterating to force the schema to download.
try:
next(iter(results))
except StopIteration:
pass
return list(results.fields)
47 changes: 47 additions & 0 deletions tests/system/test_system.py
Expand Up @@ -42,6 +42,7 @@
from google.cloud.spanner_v1 import KeySet
from google.cloud.spanner_v1.instance import Backup
from google.cloud.spanner_v1.instance import Instance
from google.cloud.spanner_v1.table import Table

from test_utils.retry import RetryErrors
from test_utils.retry import RetryInstanceState
Expand Down Expand Up @@ -465,6 +466,52 @@ def _unit_of_work(transaction, name):
self.assertEqual(len(rows), 2)


class TestTableAPI(unittest.TestCase, _TestData):
DATABASE_NAME = "test_database" + unique_resource_id("_")

@classmethod
def setUpClass(cls):
pool = BurstyPool(labels={"testcase": "database_api"})
ddl_statements = EMULATOR_DDL_STATEMENTS if USE_EMULATOR else DDL_STATEMENTS
cls._db = Config.INSTANCE.database(
cls.DATABASE_NAME, ddl_statements=ddl_statements, pool=pool
)
operation = cls._db.create()
operation.result(30) # raises on failure / timeout.

@classmethod
def tearDownClass(cls):
cls._db.drop()

def test_list_tables(self):
tables = self._db.list_tables()
table_ids = set(table.table_id for table in tables)
self.assertIn("contacts", table_ids)
self.assertIn("contact_phones", table_ids)
self.assertIn("all_types", table_ids)

def test_list_tables_get_schema(self):
tables = self._db.list_tables()
for table in tables:
schema = table.get_schema()
self.assertIsInstance(schema, list)

def test_get_schema(self):
table = Table("all_types", self._db)
schema = table.get_schema()
names_and_types = set((field.name, field.type_.code) for field in schema)
self.assertIn(("pkey", TypeCode.INT64), names_and_types)
self.assertIn(("int_value", TypeCode.INT64), names_and_types)
self.assertIn(("int_array", TypeCode.ARRAY), names_and_types)
self.assertIn(("bool_value", TypeCode.BOOL), names_and_types)
self.assertIn(("bytes_value", TypeCode.BYTES), names_and_types)
self.assertIn(("date_value", TypeCode.DATE), names_and_types)
self.assertIn(("float_value", TypeCode.FLOAT64), names_and_types)
self.assertIn(("string_value", TypeCode.STRING), names_and_types)
self.assertIn(("timestamp_value", TypeCode.TIMESTAMP), names_and_types)
self.assertIn(("numeric_value", TypeCode.NUMERIC), names_and_types)


@unittest.skipIf(USE_EMULATOR, "Skipping backup tests")
@unittest.skipIf(SKIP_BACKUP_TESTS, "Skipping backup tests")
class TestBackupAPI(unittest.TestCase, _TestData):
Expand Down

0 comments on commit 40a60e3

Please sign in to comment.