@@ -448,6 +448,62 @@ def test_resource():
448
448
...
449
449
```
450
450
451
+ # ## Use filters with list methods
452
+
453
+ When writing a test for a `list ` method, consider filtering the possible results.
454
+ Listing all resources in the test project may take a considerable amount of time.
455
+ The exact way to do this depends on the API .
456
+
457
+ Some `list ` methods take a `filter ` / `filter_` parameter:
458
+
459
+ ```python
460
+ from datetime import datetime
461
+
462
+ from google.cloud import logging_v2
463
+
464
+ client = logging_v2.LoggingServiceV2Client()
465
+ resource_names = [f " projects/ { project} " ]
466
+ # We add timestamp for making the query faster.
467
+ now = datetime.datetime.now(datetime.timezone.utc)
468
+ filter_date = now - datetime.timedelta(minutes = 1 )
469
+ filters = (
470
+ f " timestamp>= \" { filter_date.isoformat(' T' )} \" "
471
+ " resource.type=cloud_run_revision "
472
+ " AND severity=NOTICE "
473
+ )
474
+
475
+ entries = client.list_log_entries(resource_names, filter_ = filters)
476
+
477
+ ```
478
+
479
+ Others allow you to limit the result set with additional arguments
480
+ to the request:
481
+
482
+ ```python
483
+ from google.cloud import asset_v1p5beta1
484
+
485
+ # TODO project_id = 'Your Google Cloud Project ID'
486
+ # TODO asset_types = 'Your asset type list, e.g.,
487
+ # ["storage.googleapis.com/Bucket","bigquery.googleapis.com/Table"]'
488
+ # TODO page_size = 'Num of assets in one page, which must be between 1 and
489
+ # 1000 (both inclusively)'
490
+
491
+ project_resource = " projects/{} " .format(project_id)
492
+ content_type = asset_v1p5beta1.ContentType.RESOURCE
493
+ client = asset_v1p5beta1.AssetServiceClient()
494
+
495
+ # Call ListAssets v1p5beta1 to list assets.
496
+ response = client.list_assets(
497
+ request = {
498
+ " parent" : project_resource,
499
+ " read_time" : None ,
500
+ " asset_types" : asset_types,
501
+ " content_type" : content_type,
502
+ " page_size" : page_size,
503
+ }
504
+ )
505
+ ```
506
+
451
507
# ## Test Environment Setup
452
508
453
509
Because all tests are system tests that use live resources, running tests
0 commit comments