|
42 | 42 | from google.cloud.spanner_v1 import KeySet |
43 | 43 | from google.cloud.spanner_v1.instance import Backup |
44 | 44 | from google.cloud.spanner_v1.instance import Instance |
| 45 | +from google.cloud.spanner_v1.table import Table |
45 | 46 |
|
46 | 47 | from test_utils.retry import RetryErrors |
47 | 48 | from test_utils.retry import RetryInstanceState |
@@ -465,6 +466,52 @@ def _unit_of_work(transaction, name): |
465 | 466 | self.assertEqual(len(rows), 2) |
466 | 467 |
|
467 | 468 |
|
| 469 | +class TestTableAPI(unittest.TestCase, _TestData): |
| 470 | + DATABASE_NAME = "test_database" + unique_resource_id("_") |
| 471 | + |
| 472 | + @classmethod |
| 473 | + def setUpClass(cls): |
| 474 | + pool = BurstyPool(labels={"testcase": "database_api"}) |
| 475 | + ddl_statements = EMULATOR_DDL_STATEMENTS if USE_EMULATOR else DDL_STATEMENTS |
| 476 | + cls._db = Config.INSTANCE.database( |
| 477 | + cls.DATABASE_NAME, ddl_statements=ddl_statements, pool=pool |
| 478 | + ) |
| 479 | + operation = cls._db.create() |
| 480 | + operation.result(30) # raises on failure / timeout. |
| 481 | + |
| 482 | + @classmethod |
| 483 | + def tearDownClass(cls): |
| 484 | + cls._db.drop() |
| 485 | + |
| 486 | + def test_list_tables(self): |
| 487 | + tables = self._db.list_tables() |
| 488 | + table_ids = set(table.table_id for table in tables) |
| 489 | + self.assertIn("contacts", table_ids) |
| 490 | + self.assertIn("contact_phones", table_ids) |
| 491 | + self.assertIn("all_types", table_ids) |
| 492 | + |
| 493 | + def test_list_tables_get_schema(self): |
| 494 | + tables = self._db.list_tables() |
| 495 | + for table in tables: |
| 496 | + schema = table.get_schema() |
| 497 | + self.assertIsInstance(schema, list) |
| 498 | + |
| 499 | + def test_get_schema(self): |
| 500 | + table = Table("all_types", self._db) |
| 501 | + schema = table.get_schema() |
| 502 | + names_and_types = set((field.name, field.type_.code) for field in schema) |
| 503 | + self.assertIn(("pkey", TypeCode.INT64), names_and_types) |
| 504 | + self.assertIn(("int_value", TypeCode.INT64), names_and_types) |
| 505 | + self.assertIn(("int_array", TypeCode.ARRAY), names_and_types) |
| 506 | + self.assertIn(("bool_value", TypeCode.BOOL), names_and_types) |
| 507 | + self.assertIn(("bytes_value", TypeCode.BYTES), names_and_types) |
| 508 | + self.assertIn(("date_value", TypeCode.DATE), names_and_types) |
| 509 | + self.assertIn(("float_value", TypeCode.FLOAT64), names_and_types) |
| 510 | + self.assertIn(("string_value", TypeCode.STRING), names_and_types) |
| 511 | + self.assertIn(("timestamp_value", TypeCode.TIMESTAMP), names_and_types) |
| 512 | + self.assertIn(("numeric_value", TypeCode.NUMERIC), names_and_types) |
| 513 | + |
| 514 | + |
468 | 515 | @unittest.skipIf(USE_EMULATOR, "Skipping backup tests") |
469 | 516 | @unittest.skipIf(SKIP_BACKUP_TESTS, "Skipping backup tests") |
470 | 517 | class TestBackupAPI(unittest.TestCase, _TestData): |
|
0 commit comments