Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
fix: fix list_projects behavior for multiple filter params (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmathrubootham committed May 19, 2020
1 parent 27cf673 commit 26a708a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
10 changes: 6 additions & 4 deletions google/cloud/resource_manager/client.py
Expand Up @@ -187,10 +187,12 @@ def list_projects(self, filter_params=None, page_size=None):
extra_params["pageSize"] = page_size

if filter_params is not None:
extra_params["filter"] = [
"{}:{}".format(key, value)
for key, value in six.iteritems(filter_params)
]
extra_params["filter"] = " ".join(
[
"{}:{}".format(key, value)
for key, value in six.iteritems(filter_params)
]
)

return page_iterator.HTTPIterator(
client=self,
Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Expand Up @@ -138,7 +138,7 @@ def docs(session):
"""Build the docs for this library."""

session.install("-e", ".")
session.install("sphinx", "alabaster", "recommonmark")
session.install("sphinx<3.0.0", "alabaster", "recommonmark")

shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True)
session.run(
Expand Down
43 changes: 42 additions & 1 deletion tests/unit/test_client.py
Expand Up @@ -266,7 +266,7 @@ def test_list_projects_with_filter(self):
self.assertEqual(project.status, STATUS)

# Check that the filter made it in the request.
FLATTENED_FILTER_PARAMS = ["id:project-id"]
FLATTENED_FILTER_PARAMS = "id:project-id"
(request,) = client._connection._requested
self.assertEqual(
request,
Expand All @@ -277,6 +277,47 @@ def test_list_projects_with_filter(self):
},
)

def test_list_projects_with_multiple_filters(self):
credentials = _make_credentials()
client = self._make_one(credentials=credentials)

PROJECT_ID = "project-id"
PROJECT_NAME = "MyProjectName"
PROJECT_NUMBER = 1
STATUS = "ACTIVE"
PROJECTS_RESOURCE = {
"projects": [
{
"projectId": PROJECT_ID,
"name": PROJECT_NAME,
"projectNumber": PROJECT_NUMBER,
"lifecycleState": STATUS,
}
]
}
# Patch the connection with one we can easily control.
client._connection = _Connection(PROJECTS_RESOURCE)

FILTER_PARAMS = {"id": "project-id", "name": "MyProjectName"}
results = list(client.list_projects(filter_params=FILTER_PARAMS))

(project,) = results
self.assertEqual(project.project_id, PROJECT_ID)
self.assertEqual(project.number, PROJECT_NUMBER)
self.assertEqual(project.status, STATUS)

# Check that the filter made it in the request.
(request,) = client._connection._requested
self.assertEquals(len(request.keys()), 3)
self.assertEqual(request["path"], "/projects")
self.assertEqual(request["method"], "GET")

# Filter param order can change (since it is in a dict)
request_filter_params = request["query_params"]["filter"].split()
self.assertEquals(len(request_filter_params), 2)
self.assertTrue("id:project-id" in request_filter_params)
self.assertTrue("name:MyProjectName" in request_filter_params)

def test_page_empty_response(self):
from google.api_core import page_iterator

Expand Down

0 comments on commit 26a708a

Please sign in to comment.