From 76c2a2e23d1239d49c39bf602f8206d006c694b4 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Date: Thu, 3 Sep 2020 14:37:25 -0600 Subject: [PATCH 1/2] docs: add note about filtering for list methods --- AUTHORING_GUIDE.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/AUTHORING_GUIDE.md b/AUTHORING_GUIDE.md index 8712fcaf9c5..28e7ef5cfb2 100644 --- a/AUTHORING_GUIDE.md +++ b/AUTHORING_GUIDE.md @@ -448,6 +448,62 @@ def test_resource(): ... ``` +### Use filters with list methods + +When writing a test for a `list` method, consider filtering the possible results +Listing all resources in the test project may take a considerable amount of time. +The exact way to do this depends on the API. + +Some `list` methods take a `filter`/`filter_` parameter: + +```python +from datetime import datetime + +from google.cloud import logging_v2 + +client = logging_v2.LoggingServiceV2Client() +resource_names = [f"projects/{project}"] + # We add timestamp for making the query faster. + now = datetime.datetime.now(datetime.timezone.utc) + filter_date = now - datetime.timedelta(minutes=1) + filters = ( + f"timestamp>=\"{filter_date.isoformat('T')}\" " + "resource.type=cloud_run_revision " + "AND severity=NOTICE " +) + +entries = client.list_log_entries(resource_names, filter_=filters) + +``` + +Others allow you to limit the result set with additional arguments +to the request: + +```python +from google.cloud import asset_v1p5beta1 + +# TODO project_id = 'Your Google Cloud Project ID' +# TODO asset_types = 'Your asset type list, e.g., +# ["storage.googleapis.com/Bucket","bigquery.googleapis.com/Table"]' +# TODO page_size = 'Num of assets in one page, which must be between 1 and +# 1000 (both inclusively)' + +project_resource = "projects/{}".format(project_id) +content_type = asset_v1p5beta1.ContentType.RESOURCE +client = asset_v1p5beta1.AssetServiceClient() + +# Call ListAssets v1p5beta1 to list assets. +response = client.list_assets( + request={ + "parent": project_resource, + "read_time": None, + "asset_types": asset_types, + "content_type": content_type, + "page_size": page_size, + } +) +``` + ### Test Environment Setup Because all tests are system tests that use live resources, running tests From ada38b4c3c308609c84e0bd20797e1ccbd284672 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> Date: Thu, 3 Sep 2020 14:39:09 -0600 Subject: [PATCH 2/2] Update AUTHORING_GUIDE.md --- AUTHORING_GUIDE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AUTHORING_GUIDE.md b/AUTHORING_GUIDE.md index 28e7ef5cfb2..6ba0c64e1f9 100644 --- a/AUTHORING_GUIDE.md +++ b/AUTHORING_GUIDE.md @@ -450,7 +450,7 @@ def test_resource(): ### Use filters with list methods -When writing a test for a `list` method, consider filtering the possible results +When writing a test for a `list` method, consider filtering the possible results. Listing all resources in the test project may take a considerable amount of time. The exact way to do this depends on the API.