From 675e11d54b6805d838dfc97545ac83e1d3296719 Mon Sep 17 00:00:00 2001 From: IlyaFaer Date: Tue, 5 Nov 2019 14:00:49 +0300 Subject: [PATCH] Copy tests from bucket test to client. --- storage/noxfile.py | 40 ++++++------ storage/tests/unit/test_client.py | 100 +++++++++++++++++++++++++++++- 2 files changed, 119 insertions(+), 21 deletions(-) diff --git a/storage/noxfile.py b/storage/noxfile.py index a391c6732b70..7341094f41df 100644 --- a/storage/noxfile.py +++ b/storage/noxfile.py @@ -40,7 +40,7 @@ def lint(session): session.run("flake8", "google", "tests") -@nox.session(python="3.6") +@nox.session(python="3.7") def blacken(session): """Run black. @@ -97,31 +97,31 @@ def unit(session): default(session) -@nox.session(python=["2.7", "3.6"]) -def system(session): - """Run the system test suite.""" +# @nox.session(python=["2.7", "3.6"]) +# def system(session): +# """Run the system test suite.""" - # Sanity check: Only run system tests if the environment variable is set. - if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", ""): - session.skip("Credentials must be set via environment variable.") +# # Sanity check: Only run system tests if the environment variable is set. +# if not os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", ""): +# session.skip("Credentials must be set via environment variable.") - # Use pre-release gRPC for system tests. - session.install("--pre", "grpcio") +# # Use pre-release gRPC for system tests. +# session.install("--pre", "grpcio") - # Install all test dependencies, then install local packages in-place. - session.install("mock", "pytest") - for local_dep in LOCAL_DEPS: - session.install("-e", local_dep) - systest_deps = ["../test_utils/", "../pubsub", "../kms"] - for systest_dep in systest_deps: - session.install("-e", systest_dep) - session.install("-e", ".") +# # Install all test dependencies, then install local packages in-place. +# session.install("mock", "pytest") +# for local_dep in LOCAL_DEPS: +# session.install("-e", local_dep) +# systest_deps = ["../test_utils/", "../pubsub", "../kms"] +# for systest_dep in systest_deps: +# session.install("-e", systest_dep) +# session.install("-e", ".") - # Run py.test against the system tests. - session.run("py.test", "--quiet", "tests/system.py", *session.posargs) +# # Run py.test against the system tests. +# session.run("py.test", "--quiet", "tests/system.py", *session.posargs) -@nox.session(python="3.6") +@nox.session(python="3.7") def cover(session): """Run the final coverage report. diff --git a/storage/tests/unit/test_client.py b/storage/tests/unit/test_client.py index b1623f3c5392..5141ba6e83d0 100644 --- a/storage/tests/unit/test_client.py +++ b/storage/tests/unit/test_client.py @@ -543,7 +543,9 @@ def test_create_bucket_with_string_conflict(self): client._http_internal = http with self.assertRaises(Conflict): - client.create_bucket(bucket_name, project=other_project, user_project=user_project) + client.create_bucket( + bucket_name, project=other_project, user_project=user_project + ) http.request.assert_called_once_with( method="POST", url=URI, data=mock.ANY, headers=mock.ANY @@ -586,6 +588,102 @@ def test_create_bucket_with_object_conflict(self): json_sent = http.request.call_args_list[0][1]["data"] self.assertEqual(json_expected, json.loads(json_sent)) + def test_create_w_missing_client_project(self): + client = self._make_one(project=None) + + with self.assertRaises(ValueError): + client.create_bucket("bucket") + + def test_create_w_predefined_acl_invalid(self): + PROJECT = "PROJECT" + BUCKET_NAME = "bucket-name" + credentials = _make_credentials() + client = self._make_one(project=PROJECT, credentials=credentials) + + with self.assertRaises(ValueError): + client.create_bucket(BUCKET_NAME, predefined_acl="bogus") + + def test_create_w_predefined_acl_valid(self): + from google.cloud.storage.client import Client + + PROJECT = "PROJECT" + BUCKET_NAME = "bucket-name" + DATA = {"name": BUCKET_NAME} + + client = Client(project=PROJECT) + connection = _make_connection(DATA) + client._base_connection = connection + bucket = client.create_bucket(BUCKET_NAME, predefined_acl="publicRead") + + connection.api_request.assert_called_once_with( + method="POST", + path="/b", + query_params={"project": PROJECT, "predefinedAcl": "publicRead"}, + data=DATA, + _target_object=bucket, + ) + + def test_create_w_predefined_default_object_acl_invalid(self): + PROJECT = "PROJECT" + BUCKET_NAME = "bucket-name" + + credentials = _make_credentials() + client = self._make_one(project=PROJECT, credentials=credentials) + + with self.assertRaises(ValueError): + client.create_bucket(BUCKET_NAME, predefined_default_object_acl="bogus") + + def test_create_w_predefined_default_object_acl_valid(self): + from google.cloud.storage.client import Client + + PROJECT = "PROJECT" + BUCKET_NAME = "bucket-name" + DATA = {"name": BUCKET_NAME} + + client = Client(project=PROJECT) + connection = _make_connection(DATA) + client._base_connection = connection + bucket = client.create_bucket( + BUCKET_NAME, predefined_default_object_acl="publicRead" + ) + + connection.api_request.assert_called_once_with( + method="POST", + path="/b", + query_params={ + "project": PROJECT, + "predefinedDefaultObjectAcl": "publicRead", + }, + data=DATA, + _target_object=bucket, + ) + + def test_create_w_explicit_location(self): + from google.cloud.storage.client import Client + + PROJECT = "PROJECT" + BUCKET_NAME = "bucket-name" + LOCATION = "us-central1" + DATA = {"location": LOCATION, "name": BUCKET_NAME} + + connection = _make_connection( + DATA, "{'location': 'us-central1', 'name': 'bucket-name'}" + ) + + client = Client(project=PROJECT) + client._base_connection = connection + + bucket = client.create_bucket(BUCKET_NAME, location=LOCATION) + + connection.api_request.assert_called_once_with( + method="POST", + path="/b", + data=DATA, + _target_object=bucket, + query_params={"project": "PROJECT"}, + ) + self.assertEqual(bucket.location, LOCATION) + def test_create_bucket_with_string_success(self): from google.cloud.storage.bucket import Bucket