@@ -35,13 +35,22 @@ def test_it(self):
3535 self .assertTrue (client ._get_app_default_called )
3636
3737
38- class Test__set_dataset_from_environ (unittest2 .TestCase ):
38+ class Test_set_default_dataset (unittest2 .TestCase ):
3939
40- def _callFUT (self ):
41- from gcloud .datastore import _set_dataset_from_environ
42- return _set_dataset_from_environ ()
40+ def setUp (self ):
41+ from gcloud .datastore import _implicit_environ
42+ self ._replaced_dataset = _implicit_environ .DATASET
43+ _implicit_environ .DATASET = None
44+
45+ def tearDown (self ):
46+ from gcloud .datastore import _implicit_environ
47+ _implicit_environ .DATASET = self ._replaced_dataset
4348
44- def _test_with_environ (self , environ , expected_result ):
49+ def _callFUT (self , dataset_id = None ):
50+ from gcloud .datastore import set_default_dataset
51+ return set_default_dataset (dataset_id = dataset_id )
52+
53+ def _test_with_environ (self , environ , expected_result , dataset_id = None ):
4554 import os
4655 from gcloud ._testing import _Monkey
4756 from gcloud import datastore
@@ -53,12 +62,12 @@ def _test_with_environ(self, environ, expected_result):
5362 def custom_getenv (key ):
5463 return environ .get (key )
5564
56- def custom_get_dataset (dataset_id ):
57- return dataset_id
65+ def custom_get_dataset (local_dataset_id ):
66+ return local_dataset_id
5867
5968 with _Monkey (os , getenv = custom_getenv ):
6069 with _Monkey (datastore , get_dataset = custom_get_dataset ):
61- self ._callFUT ()
70+ self ._callFUT (dataset_id = dataset_id )
6271
6372 self .assertEqual (_implicit_environ .DATASET , expected_result )
6473
@@ -75,6 +84,10 @@ def test_set_from_env_var(self):
7584 def test_no_env_var_set (self ):
7685 self ._test_with_environ ({}, None )
7786
87+ def test_set_explicit (self ):
88+ DATASET_ID = 'DATASET'
89+ self ._test_with_environ ({}, DATASET_ID , dataset_id = DATASET_ID )
90+
7891
7992class Test_get_dataset (unittest2 .TestCase ):
8093
@@ -161,40 +174,3 @@ def test_allocate_ids(self):
161174
162175 # Check the IDs returned.
163176 self .assertEqual ([key .id () for key in result ], range (1 , NUM_IDS + 1 ))
164-
165- def test_set_DATASET (self ):
166- import os
167- from gcloud ._testing import _Monkey
168- from gcloud .test_credentials import _Client
169- from gcloud import credentials
170- from gcloud .datastore import _implicit_environ
171-
172- # Make custom client for doing auth. Have to fake auth since we
173- # can't monkey patch `datastore.get_dataset` while reloading the
174- # `datastore.__init__` module.
175- client = _Client ()
176-
177- # Fake auth variables.
178- DATASET = 'dataset'
179-
180- # Make a custom getenv function to Monkey.
181- VALUES = {
182- 'GCLOUD_DATASET_ID' : DATASET ,
183- }
184-
185- def custom_getenv (key ):
186- return VALUES .get (key )
187-
188- # Perform the import again with our test patches.
189- with _Monkey (credentials , client = client ):
190- with _Monkey (os , getenv = custom_getenv ):
191- import gcloud .datastore
192- reload (gcloud .datastore )
193-
194- # Check that the DATASET was correctly implied from the environ.
195- implicit_dataset = _implicit_environ .DATASET
196- self .assertEqual (implicit_dataset .id (), DATASET )
197- # Check that the credentials on the implicit DATASET was set on the
198- # fake client.
199- cnxn_credentials = implicit_dataset .connection ().credentials
200- self .assertTrue (cnxn_credentials is client ._signed )
0 commit comments