Skip to content

Commit de4a42e

Browse files
committed
fixed tests
1 parent 0c0f668 commit de4a42e

2 files changed

Lines changed: 179 additions & 70 deletions

File tree

packages/google-cloud-bigtable/tests/unit/data/_async/test_client.py

Lines changed: 87 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -223,26 +223,32 @@ async def test_ctor_dict_options(self):
223223
await client.close()
224224

225225
@CrossSync.pytest
226-
async def test_metrics_exporter_init_shares_arguments(self):
226+
@mock.patch("google.cloud.bigtable.data._async.client.BigtableMetricsExporter")
227+
@mock.patch(
228+
"google.cloud.bigtable.data._sync_autogen.client.BigtableMetricsExporter"
229+
)
230+
async def test_metrics_exporter_init_shares_arguments(
231+
self, exporter_mock_sync, exporter_mock_async
232+
):
227233
expected_credentials = AnonymousCredentials()
228234
expected_project = "custom_project"
229235
expected_options = client_options.ClientOptions()
230236
expected_options.credentials_file = None
231237
expected_options.quota_project_id = None
232-
with mock.patch(
233-
"google.cloud.bigtable.data._metrics.handlers.gcp_exporter.BigtableMetricsExporter",
234-
) as exporter_mock:
235-
async with self._make_client(
236-
project=expected_project,
238+
mock_called = (
239+
exporter_mock_async if CrossSync.is_async else exporter_mock_sync
240+
)
241+
async with self._make_client(
242+
project=expected_project,
243+
credentials=expected_credentials,
244+
client_options=expected_options,
245+
use_emulator=False,
246+
):
247+
mock_called.assert_called_once_with(
248+
project_id=expected_project,
237249
credentials=expected_credentials,
238250
client_options=expected_options,
239-
use_emulator=False,
240-
):
241-
exporter_mock.assert_called_once_with(
242-
project_id=expected_project,
243-
credentials=expected_credentials,
244-
client_options=expected_options,
245-
)
251+
)
246252

247253
@CrossSync.pytest
248254
async def test_metrics_exporter_init_implicit_project(self):
@@ -1268,13 +1274,20 @@ def _make_one(
12681274
client, instance_id, table_id, app_profile_id, **kwargs
12691275
)
12701276

1277+
@pytest.mark.parametrize("use_emulator", [True, False])
12711278
@CrossSync.pytest
1272-
async def test_ctor(self):
1279+
async def test_ctor(self, use_emulator):
12731280
from google.cloud.bigtable.data._helpers import _WarmedInstanceKey
12741281
from google.cloud.bigtable.data._metrics import (
12751282
BigtableClientSideMetricsController,
12761283
GoogleCloudMetricsHandler,
12771284
)
1285+
from google.cloud.bigtable.data._metrics.handlers.gcp_exporter import (
1286+
BigtableMetricsExporter,
1287+
)
1288+
from google.cloud.bigtable.data._metrics.handlers.opentelemetry import (
1289+
OpenTelemetryMetricsHandler,
1290+
)
12781291

12791292
expected_table_id = "table-id"
12801293
expected_instance_id = "instance-id"
@@ -1285,7 +1298,7 @@ async def test_ctor(self):
12851298
expected_read_rows_attempt_timeout = 0.5
12861299
expected_mutate_rows_operation_timeout = 2.5
12871300
expected_mutate_rows_attempt_timeout = 0.75
1288-
client = self._make_client(use_emulator=False)
1301+
client = self._make_client(use_emulator=use_emulator)
12891302
assert not client._active_instances
12901303

12911304
table = self._get_target_class()(
@@ -1318,7 +1331,12 @@ async def test_ctor(self):
13181331
assert client._instance_owners[instance_key] == {id(table)}
13191332
assert isinstance(client._metrics, BigtableClientSideMetricsController)
13201333
assert len(client._metrics.handlers) == 1
1321-
assert isinstance(client._metrics.handlers[0], GoogleCloudMetricsHandler)
1334+
if use_emulator:
1335+
assert isinstance(client._metrics.handlers[0], OpenTelemetryMetricsHandler)
1336+
assert client._gcp_metrics_exporter is None
1337+
else:
1338+
assert isinstance(client._metrics.handlers[0], GoogleCloudMetricsHandler)
1339+
assert isinstance(client._gcp_metrics_exporter, BigtableMetricsExporter)
13221340
assert table.default_operation_timeout == expected_operation_timeout
13231341
assert table.default_attempt_timeout == expected_attempt_timeout
13241342
assert (
@@ -1337,11 +1355,13 @@ async def test_ctor(self):
13371355
table.default_mutate_rows_attempt_timeout
13381356
== expected_mutate_rows_attempt_timeout
13391357
)
1340-
# ensure task reaches completion
1341-
await table._register_instance_future
1342-
assert table._register_instance_future.done()
1343-
assert not table._register_instance_future.cancelled()
1344-
assert table._register_instance_future.exception() is None
1358+
if use_emulator:
1359+
await table._register_instance_future
1360+
assert table._register_instance_future.done()
1361+
assert not table._register_instance_future.cancelled()
1362+
assert table._register_instance_future.exception() is None
1363+
elif table._register_instance_future:
1364+
table._register_instance_future.cancel()
13451365
await client.close()
13461366

13471367
@CrossSync.pytest
@@ -1630,13 +1650,20 @@ def _make_one(
16301650
client, instance_id, table_id, view_id, app_profile_id, **kwargs
16311651
)
16321652

1653+
@pytest.mark.parametrize("use_emulator", [True, False])
16331654
@CrossSync.pytest
1634-
async def test_ctor(self):
1655+
async def test_ctor(self, use_emulator):
16351656
from google.cloud.bigtable.data._helpers import _WarmedInstanceKey
16361657
from google.cloud.bigtable.data._metrics import (
16371658
BigtableClientSideMetricsController,
16381659
GoogleCloudMetricsHandler,
16391660
)
1661+
from google.cloud.bigtable.data._metrics.handlers.gcp_exporter import (
1662+
BigtableMetricsExporter,
1663+
)
1664+
from google.cloud.bigtable.data._metrics.handlers.opentelemetry import (
1665+
OpenTelemetryMetricsHandler,
1666+
)
16401667

16411668
expected_table_id = "table-id"
16421669
expected_instance_id = "instance-id"
@@ -1648,7 +1675,7 @@ async def test_ctor(self):
16481675
expected_read_rows_attempt_timeout = 0.5
16491676
expected_mutate_rows_operation_timeout = 2.5
16501677
expected_mutate_rows_attempt_timeout = 0.75
1651-
client = self._make_client(use_emulator=False)
1678+
client = self._make_client(use_emulator=use_emulator)
16521679
assert not client._active_instances
16531680

16541681
view = self._get_target_class()(
@@ -1687,7 +1714,12 @@ async def test_ctor(self):
16871714
assert client._instance_owners[instance_key] == {id(view)}
16881715
assert isinstance(client._metrics, BigtableClientSideMetricsController)
16891716
assert len(client._metrics.handlers) == 1
1690-
assert isinstance(client._metrics.handlers[0], GoogleCloudMetricsHandler)
1717+
if use_emulator:
1718+
assert isinstance(client._metrics.handlers[0], OpenTelemetryMetricsHandler)
1719+
assert client._gcp_metrics_exporter is None
1720+
else:
1721+
assert isinstance(client._metrics.handlers[0], GoogleCloudMetricsHandler)
1722+
assert isinstance(client._gcp_metrics_exporter, BigtableMetricsExporter)
16911723
assert view.default_operation_timeout == expected_operation_timeout
16921724
assert view.default_attempt_timeout == expected_attempt_timeout
16931725
assert (
@@ -1705,11 +1737,13 @@ async def test_ctor(self):
17051737
view.default_mutate_rows_attempt_timeout
17061738
== expected_mutate_rows_attempt_timeout
17071739
)
1708-
# ensure task reaches completion
1709-
await view._register_instance_future
1710-
assert view._register_instance_future.done()
1711-
assert not view._register_instance_future.cancelled()
1712-
assert view._register_instance_future.exception() is None
1740+
if use_emulator:
1741+
await view._register_instance_future
1742+
assert view._register_instance_future.done()
1743+
assert not view._register_instance_future.cancelled()
1744+
assert view._register_instance_future.exception() is None
1745+
elif view._register_instance_future:
1746+
view._register_instance_future.cancel()
17131747
await client.close()
17141748

17151749

@@ -1738,11 +1772,19 @@ def _make_one(
17381772
client, instance_id, view_id, app_profile_id, **kwargs
17391773
)
17401774

1775+
@pytest.mark.parametrize("use_emulator", [True, False])
17411776
@CrossSync.pytest
1742-
async def test_ctor(self):
1777+
async def test_ctor(self, use_emulator):
17431778
from google.cloud.bigtable.data._helpers import _WarmedInstanceKey
17441779
from google.cloud.bigtable.data._metrics import (
17451780
BigtableClientSideMetricsController,
1781+
GoogleCloudMetricsHandler,
1782+
)
1783+
from google.cloud.bigtable.data._metrics.handlers.gcp_exporter import (
1784+
BigtableMetricsExporter,
1785+
)
1786+
from google.cloud.bigtable.data._metrics.handlers.opentelemetry import (
1787+
OpenTelemetryMetricsHandler,
17461788
)
17471789

17481790
expected_instance_id = "instance-id"
@@ -1754,7 +1796,7 @@ async def test_ctor(self):
17541796
expected_read_rows_attempt_timeout = 0.5
17551797
expected_mutate_rows_operation_timeout = 2.5
17561798
expected_mutate_rows_attempt_timeout = 0.75
1757-
client = self._make_client()
1799+
client = self._make_client(use_emulator=use_emulator)
17581800
assert not client._active_instances
17591801

17601802
view = self._get_target_class()(
@@ -1786,6 +1828,13 @@ async def test_ctor(self):
17861828
assert instance_key in client._active_instances
17871829
assert client._instance_owners[instance_key] == {id(view)}
17881830
assert isinstance(client._metrics, BigtableClientSideMetricsController)
1831+
assert len(client._metrics.handlers) == 1
1832+
if use_emulator:
1833+
assert isinstance(client._metrics.handlers[0], OpenTelemetryMetricsHandler)
1834+
assert client._gcp_metrics_exporter is None
1835+
else:
1836+
assert isinstance(client._metrics.handlers[0], GoogleCloudMetricsHandler)
1837+
assert isinstance(client._gcp_metrics_exporter, BigtableMetricsExporter)
17891838
assert view.default_operation_timeout == expected_operation_timeout
17901839
assert view.default_attempt_timeout == expected_attempt_timeout
17911840
assert (
@@ -1803,11 +1852,13 @@ async def test_ctor(self):
18031852
view.default_mutate_rows_attempt_timeout
18041853
== expected_mutate_rows_attempt_timeout
18051854
)
1806-
# ensure task reaches completion
1807-
await view._register_instance_future
1808-
assert view._register_instance_future.done()
1809-
assert not view._register_instance_future.cancelled()
1810-
assert view._register_instance_future.exception() is None
1855+
if use_emulator:
1856+
await view._register_instance_future
1857+
assert view._register_instance_future.done()
1858+
assert not view._register_instance_future.cancelled()
1859+
assert view._register_instance_future.exception() is None
1860+
elif view._register_instance_future:
1861+
view._register_instance_future.cancel()
18111862
await client.close()
18121863

18131864
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)