Skip to content

Commit ce04e30

Browse files
authored
tests(storage): fix query-string order dependent assert (#9728)
Closes #9714.
1 parent 4c796c5 commit ce04e30

File tree

1 file changed

+60
-105
lines changed

1 file changed

+60
-105
lines changed

storage/tests/unit/test_client.py

+60-105
Original file line numberDiff line numberDiff line change
@@ -517,174 +517,129 @@ def test_lookup_bucket_hit(self):
517517
method="GET", url=URI, data=mock.ANY, headers=mock.ANY
518518
)
519519

520-
def test_create_bucket_with_string_conflict(self):
520+
def test_create_bucket_w_missing_client_project(self):
521+
credentials = _make_credentials()
522+
client = self._make_one(project=None, credentials=credentials)
523+
524+
with self.assertRaises(ValueError):
525+
client.create_bucket("bucket")
526+
527+
def test_create_bucket_w_conflict(self):
521528
from google.cloud.exceptions import Conflict
522529

523530
project = "PROJECT"
524531
user_project = "USER_PROJECT"
525532
other_project = "OTHER_PROJECT"
526533
credentials = _make_credentials()
527534
client = self._make_one(project=project, credentials=credentials)
535+
connection = _make_connection()
536+
client._base_connection = connection
537+
connection.api_request.side_effect = Conflict("testing")
528538

529539
bucket_name = "bucket-name"
530-
URI = "/".join(
531-
[
532-
client._connection.API_BASE_URL,
533-
"storage",
534-
client._connection.API_VERSION,
535-
"b?project=%s&userProject=%s" % (other_project, user_project),
536-
]
537-
)
538-
data = {"error": {"message": "Conflict"}}
539-
json_expected = {"name": bucket_name}
540-
http = _make_requests_session(
541-
[_make_json_response(data, status=http_client.CONFLICT)]
542-
)
543-
client._http_internal = http
540+
data = {"name": bucket_name}
544541

545542
with self.assertRaises(Conflict):
546543
client.create_bucket(
547544
bucket_name, project=other_project, user_project=user_project
548545
)
549546

550-
http.request.assert_called_once_with(
551-
method="POST", url=URI, data=mock.ANY, headers=mock.ANY
547+
connection.api_request.assert_called_once_with(
548+
method="POST",
549+
path="/b",
550+
query_params={"project": other_project, "userProject": user_project},
551+
data=data,
552+
_target_object=mock.ANY,
552553
)
553-
json_sent = http.request.call_args_list[0][1]["data"]
554-
self.assertEqual(json_expected, json.loads(json_sent))
555-
556-
def test_create_bucket_with_object_conflict(self):
557-
from google.cloud.exceptions import Conflict
558-
from google.cloud.storage.bucket import Bucket
559554

555+
def test_create_bucket_w_predefined_acl_invalid(self):
560556
project = "PROJECT"
561-
other_project = "OTHER_PROJECT"
562-
credentials = _make_credentials()
563-
client = self._make_one(project=project, credentials=credentials)
564-
565557
bucket_name = "bucket-name"
566-
bucket_obj = Bucket(client, bucket_name)
567-
URI = "/".join(
568-
[
569-
client._connection.API_BASE_URL,
570-
"storage",
571-
client._connection.API_VERSION,
572-
"b?project=%s" % (other_project,),
573-
]
574-
)
575-
data = {"error": {"message": "Conflict"}}
576-
http = _make_requests_session(
577-
[_make_json_response(data, status=http_client.CONFLICT)]
578-
)
579-
client._http_internal = http
580-
581-
with self.assertRaises(Conflict):
582-
client.create_bucket(bucket_obj, project=other_project)
583-
584-
http.request.assert_called_once_with(
585-
method="POST", url=URI, data=mock.ANY, headers=mock.ANY
586-
)
587-
json_expected = {"name": bucket_name}
588-
json_sent = http.request.call_args_list[0][1]["data"]
589-
self.assertEqual(json_expected, json.loads(json_sent))
590-
591-
def test_create_w_missing_client_project(self):
592-
client = self._make_one(project=None)
593-
594-
with self.assertRaises(ValueError):
595-
client.create_bucket("bucket")
596-
597-
def test_create_w_predefined_acl_invalid(self):
598-
PROJECT = "PROJECT"
599-
BUCKET_NAME = "bucket-name"
600558
credentials = _make_credentials()
601-
client = self._make_one(project=PROJECT, credentials=credentials)
559+
client = self._make_one(project=project, credentials=credentials)
602560

603561
with self.assertRaises(ValueError):
604-
client.create_bucket(BUCKET_NAME, predefined_acl="bogus")
605-
606-
def test_create_w_predefined_acl_valid(self):
607-
from google.cloud.storage.client import Client
562+
client.create_bucket(bucket_name, predefined_acl="bogus")
608563

609-
PROJECT = "PROJECT"
610-
BUCKET_NAME = "bucket-name"
611-
DATA = {"name": BUCKET_NAME}
564+
def test_create_bucket_w_predefined_acl_valid(self):
565+
project = "PROJECT"
566+
bucket_name = "bucket-name"
567+
data = {"name": bucket_name}
612568

613-
client = Client(project=PROJECT)
614-
connection = _make_connection(DATA)
569+
credentials = _make_credentials()
570+
client = self._make_one(project=project, credentials=credentials)
571+
connection = _make_connection(data)
615572
client._base_connection = connection
616-
bucket = client.create_bucket(BUCKET_NAME, predefined_acl="publicRead")
573+
bucket = client.create_bucket(bucket_name, predefined_acl="publicRead")
617574

618575
connection.api_request.assert_called_once_with(
619576
method="POST",
620577
path="/b",
621-
query_params={"project": PROJECT, "predefinedAcl": "publicRead"},
622-
data=DATA,
578+
query_params={"project": project, "predefinedAcl": "publicRead"},
579+
data=data,
623580
_target_object=bucket,
624581
)
625582

626-
def test_create_w_predefined_default_object_acl_invalid(self):
627-
PROJECT = "PROJECT"
628-
BUCKET_NAME = "bucket-name"
583+
def test_create_bucket_w_predefined_default_object_acl_invalid(self):
584+
project = "PROJECT"
585+
bucket_name = "bucket-name"
629586

630587
credentials = _make_credentials()
631-
client = self._make_one(project=PROJECT, credentials=credentials)
588+
client = self._make_one(project=project, credentials=credentials)
632589

633590
with self.assertRaises(ValueError):
634-
client.create_bucket(BUCKET_NAME, predefined_default_object_acl="bogus")
591+
client.create_bucket(bucket_name, predefined_default_object_acl="bogus")
635592

636-
def test_create_w_predefined_default_object_acl_valid(self):
637-
from google.cloud.storage.client import Client
638-
639-
PROJECT = "PROJECT"
640-
BUCKET_NAME = "bucket-name"
641-
DATA = {"name": BUCKET_NAME}
593+
def test_create_bucket_w_predefined_default_object_acl_valid(self):
594+
project = "PROJECT"
595+
bucket_name = "bucket-name"
596+
data = {"name": bucket_name}
642597

643-
client = Client(project=PROJECT)
644-
connection = _make_connection(DATA)
598+
credentials = _make_credentials()
599+
client = self._make_one(project=project, credentials=credentials)
600+
connection = _make_connection(data)
645601
client._base_connection = connection
646602
bucket = client.create_bucket(
647-
BUCKET_NAME, predefined_default_object_acl="publicRead"
603+
bucket_name, predefined_default_object_acl="publicRead"
648604
)
649605

650606
connection.api_request.assert_called_once_with(
651607
method="POST",
652608
path="/b",
653609
query_params={
654-
"project": PROJECT,
610+
"project": project,
655611
"predefinedDefaultObjectAcl": "publicRead",
656612
},
657-
data=DATA,
613+
data=data,
658614
_target_object=bucket,
659615
)
660616

661-
def test_create_w_explicit_location(self):
662-
from google.cloud.storage.client import Client
663-
664-
PROJECT = "PROJECT"
665-
BUCKET_NAME = "bucket-name"
666-
LOCATION = "us-central1"
667-
DATA = {"location": LOCATION, "name": BUCKET_NAME}
617+
def test_create_bucket_w_explicit_location(self):
618+
project = "PROJECT"
619+
bucket_name = "bucket-name"
620+
location = "us-central1"
621+
data = {"location": location, "name": bucket_name}
668622

669623
connection = _make_connection(
670-
DATA, "{'location': 'us-central1', 'name': 'bucket-name'}"
624+
data, "{'location': 'us-central1', 'name': 'bucket-name'}"
671625
)
672626

673-
client = Client(project=PROJECT)
627+
credentials = _make_credentials()
628+
client = self._make_one(project=project, credentials=credentials)
674629
client._base_connection = connection
675630

676-
bucket = client.create_bucket(BUCKET_NAME, location=LOCATION)
631+
bucket = client.create_bucket(bucket_name, location=location)
677632

678633
connection.api_request.assert_called_once_with(
679634
method="POST",
680635
path="/b",
681-
data=DATA,
636+
data=data,
682637
_target_object=bucket,
683-
query_params={"project": "PROJECT"},
638+
query_params={"project": project},
684639
)
685-
self.assertEqual(bucket.location, LOCATION)
640+
self.assertEqual(bucket.location, location)
686641

687-
def test_create_bucket_with_string_success(self):
642+
def test_create_bucket_w_string_success(self):
688643
from google.cloud.storage.bucket import Bucket
689644

690645
project = "PROJECT"
@@ -716,7 +671,7 @@ def test_create_bucket_with_string_success(self):
716671
json_sent = http.request.call_args_list[0][1]["data"]
717672
self.assertEqual(json_expected, json.loads(json_sent))
718673

719-
def test_create_bucket_with_object_success(self):
674+
def test_create_bucket_w_object_success(self):
720675
from google.cloud.storage.bucket import Bucket
721676

722677
project = "PROJECT"

0 commit comments

Comments
 (0)