FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-spanner/samples/samples/snippets_test.py at main · stankiewicz/python-spanner · GitHub
stankiewicz
/
python-spanner
Public
forked from
googleapis/python-spanner
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-spanner
/
samples
/
samples
/
snippets_test.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
783 lines (592 loc) · 28.9 KB
Breadcrumbs
python-spanner
/
samples
/
samples
/
snippets_test.py
Copy path
File metadata and controls
783 lines (592 loc) · 28.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# 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
time
import
uuid
from
google
.
api_core
import
exceptions
from
google
.
cloud
import
spanner
from
google
.
cloud
.
spanner_admin_database_v1
.
types
.
common
import
DatabaseDialect
import
pytest
from
test_utils
.
retry
import
RetryErrors
import
snippets
CREATE_TABLE_SINGERS
=
"""
\
CREATE TABLE Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
SingerInfo BYTES(MAX),
FullName STRING(2048) AS (
ARRAY_TO_STRING([FirstName, LastName], " ")
) STORED
) PRIMARY KEY (SingerId)
"""
CREATE_TABLE_ALBUMS
=
"""
\
CREATE TABLE Albums (
SingerId INT64 NOT NULL,
AlbumId INT64 NOT NULL,
AlbumTitle STRING(MAX)
) PRIMARY KEY (SingerId, AlbumId),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE
"""
retry_429
=
RetryErrors
(
exceptions
.
ResourceExhausted
,
delay
=
15
)
@
pytest
.
fixture
(
scope
=
"module"
)
def
sample_name
():
return
"snippets"
@
pytest
.
fixture
(
scope
=
"module"
)
def
database_dialect
():
"""Spanner dialect to be used for this sample.
The dialect is used to initialize the dialect for the database.
It can either be GoogleStandardSql or PostgreSql.
"""
return
DatabaseDialect
.
GOOGLE_STANDARD_SQL
@
pytest
.
fixture
(
scope
=
"module"
)
def
create_instance_id
():
"""Id for the low-cost instance."""
return
f"create-instance-
{
uuid
.
uuid4
().
hex
[:
10
]
}
"
@
pytest
.
fixture
(
scope
=
"module"
)
def
lci_instance_id
():
"""Id for the low-cost instance."""
return
f"lci-instance-
{
uuid
.
uuid4
().
hex
[:
10
]
}
"
@
pytest
.
fixture
(
scope
=
"module"
)
def
database_id
():
return
f"test-db-
{
uuid
.
uuid4
().
hex
[:
10
]
}
"
@
pytest
.
fixture
(
scope
=
"module"
)
def
create_database_id
():
return
f"create-db-
{
uuid
.
uuid4
().
hex
[:
10
]
}
"
@
pytest
.
fixture
(
scope
=
"module"
)
def
cmek_database_id
():
return
f"cmek-db-
{
uuid
.
uuid4
().
hex
[:
10
]
}
"
@
pytest
.
fixture
(
scope
=
"module"
)
def
default_leader_database_id
():
return
f"leader_db_
{
uuid
.
uuid4
().
hex
[:
10
]
}
"
@
pytest
.
fixture
(
scope
=
"module"
)
def
database_ddl
():
"""Sequence of DDL statements used to set up the database.
Sample testcase modules can override as needed.
"""
return
[
CREATE_TABLE_SINGERS
,
CREATE_TABLE_ALBUMS
]
@
pytest
.
fixture
(
scope
=
"module"
)
def
default_leader
():
"""Default leader for multi-region instances."""
return
"us-east4"
@
pytest
.
fixture
(
scope
=
"module"
)
def
user_managed_instance_config_name
(
spanner_client
):
name
=
f"custom-python-samples-config-
{
uuid
.
uuid4
().
hex
[:
10
]
}
"
yield
name
snippets
.
delete_instance_config
(
"{}/instanceConfigs/{}"
.
format
(
spanner_client
.
project_name
,
name
)
)
return
@
pytest
.
fixture
(
scope
=
"module"
)
def
base_instance_config_id
(
spanner_client
):
return
"{}/instanceConfigs/{}"
.
format
(
spanner_client
.
project_name
,
"nam7"
)
def
test_create_instance_explicit
(
spanner_client
,
create_instance_id
):
# Rather than re-use 'sample_isntance', we create a new instance, to
# ensure that the 'create_instance' snippet is tested.
retry_429
(
snippets
.
create_instance
)(
create_instance_id
)
instance
=
spanner_client
.
instance
(
create_instance_id
)
retry_429
(
instance
.
delete
)()
def
test_create_database_explicit
(
sample_instance
,
create_database_id
):
# Rather than re-use 'sample_database', we create a new database, to
# ensure that the 'create_database' snippet is tested.
snippets
.
create_database
(
sample_instance
.
instance_id
,
create_database_id
)
database
=
sample_instance
.
database
(
create_database_id
)
database
.
drop
()
def
test_create_instance_with_processing_units
(
capsys
,
lci_instance_id
):
processing_units
=
500
retry_429
(
snippets
.
create_instance_with_processing_units
)(
lci_instance_id
,
processing_units
,
)
out
,
_
=
capsys
.
readouterr
()
assert
lci_instance_id
in
out
assert
"{} processing units"
.
format
(
processing_units
)
in
out
spanner_client
=
spanner
.
Client
()
instance
=
spanner_client
.
instance
(
lci_instance_id
)
retry_429
(
instance
.
delete
)()
def
test_create_database_with_encryption_config
(
capsys
,
instance_id
,
cmek_database_id
,
kms_key_name
):
snippets
.
create_database_with_encryption_key
(
instance_id
,
cmek_database_id
,
kms_key_name
)
out
,
_
=
capsys
.
readouterr
()
assert
cmek_database_id
in
out
assert
kms_key_name
in
out
def
test_get_instance_config
(
capsys
):
instance_config
=
"nam6"
snippets
.
get_instance_config
(
instance_config
)
out
,
_
=
capsys
.
readouterr
()
assert
instance_config
in
out
def
test_list_instance_config
(
capsys
):
snippets
.
list_instance_config
()
out
,
_
=
capsys
.
readouterr
()
assert
"regional-us-central1"
in
out
@
pytest
.
mark
.
dependency
(
name
=
"create_instance_config"
)
def
test_create_instance_config
(
capsys
,
user_managed_instance_config_name
,
base_instance_config_id
):
snippets
.
create_instance_config
(
user_managed_instance_config_name
,
base_instance_config_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Created instance configuration"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"create_instance_config"
])
def
test_update_instance_config
(
capsys
,
user_managed_instance_config_name
):
snippets
.
update_instance_config
(
user_managed_instance_config_name
)
out
,
_
=
capsys
.
readouterr
()
assert
"Updated instance configuration"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"create_instance_config"
])
def
test_delete_instance_config
(
capsys
,
user_managed_instance_config_name
):
spanner_client
=
spanner
.
Client
()
snippets
.
delete_instance_config
(
"{}/instanceConfigs/{}"
.
format
(
spanner_client
.
project_name
,
user_managed_instance_config_name
)
)
out
,
_
=
capsys
.
readouterr
()
assert
"successfully deleted"
in
out
def
test_list_instance_config_operations
(
capsys
):
snippets
.
list_instance_config_operations
()
out
,
_
=
capsys
.
readouterr
()
assert
"List instance config operations"
in
out
def
test_list_databases
(
capsys
,
instance_id
):
snippets
.
list_databases
(
instance_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"has default leader"
in
out
def
test_create_database_with_default_leader
(
capsys
,
multi_region_instance
,
multi_region_instance_id
,
default_leader_database_id
,
default_leader
,
):
retry_429
=
RetryErrors
(
exceptions
.
ResourceExhausted
,
delay
=
15
)
retry_429
(
snippets
.
create_database_with_default_leader
)(
multi_region_instance_id
,
default_leader_database_id
,
default_leader
)
out
,
_
=
capsys
.
readouterr
()
assert
default_leader_database_id
in
out
assert
default_leader
in
out
def
test_update_database_with_default_leader
(
capsys
,
multi_region_instance
,
multi_region_instance_id
,
default_leader_database_id
,
default_leader
,
):
retry_429
=
RetryErrors
(
exceptions
.
ResourceExhausted
,
delay
=
15
)
retry_429
(
snippets
.
update_database_with_default_leader
)(
multi_region_instance_id
,
default_leader_database_id
,
default_leader
)
out
,
_
=
capsys
.
readouterr
()
assert
default_leader_database_id
in
out
assert
default_leader
in
out
def
test_get_database_ddl
(
capsys
,
instance_id
,
sample_database
):
snippets
.
get_database_ddl
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
sample_database
.
database_id
in
out
def
test_query_information_schema_database_options
(
capsys
,
multi_region_instance
,
multi_region_instance_id
,
default_leader_database_id
,
default_leader
,
):
snippets
.
query_information_schema_database_options
(
multi_region_instance_id
,
default_leader_database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
default_leader
in
out
@
pytest
.
mark
.
dependency
(
name
=
"insert_data"
)
def
test_insert_data
(
capsys
,
instance_id
,
sample_database
):
snippets
.
insert_data
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Inserted data"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_data"
])
def
test_delete_data
(
capsys
,
instance_id
,
sample_database
):
snippets
.
delete_data
(
instance_id
,
sample_database
.
database_id
)
# put it back for other tests
snippets
.
insert_data
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Deleted data"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_data"
])
def
test_query_data
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk"
in
out
@
pytest
.
mark
.
dependency
(
name
=
"add_column"
,
depends
=
[
"insert_data"
])
def
test_add_column
(
capsys
,
instance_id
,
sample_database
):
snippets
.
add_column
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Added the MarketingBudget column."
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_data"
])
def
test_read_data
(
capsys
,
instance_id
,
sample_database
):
snippets
.
read_data
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk"
in
out
@
pytest
.
mark
.
dependency
(
name
=
"update_data"
,
depends
=
[
"add_column"
])
def
test_update_data
(
capsys
,
instance_id
,
sample_database
):
# 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
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Updated data."
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"update_data"
])
def
test_read_stale_data
(
capsys
,
instance_id
,
sample_database
):
# This snippet relies on test_update_data inserting data
# at least 15 seconds after the previous insert
snippets
.
read_stale_data
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"SingerId: 1, AlbumId: 1, MarketingBudget: None"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_column"
])
def
test_read_write_transaction
(
capsys
,
instance_id
,
sample_database
):
snippets
.
read_write_transaction
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Transaction complete"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_column"
])
def
test_query_data_with_new_column
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_new_column
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"SingerId: 1, AlbumId: 1, MarketingBudget: 300000"
in
out
assert
"SingerId: 2, AlbumId: 2, MarketingBudget: 300000"
in
out
@
pytest
.
mark
.
dependency
(
name
=
"add_index"
,
depends
=
[
"insert_data"
])
def
test_add_index
(
capsys
,
instance_id
,
sample_database
):
snippets
.
add_index
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Added the AlbumsByAlbumTitle index"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_index"
])
def
test_query_data_with_index
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_index
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Go, Go, Go"
in
out
assert
"Forever Hold Your Peace"
in
out
assert
"Green"
not
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_index"
])
def
test_read_data_with_index
(
capsys
,
instance_id
,
sample_database
):
snippets
.
read_data_with_index
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Go, Go, Go"
in
out
assert
"Forever Hold Your Peace"
in
out
assert
"Green"
in
out
@
pytest
.
mark
.
dependency
(
name
=
"add_storing_index"
,
depends
=
[
"insert_data"
])
def
test_add_storing_index
(
capsys
,
instance_id
,
sample_database
):
snippets
.
add_storing_index
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Added the AlbumsByAlbumTitle2 index."
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_storing_index"
])
def
test_read_data_with_storing_index
(
capsys
,
instance_id
,
sample_database
):
snippets
.
read_data_with_storing_index
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"300000"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_data"
])
def
test_read_only_transaction
(
capsys
,
instance_id
,
sample_database
):
snippets
.
read_only_transaction
(
instance_id
,
sample_database
.
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
@
pytest
.
mark
.
dependency
(
name
=
"add_timestamp_column"
,
depends
=
[
"insert_data"
])
def
test_add_timestamp_column
(
capsys
,
instance_id
,
sample_database
):
snippets
.
add_timestamp_column
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
'Altered table "Albums" on database '
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_timestamp_column"
])
def
test_update_data_with_timestamp
(
capsys
,
instance_id
,
sample_database
):
snippets
.
update_data_with_timestamp
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Updated data"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_timestamp_column"
])
def
test_query_data_with_timestamp
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_timestamp
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"SingerId: 1, AlbumId: 1, MarketingBudget: 1000000"
in
out
assert
"SingerId: 2, AlbumId: 2, MarketingBudget: 750000"
in
out
@
pytest
.
mark
.
dependency
(
name
=
"create_table_with_timestamp"
)
def
test_create_table_with_timestamp
(
capsys
,
instance_id
,
sample_database
):
snippets
.
create_table_with_timestamp
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Created Performances table on database"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"create_table_with_timestamp"
])
def
test_insert_data_with_timestamp
(
capsys
,
instance_id
,
sample_database
):
snippets
.
insert_data_with_timestamp
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Inserted data."
in
out
@
pytest
.
mark
.
dependency
(
name
=
"write_struct_data"
)
def
test_write_struct_data
(
capsys
,
instance_id
,
sample_database
):
snippets
.
write_struct_data
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Inserted sample data for STRUCT queries"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"write_struct_data"
])
def
test_query_with_struct
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_with_struct
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"SingerId: 6"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"write_struct_data"
])
def
test_query_with_array_of_struct
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_with_array_of_struct
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"SingerId: 8"
in
out
assert
"SingerId: 7"
in
out
assert
"SingerId: 6"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"write_struct_data"
])
def
test_query_struct_field
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_struct_field
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"SingerId: 6"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"write_struct_data"
])
def
test_query_nested_struct_field
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_nested_struct_field
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"SingerId: 6 SongName: Imagination"
in
out
assert
"SingerId: 9 SongName: Imagination"
in
out
@
pytest
.
mark
.
dependency
(
name
=
"insert_data_with_dml"
)
def
test_insert_data_with_dml
(
capsys
,
instance_id
,
sample_database
):
snippets
.
insert_data_with_dml
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"1 record(s) inserted."
in
out
@
pytest
.
mark
.
dependency
(
name
=
"log_commit_stats"
)
def
test_log_commit_stats
(
capsys
,
instance_id
,
sample_database
):
snippets
.
log_commit_stats
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"1 record(s) inserted."
in
out
# SingerId, FirstName, and LastName plus FullName which is generated.
assert
"4 mutation(s) in transaction."
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_data"
])
def
test_update_data_with_dml
(
capsys
,
instance_id
,
sample_database
):
snippets
.
update_data_with_dml
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"1 record(s) updated."
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_column"
])
def
test_update_data_with_dml_returning
(
capsys
,
instance_id
,
sample_database
):
snippets
.
update_data_with_dml_returning
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"1 record(s) updated."
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_data"
])
def
test_delete_data_with_dml
(
capsys
,
instance_id
,
sample_database
):
snippets
.
delete_data_with_dml
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"1 record(s) deleted."
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_data"
])
def
test_delete_data_with_dml_returning
(
capsys
,
instance_id
,
sample_database
):
snippets
.
delete_data_with_dml_returning
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"1 record(s) deleted."
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_timestamp_column"
])
def
test_update_data_with_dml_timestamp
(
capsys
,
instance_id
,
sample_database
):
snippets
.
update_data_with_dml_timestamp
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"2 record(s) updated."
in
out
@
pytest
.
mark
.
dependency
(
name
=
"dml_write_read_transaction"
)
def
test_dml_write_read_transaction
(
capsys
,
instance_id
,
sample_database
):
snippets
.
dml_write_read_transaction
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"1 record(s) inserted."
in
out
assert
"FirstName: Timothy, LastName: Campbell"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"dml_write_read_transaction"
])
def
test_update_data_with_dml_struct
(
capsys
,
instance_id
,
sample_database
):
snippets
.
update_data_with_dml_struct
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"1 record(s) updated"
in
out
@
pytest
.
mark
.
dependency
(
name
=
"insert_with_dml"
)
def
test_insert_with_dml
(
capsys
,
instance_id
,
sample_database
):
snippets
.
insert_with_dml
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"4 record(s) inserted"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
""
])
def
test_insert_with_dml_returning
(
capsys
,
instance_id
,
sample_database
):
snippets
.
insert_with_dml_returning
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"4 record(s) inserted"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_with_dml"
])
def
test_query_data_with_parameter
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_parameter
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"SingerId: 12, FirstName: Melissa, LastName: Garcia"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_column"
])
def
test_write_with_dml_transaction
(
capsys
,
instance_id
,
sample_database
):
snippets
.
write_with_dml_transaction
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Transferred 200000 from Album2's budget to Album1's"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_column"
])
def
update_data_with_partitioned_dml
(
capsys
,
instance_id
,
sample_database
):
snippets
.
update_data_with_partitioned_dml
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"3 record(s) updated"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_with_dml"
])
def
test_delete_data_with_partitioned_dml
(
capsys
,
instance_id
,
sample_database
):
snippets
.
delete_data_with_partitioned_dml
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"6 record(s) deleted"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_column"
])
def
test_update_with_batch_dml
(
capsys
,
instance_id
,
sample_database
):
snippets
.
update_with_batch_dml
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Executed 2 SQL statements using Batch DML"
in
out
@
pytest
.
mark
.
dependency
(
name
=
"create_table_with_datatypes"
)
def
test_create_table_with_datatypes
(
capsys
,
instance_id
,
sample_database
):
snippets
.
create_table_with_datatypes
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Created Venues table on database"
in
out
@
pytest
.
mark
.
dependency
(
name
=
"insert_datatypes_data"
,
depends
=
[
"create_table_with_datatypes"
],
)
def
test_insert_datatypes_data
(
capsys
,
instance_id
,
sample_database
):
snippets
.
insert_datatypes_data
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Inserted data."
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_datatypes_data"
])
def
test_query_data_with_array
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_array
(
instance_id
,
sample_database
.
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
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_datatypes_data"
])
def
test_query_data_with_bool
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_bool
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"VenueId: 19, VenueName: Venue 19, OutdoorVenue: True"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_datatypes_data"
])
def
test_query_data_with_bytes
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_bytes
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"VenueId: 4, VenueName: Venue 4"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_datatypes_data"
])
def
test_query_data_with_date
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_date
(
instance_id
,
sample_database
.
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
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_datatypes_data"
])
def
test_query_data_with_float
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_float
(
instance_id
,
sample_database
.
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
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_datatypes_data"
])
def
test_query_data_with_int
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_int
(
instance_id
,
sample_database
.
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
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_datatypes_data"
])
def
test_query_data_with_string
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_string
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"VenueId: 42, VenueName: Venue 42"
in
out
@
pytest
.
mark
.
dependency
(
name
=
"add_numeric_column"
,
depends
=
[
"create_table_with_datatypes"
],
)
def
test_add_numeric_column
(
capsys
,
instance_id
,
sample_database
):
snippets
.
add_numeric_column
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
'Altered table "Venues" on database '
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_numeric_column"
,
"insert_datatypes_data"
])
def
test_update_data_with_numeric
(
capsys
,
instance_id
,
sample_database
):
snippets
.
update_data_with_numeric
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Updated data"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_numeric_column"
])
def
test_query_data_with_numeric_parameter
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_numeric_parameter
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"VenueId: 4, Revenue: 35000"
in
out
@
pytest
.
mark
.
dependency
(
name
=
"add_json_column"
,
depends
=
[
"create_table_with_datatypes"
],
)
def
test_add_json_column
(
capsys
,
instance_id
,
sample_database
):
snippets
.
add_json_column
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
'Altered table "Venues" on database '
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_json_column"
,
"insert_datatypes_data"
])
def
test_update_data_with_json
(
capsys
,
instance_id
,
sample_database
):
snippets
.
update_data_with_json
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Updated data"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_json_column"
])
def
test_query_data_with_json_parameter
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_json_parameter
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"VenueId: 19, VenueDetails: {'open': True, 'rating': 9}"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_datatypes_data"
])
def
test_query_data_with_timestamp_parameter
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_timestamp_parameter
(
instance_id
,
sample_database
.
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
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_datatypes_data"
])
def
test_query_data_with_query_options
(
capsys
,
instance_id
,
sample_database
):
snippets
.
query_data_with_query_options
(
instance_id
,
sample_database
.
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
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_datatypes_data"
])
def
test_create_client_with_query_options
(
capsys
,
instance_id
,
sample_database
):
snippets
.
create_client_with_query_options
(
instance_id
,
sample_database
.
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
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_datatypes_data"
])
def
test_set_transaction_tag
(
capsys
,
instance_id
,
sample_database
):
snippets
.
set_transaction_tag
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Venue capacities updated."
in
out
assert
"New venue inserted."
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"insert_data"
])
def
test_set_request_tag
(
capsys
,
instance_id
,
sample_database
):
snippets
.
set_request_tag
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"SingerId: 1, AlbumId: 1, AlbumTitle: Total Junk"
in
out
@
pytest
.
mark
.
dependency
(
name
=
"add_and_drop_database_roles"
,
depends
=
[
"insert_data"
])
def
test_add_and_drop_database_roles
(
capsys
,
instance_id
,
sample_database
):
snippets
.
add_and_drop_database_roles
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"Created roles new_parent and new_child and granted privileges"
in
out
assert
"Revoked privileges and dropped role new_child"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_and_drop_database_roles"
])
def
test_read_data_with_database_role
(
capsys
,
instance_id
,
sample_database
):
snippets
.
read_data_with_database_role
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"ingerId: 1, FirstName: Marc, LastName: Richards"
in
out
@
pytest
.
mark
.
dependency
(
depends
=
[
"add_and_drop_database_roles"
])
def
test_list_database_roles
(
capsys
,
instance_id
,
sample_database
):
snippets
.
list_database_roles
(
instance_id
,
sample_database
.
database_id
)
out
,
_
=
capsys
.
readouterr
()
assert
"new_parent"
in
out
Back
|
FazBrowse Home
|
New Git URL