19
19
import pytest
20
20
21
21
22
+ def _make_connection (* responses ):
23
+ import google .cloud .storage ._http
24
+
25
+ mock_connection = mock .create_autospec (google .cloud .storage ._http .Connection )
26
+ mock_connection .user_agent = "testing 1.2.3"
27
+ mock_connection .api_request .side_effect = list (responses )
28
+ return mock_connection
29
+
30
+
22
31
def _create_signing_credentials ():
23
32
import google .auth .credentials
24
33
@@ -599,77 +608,104 @@ def api_request(cls, *args, **kwargs):
599
608
self .assertEqual (_FakeConnection ._called_with , expected_cw )
600
609
601
610
def test_create_w_user_project (self ):
611
+ from google .cloud .storage .client import Client
612
+
602
613
PROJECT = "PROJECT"
603
614
BUCKET_NAME = "bucket-name"
604
615
USER_PROJECT = "user-project-123"
605
- connection = _Connection ()
606
- client = _Client (connection , project = PROJECT )
616
+
617
+ client = Client (project = PROJECT )
618
+ client ._base_connection = _Connection ()
619
+
607
620
bucket = self ._make_one (client , BUCKET_NAME , user_project = USER_PROJECT )
608
621
609
622
with self .assertRaises (ValueError ):
610
623
bucket .create ()
611
624
612
625
def test_create_w_missing_client_project (self ):
626
+ from google .cloud .storage .client import Client
627
+
613
628
BUCKET_NAME = "bucket-name"
614
- connection = _Connection ()
615
- client = _Client ( connection , project = None )
629
+
630
+ client = Client ( project = None )
616
631
bucket = self ._make_one (client , BUCKET_NAME )
617
632
618
633
with self .assertRaises (ValueError ):
619
634
bucket .create ()
620
635
621
636
def test_create_w_explicit_project (self ):
637
+ from google .cloud .storage .client import Client
638
+
622
639
PROJECT = "PROJECT"
623
640
BUCKET_NAME = "bucket-name"
624
641
OTHER_PROJECT = "other-project-123"
625
642
DATA = {"name" : BUCKET_NAME }
626
- connection = _Connection (DATA )
627
- client = _Client (connection , project = PROJECT )
628
- bucket = self ._make_one (client , BUCKET_NAME )
643
+ connection = _make_connection (DATA )
629
644
630
- bucket .create (project = OTHER_PROJECT )
645
+ client = Client (project = PROJECT )
646
+ client ._base_connection = connection
631
647
632
- kw , = connection ._requested
633
- self .assertEqual (kw ["method" ], "POST" )
634
- self .assertEqual (kw ["path" ], "/b" )
635
- self .assertEqual (kw ["query_params" ], {"project" : OTHER_PROJECT })
636
- self .assertEqual (kw ["data" ], DATA )
648
+ bucket = self ._make_one (client , BUCKET_NAME )
649
+ bucket .create (project = OTHER_PROJECT )
650
+ connection .api_request .assert_called_once_with (
651
+ method = "POST" ,
652
+ path = "/b" ,
653
+ query_params = {"project" : OTHER_PROJECT },
654
+ data = DATA ,
655
+ _target_object = bucket ,
656
+ )
637
657
638
658
def test_create_w_explicit_location (self ):
659
+ from google .cloud .storage .client import Client
660
+
639
661
PROJECT = "PROJECT"
640
662
BUCKET_NAME = "bucket-name"
641
663
LOCATION = "us-central1"
642
664
DATA = {"location" : LOCATION , "name" : BUCKET_NAME }
643
- connection = _Connection (
665
+
666
+ connection = _make_connection (
644
667
DATA , "{'location': 'us-central1', 'name': 'bucket-name'}"
645
668
)
646
- client = _Client (connection , project = PROJECT )
647
- bucket = self ._make_one (client , BUCKET_NAME )
648
669
670
+ client = Client (project = PROJECT )
671
+ client ._base_connection = connection
672
+
673
+ bucket = self ._make_one (client , BUCKET_NAME )
649
674
bucket .create (location = LOCATION )
650
675
651
- kw , = connection ._requested
652
- self .assertEqual (kw ["method" ], "POST" )
653
- self .assertEqual (kw ["path" ], "/b" )
654
- self .assertEqual (kw ["data" ], DATA )
676
+ connection .api_request .assert_called_once_with (
677
+ method = "POST" ,
678
+ path = "/b" ,
679
+ data = DATA ,
680
+ _target_object = bucket ,
681
+ query_params = {"project" : "PROJECT" },
682
+ )
655
683
self .assertEqual (bucket .location , LOCATION )
656
684
657
685
def test_create_hit (self ):
686
+ from google .cloud .storage .client import Client
687
+
658
688
PROJECT = "PROJECT"
659
689
BUCKET_NAME = "bucket-name"
660
690
DATA = {"name" : BUCKET_NAME }
661
- connection = _Connection (DATA )
662
- client = _Client (connection , project = PROJECT )
691
+ connection = _make_connection (DATA )
692
+ client = Client (project = PROJECT )
693
+ client ._base_connection = connection
694
+
663
695
bucket = self ._make_one (client = client , name = BUCKET_NAME )
664
696
bucket .create ()
665
697
666
- kw , = connection ._requested
667
- self .assertEqual (kw ["method" ], "POST" )
668
- self .assertEqual (kw ["path" ], "/b" )
669
- self .assertEqual (kw ["query_params" ], {"project" : PROJECT })
670
- self .assertEqual (kw ["data" ], DATA )
698
+ connection .api_request .assert_called_once_with (
699
+ method = "POST" ,
700
+ path = "/b" ,
701
+ query_params = {"project" : PROJECT },
702
+ data = DATA ,
703
+ _target_object = bucket ,
704
+ )
671
705
672
706
def test_create_w_extra_properties (self ):
707
+ from google .cloud .storage .client import Client
708
+
673
709
BUCKET_NAME = "bucket-name"
674
710
PROJECT = "PROJECT"
675
711
CORS = [
@@ -694,8 +730,11 @@ def test_create_w_extra_properties(self):
694
730
"billing" : {"requesterPays" : True },
695
731
"labels" : LABELS ,
696
732
}
697
- connection = _Connection (DATA )
698
- client = _Client (connection , project = PROJECT )
733
+
734
+ connection = _make_connection (DATA )
735
+ client = Client (project = PROJECT )
736
+ client ._base_connection = connection
737
+
699
738
bucket = self ._make_one (client = client , name = BUCKET_NAME )
700
739
bucket .cors = CORS
701
740
bucket .lifecycle_rules = LIFECYCLE_RULES
@@ -705,29 +744,37 @@ def test_create_w_extra_properties(self):
705
744
bucket .labels = LABELS
706
745
bucket .create (location = LOCATION )
707
746
708
- kw , = connection ._requested
709
- self .assertEqual (kw ["method" ], "POST" )
710
- self .assertEqual (kw ["path" ], "/b" )
711
- self .assertEqual (kw ["query_params" ], {"project" : PROJECT })
712
- self .assertEqual (kw ["data" ], DATA )
747
+ connection .api_request .assert_called_once_with (
748
+ method = "POST" ,
749
+ path = "/b" ,
750
+ query_params = {"project" : PROJECT },
751
+ data = DATA ,
752
+ _target_object = bucket ,
753
+ )
713
754
714
755
def test_create_w_predefined_acl_invalid (self ):
756
+ from google .cloud .storage .client import Client
757
+
715
758
PROJECT = "PROJECT"
716
759
BUCKET_NAME = "bucket-name"
717
760
DATA = {"name" : BUCKET_NAME }
718
761
connection = _Connection (DATA )
719
- client = _Client (connection , project = PROJECT )
762
+ client = Client (project = PROJECT )
763
+ client ._base_connection = connection
720
764
bucket = self ._make_one (client = client , name = BUCKET_NAME )
721
765
722
766
with self .assertRaises (ValueError ):
723
767
bucket .create (predefined_acl = "bogus" )
724
768
725
769
def test_create_w_predefined_acl_valid (self ):
770
+ from google .cloud .storage .client import Client
771
+
726
772
PROJECT = "PROJECT"
727
773
BUCKET_NAME = "bucket-name"
728
774
DATA = {"name" : BUCKET_NAME }
729
775
connection = _Connection (DATA )
730
- client = _Client (connection , project = PROJECT )
776
+ client = Client (project = PROJECT )
777
+ client ._base_connection = connection
731
778
bucket = self ._make_one (client = client , name = BUCKET_NAME )
732
779
bucket .create (predefined_acl = "publicRead" )
733
780
@@ -739,22 +786,28 @@ def test_create_w_predefined_acl_valid(self):
739
786
self .assertEqual (kw ["data" ], DATA )
740
787
741
788
def test_create_w_predefined_default_object_acl_invalid (self ):
789
+ from google .cloud .storage .client import Client
790
+
742
791
PROJECT = "PROJECT"
743
792
BUCKET_NAME = "bucket-name"
744
793
DATA = {"name" : BUCKET_NAME }
745
794
connection = _Connection (DATA )
746
- client = _Client (connection , project = PROJECT )
795
+ client = Client (project = PROJECT )
796
+ client ._base_connection = connection
747
797
bucket = self ._make_one (client = client , name = BUCKET_NAME )
748
798
749
799
with self .assertRaises (ValueError ):
750
800
bucket .create (predefined_default_object_acl = "bogus" )
751
801
752
802
def test_create_w_predefined_default_object_acl_valid (self ):
803
+ from google .cloud .storage .client import Client
804
+
753
805
PROJECT = "PROJECT"
754
806
BUCKET_NAME = "bucket-name"
755
807
DATA = {"name" : BUCKET_NAME }
756
808
connection = _Connection (DATA )
757
- client = _Client (connection , project = PROJECT )
809
+ client = Client (project = PROJECT )
810
+ client ._base_connection = connection
758
811
bucket = self ._make_one (client = client , name = BUCKET_NAME )
759
812
bucket .create (predefined_default_object_acl = "publicRead" )
760
813
0 commit comments