forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippets_test.py
360 lines (252 loc) · 11.3 KB
/
snippets_test.py
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# 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
import time
from google.cloud import spanner
import pytest
import snippets
def unique_database_id():
""" Creates a unique id for the database. """
return 'test-db-{}'.format(''.join(random.choice(
string.ascii_lowercase + string.digits) for _ in range(5)))
INSTANCE_ID = os.environ['SPANNER_INSTANCE']
DATABASE_ID = unique_database_id()
@pytest.fixture(scope='module')
def spanner_instance():
spanner_client = spanner.Client()
return spanner_client.instance(INSTANCE_ID)
@pytest.fixture(scope='module')
def database(spanner_instance):
""" Creates a temporary database that is removed after testing. """
snippets.create_database(INSTANCE_ID, DATABASE_ID)
db = spanner_instance.database(DATABASE_ID)
yield db
db.drop()
def test_create_database(database):
# Reload will only succeed if the database exists.
database.reload()
def test_insert_data(capsys):
snippets.insert_data(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Inserted data' in out
def test_delete_data(capsys):
snippets.delete_data(INSTANCE_ID, DATABASE_ID)
snippets.insert_data(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Deleted data' in out
def test_query_data(capsys):
snippets.query_data(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk' in out
def test_add_column(capsys):
snippets.add_column(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Added the MarketingBudget column.' in out
def test_read_data(capsys):
snippets.read_data(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk' in out
def test_update_data(capsys):
# Sleep for 15 seconds to ensure previous inserts will be
# 'stale' by the time test_read_stale_data is run.
time.sleep(15)
snippets.update_data(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Updated data.' in out
def test_read_stale_data(capsys):
# This snippet relies on test_update_data inserting data
# at least 15 seconds after the previous insert
snippets.read_stale_data(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'SingerId: 1, AlbumId: 1, MarketingBudget: None' in out
def test_read_write_transaction(capsys):
snippets.read_write_transaction(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Transaction complete' in out
def test_query_data_with_new_column(capsys):
snippets.query_data_with_new_column(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'SingerId: 1, AlbumId: 1, MarketingBudget: 300000' in out
assert 'SingerId: 2, AlbumId: 2, MarketingBudget: 300000' in out
def test_add_index(capsys):
snippets.add_index(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Added the AlbumsByAlbumTitle index' in out
def test_query_data_with_index(capsys):
snippets.query_data_with_index(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Go, Go, Go' in out
assert 'Forever Hold Your Peace' in out
assert 'Green' not in out
def test_read_data_with_index(capsys):
snippets.read_data_with_index(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Go, Go, Go' in out
assert 'Forever Hold Your Peace' in out
assert 'Green' in out
def test_add_storing_index(capsys):
snippets.add_storing_index(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Added the AlbumsByAlbumTitle2 index.' in out
def test_read_data_with_storing_index(capsys):
snippets.read_data_with_storing_index(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert '300000' in out
def test_read_only_transaction(capsys):
snippets.read_only_transaction(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
# Snippet does two reads, so entry should be listed twice
assert out.count('SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk') == 2
def test_add_timestamp_column(capsys):
snippets.add_timestamp_column(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Altered table "Albums" on database ' in out
def test_update_data_with_timestamp(capsys):
snippets.update_data_with_timestamp(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Updated data' in out
def test_query_data_with_timestamp(capsys):
snippets.query_data_with_timestamp(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'SingerId: 1, AlbumId: 1, MarketingBudget: 1000000' in out
assert 'SingerId: 2, AlbumId: 2, MarketingBudget: 750000' in out
def test_create_table_with_timestamp(capsys):
snippets.create_table_with_timestamp(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Created Performances table on database' in out
def test_insert_data_with_timestamp(capsys):
snippets.insert_data_with_timestamp(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Inserted data.' in out
def test_write_struct_data(capsys):
snippets.write_struct_data(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Inserted sample data for STRUCT queries' in out
def test_query_with_struct(capsys):
snippets.query_with_struct(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'SingerId: 6' in out
def test_query_with_array_of_struct(capsys):
snippets.query_with_array_of_struct(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'SingerId: 8' in out
assert 'SingerId: 7' in out
assert 'SingerId: 6' in out
def test_query_struct_field(capsys):
snippets.query_struct_field(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'SingerId: 6' in out
def test_query_nested_struct_field(capsys):
snippets.query_nested_struct_field(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'SingerId: 6 SongName: Imagination' in out
assert 'SingerId: 9 SongName: Imagination' in out
def test_insert_data_with_dml(capsys):
snippets.insert_data_with_dml(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert '1 record(s) inserted.' in out
def test_update_data_with_dml(capsys):
snippets.update_data_with_dml(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert '1 record(s) updated.' in out
def test_delete_data_with_dml(capsys):
snippets.delete_data_with_dml(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert '1 record(s) deleted.' in out
def test_update_data_with_dml_timestamp(capsys):
snippets.update_data_with_dml_timestamp(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert '2 record(s) updated.' in out
def test_dml_write_read_transaction(capsys):
snippets.dml_write_read_transaction(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert '1 record(s) inserted.' in out
assert 'FirstName: Timothy, LastName: Campbell' in out
def test_update_data_with_dml_struct(capsys):
snippets.update_data_with_dml_struct(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert '1 record(s) updated' in out
def test_insert_with_dml(capsys):
snippets.insert_with_dml(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert '4 record(s) inserted' in out
def test_query_data_with_parameter(capsys):
snippets.query_data_with_parameter(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'SingerId: 12, FirstName: Melissa, LastName: Garcia' in out
def test_write_with_dml_transaction(capsys):
snippets.write_with_dml_transaction(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert "Transferred 200000 from Album2's budget to Album1's" in out
def update_data_with_partitioned_dml(capsys):
snippets.update_data_with_partitioned_dml(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert "3 record(s) updated" in out
def delete_data_with_partitioned_dml(capsys):
snippets.delete_data_with_partitioned_dml(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert "5 record(s) deleted" in out
def update_with_batch_dml(capsys):
snippets.update_with_batch_dml(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert "Executed 2 SQL statements using Batch DML" in out
def test_create_table_with_datatypes(capsys):
snippets.create_table_with_datatypes(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Created Venues table on database' in out
def test_insert_datatypes_data(capsys):
snippets.insert_datatypes_data(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'Inserted data.' in out
def test_query_data_with_array(capsys):
snippets.query_data_with_array(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'VenueId: 19, VenueName: Venue 19, AvailableDate: 2020-11-01' in out
assert 'VenueId: 42, VenueName: Venue 42, AvailableDate: 2020-10-01' in out
def test_query_data_with_bool(capsys):
snippets.query_data_with_bool(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'VenueId: 19, VenueName: Venue 19, OutdoorVenue: True' in out
def test_query_data_with_bytes(capsys):
snippets.query_data_with_bytes(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'VenueId: 4, VenueName: Venue 4' in out
def test_query_data_with_date(capsys):
snippets.query_data_with_date(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'VenueId: 4, VenueName: Venue 4, LastContactDate: 2018-09-02' in out
assert 'VenueId: 42, VenueName: Venue 42, LastContactDate: 2018-10-01' \
in out
def test_query_data_with_float(capsys):
snippets.query_data_with_float(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'VenueId: 4, VenueName: Venue 4, PopularityScore: 0.8' in out
assert 'VenueId: 19, VenueName: Venue 19, PopularityScore: 0.9' in out
def test_query_data_with_int(capsys):
snippets.query_data_with_int(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'VenueId: 19, VenueName: Venue 19, Capacity: 6300' in out
assert 'VenueId: 42, VenueName: Venue 42, Capacity: 3000' in out
def test_query_data_with_string(capsys):
snippets.query_data_with_string(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'VenueId: 42, VenueName: Venue 42' in out
def test_query_data_with_timestamp_parameter(capsys):
snippets.query_data_with_timestamp_parameter(INSTANCE_ID, DATABASE_ID)
out, _ = capsys.readouterr()
assert 'VenueId: 4, VenueName: Venue 4, LastUpdateTime:' in out
assert 'VenueId: 19, VenueName: Venue 19, LastUpdateTime:' in out
assert 'VenueId: 42, VenueName: Venue 42, LastUpdateTime:' in out