Skip to content

Commit

Permalink
Copy tests from bucket test to client.
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaFaer committed Nov 5, 2019
1 parent 2cd2321 commit 675e11d
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 21 deletions.
40 changes: 20 additions & 20 deletions storage/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
100 changes: 99 additions & 1 deletion storage/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 675e11d

Please sign in to comment.