forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnippets_test.py
More file actions
297 lines (213 loc) · 8.2 KB
/
snippets_test.py
File metadata and controls
297 lines (213 loc) · 8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Copyright 2016 Google, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import random
import string
from gcp_devrel.testing import eventually_consistent
from google.cloud import spanner
import pytest
import snippets
SPANNER_INSTANCE = os.environ['SPANNER_INSTANCE']
@pytest.fixture(scope='module')
def spanner_instance():
spanner_client = spanner.Client()
return spanner_client.instance(SPANNER_INSTANCE)
def unique_database_id():
return 'test-db-{}'.format(''.join(random.choice(
string.ascii_lowercase + string.digits) for _ in range(5)))
def test_create_database(spanner_instance):
database_id = unique_database_id()
print(SPANNER_INSTANCE, database_id)
snippets.create_database(SPANNER_INSTANCE, database_id)
database = spanner_instance.database(database_id)
database.reload() # Will only succeed if the database exists.
database.drop()
@pytest.fixture(scope='module')
def temporary_database(spanner_instance):
database_id = unique_database_id()
snippets.create_database(SPANNER_INSTANCE, database_id)
snippets.insert_data(SPANNER_INSTANCE, database_id)
database = spanner_instance.database(database_id)
database.reload()
yield database
database.drop()
def test_query_data(temporary_database, capsys):
snippets.query_data(SPANNER_INSTANCE, temporary_database.database_id)
out, _ = capsys.readouterr()
assert 'Total Junk' in out
def test_read_data(temporary_database, capsys):
snippets.read_data(SPANNER_INSTANCE, temporary_database.database_id)
out, _ = capsys.readouterr()
assert 'Total Junk' in out
def test_read_stale_data(temporary_database, capsys):
snippets.read_stale_data(SPANNER_INSTANCE, temporary_database.database_id)
out, _ = capsys.readouterr()
# It shouldn't be in the output because it was *just* inserted by the
# temporary database fixture and this sample reads 15 seconds into the
# past.
assert 'Total Junk' not in out
@pytest.fixture(scope='module')
def temporary_database_with_column(temporary_database):
snippets.add_column(SPANNER_INSTANCE, temporary_database.database_id)
yield temporary_database
def test_update_data(temporary_database_with_column):
snippets.update_data(
SPANNER_INSTANCE,
temporary_database_with_column.database_id)
def test_query_data_with_new_column(temporary_database_with_column, capsys):
snippets.query_data_with_new_column(
SPANNER_INSTANCE,
temporary_database_with_column.database_id)
out, _ = capsys.readouterr()
assert 'MarketingBudget' in out
@pytest.fixture(scope='module')
def temporary_database_with_indexes(temporary_database_with_column):
snippets.add_index(
SPANNER_INSTANCE,
temporary_database_with_column.database_id)
snippets.add_storing_index(
SPANNER_INSTANCE,
temporary_database_with_column.database_id)
yield temporary_database_with_column
@pytest.mark.slow
def test_query_data_with_index(temporary_database_with_indexes, capsys):
@eventually_consistent.call
def _():
snippets.query_data_with_index(
SPANNER_INSTANCE,
temporary_database_with_indexes.database_id)
out, _ = capsys.readouterr()
assert 'Go, Go, Go' in out
@pytest.mark.slow
def test_read_data_with_index(temporary_database_with_indexes, capsys):
@eventually_consistent.call
def _():
snippets.read_data_with_index(
SPANNER_INSTANCE,
temporary_database_with_indexes.database_id)
out, _ = capsys.readouterr()
assert 'Go, Go, Go' in out
@pytest.mark.slow
def test_read_data_with_storing_index(temporary_database_with_indexes, capsys):
@eventually_consistent.call
def _():
snippets.read_data_with_storing_index(
SPANNER_INSTANCE,
temporary_database_with_indexes.database_id)
out, _ = capsys.readouterr()
assert 'Go, Go, Go' in out
@pytest.mark.slow
def test_read_write_transaction(temporary_database_with_column, capsys):
@eventually_consistent.call
def _():
snippets.update_data(
SPANNER_INSTANCE,
temporary_database_with_column.database_id)
snippets.read_write_transaction(
SPANNER_INSTANCE,
temporary_database_with_column.database_id)
out, _ = capsys.readouterr()
assert '300000' in out
@pytest.mark.slow
def test_read_only_transaction(temporary_database, capsys):
@eventually_consistent.call
def _():
snippets.read_only_transaction(
SPANNER_INSTANCE,
temporary_database.database_id)
out, _ = capsys.readouterr()
assert 'Forever Hold Your Peace' in out
def test_create_table_with_timestamp(temporary_database, capsys):
snippets.create_table_with_timestamp(
SPANNER_INSTANCE,
temporary_database.database_id)
out, _ = capsys.readouterr()
assert 'Performances' in out
def test_insert_data_with_timestamp(temporary_database, capsys):
snippets.insert_data_with_timestamp(
SPANNER_INSTANCE,
temporary_database.database_id)
out, _ = capsys.readouterr()
assert 'Inserted data.' in out
def test_add_timestamp_column(temporary_database, capsys):
snippets.add_timestamp_column(
SPANNER_INSTANCE,
temporary_database.database_id)
out, _ = capsys.readouterr()
assert 'Albums' in out
@pytest.mark.slow
def test_update_data_with_timestamp(temporary_database, capsys):
snippets.update_data_with_timestamp(
SPANNER_INSTANCE,
temporary_database.database_id)
out, _ = capsys.readouterr()
assert 'Updated data.' in out
@pytest.mark.slow
def test_query_data_with_timestamp(temporary_database, capsys):
@eventually_consistent.call
def _():
snippets.query_data_with_timestamp(
SPANNER_INSTANCE,
temporary_database.database_id)
out, _ = capsys.readouterr()
assert 'Go, Go, Go' in out
@pytest.mark.slow
def test_query_data_with_struct(temporary_database, capsys):
@eventually_consistent.call
def _():
snippets.write_struct_data(
SPANNER_INSTANCE,
temporary_database.database_id)
snippets.query_with_struct(
SPANNER_INSTANCE,
temporary_database.database_id)
out, _ = capsys.readouterr()
assert 'SingerId: 6' in out
@pytest.mark.slow
def test_query_data_with_array_struct(temporary_database, capsys):
@eventually_consistent.call
def _():
snippets.write_struct_data(
SPANNER_INSTANCE,
temporary_database.database_id)
snippets.query_with_array_of_struct(
SPANNER_INSTANCE,
temporary_database.database_id)
out, _ = capsys.readouterr()
assert 'SingerId: 6\nSingerId: 7' in out
@pytest.mark.slow
def test_query_data_with_field_struct(temporary_database, capsys):
@eventually_consistent.call
def _():
snippets.write_struct_data(
SPANNER_INSTANCE,
temporary_database.database_id)
snippets.query_struct_field(
SPANNER_INSTANCE,
temporary_database.database_id)
out, _ = capsys.readouterr()
assert 'SingerId: 6' in out
@pytest.mark.slow
def test_query_data_with_nested_field_struct(temporary_database, capsys):
@eventually_consistent.call
def _():
snippets.write_struct_data(
SPANNER_INSTANCE,
temporary_database.database_id)
snippets.query_nested_struct_field(
SPANNER_INSTANCE,
temporary_database.database_id)
out, _ = capsys.readouterr()
assert 'SingerId: 6 SongName: Imagination' in out
assert 'SingerId: 9 SongName: Imagination' in out