|
14 | 14 |
|
15 | 15 | import unittest |
16 | 16 |
|
| 17 | +import logging |
17 | 18 | import os |
18 | 19 | import mock |
19 | 20 | from google.auth.credentials import AnonymousCredentials |
@@ -98,7 +99,7 @@ def _constructor_test_helper( |
98 | 99 | query_options=query_options, |
99 | 100 | directed_read_options=directed_read_options, |
100 | 101 | default_transaction_options=default_transaction_options, |
101 | | - **kwargs |
| 102 | + **kwargs, |
102 | 103 | ) |
103 | 104 |
|
104 | 105 | expected_creds = expected_creds or creds.with_scopes.return_value |
@@ -329,6 +330,62 @@ def test_constructor_w_default_transaction_options(self): |
329 | 330 | default_transaction_options=self.DEFAULT_TRANSACTION_OPTIONS, |
330 | 331 | ) |
331 | 332 |
|
| 333 | + def test_constructor_logs_options(self): |
| 334 | + from google.cloud.spanner_v1 import client as MUT |
| 335 | + |
| 336 | + creds = build_scoped_credentials() |
| 337 | + observability_options = {"enable_extended_tracing": True} |
| 338 | + with self.assertLogs(MUT.__name__, level="INFO") as cm: |
| 339 | + client = self._make_one( |
| 340 | + project=self.PROJECT, |
| 341 | + credentials=creds, |
| 342 | + route_to_leader_enabled=False, |
| 343 | + directed_read_options=self.DIRECTED_READ_OPTIONS, |
| 344 | + default_transaction_options=self.DEFAULT_TRANSACTION_OPTIONS, |
| 345 | + observability_options=observability_options, |
| 346 | + ) |
| 347 | + self.assertIsNotNone(client) |
| 348 | + |
| 349 | + self.assertEqual(len(cm.output), 1) |
| 350 | + log_output = cm.output[0] |
| 351 | + self.assertIn("Spanner options:", log_output) |
| 352 | + self.assertIn(f"\n Project ID: {self.PROJECT}", log_output) |
| 353 | + self.assertIn("\n Host: spanner.googleapis.com", log_output) |
| 354 | + self.assertIn("\n Route to leader enabled: False", log_output) |
| 355 | + self.assertIn( |
| 356 | + f"\n Directed read options: {self.DIRECTED_READ_OPTIONS}", log_output |
| 357 | + ) |
| 358 | + self.assertIn( |
| 359 | + f"\n Default transaction options: {self.DEFAULT_TRANSACTION_OPTIONS}", |
| 360 | + log_output, |
| 361 | + ) |
| 362 | + self.assertIn(f"\n Observability options: {observability_options}", log_output) |
| 363 | + # SPANNER_DISABLE_BUILTIN_METRICS is "true" from class-level patch |
| 364 | + self.assertIn("\n Built-in metrics enabled: False", log_output) |
| 365 | + |
| 366 | + # Test with custom host |
| 367 | + endpoint = "test.googleapis.com" |
| 368 | + with self.assertLogs(MUT.__name__, level="INFO") as cm: |
| 369 | + self._make_one( |
| 370 | + project=self.PROJECT, |
| 371 | + credentials=creds, |
| 372 | + client_options={"api_endpoint": endpoint}, |
| 373 | + ) |
| 374 | + |
| 375 | + self.assertEqual(len(cm.output), 1) |
| 376 | + log_output = cm.output[0] |
| 377 | + self.assertIn(f"\n Host: {endpoint}", log_output) |
| 378 | + |
| 379 | + # Test with emulator host |
| 380 | + emulator_host = "localhost:9010" |
| 381 | + with mock.patch.dict(os.environ, {MUT.EMULATOR_ENV_VAR: emulator_host}): |
| 382 | + with self.assertLogs(MUT.__name__, level="INFO") as cm: |
| 383 | + self._make_one(project=self.PROJECT) |
| 384 | + |
| 385 | + self.assertEqual(len(cm.output), 1) |
| 386 | + log_output = cm.output[0] |
| 387 | + self.assertIn(f"\n Host: {emulator_host}", log_output) |
| 388 | + |
332 | 389 | @mock.patch("google.cloud.spanner_v1.client._get_spanner_emulator_host") |
333 | 390 | def test_instance_admin_api(self, mock_em): |
334 | 391 | from google.cloud.spanner_v1.client import SPANNER_ADMIN_SCOPE |
|
0 commit comments