This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathtest_target_snowflake.py
957 lines (808 loc) · 52.9 KB
/
test_target_snowflake.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
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
import datetime
import json
import unittest
import mock
import os
from nose.tools import assert_raises
import target_snowflake
from target_snowflake import RecordValidationException
from target_snowflake.db_sync import DbSync
from snowflake.connector.errors import ProgrammingError
try:
import tests.utils as test_utils
except ImportError:
import utils as test_utils
METADATA_COLUMNS = [
'_SDC_EXTRACTED_AT',
'_SDC_BATCHED_AT',
'_SDC_DELETED_AT'
]
class TestIntegration(unittest.TestCase):
"""
Integration Tests
"""
maxDiff = None
def setUp(self):
self.config = test_utils.get_test_config()
snowflake = DbSync(self.config)
# Drop target schema
if self.config['default_target_schema']:
snowflake.query("DROP SCHEMA IF EXISTS {}".format(self.config['default_target_schema']))
def persist_lines(self, lines):
"""Loads singer messages into snowflake without table caching option"""
target_snowflake.persist_lines(self.config, lines)
def persist_lines_with_cache(self, lines):
"""Enables table caching option and loads singer messages into snowflake.
Table caching mechanism is creating and maintaining an extra table in snowflake about
the table structures. It's very similar to the INFORMATION_SCHEMA.COLUMNS system views
but querying INFORMATION_SCHEMA is slow especially when lot of taps running
in parallel.
Selecting from a real table instead of INFORMATION_SCHEMA and keeping it
in memory while the target-snowflake is running results better load performance.
"""
table_cache = target_snowflake.load_table_cache(self.config)
target_snowflake.persist_lines(self.config, lines, table_cache)
def remove_metadata_columns_from_rows(self, rows):
"""Removes metadata columns from a list of rows"""
d_rows = []
for r in rows:
# Copy the original row to a new dict to keep the original dict
# and remove metadata columns
d_row = r.copy()
for md_c in METADATA_COLUMNS:
d_row.pop(md_c, None)
# Add new row without metadata columns to the new list
d_rows.append(d_row)
return d_rows
def assert_metadata_columns_exist(self, rows):
"""This is a helper assertion that checks if every row in a list has metadata columns"""
for r in rows:
for md_c in METADATA_COLUMNS:
self.assertTrue(md_c in r)
def assert_metadata_columns_not_exist(self, rows):
"""This is a helper assertion that checks metadata columns don't exist in any row"""
for r in rows:
for md_c in METADATA_COLUMNS:
self.assertFalse(md_c in r)
def assert_three_streams_are_into_snowflake(self, should_metadata_columns_exist=False,
should_hard_deleted_rows=False):
"""
This is a helper assertion that checks if every data from the message-with-three-streams.json
file is available in Snowflake tables correctly.
Useful to check different loading methods (unencrypted, Client-Side encryption, gzip, etc.)
without duplicating assertions
"""
snowflake = DbSync(self.config)
default_target_schema = self.config.get('default_target_schema', '')
schema_mapping = self.config.get('schema_mapping', {})
# Identify target schema name
target_schema = None
if default_target_schema is not None and default_target_schema.strip():
target_schema = default_target_schema
elif schema_mapping:
target_schema = "tap_mysql_test"
# Get loaded rows from tables
table_one = snowflake.query("SELECT * FROM {}.test_table_one ORDER BY c_pk".format(target_schema))
table_two = snowflake.query("SELECT * FROM {}.test_table_two ORDER BY c_pk".format(target_schema))
table_three = snowflake.query("SELECT * FROM {}.test_table_three ORDER BY c_pk".format(target_schema))
# ----------------------------------------------------------------------
# Check rows in table_one
# ----------------------------------------------------------------------
expected_table_one = [
{'C_INT': 1, 'C_PK': 1, 'C_VARCHAR': '1'}
]
self.assertEqual(
self.remove_metadata_columns_from_rows(table_one), expected_table_one)
# ----------------------------------------------------------------------
# Check rows in table_tow
# ----------------------------------------------------------------------
expected_table_two = []
if not should_hard_deleted_rows:
expected_table_two = [
{'C_INT': 1, 'C_PK': 1, 'C_VARCHAR': '1', 'C_DATE': datetime.datetime(2019, 2, 1, 15, 12, 45)},
{'C_INT': 2, 'C_PK': 2, 'C_VARCHAR': '2', 'C_DATE': datetime.datetime(2019, 2, 10, 2, 0, 0)}
]
else:
expected_table_two = [
{'C_INT': 2, 'C_PK': 2, 'C_VARCHAR': '2', 'C_DATE': datetime.datetime(2019, 2, 10, 2, 0, 0)}
]
self.assertEqual(
self.remove_metadata_columns_from_rows(table_two), expected_table_two)
# ----------------------------------------------------------------------
# Check rows in table_three
# ----------------------------------------------------------------------
expected_table_three = []
if not should_hard_deleted_rows:
expected_table_three = [
{'C_INT': 1, 'C_PK': 1, 'C_VARCHAR': '1', 'C_TIME': datetime.time(4, 0, 0)},
{'C_INT': 2, 'C_PK': 2, 'C_VARCHAR': '2', 'C_TIME': datetime.time(7, 15, 0)},
{'C_INT': 3, 'C_PK': 3, 'C_VARCHAR': '3', 'C_TIME': datetime.time(23, 0, 3)}
]
else:
expected_table_three = [
{'C_INT': 1, 'C_PK': 1, 'C_VARCHAR': '1', 'C_TIME': datetime.time(4, 0, 0)},
{'C_INT': 2, 'C_PK': 2, 'C_VARCHAR': '2', 'C_TIME': datetime.time(7, 15, 0)}
]
self.assertEqual(
self.remove_metadata_columns_from_rows(table_three), expected_table_three)
# ----------------------------------------------------------------------
# Check if metadata columns exist or not
# ----------------------------------------------------------------------
if should_metadata_columns_exist:
self.assert_metadata_columns_exist(table_one)
self.assert_metadata_columns_exist(table_two)
self.assert_metadata_columns_exist(table_three)
else:
self.assert_metadata_columns_not_exist(table_one)
self.assert_metadata_columns_not_exist(table_two)
self.assert_metadata_columns_not_exist(table_three)
def assert_logical_streams_are_in_snowflake(self, should_metadata_columns_exist=False):
# Get loaded rows from tables
snowflake = DbSync(self.config)
target_schema = self.config.get('default_target_schema', '')
table_one = snowflake.query("SELECT * FROM {}.logical1_table1 ORDER BY CID".format(target_schema))
table_two = snowflake.query("SELECT * FROM {}.logical1_table2 ORDER BY CID".format(target_schema))
table_three = snowflake.query("SELECT * FROM {}.logical2_table1 ORDER BY CID".format(target_schema))
table_four = snowflake.query("SELECT CID, CTIMENTZ, CTIMETZ FROM {}.logical1_edgydata WHERE CID IN(1,2,3,4,5,6,8,9) ORDER BY CID".format(target_schema))
# ----------------------------------------------------------------------
# Check rows in table_one
# ----------------------------------------------------------------------
expected_table_one = [
{'CID': 1, 'CVARCHAR': "inserted row", 'CVARCHAR2': None},
{'CID': 2, 'CVARCHAR': 'inserted row', "CVARCHAR2": "inserted row"},
{'CID': 3, 'CVARCHAR': "inserted row", 'CVARCHAR2': "inserted row"},
{'CID': 4, 'CVARCHAR': "inserted row", 'CVARCHAR2': "inserted row"}
]
# ----------------------------------------------------------------------
# Check rows in table_tow
# ----------------------------------------------------------------------
expected_table_two = [
{'CID': 1, 'CVARCHAR': "updated row"},
{'CID': 2, 'CVARCHAR': 'updated row'},
{'CID': 3, 'CVARCHAR': "updated row"},
{'CID': 5, 'CVARCHAR': "updated row"},
{'CID': 7, 'CVARCHAR': "updated row"},
{'CID': 8, 'CVARCHAR': 'updated row'},
{'CID': 9, 'CVARCHAR': "updated row"},
{'CID': 10, 'CVARCHAR': 'updated row'}
]
# ----------------------------------------------------------------------
# Check rows in table_three
# ----------------------------------------------------------------------
expected_table_three = [
{'CID': 1, 'CVARCHAR': "updated row"},
{'CID': 2, 'CVARCHAR': 'updated row'},
{'CID': 3, 'CVARCHAR': "updated row"},
]
# ----------------------------------------------------------------------
# Check rows in table_four
# ----------------------------------------------------------------------
expected_table_four = [
{'CID': 1, 'CTIMENTZ': None, 'CTIMETZ': None},
{'CID': 2, 'CTIMENTZ': datetime.time(23, 0, 15), 'CTIMETZ': datetime.time(23, 0, 15)},
{'CID': 3, 'CTIMENTZ': datetime.time(12, 0, 15), 'CTIMETZ': datetime.time(12, 0, 15)},
{'CID': 4, 'CTIMENTZ': datetime.time(12, 0, 15), 'CTIMETZ': datetime.time(9, 0, 15)},
{'CID': 5, 'CTIMENTZ': datetime.time(12, 0, 15), 'CTIMETZ': datetime.time(15, 0, 15)},
{'CID': 6, 'CTIMENTZ': datetime.time(0, 0), 'CTIMETZ': datetime.time(0, 0)},
{'CID': 8, 'CTIMENTZ': datetime.time(0, 0), 'CTIMETZ': datetime.time(1, 0)},
{'CID': 9, 'CTIMENTZ': datetime.time(0, 0), 'CTIMETZ': datetime.time(0, 0)}
]
if should_metadata_columns_exist:
self.assertEqual(self.remove_metadata_columns_from_rows(table_one), expected_table_one)
self.assertEqual(self.remove_metadata_columns_from_rows(table_two), expected_table_two)
self.assertEqual(self.remove_metadata_columns_from_rows(table_three), expected_table_three)
self.assertEqual(table_four, expected_table_four)
else:
self.assertEqual(table_one, expected_table_one)
self.assertEqual(table_two, expected_table_two)
self.assertEqual(table_three, expected_table_three)
self.assertEqual(table_four, expected_table_four)
def assert_logical_streams_are_in_snowflake_and_are_empty(self):
# Get loaded rows from tables
snowflake = DbSync(self.config)
target_schema = self.config.get('default_target_schema', '')
table_one = snowflake.query("SELECT * FROM {}.logical1_table1 ORDER BY CID".format(target_schema))
table_two = snowflake.query("SELECT * FROM {}.logical1_table2 ORDER BY CID".format(target_schema))
table_three = snowflake.query("SELECT * FROM {}.logical2_table1 ORDER BY CID".format(target_schema))
table_four = snowflake.query("SELECT CID, CTIMENTZ, CTIMETZ FROM {}.logical1_edgydata WHERE CID IN(1,2,3,4,5,6,8,9) ORDER BY CID".format(target_schema))
self.assertEqual(table_one, [])
self.assertEqual(table_two, [])
self.assertEqual(table_three, [])
self.assertEqual(table_four, [])
def assert_binary_data_are_in_snowflake(self, table_name, should_metadata_columns_exist=False):
# Get loaded rows from tables
snowflake = DbSync(self.config)
target_schema = self.config.get('default_target_schema', '')
table_one = snowflake.query("SELECT * FROM {}.{} ORDER BY ID".format(target_schema, table_name))
# ----------------------------------------------------------------------
# Check rows in table_one
# ----------------------------------------------------------------------
expected_table_one = [
{'ID': b'pk2', 'DATA': b'data2', 'CREATED_AT': datetime.datetime(2019, 12, 17, 16, 2, 55)},
{'ID': b'pk4', 'DATA': b'data4', "CREATED_AT": datetime.datetime(2019, 12, 17, 16, 32, 22)},
]
if should_metadata_columns_exist:
self.assertEqual(self.remove_metadata_columns_from_rows(table_one), expected_table_one)
else:
self.assertEqual(table_one, expected_table_one)
#################################
# TESTS #
#################################
def test_invalid_json(self):
"""Receiving invalid JSONs should raise an exception"""
tap_lines = test_utils.get_test_tap_lines('invalid-json.json')
with assert_raises(json.decoder.JSONDecodeError):
self.persist_lines_with_cache(tap_lines)
def test_message_order(self):
"""RECORD message without a previously received SCHEMA message should raise an exception"""
tap_lines = test_utils.get_test_tap_lines('invalid-message-order.json')
with assert_raises(Exception):
self.persist_lines_with_cache(tap_lines)
def test_run_query(self):
"""Running SQLs"""
snowflake = DbSync(self.config)
# Running single SQL should return as array
self.assertEqual(snowflake.query("SELECT 1 col1, 2 col2"),
[{'COL1': 1, 'COL2': 2}])
# Running multiple SQLs should return the result of the last query
self.assertEqual(snowflake.query(["SELECT 1 col1, 2 col2",
"SELECT 3 col1, 4 col2",
"SELECT 5 col1, 6 col2"]),
[{'COL1': 5, 'COL2': 6}])
# Running multiple SQLs should return empty list if the last query returns zero record
self.assertEqual(snowflake.query(["SELECT 1 col1, 2 col2",
"SELECT 3 col1, 4 col2",
"SELECT 5 col1, 6 col2 WHERE 1 = 2"]),
[])
# Running multiple SQLs should return the result of the last query even if a previous query returns zero record
self.assertEqual(snowflake.query(["SELECT 1 col1, 2 col2 WHERE 1 =2 ",
"SELECT 3 col1, 4 col2",
"SELECT 5 col1, 6 col2"]),
[{'COL1': 5, 'COL2': 6}])
# Running multiple SQLs should return empty list if every query returns zero record
self.assertEqual(snowflake.query(["SELECT 1 col1, 2 col2 WHERE 1 = 2 ",
"SELECT 3 col1, 4 col2 WHERE 1 = 2",
"SELECT 5 col1, 6 col2 WHERE 1 = 2"]),
[])
def test_loading_tables_with_no_encryption(self):
"""Loading multiple tables from the same input tap with various columns types"""
tap_lines = test_utils.get_test_tap_lines('messages-with-three-streams.json')
# Turning off client-side encryption and load
self.config['client_side_encryption_master_key'] = ''
self.persist_lines_with_cache(tap_lines)
self.assert_three_streams_are_into_snowflake()
def test_loading_tables_with_client_side_encryption(self):
"""Loading multiple tables from the same input tap with various columns types"""
tap_lines = test_utils.get_test_tap_lines('messages-with-three-streams.json')
# Turning on client-side encryption and load
self.config['client_side_encryption_master_key'] = os.environ.get('CLIENT_SIDE_ENCRYPTION_MASTER_KEY')
self.persist_lines_with_cache(tap_lines)
self.assert_three_streams_are_into_snowflake()
def test_loading_tables_with_client_side_encryption_and_wrong_master_key(self):
"""Loading multiple tables from the same input tap with various columns types"""
tap_lines = test_utils.get_test_tap_lines('messages-with-three-streams.json')
# Turning on client-side encryption and load but using a well formatted but wrong master key
self.config['client_side_encryption_master_key'] = "Wr0n6m45t3rKeY0123456789a0123456789a0123456="
with assert_raises(ProgrammingError):
self.persist_lines_with_cache(tap_lines)
def test_loading_tables_with_metadata_columns(self):
"""Loading multiple tables from the same input tap with various columns types"""
tap_lines = test_utils.get_test_tap_lines('messages-with-three-streams.json')
# Turning on adding metadata columns
self.config['add_metadata_columns'] = True
self.persist_lines_with_cache(tap_lines)
# Check if data loaded correctly and metadata columns exist
self.assert_three_streams_are_into_snowflake(should_metadata_columns_exist=True)
def test_loading_tables_with_defined_parallelism(self):
"""Loading multiple tables from the same input tap with various columns types"""
tap_lines = test_utils.get_test_tap_lines('messages-with-three-streams.json')
# Using fixed 1 thread parallelism
self.config['parallelism'] = 1
self.persist_lines_with_cache(tap_lines)
# Check if data loaded correctly and metadata columns exist
self.assert_three_streams_are_into_snowflake()
def test_loading_tables_with_hard_delete(self):
"""Loading multiple tables from the same input tap with deleted rows"""
tap_lines = test_utils.get_test_tap_lines('messages-with-three-streams.json')
# Turning on hard delete mode
self.config['hard_delete'] = True
self.persist_lines_with_cache(tap_lines)
# Check if data loaded correctly and metadata columns exist
self.assert_three_streams_are_into_snowflake(
should_metadata_columns_exist=True,
should_hard_deleted_rows=True
)
def test_loading_with_multiple_schema(self):
"""Loading table with multiple SCHEMA messages"""
tap_lines = test_utils.get_test_tap_lines('messages-with-multi-schemas.json')
# Load with default settings
self.persist_lines_with_cache(tap_lines)
# Check if data loaded correctly
self.assert_three_streams_are_into_snowflake(
should_metadata_columns_exist=False,
should_hard_deleted_rows=False
)
def test_loading_tables_with_binary_columns_and_hard_delete(self):
"""Loading multiple tables from the same input tap with deleted rows"""
tap_lines = test_utils.get_test_tap_lines('messages-with-binary-columns.json')
# Turning on hard delete mode
self.config['hard_delete'] = True
self.persist_lines_with_cache(tap_lines)
# Check if data loaded correctly and metadata columns exist
self.assert_binary_data_are_in_snowflake(
table_name='test_binary',
should_metadata_columns_exist=True
)
def test_loading_table_with_reserved_word_as_name_and_hard_delete(self):
"""Loading a table where the name is a reserved word with deleted rows"""
tap_lines = test_utils.get_test_tap_lines('messages-with-reserved-name-as-table-name.json')
# Turning on hard delete mode
self.config['hard_delete'] = True
self.persist_lines_with_cache(tap_lines)
# Check if data loaded correctly and metadata columns exist
self.assert_binary_data_are_in_snowflake(
table_name='"ORDER"',
should_metadata_columns_exist=True
)
def test_loading_table_with_space(self):
"""Loading a table where the name has space"""
tap_lines = test_utils.get_test_tap_lines('messages-with-space-in-table-name.json')
# Turning on hard delete mode
self.config['hard_delete'] = True
self.persist_lines_with_cache(tap_lines)
# Check if data loaded correctly and metadata columns exist
self.assert_binary_data_are_in_snowflake(
table_name='"TABLE WITH SPACE AND UPPERCASE"',
should_metadata_columns_exist=True
)
def test_loading_unicode_characters(self):
"""Loading unicode encoded characters"""
tap_lines = test_utils.get_test_tap_lines('messages-with-unicode-characters.json')
# Load with default settings
self.persist_lines_with_cache(tap_lines)
# Get loaded rows from tables
snowflake = DbSync(self.config)
target_schema = self.config.get('default_target_schema', '')
table_unicode = snowflake.query("SELECT * FROM {}.test_table_unicode ORDER BY C_INT".format(target_schema))
self.assertEqual(
table_unicode,
[
{'C_INT': 1, 'C_PK': 1, 'C_VARCHAR': 'Hello world, Καλημέρα κόσμε, コンニチハ'},
{'C_INT': 2, 'C_PK': 2, 'C_VARCHAR': 'Chinese: 和毛泽东 <<重上井冈山>>. 严永欣, 一九八八年.'},
{'C_INT': 3, 'C_PK': 3,
'C_VARCHAR': 'Russian: Зарегистрируйтесь сейчас на Десятую Международную Конференцию по'},
{'C_INT': 4, 'C_PK': 4, 'C_VARCHAR': 'Thai: แผ่นดินฮั่นเสื่อมโทรมแสนสังเวช'},
{'C_INT': 5, 'C_PK': 5, 'C_VARCHAR': 'Arabic: لقد لعبت أنت وأصدقاؤك لمدة وحصلتم علي من إجمالي النقاط'},
{'C_INT': 6, 'C_PK': 6, 'C_VARCHAR': 'Special Characters: [",\'!@£$%^&*()]'}
])
def test_non_db_friendly_columns(self):
"""Loading non-db friendly columns like, camelcase, minus signs, etc."""
tap_lines = test_utils.get_test_tap_lines('messages-with-non-db-friendly-columns.json')
# Load with default settings
self.persist_lines_with_cache(tap_lines)
# Get loaded rows from tables
snowflake = DbSync(self.config)
target_schema = self.config.get('default_target_schema', '')
table_non_db_friendly_columns = snowflake.query(
"SELECT * FROM {}.test_table_non_db_friendly_columns ORDER BY c_pk".format(target_schema))
self.assertEqual(
table_non_db_friendly_columns,
[
{'C_PK': 1, 'CAMELCASECOLUMN': 'Dummy row 1', 'MINUS-COLUMN': 'Dummy row 1'},
{'C_PK': 2, 'CAMELCASECOLUMN': 'Dummy row 2', 'MINUS-COLUMN': 'Dummy row 2'},
{'C_PK': 3, 'CAMELCASECOLUMN': 'Dummy row 3', 'MINUS-COLUMN': 'Dummy row 3'},
{'C_PK': 4, 'CAMELCASECOLUMN': 'Dummy row 4', 'MINUS-COLUMN': 'Dummy row 4'},
{'C_PK': 5, 'CAMELCASECOLUMN': 'Dummy row 5', 'MINUS-COLUMN': 'Dummy row 5'},
])
def test_nested_schema_unflattening(self):
"""Loading nested JSON objects into VARIANT columns without flattening"""
tap_lines = test_utils.get_test_tap_lines('messages-with-nested-schema.json')
# Load with default settings - Flattening disabled
self.persist_lines_with_cache(tap_lines)
# Get loaded rows from tables - Transform JSON to string at query time
snowflake = DbSync(self.config)
target_schema = self.config.get('default_target_schema', '')
unflattened_table = snowflake.query("""
SELECT c_pk
,TO_CHAR(c_array) c_array
,TO_CHAR(c_object) c_object
,TO_CHAR(c_object) c_object_with_props
,TO_CHAR(c_nested_object) c_nested_object
FROM {}.test_table_nested_schema
ORDER BY c_pk""".format(target_schema))
# Should be valid nested JSON strings
self.assertEqual(
unflattened_table,
[{
'C_PK': 1,
'C_ARRAY': '[1,2,3]',
'C_OBJECT': '{"key_1":"value_1"}',
'C_OBJECT_WITH_PROPS': '{"key_1":"value_1"}',
'C_NESTED_OBJECT': '{"nested_prop_1":"nested_value_1","nested_prop_2":"nested_value_2","nested_prop_3":{"multi_nested_prop_1":"multi_value_1","multi_nested_prop_2":"multi_value_2"}}'
}])
def test_nested_schema_flattening(self):
"""Loading nested JSON objects with flattening and not not flattening"""
tap_lines = test_utils.get_test_tap_lines('messages-with-nested-schema.json')
# Turning on data flattening
self.config['data_flattening_max_level'] = 10
# Load with default settings - Flattening disabled
self.persist_lines_with_cache(tap_lines)
# Get loaded rows from tables
snowflake = DbSync(self.config)
target_schema = self.config.get('default_target_schema', '')
flattened_table = snowflake.query(
"SELECT * FROM {}.test_table_nested_schema ORDER BY c_pk".format(target_schema))
# Should be flattened columns
self.assertEqual(
flattened_table,
[{
'C_PK': 1,
'C_ARRAY': '[\n 1,\n 2,\n 3\n]',
'C_OBJECT': None,
# Cannot map RECORD to SCHEMA. SCHEMA doesn't have properties that requires for flattening
'C_OBJECT_WITH_PROPS__KEY_1': 'value_1',
'C_NESTED_OBJECT__NESTED_PROP_1': 'nested_value_1',
'C_NESTED_OBJECT__NESTED_PROP_2': 'nested_value_2',
'C_NESTED_OBJECT__NESTED_PROP_3__MULTI_NESTED_PROP_1': 'multi_value_1',
'C_NESTED_OBJECT__NESTED_PROP_3__MULTI_NESTED_PROP_2': 'multi_value_2',
}])
def test_column_name_change(self):
"""Tests correct renaming of snowflake columns after source change"""
tap_lines_before_column_name_change = test_utils.get_test_tap_lines('messages-with-three-streams.json')
tap_lines_after_column_name_change = test_utils.get_test_tap_lines(
'messages-with-three-streams-modified-column.json')
# Load with default settings
self.persist_lines_with_cache(tap_lines_before_column_name_change)
self.persist_lines_with_cache(tap_lines_after_column_name_change)
# Get loaded rows from tables
snowflake = DbSync(self.config)
target_schema = self.config.get('default_target_schema', '')
table_one = snowflake.query("SELECT * FROM {}.test_table_one ORDER BY c_pk".format(target_schema))
table_two = snowflake.query("SELECT * FROM {}.test_table_two ORDER BY c_pk".format(target_schema))
table_three = snowflake.query("SELECT * FROM {}.test_table_three ORDER BY c_pk".format(target_schema))
# Get the previous column name from information schema in test_table_two
previous_column_name = snowflake.query("""
SELECT column_name
FROM information_schema.columns
WHERE table_catalog = '{}'
AND table_schema = '{}'
AND table_name = 'TEST_TABLE_TWO'
AND ordinal_position = 1
""".format(
self.config.get('dbname', '').upper(),
target_schema.upper()))[0]["COLUMN_NAME"]
# Table one should have no changes
self.assertEqual(
table_one,
[{'C_INT': 1, 'C_PK': 1, 'C_VARCHAR': '1'}])
# Table two should have a versioned column and a new column
self.assertEquals(
table_two,
[
{previous_column_name: datetime.datetime(2019, 2, 1, 15, 12, 45), 'C_INT': 1, 'C_PK': 1,
'C_VARCHAR': '1', 'C_DATE': None, 'C_NEW_COLUMN': None},
{previous_column_name: datetime.datetime(2019, 2, 10, 2), 'C_INT': 2, 'C_PK': 2, 'C_VARCHAR': '2',
'C_DATE': '2019-02-12 02:00:00', 'C_NEW_COLUMN': 'data 1'},
{previous_column_name: None, 'C_INT': 3, 'C_PK': 3, 'C_VARCHAR': '2', 'C_DATE': '2019-02-15 02:00:00',
'C_NEW_COLUMN': 'data 2'}
]
)
# Table three should have a renamed columns and a new column
self.assertEqual(
table_three,
[
{'C_INT': 1, 'C_PK': 1, 'C_TIME': datetime.time(4, 0), 'C_VARCHAR': '1', 'C_TIME_RENAMED': None,
'C_NEW_COLUMN': None},
{'C_INT': 2, 'C_PK': 2, 'C_TIME': datetime.time(7, 15), 'C_VARCHAR': '2', 'C_TIME_RENAMED': None,
'C_NEW_COLUMN': None},
{'C_INT': 3, 'C_PK': 3, 'C_TIME': datetime.time(23, 0, 3), 'C_VARCHAR': '3',
'C_TIME_RENAMED': datetime.time(8, 15), 'C_NEW_COLUMN': 'data 1'},
{'C_INT': 4, 'C_PK': 4, 'C_TIME': None, 'C_VARCHAR': '4', 'C_TIME_RENAMED': datetime.time(23, 0, 3),
'C_NEW_COLUMN': 'data 2'}
])
def test_column_name_change_without_table_cache(self):
"""Tests correct renaming of snowflake columns after source change with not using table caching"""
tap_lines_before_column_name_change = test_utils.get_test_tap_lines('messages-with-three-streams.json')
tap_lines_after_column_name_change = test_utils.get_test_tap_lines(
'messages-with-three-streams-modified-column.json')
# Load with default settings
self.persist_lines(tap_lines_before_column_name_change)
self.persist_lines(tap_lines_after_column_name_change)
# Get loaded rows from tables
snowflake = DbSync(self.config)
target_schema = self.config.get('default_target_schema', '')
table_one = snowflake.query("SELECT * FROM {}.test_table_one ORDER BY c_pk".format(target_schema))
table_two = snowflake.query("SELECT * FROM {}.test_table_two ORDER BY c_pk".format(target_schema))
table_three = snowflake.query("SELECT * FROM {}.test_table_three ORDER BY c_pk".format(target_schema))
# Get the previous column name from information schema in test_table_two
previous_column_name = snowflake.query("""
SELECT column_name
FROM information_schema.columns
WHERE table_catalog = '{}'
AND table_schema = '{}'
AND table_name = 'TEST_TABLE_TWO'
AND ordinal_position = 1
""".format(
self.config.get('dbname', '').upper(),
target_schema.upper()))[0]["COLUMN_NAME"]
# Table one should have no changes
self.assertEqual(
table_one,
[{'C_INT': 1, 'C_PK': 1, 'C_VARCHAR': '1'}])
# Table two should have a versioned column and a new column
self.assertEquals(
table_two,
[
{previous_column_name: datetime.datetime(2019, 2, 1, 15, 12, 45), 'C_INT': 1, 'C_PK': 1,
'C_VARCHAR': '1', 'C_DATE': None, 'C_NEW_COLUMN': None},
{previous_column_name: datetime.datetime(2019, 2, 10, 2), 'C_INT': 2, 'C_PK': 2, 'C_VARCHAR': '2',
'C_DATE': '2019-02-12 02:00:00', 'C_NEW_COLUMN': 'data 1'},
{previous_column_name: None, 'C_INT': 3, 'C_PK': 3, 'C_VARCHAR': '2', 'C_DATE': '2019-02-15 02:00:00',
'C_NEW_COLUMN': 'data 2'}
]
)
# Table three should have a renamed columns and a new column
self.assertEqual(
table_three,
[
{'C_INT': 1, 'C_PK': 1, 'C_TIME': datetime.time(4, 0), 'C_VARCHAR': '1', 'C_TIME_RENAMED': None,
'C_NEW_COLUMN': None},
{'C_INT': 2, 'C_PK': 2, 'C_TIME': datetime.time(7, 15), 'C_VARCHAR': '2', 'C_TIME_RENAMED': None,
'C_NEW_COLUMN': None},
{'C_INT': 3, 'C_PK': 3, 'C_TIME': datetime.time(23, 0, 3), 'C_VARCHAR': '3',
'C_TIME_RENAMED': datetime.time(8, 15), 'C_NEW_COLUMN': 'data 1'},
{'C_INT': 4, 'C_PK': 4, 'C_TIME': None, 'C_VARCHAR': '4', 'C_TIME_RENAMED': datetime.time(23, 0, 3),
'C_NEW_COLUMN': 'data 2'}
])
def test_logical_streams_from_pg_with_hard_delete_and_default_batch_size_should_pass(self):
"""Tests logical streams from pg with inserts, updates and deletes"""
tap_lines = test_utils.get_test_tap_lines('messages-pg-logical-streams.json')
# Turning on hard delete mode
self.config['hard_delete'] = True
self.persist_lines_with_cache(tap_lines)
self.assert_logical_streams_are_in_snowflake(True)
def test_logical_streams_from_pg_with_hard_delete_and_batch_size_of_5_should_pass(self):
"""Tests logical streams from pg with inserts, updates and deletes"""
tap_lines = test_utils.get_test_tap_lines('messages-pg-logical-streams.json')
# Turning on hard delete mode
self.config['hard_delete'] = True
self.config['batch_size_rows'] = 5
self.persist_lines_with_cache(tap_lines)
self.assert_logical_streams_are_in_snowflake(True)
def test_logical_streams_from_pg_with_hard_delete_and_batch_size_of_5_and_no_records_should_pass(self):
"""Tests logical streams from pg with inserts, updates and deletes"""
tap_lines = test_utils.get_test_tap_lines('messages-pg-logical-streams-no-records.json')
# Turning on hard delete mode
self.config['hard_delete'] = True
self.config['batch_size_rows'] = 5
self.persist_lines_with_cache(tap_lines)
self.assert_logical_streams_are_in_snowflake_and_are_empty()
@mock.patch('target_snowflake.emit_state')
def test_flush_streams_with_no_intermediate_flushes(self, mock_emit_state):
"""Test emitting states when no intermediate flush required"""
mock_emit_state.get.return_value = None
tap_lines = test_utils.get_test_tap_lines('messages-pg-logical-streams.json')
# Set batch size big enough to never has to flush in the middle
self.config['hard_delete'] = True
self.config['batch_size_rows'] = 1000
self.persist_lines_with_cache(tap_lines)
# State should be emitted only once with the latest received STATE message
self.assertEquals(
mock_emit_state.mock_calls,
[
mock.call({"currently_syncing": None, "bookmarks": {
"logical1-logical1_edgydata": {"last_replication_method": "LOG_BASED", "lsn": 108240872, "version": 1570922723596, "xmin": None},
"logical1-logical1_table1": {"last_replication_method": "LOG_BASED", "lsn": 108240872, "version": 1570922723618, "xmin": None},
"logical1-logical1_table2": {"last_replication_method": "LOG_BASED", "lsn": 108240872, "version": 1570922723635, "xmin": None},
"logical2-logical2_table1": {"last_replication_method": "LOG_BASED", "lsn": 108240872, "version": 1570922723651, "xmin": None},
"public-city": {"last_replication_method": "INCREMENTAL", "replication_key": "id", "version": 1570922723667, "replication_key_value": 4079},
"public-country": {"last_replication_method": "FULL_TABLE", "version": 1570922730456, "xmin": None},
"public2-wearehere": {}}})
])
# Every table should be loaded correctly
self.assert_logical_streams_are_in_snowflake(True)
@mock.patch('target_snowflake.emit_state')
def test_flush_streams_with_intermediate_flushes(self, mock_emit_state):
"""Test emitting states when intermediate flushes required"""
mock_emit_state.get.return_value = None
tap_lines = test_utils.get_test_tap_lines('messages-pg-logical-streams.json')
# Set batch size small enough to trigger multiple stream flushes
self.config['hard_delete'] = True
self.config['batch_size_rows'] = 10
self.persist_lines_with_cache(tap_lines)
# State should be emitted multiple times, updating the positions only in the stream which got flushed
self.assertEquals(
mock_emit_state.call_args_list,
[
# Flush #1 - Flushed edgydata until lsn: 108197216
mock.call({"currently_syncing": None, "bookmarks": {
"logical1-logical1_edgydata": {"last_replication_method": "LOG_BASED", "lsn": 108197216, "version": 1570922723596, "xmin": None},
"logical1-logical1_table1": {"last_replication_method": "LOG_BASED", "lsn": 108196176, "version": 1570922723618, "xmin": None},
"logical1-logical1_table2": {"last_replication_method": "LOG_BASED", "lsn": 108196176, "version": 1570922723635, "xmin": None},
"logical2-logical2_table1": {"last_replication_method": "LOG_BASED", "lsn": 108196176, "version": 1570922723651, "xmin": None},
"public-city": {"last_replication_method": "INCREMENTAL", "replication_key": "id", "version": 1570922723667, "replication_key_value": 4079},
"public-country": {"last_replication_method": "FULL_TABLE", "version": 1570922730456, "xmin": None},
"public2-wearehere": {}}}),
# Flush #2 - Flushed logical1-logical1_table2 until lsn: 108201336
mock.call({"currently_syncing": None, "bookmarks": {
"logical1-logical1_edgydata": {"last_replication_method": "LOG_BASED", "lsn": 108197216, "version": 1570922723596, "xmin": None},
"logical1-logical1_table1": {"last_replication_method": "LOG_BASED", "lsn": 108196176, "version": 1570922723618, "xmin": None},
"logical1-logical1_table2": {"last_replication_method": "LOG_BASED", "lsn": 108201336, "version": 1570922723635, "xmin": None},
"logical2-logical2_table1": {"last_replication_method": "LOG_BASED", "lsn": 108196176, "version": 1570922723651, "xmin": None},
"public-city": {"last_replication_method": "INCREMENTAL", "replication_key": "id", "version": 1570922723667, "replication_key_value": 4079},
"public-country": {"last_replication_method": "FULL_TABLE", "version": 1570922730456, "xmin": None},
"public2-wearehere": {}}}),
# Flush #3 - Flushed logical1-logical1_table2 until lsn: 108237600
mock.call({"currently_syncing": None, "bookmarks": {
"logical1-logical1_edgydata": {"last_replication_method": "LOG_BASED", "lsn": 108197216, "version": 1570922723596, "xmin": None},
"logical1-logical1_table1": {"last_replication_method": "LOG_BASED", "lsn": 108196176, "version": 1570922723618, "xmin": None},
"logical1-logical1_table2": {"last_replication_method": "LOG_BASED", "lsn": 108237600, "version": 1570922723635, "xmin": None},
"logical2-logical2_table1": {"last_replication_method": "LOG_BASED", "lsn": 108196176, "version": 1570922723651, "xmin": None},
"public-city": {"last_replication_method": "INCREMENTAL", "replication_key": "id", "version": 1570922723667, "replication_key_value": 4079},
"public-country": {"last_replication_method": "FULL_TABLE", "version": 1570922730456, "xmin": None},
"public2-wearehere": {}}}),
# Flush #4 - Flushed logical1-logical1_table2 until lsn: 108238768
mock.call({"currently_syncing": None, "bookmarks": {
"logical1-logical1_edgydata": {"last_replication_method": "LOG_BASED", "lsn": 108197216, "version": 1570922723596, "xmin": None},
"logical1-logical1_table1": {"last_replication_method": "LOG_BASED", "lsn": 108196176, "version": 1570922723618, "xmin": None},
"logical1-logical1_table2": {"last_replication_method": "LOG_BASED", "lsn": 108238768, "version": 1570922723635, "xmin": None},
"logical2-logical2_table1": {"last_replication_method": "LOG_BASED", "lsn": 108196176, "version": 1570922723651, "xmin": None},
"public-city": {"last_replication_method": "INCREMENTAL", "replication_key": "id", "version": 1570922723667, "replication_key_value": 4079},
"public-country": {"last_replication_method": "FULL_TABLE", "version": 1570922730456, "xmin": None},
"public2-wearehere": {}}}),
# Flush #5 - Flushed logical1-logical1_table2 until lsn: 108239704,
mock.call({"currently_syncing": None, "bookmarks": {
"logical1-logical1_edgydata": {"last_replication_method": "LOG_BASED", "lsn": 108197216, "version": 1570922723596, "xmin": None},
"logical1-logical1_table1": {"last_replication_method": "LOG_BASED", "lsn": 108196176, "version": 1570922723618, "xmin": None},
"logical1-logical1_table2": {"last_replication_method": "LOG_BASED", "lsn": 108239896, "version": 1570922723635, "xmin": None},
"logical2-logical2_table1": {"last_replication_method": "LOG_BASED", "lsn": 108196176, "version": 1570922723651, "xmin": None},
"public-city": {"last_replication_method": "INCREMENTAL", "replication_key": "id", "version": 1570922723667, "replication_key_value": 4079},
"public-country": {"last_replication_method": "FULL_TABLE", "version": 1570922730456, "xmin": None},
"public2-wearehere": {}}}),
# Flush #6 - Last flush, update every stream lsn: 108240872,
mock.call({"currently_syncing": None, "bookmarks": {
"logical1-logical1_edgydata": {"last_replication_method": "LOG_BASED", "lsn": 108240872, "version": 1570922723596, "xmin": None},
"logical1-logical1_table1": {"last_replication_method": "LOG_BASED", "lsn": 108240872, "version": 1570922723618, "xmin": None},
"logical1-logical1_table2": {"last_replication_method": "LOG_BASED", "lsn": 108240872, "version": 1570922723635, "xmin": None},
"logical2-logical2_table1": {"last_replication_method": "LOG_BASED", "lsn": 108240872, "version": 1570922723651, "xmin": None},
"public-city": {"last_replication_method": "INCREMENTAL", "replication_key": "id", "version": 1570922723667, "replication_key_value": 4079},
"public-country": {"last_replication_method": "FULL_TABLE", "version": 1570922730456, "xmin": None},
"public2-wearehere": {}}}),
])
# Every table should be loaded correctly
self.assert_logical_streams_are_in_snowflake(True)
@mock.patch('target_snowflake.emit_state')
def test_flush_streams_with_intermediate_flushes_on_all_streams(self, mock_emit_state):
"""Test emitting states when intermediate flushes required and flush_all_streams is enabled"""
mock_emit_state.get.return_value = None
tap_lines = test_utils.get_test_tap_lines('messages-pg-logical-streams.json')
# Set batch size small enough to trigger multiple stream flushes
self.config['hard_delete'] = True
self.config['batch_size_rows'] = 10
self.config['flush_all_streams'] = True
self.persist_lines_with_cache(tap_lines)
# State should be emitted 6 times, flushing every stream and updating every stream position
self.assertEquals(
mock_emit_state.call_args_list,
[
# Flush #1 - Flush every stream until lsn: 108197216
mock.call({"currently_syncing": None, "bookmarks": {
"logical1-logical1_edgydata": {"last_replication_method": "LOG_BASED", "lsn": 108197216, "version": 1570922723596, "xmin": None},
"logical1-logical1_table1": {"last_replication_method": "LOG_BASED", "lsn": 108197216, "version": 1570922723618, "xmin": None},
"logical1-logical1_table2": {"last_replication_method": "LOG_BASED", "lsn": 108197216, "version": 1570922723635, "xmin": None},
"logical2-logical2_table1": {"last_replication_method": "LOG_BASED", "lsn": 108197216, "version": 1570922723651, "xmin": None},
"public-city": {"last_replication_method": "INCREMENTAL", "replication_key": "id", "version": 1570922723667, "replication_key_value": 4079},
"public-country": {"last_replication_method": "FULL_TABLE", "version": 1570922730456, "xmin": None},
"public2-wearehere": {}}}),
# Flush #2 - Flush every stream until lsn 108201336
mock.call({'currently_syncing': None, 'bookmarks': {
"logical1-logical1_edgydata": {"last_replication_method": "LOG_BASED", "lsn": 108201336, "version": 1570922723596, "xmin": None},
"logical1-logical1_table1": {"last_replication_method": "LOG_BASED", "lsn": 108201336, "version": 1570922723618, "xmin": None},
"logical1-logical1_table2": {"last_replication_method": "LOG_BASED", "lsn": 108201336, "version": 1570922723635, "xmin": None},
"logical2-logical2_table1": {"last_replication_method": "LOG_BASED", "lsn": 108201336, "version": 1570922723651, "xmin": None},
"public-city": {"last_replication_method": "INCREMENTAL", "replication_key": "id", "version": 1570922723667, "replication_key_value": 4079},
"public-country": {"last_replication_method": "FULL_TABLE", "version": 1570922730456, "xmin": None},
"public2-wearehere": {}}}),
# Flush #3 - Flush every stream until lsn: 108237600
mock.call({'currently_syncing': None, 'bookmarks': {
"logical1-logical1_edgydata": {"last_replication_method": "LOG_BASED", "lsn": 108237600, "version": 1570922723596, "xmin": None},
"logical1-logical1_table1": {"last_replication_method": "LOG_BASED", "lsn": 108237600, "version": 1570922723618, "xmin": None},
"logical1-logical1_table2": {"last_replication_method": "LOG_BASED", "lsn": 108237600, "version": 1570922723635, "xmin": None},
"logical2-logical2_table1": {"last_replication_method": "LOG_BASED", "lsn": 108237600, "version": 1570922723651, "xmin": None},
"public-city": {"last_replication_method": "INCREMENTAL", "replication_key": "id", "version": 1570922723667, "replication_key_value": 4079},
"public-country": {"last_replication_method": "FULL_TABLE", "version": 1570922730456, "xmin": None},
"public2-wearehere": {}}}),
# Flush #4 - Flush every stream until lsn: 108238768
mock.call({'currently_syncing': None, 'bookmarks': {
"logical1-logical1_edgydata": {"last_replication_method": "LOG_BASED", "lsn": 108238768, "version": 1570922723596, "xmin": None},
"logical1-logical1_table1": {"last_replication_method": "LOG_BASED", "lsn": 108238768, "version": 1570922723618, "xmin": None},
"logical1-logical1_table2": {"last_replication_method": "LOG_BASED", "lsn": 108238768, "version": 1570922723635, "xmin": None},
"logical2-logical2_table1": {"last_replication_method": "LOG_BASED", "lsn": 108238768, "version": 1570922723651, "xmin": None},
"public-city": {"last_replication_method": "INCREMENTAL", "replication_key": "id", "version": 1570922723667, "replication_key_value": 4079},
"public-country": {"last_replication_method": "FULL_TABLE", "version": 1570922730456, "xmin": None},
"public2-wearehere": {}}}),
# Flush #5 - Flush every stream until lsn: 108239704,
mock.call({'currently_syncing': None, 'bookmarks': {
"logical1-logical1_edgydata": {"last_replication_method": "LOG_BASED", "lsn": 108239896, "version": 1570922723596, "xmin": None},
"logical1-logical1_table1": {"last_replication_method": "LOG_BASED", "lsn": 108239896, "version": 1570922723618, "xmin": None},
"logical1-logical1_table2": {"last_replication_method": "LOG_BASED", "lsn": 108239896, "version": 1570922723635, "xmin": None},
"logical2-logical2_table1": {"last_replication_method": "LOG_BASED", "lsn": 108239896, "version": 1570922723651, "xmin": None},
"public-city": {"last_replication_method": "INCREMENTAL", "replication_key": "id", "version": 1570922723667, "replication_key_value": 4079},
"public-country": {"last_replication_method": "FULL_TABLE", "version": 1570922730456, "xmin": None},
"public2-wearehere": {}}}),
# Flush #6 - Last flush, update every stream until lsn: 108240872,
mock.call({'currently_syncing': None, 'bookmarks': {
"logical1-logical1_edgydata": {"last_replication_method": "LOG_BASED", "lsn": 108240872, "version": 1570922723596, "xmin": None},
"logical1-logical1_table1": {"last_replication_method": "LOG_BASED", "lsn": 108240872, "version": 1570922723618, "xmin": None},
"logical1-logical1_table2": {"last_replication_method": "LOG_BASED", "lsn": 108240872, "version": 1570922723635, "xmin": None},
"logical2-logical2_table1": {"last_replication_method": "LOG_BASED", "lsn": 108240872, "version": 1570922723651, "xmin": None},
"public-city": {"last_replication_method": "INCREMENTAL", "replication_key": "id", "version": 1570922723667, "replication_key_value": 4079},
"public-country": {"last_replication_method": "FULL_TABLE", "version": 1570922730456, "xmin": None},
"public2-wearehere": {}}}),
])
# Every table should be loaded correctly
self.assert_logical_streams_are_in_snowflake(True)
def test_record_validation(self):
"""Test validating records"""
tap_lines = test_utils.get_test_tap_lines('messages-with-invalid-records.json')
# Loading invalid records when record validation enabled should fail at ...
self.config['validate_records'] = True
with assert_raises(RecordValidationException):
self.persist_lines_with_cache(tap_lines)
# Loading invalid records when record validation disabled should fail at load time
self.config['validate_records'] = False
with assert_raises(ProgrammingError):
self.persist_lines_with_cache(tap_lines)
def test_pg_records_validation(self):
"""Test validating records from postgres tap"""
tap_lines_invalid_records = test_utils.get_test_tap_lines('messages-pg-with-invalid-records.json')
# Loading invalid records when record validation enabled should fail at ...
self.config['validate_records'] = True
with assert_raises(RecordValidationException):
self.persist_lines_with_cache(tap_lines_invalid_records)
# Loading invalid records when record validation disabled, should pass without any exceptions
self.config['validate_records'] = False
self.persist_lines_with_cache(tap_lines_invalid_records)
# Valid records should pass for both with and without validation
tap_lines_valid_records = test_utils.get_test_tap_lines('messages-pg-with-valid-records.json')
self.config['validate_records'] = True
self.persist_lines_with_cache(tap_lines_valid_records)
def test_loading_tables_with_custom_temp_dir(self):
"""Loading multiple tables from the same input tap using custom temp directory"""
tap_lines = test_utils.get_test_tap_lines('messages-with-three-streams.json')
# Turning on client-side encryption and load
self.config['temp_dir'] = ('~/.pipelinewise/tmp')
self.persist_lines_with_cache(tap_lines)
self.assert_three_streams_are_into_snowflake()
def test_using_aws_environment_variables(self):
"""Test loading data with aws in the environment rather than explicitly provided access keys"""
tap_lines = test_utils.get_test_tap_lines("messages-with-three-streams.json")
try:
os.environ["AWS_ACCESS_KEY_ID"] = os.environ.get(
"TARGET_SNOWFLAKE_AWS_ACCESS_KEY"
)
os.environ["AWS_SECRET_ACCESS_KEY"] = os.environ.get(
"TARGET_SNOWFLAKE_AWS_SECRET_ACCESS_KEY"
)
self.config["aws_access_key_id"] = None
self.config["aws_secret_access_key"] = None
target_snowflake.persist_lines(self.config, tap_lines)
finally:
del os.environ["AWS_ACCESS_KEY_ID"]
del os.environ["AWS_SECRET_ACCESS_KEY"]
def test_too_many_records_exception(self):
"""Test if query function raise exception if max_records exceeded"""
snowflake = DbSync(self.config)
# No max_record limit by default
sample_rows = snowflake.query("SELECT seq4() FROM TABLE(GENERATOR(ROWCOUNT => 50000))")
self.assertEqual(len(sample_rows), 50000)
# Should raise exception when max_records exceeded
with assert_raises(target_snowflake.db_sync.TooManyRecordsException):
snowflake.query("SELECT seq4() FROM TABLE(GENERATOR(ROWCOUNT => 50000))", max_records=10000)
def test_loading_tables_with_no_compression(self):
"""Loading multiple tables with compression turned off"""
tap_lines = test_utils.get_test_tap_lines('messages-with-three-streams.json')
# Turning off client-side encryption and load
self.config['no_compression'] = True
self.persist_lines_with_cache(tap_lines)
self.assert_three_streams_are_into_snowflake()