Skip to content

Commit d6c3b87

Browse files
committed
Updating bigquery and pubsub system tests to use global config.
1 parent 6990c36 commit d6c3b87

2 files changed

Lines changed: 50 additions & 34 deletions

File tree

system_tests/bigquery.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,24 @@
2222
from gcloud import bigquery
2323

2424

25-
_helpers.PROJECT = TESTS_PROJECT
2625
DATASET_NAME = 'system_tests_%012d' % (1000 * time.time(),)
2726

2827

29-
class TestBigQuery(unittest2.TestCase):
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+
3036

31-
@classmethod
32-
def setUpClass(cls):
33-
cls.client = bigquery.Client()
37+
def setUpModule():
38+
_helpers.PROJECT = TESTS_PROJECT
39+
Config.CLIENT = bigquery.Client()
40+
41+
42+
class TestBigQuery(unittest2.TestCase):
3443

3544
def setUp(self):
3645
self.to_delete = []
@@ -40,26 +49,26 @@ def tearDown(self):
4049
doomed.delete()
4150

4251
def test_create_dataset(self):
43-
dataset = self.client.dataset(DATASET_NAME)
52+
dataset = Config.CLIENT.dataset(DATASET_NAME)
4453
self.assertFalse(dataset.exists())
4554
dataset.create()
4655
self.to_delete.append(dataset)
4756
self.assertTrue(dataset.exists())
4857
self.assertEqual(dataset.name, DATASET_NAME)
4958

5059
def test_reload_dataset(self):
51-
dataset = self.client.dataset(DATASET_NAME)
60+
dataset = Config.CLIENT.dataset(DATASET_NAME)
5261
dataset.friendly_name = 'Friendly'
5362
dataset.description = 'Description'
5463
dataset.create()
5564
self.to_delete.append(dataset)
56-
other = self.client.dataset(DATASET_NAME)
65+
other = Config.CLIENT.dataset(DATASET_NAME)
5766
other.reload()
5867
self.assertEqual(other.friendly_name, 'Friendly')
5968
self.assertEqual(other.description, 'Description')
6069

6170
def test_patch_dataset(self):
62-
dataset = self.client.dataset(DATASET_NAME)
71+
dataset = Config.CLIENT.dataset(DATASET_NAME)
6372
self.assertFalse(dataset.exists())
6473
dataset.create()
6574
self.to_delete.append(dataset)
@@ -71,7 +80,7 @@ def test_patch_dataset(self):
7180
self.assertEqual(dataset.description, 'Description')
7281

7382
def test_update_dataset(self):
74-
dataset = self.client.dataset(DATASET_NAME)
83+
dataset = Config.CLIENT.dataset(DATASET_NAME)
7584
self.assertFalse(dataset.exists())
7685
dataset.create()
7786
self.to_delete.append(dataset)
@@ -93,20 +102,20 @@ def test_list_datasets(self):
93102
'newest%d' % (1000 * time.time(),),
94103
]
95104
for dataset_name in datasets_to_create:
96-
dataset = self.client.dataset(dataset_name)
105+
dataset = Config.CLIENT.dataset(dataset_name)
97106
dataset.create()
98107
self.to_delete.append(dataset)
99108

100109
# Retrieve the datasets.
101-
all_datasets, token = self.client.list_datasets()
110+
all_datasets, token = Config.CLIENT.list_datasets()
102111
self.assertTrue(token is None)
103112
created = [dataset for dataset in all_datasets
104113
if dataset.name in datasets_to_create and
105-
dataset.project == self.client.project]
114+
dataset.project == Config.CLIENT.project]
106115
self.assertEqual(len(created), len(datasets_to_create))
107116

108117
def test_create_table(self):
109-
dataset = self.client.dataset(DATASET_NAME)
118+
dataset = Config.CLIENT.dataset(DATASET_NAME)
110119
self.assertFalse(dataset.exists())
111120
dataset.create()
112121
self.to_delete.append(dataset)
@@ -122,7 +131,7 @@ def test_create_table(self):
122131
self.assertEqual(table.name, TABLE_NAME)
123132

124133
def test_list_tables(self):
125-
dataset = self.client.dataset(DATASET_NAME)
134+
dataset = Config.CLIENT.dataset(DATASET_NAME)
126135
self.assertFalse(dataset.exists())
127136
dataset.create()
128137
self.to_delete.append(dataset)
@@ -148,7 +157,7 @@ def test_list_tables(self):
148157
self.assertEqual(len(created), len(tables_to_create))
149158

150159
def test_patch_table(self):
151-
dataset = self.client.dataset(DATASET_NAME)
160+
dataset = Config.CLIENT.dataset(DATASET_NAME)
152161
self.assertFalse(dataset.exists())
153162
dataset.create()
154163
self.to_delete.append(dataset)
@@ -168,7 +177,7 @@ def test_patch_table(self):
168177
self.assertEqual(table.description, 'Description')
169178

170179
def test_update_table(self):
171-
dataset = self.client.dataset(DATASET_NAME)
180+
dataset = Config.CLIENT.dataset(DATASET_NAME)
172181
self.assertFalse(dataset.exists())
173182
dataset.create()
174183
self.to_delete.append(dataset)
@@ -206,7 +215,7 @@ def test_load_table_then_dump_table(self):
206215
('Bhettye Rhubble', 27, None),
207216
]
208217
ROW_IDS = range(len(ROWS))
209-
dataset = self.client.dataset(DATASET_NAME)
218+
dataset = Config.CLIENT.dataset(DATASET_NAME)
210219
self.assertFalse(dataset.exists())
211220
dataset.create()
212221
self.to_delete.append(dataset)
@@ -273,7 +282,7 @@ def test_load_table_from_storage_then_dump_table(self):
273282

274283
self.to_delete.insert(0, blob)
275284

276-
dataset = self.client.dataset(DATASET_NAME)
285+
dataset = Config.CLIENT.dataset(DATASET_NAME)
277286
dataset.create()
278287
self.to_delete.append(dataset)
279288

@@ -284,7 +293,7 @@ def test_load_table_from_storage_then_dump_table(self):
284293
table.create()
285294
self.to_delete.insert(0, table)
286295

287-
job = self.client.load_table_from_storage(
296+
job = Config.CLIENT.load_table_from_storage(
288297
'bq_load_storage_test_%d' % (TIMESTAMP,), table, GS_URL)
289298
job.create_disposition = 'CREATE_NEVER'
290299
job.skip_leading_rows = 1

system_tests/pubsub.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@
2121
from gcloud import pubsub
2222

2323

24-
_helpers.PROJECT = TESTS_PROJECT
24+
class Config(object):
25+
"""Run-time configuration to be modified at set-up.
2526
27+
This is a mutable stand-in to allow test set-up to modify
28+
global state.
29+
"""
30+
CLIENT = None
2631

27-
class TestPubsub(unittest2.TestCase):
2832

29-
@classmethod
30-
def setUpClass(cls):
31-
cls.client = pubsub.Client()
33+
def setUpModule():
34+
_helpers.PROJECT = TESTS_PROJECT
35+
Config.CLIENT = pubsub.Client()
36+
37+
38+
class TestPubsub(unittest2.TestCase):
3239

3340
def setUp(self):
3441
self.to_delete = []
@@ -39,7 +46,7 @@ def tearDown(self):
3946

4047
def test_create_topic(self):
4148
TOPIC_NAME = 'a-new-topic'
42-
topic = self.client.topic(TOPIC_NAME)
49+
topic = Config.CLIENT.topic(TOPIC_NAME)
4350
self.assertFalse(topic.exists())
4451
topic.create()
4552
self.to_delete.append(topic)
@@ -53,20 +60,20 @@ def test_list_topics(self):
5360
'newest%d' % (1000 * time.time(),),
5461
]
5562
for topic_name in topics_to_create:
56-
topic = self.client.topic(topic_name)
63+
topic = Config.CLIENT.topic(topic_name)
5764
topic.create()
5865
self.to_delete.append(topic)
5966

6067
# Retrieve the topics.
61-
all_topics, _ = self.client.list_topics()
68+
all_topics, _ = Config.CLIENT.list_topics()
6269
created = [topic for topic in all_topics
6370
if topic.name in topics_to_create and
64-
topic.project == self.client.project]
71+
topic.project == Config.CLIENT.project]
6572
self.assertEqual(len(created), len(topics_to_create))
6673

6774
def test_create_subscription_defaults(self):
6875
TOPIC_NAME = 'subscribe-me'
69-
topic = self.client.topic(TOPIC_NAME)
76+
topic = Config.CLIENT.topic(TOPIC_NAME)
7077
self.assertFalse(topic.exists())
7178
topic.create()
7279
self.to_delete.append(topic)
@@ -81,7 +88,7 @@ def test_create_subscription_defaults(self):
8188

8289
def test_create_subscription_w_ack_deadline(self):
8390
TOPIC_NAME = 'subscribe-me'
84-
topic = self.client.topic(TOPIC_NAME)
91+
topic = Config.CLIENT.topic(TOPIC_NAME)
8592
self.assertFalse(topic.exists())
8693
topic.create()
8794
self.to_delete.append(topic)
@@ -97,7 +104,7 @@ def test_create_subscription_w_ack_deadline(self):
97104

98105
def test_list_subscriptions(self):
99106
TOPIC_NAME = 'subscribe-me'
100-
topic = self.client.topic(TOPIC_NAME)
107+
topic = Config.CLIENT.topic(TOPIC_NAME)
101108
self.assertFalse(topic.exists())
102109
topic.create()
103110
self.to_delete.append(topic)
@@ -112,15 +119,15 @@ def test_list_subscriptions(self):
112119
self.to_delete.append(subscription)
113120

114121
# Retrieve the subscriptions.
115-
all_subscriptions, _ = self.client.list_subscriptions()
122+
all_subscriptions, _ = Config.CLIENT.list_subscriptions()
116123
created = [subscription for subscription in all_subscriptions
117124
if subscription.name in subscriptions_to_create and
118125
subscription.topic.name == TOPIC_NAME]
119126
self.assertEqual(len(created), len(subscriptions_to_create))
120127

121128
def test_message_pull_mode_e2e(self):
122129
TOPIC_NAME = 'subscribe-me'
123-
topic = self.client.topic(TOPIC_NAME, timestamp_messages=True)
130+
topic = Config.CLIENT.topic(TOPIC_NAME, timestamp_messages=True)
124131
self.assertFalse(topic.exists())
125132
topic.create()
126133
self.to_delete.append(topic)

0 commit comments

Comments
 (0)