Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: run sample tests in separate projects #906

Closed
6 changes: 6 additions & 0 deletions samples/hello_happybase/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ def main(project_id, instance_id, table_name):

finally:
connection.close()
# make sure table is deleted
try:
table = instance.table(table_name)
table.delete()
except Exception:
pass


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions samples/instanceadmin/instanceadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def run_instance_operations(project_id, instance_id, cluster_id):
:type instance_id: str
:param instance_id: Instance of the client.
"""
print(f"Running instance operations on {project_id} and {instance_id}")
client = bigtable.Client(project=project_id, admin=True)
location_id = "us-central1-f"
serve_nodes = 1
Expand Down
41 changes: 41 additions & 0 deletions samples/instanceadmin/noxfile_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Default TEST_CONFIG_OVERRIDE for python repos.

# You can copy this file into your directory, then it will be imported from
# the noxfile.py.

# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
"ignored_versions": ["2.7"],
# Old samples are opted out of enforcing Python type hints
# All new samples should feature them
"enforce_type_hints": False,
# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
"gcloud_project_env": "BUILD_SPECIFIC_GCLOUD_PROJECT",
# If you need to use a specific version of pip,
# change pip_version_override to the string representation
# of the version number, for example, "20.2.4"
"pip_version_override": None,
# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
"envs": {},
}
2 changes: 0 additions & 2 deletions samples/instanceadmin/test_instanceadmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ def test_run_instance_operations(capsys, dispose_of):
def test_delete_instance(capsys, dispose_of):
from concurrent.futures import TimeoutError

@backoff.on_exception(backoff.expo, TimeoutError)
def _set_up_instance():
dispose_of(INSTANCE)

Expand All @@ -125,7 +124,6 @@ def _set_up_instance():
def test_add_and_delete_cluster(capsys, dispose_of):
from concurrent.futures import TimeoutError

@backoff.on_exception(backoff.expo, TimeoutError)
def _set_up_instance():
dispose_of(INSTANCE)

Expand Down
21 changes: 21 additions & 0 deletions samples/metricscaler/metricscaler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@
BIGTABLE_DEV_INSTANCE = INSTANCE_ID_FORMAT.format(str(uuid.uuid4())[:10])


@pytest.fixture(scope="module", autouse=True)
def preclean():
"""In case any test instances weren't cleared out in a previous run.

Deletes any test instances that were created over an hour ago. Newer instances may
be being used by a concurrent test run.
"""
import time
import warnings
prefix = INSTANCE_ID_FORMAT.format("")
client = bigtable.Client(project=PROJECT, admin=True)
for instance in client.list_instances()[0]:
if instance.instance_id.startswith(prefix):
timestamp = instance.instance_id.split("-")[-1]
timestamp = int(timestamp)
if time.time() - timestamp > 3600:
warnings.warn(
f"Deleting leftover test instance: {instance.name}"
)
instance.delete()

# System tests to verify API calls succeed


Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test_row_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,30 @@ def test__retry_read_rows_exception_deadline_exceeded_wrapped_in_grpc():
assert _retry_read_rows_exception(exception)


def test_partial_cell_data():
from google.cloud.bigtable.row_data import PartialCellData

expected_key = b"row-key"
expected_family_name = b"family-name"
expected_qualifier = b"qualifier"
expected_timestamp = 1234
instance = PartialCellData(
expected_key, expected_family_name, expected_qualifier, expected_timestamp
)
assert instance.row_key == expected_key
assert instance.family_name == expected_family_name
assert instance.qualifier == expected_qualifier
assert instance.timestamp_micros == expected_timestamp
assert instance.value == b""
assert instance.labels == ()
# test updating value
added_value = b"added-value"
instance.append_value(added_value)
assert instance.value == added_value
instance.append_value(added_value)
assert instance.value == added_value + added_value


def _make_partial_rows_data(*args, **kwargs):
from google.cloud.bigtable.row_data import PartialRowsData

Expand Down
Loading