2525from system_tests import populate_datastore
2626
2727
28- client .DATASET = TESTS_DATASET
29- CLIENT = datastore .Client ()
28+ class Config (object ):
29+ """Run-time configuration to be modified at set-up.
30+
31+ This is a mutable stand-in to allow test set-up to modify
32+ global state.
33+ """
34+ CLIENT = None
35+
36+
37+ def setUpModule ():
38+ client .DATASET = TESTS_DATASET
39+ Config .CLIENT = datastore .Client ()
3040
3141
3242class TestDatastore (unittest2 .TestCase ):
@@ -35,16 +45,17 @@ def setUp(self):
3545 self .case_entities_to_delete = []
3646
3747 def tearDown (self ):
38- with CLIENT .transaction ():
48+ with Config . CLIENT .transaction ():
3949 keys = [entity .key for entity in self .case_entities_to_delete ]
40- CLIENT .delete_multi (keys )
50+ Config . CLIENT .delete_multi (keys )
4151
4252
4353class TestDatastoreAllocateIDs (TestDatastore ):
4454
4555 def test_allocate_ids (self ):
4656 num_ids = 10
47- allocated_keys = CLIENT .allocate_ids (CLIENT .key ('Kind' ), num_ids )
57+ allocated_keys = Config .CLIENT .allocate_ids (
58+ Config .CLIENT .key ('Kind' ), num_ids )
4859 self .assertEqual (len (allocated_keys ), num_ids )
4960
5061 unique_ids = set ()
@@ -58,7 +69,10 @@ def test_allocate_ids(self):
5869
5970class TestDatastoreSave (TestDatastore ):
6071
61- PARENT = CLIENT .key ('Blog' , 'PizzaMan' )
72+ @classmethod
73+ def setUpClass (cls ):
74+ super (TestDatastoreSave , cls ).setUpClass ()
75+ cls .PARENT = Config .CLIENT .key ('Blog' , 'PizzaMan' )
6276
6377 def _get_post (self , id_or_name = None , post_content = None ):
6478 post_content = post_content or {
@@ -73,7 +87,7 @@ def _get_post(self, id_or_name=None, post_content=None):
7387 # Create an entity with the given content.
7488 # NOTE: Using a parent to ensure consistency for query
7589 # in `test_empty_kind`.
76- key = CLIENT .key ('Post' , parent = self .PARENT )
90+ key = Config . CLIENT .key ('Post' , parent = self .PARENT )
7791 entity = datastore .Entity (key = key )
7892 entity .update (post_content )
7993
@@ -85,7 +99,7 @@ def _get_post(self, id_or_name=None, post_content=None):
8599
86100 def _generic_test_post (self , name = None , key_id = None ):
87101 entity = self ._get_post (id_or_name = (name or key_id ))
88- CLIENT .put (entity )
102+ Config . CLIENT .put (entity )
89103
90104 # Register entity to be deleted.
91105 self .case_entities_to_delete .append (entity )
@@ -94,7 +108,7 @@ def _generic_test_post(self, name=None, key_id=None):
94108 self .assertEqual (entity .key .name , name )
95109 if key_id is not None :
96110 self .assertEqual (entity .key .id , key_id )
97- retrieved_entity = CLIENT .get (entity .key )
111+ retrieved_entity = Config . CLIENT .get (entity .key )
98112 # Check the given and retrieved are the the same.
99113 self .assertEqual (retrieved_entity , entity )
100114
@@ -108,7 +122,7 @@ def test_post_with_generated_id(self):
108122 self ._generic_test_post ()
109123
110124 def test_save_multiple (self ):
111- with CLIENT .transaction () as xact :
125+ with Config . CLIENT .transaction () as xact :
112126 entity1 = self ._get_post ()
113127 xact .put (entity1 )
114128 # Register entity to be deleted.
@@ -129,11 +143,11 @@ def test_save_multiple(self):
129143 self .case_entities_to_delete .append (entity2 )
130144
131145 keys = [entity1 .key , entity2 .key ]
132- matches = CLIENT .get_multi (keys )
146+ matches = Config . CLIENT .get_multi (keys )
133147 self .assertEqual (len (matches ), 2 )
134148
135149 def test_empty_kind (self ):
136- query = CLIENT .query (kind = 'Post' )
150+ query = Config . CLIENT .query (kind = 'Post' )
137151 query .ancestor = self .PARENT
138152 posts = list (query .fetch (limit = 2 ))
139153 self .assertEqual (posts , [])
@@ -142,16 +156,16 @@ def test_empty_kind(self):
142156class TestDatastoreSaveKeys (TestDatastore ):
143157
144158 def test_save_key_self_reference (self ):
145- parent_key = CLIENT .key ('Residence' , 'NewYork' )
146- key = CLIENT .key ('Person' , 'name' , parent = parent_key )
159+ parent_key = Config . CLIENT .key ('Residence' , 'NewYork' )
160+ key = Config . CLIENT .key ('Person' , 'name' , parent = parent_key )
147161 entity = datastore .Entity (key = key )
148162 entity ['fullName' ] = u'Full name'
149163 entity ['linkedTo' ] = key # Self reference.
150164
151- CLIENT .put (entity )
165+ Config . CLIENT .put (entity )
152166 self .case_entities_to_delete .append (entity )
153167
154- query = CLIENT .query (kind = 'Person' )
168+ query = Config . CLIENT .query (kind = 'Person' )
155169 # Adding ancestor to ensure consistency.
156170 query .ancestor = parent_key
157171 query .add_filter ('linkedTo' , '=' , key )
@@ -166,10 +180,11 @@ class TestDatastoreQuery(TestDatastore):
166180 def setUpClass (cls ):
167181 super (TestDatastoreQuery , cls ).setUpClass ()
168182 cls .CHARACTERS = populate_datastore .CHARACTERS
169- cls .ANCESTOR_KEY = CLIENT .key (* populate_datastore .ANCESTOR )
183+ cls .ANCESTOR_KEY = Config . CLIENT .key (* populate_datastore .ANCESTOR )
170184
171185 def _base_query (self ):
172- return CLIENT .query (kind = 'Character' , ancestor = self .ANCESTOR_KEY )
186+ return Config .CLIENT .query (kind = 'Character' ,
187+ ancestor = self .ANCESTOR_KEY )
173188
174189 def test_limit_queries (self ):
175190 limit = 5
@@ -214,7 +229,7 @@ def test_ancestor_query(self):
214229 self .assertEqual (len (entities ), expected_matches )
215230
216231 def test_query___key___filter (self ):
217- rickard_key = CLIENT .key (* populate_datastore .RICKARD )
232+ rickard_key = Config . CLIENT .key (* populate_datastore .RICKARD )
218233
219234 query = self ._base_query ()
220235 query .add_filter ('__key__' , '=' , rickard_key )
@@ -329,26 +344,26 @@ def test_query_group_by(self):
329344class TestDatastoreTransaction (TestDatastore ):
330345
331346 def test_transaction (self ):
332- entity = datastore .Entity (key = CLIENT .key ('Company' , 'Google' ))
347+ entity = datastore .Entity (key = Config . CLIENT .key ('Company' , 'Google' ))
333348 entity ['url' ] = u'www.google.com'
334349
335- with CLIENT .transaction () as xact :
336- result = CLIENT .get (entity .key )
350+ with Config . CLIENT .transaction () as xact :
351+ result = Config . CLIENT .get (entity .key )
337352 if result is None :
338353 xact .put (entity )
339354 self .case_entities_to_delete .append (entity )
340355
341356 # This will always return after the transaction.
342- retrieved_entity = CLIENT .get (entity .key )
357+ retrieved_entity = Config . CLIENT .get (entity .key )
343358 self .case_entities_to_delete .append (retrieved_entity )
344359 self .assertEqual (retrieved_entity , entity )
345360
346361 def test_failure_with_contention (self ):
347362 contention_key = 'baz'
348363 # Fool the Client constructor to avoid creating a new connection.
349- local_client = datastore .Client (dataset_id = CLIENT .dataset_id ,
364+ local_client = datastore .Client (dataset_id = Config . CLIENT .dataset_id ,
350365 http = object ())
351- local_client .connection = CLIENT .connection
366+ local_client .connection = Config . CLIENT .connection
352367
353368 # Insert an entity which will be retrieved in a transaction
354369 # and updated outside it with a contentious value.
@@ -364,7 +379,7 @@ def test_failure_with_contention(self):
364379
365380 # Update the original entity outside the transaction.
366381 orig_entity [contention_key ] = u'outside'
367- CLIENT .put (orig_entity )
382+ Config . CLIENT .put (orig_entity )
368383
369384 # Try to update the entity which we already updated outside the
370385 # transaction.
0 commit comments