@@ -35,6 +35,47 @@ def test_it(self):
3535 self .assertTrue (client ._get_app_default_called )
3636
3737
38+ class Test__set_dataset_from_environ (unittest2 .TestCase ):
39+
40+ def _callFUT (self ):
41+ from gcloud .datastore import _set_dataset_from_environ
42+ return _set_dataset_from_environ ()
43+
44+ def _test_with_environ (self , environ , expected_result ):
45+ import os
46+ from gcloud ._testing import _Monkey
47+ from gcloud import datastore
48+ from gcloud .datastore import _implicit_environ
49+
50+ # Check the environment is unset.
51+ self .assertEqual (_implicit_environ .DATASET , None )
52+
53+ def custom_getenv (key ):
54+ return environ .get (key )
55+
56+ def custom_get_dataset (dataset_id ):
57+ return dataset_id
58+
59+ with _Monkey (os , getenv = custom_getenv ):
60+ with _Monkey (datastore , get_dataset = custom_get_dataset ):
61+ self ._callFUT ()
62+
63+ self .assertEqual (_implicit_environ .DATASET , expected_result )
64+
65+ def test_set_from_env_var (self ):
66+ from gcloud .datastore import _DATASET_ENV_VAR_NAME
67+
68+ # Make a custom getenv function to Monkey.
69+ DATASET = 'dataset'
70+ VALUES = {
71+ _DATASET_ENV_VAR_NAME : DATASET ,
72+ }
73+ self ._test_with_environ (VALUES , DATASET )
74+
75+ def test_no_env_var_set (self ):
76+ self ._test_with_environ ({}, None )
77+
78+
3879class Test_get_dataset (unittest2 .TestCase ):
3980
4081 def _callFUT (self , dataset_id ):
@@ -56,3 +97,104 @@ def test_it(self):
5697 self .assertTrue (isinstance (found .connection (), Connection ))
5798 self .assertEqual (found .id (), DATASET_ID )
5899 self .assertTrue (client ._get_app_default_called )
100+
101+
102+ class Test_implicit_behavior (unittest2 .TestCase ):
103+
104+ def test__require_dataset (self ):
105+ import gcloud .datastore
106+ from gcloud .datastore import _implicit_environ
107+ original_dataset = _implicit_environ .DATASET
108+
109+ try :
110+ _implicit_environ .DATASET = None
111+ self .assertRaises (EnvironmentError ,
112+ gcloud .datastore ._require_dataset )
113+ NEW_DATASET = object ()
114+ _implicit_environ .DATASET = NEW_DATASET
115+ self .assertEqual (gcloud .datastore ._require_dataset (), NEW_DATASET )
116+ finally :
117+ _implicit_environ .DATASET = original_dataset
118+
119+ def test_get_entity (self ):
120+ import gcloud .datastore
121+ from gcloud .datastore import _implicit_environ
122+ from gcloud .datastore .test_entity import _Dataset
123+ from gcloud ._testing import _Monkey
124+
125+ CUSTOM_DATASET = _Dataset ()
126+ DUMMY_KEY = object ()
127+ DUMMY_VAL = object ()
128+ CUSTOM_DATASET [DUMMY_KEY ] = DUMMY_VAL
129+ with _Monkey (_implicit_environ , DATASET = CUSTOM_DATASET ):
130+ result = gcloud .datastore .get_entity (DUMMY_KEY )
131+ self .assertTrue (result is DUMMY_VAL )
132+
133+ def test_get_entities (self ):
134+ import gcloud .datastore
135+ from gcloud .datastore import _implicit_environ
136+ from gcloud .datastore .test_entity import _Dataset
137+ from gcloud ._testing import _Monkey
138+
139+ CUSTOM_DATASET = _Dataset ()
140+ DUMMY_KEYS = [object (), object ()]
141+ DUMMY_VALS = [object (), object ()]
142+ for key , val in zip (DUMMY_KEYS , DUMMY_VALS ):
143+ CUSTOM_DATASET [key ] = val
144+
145+ with _Monkey (_implicit_environ , DATASET = CUSTOM_DATASET ):
146+ result = gcloud .datastore .get_entities (DUMMY_KEYS )
147+ self .assertTrue (result == DUMMY_VALS )
148+
149+ def test_allocate_ids (self ):
150+ import gcloud .datastore
151+ from gcloud .datastore import _implicit_environ
152+ from gcloud .datastore .key import Key
153+ from gcloud .datastore .test_entity import _Dataset
154+ from gcloud ._testing import _Monkey
155+
156+ CUSTOM_DATASET = _Dataset ()
157+ INCOMPLETE_KEY = Key ()
158+ NUM_IDS = 2
159+ with _Monkey (_implicit_environ , DATASET = CUSTOM_DATASET ):
160+ result = gcloud .datastore .allocate_ids (INCOMPLETE_KEY , NUM_IDS )
161+
162+ # Check the IDs returned.
163+ 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