-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathtest_target_sqlite.py
604 lines (536 loc) · 17.8 KB
/
test_target_sqlite.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
"""Typing tests."""
from __future__ import annotations
import json
import sqlite3
import typing as t
from copy import deepcopy
from io import StringIO
from pathlib import Path
from textwrap import dedent
from uuid import uuid4
import pytest
import sqlalchemy as sa
from samples.sample_tap_hostile import SampleTapHostile
from samples.sample_tap_sqlite import SQLiteTap
from samples.sample_target_sqlite import SQLiteSink, SQLiteTarget
from singer_sdk import typing as th
from singer_sdk.testing import (
tap_sync_test,
tap_to_target_sync_test,
target_sync_test,
)
if t.TYPE_CHECKING:
from singer_sdk._singerlib import Catalog
from singer_sdk.tap_base import SQLTap
from singer_sdk.target_base import SQLTarget
@pytest.fixture
def path_to_target_db(tmp_path: Path) -> Path:
return Path(f"{tmp_path}/target_test.db")
@pytest.fixture
def sqlite_target_test_config(path_to_target_db: Path) -> dict:
"""Get configuration dictionary for target-csv."""
return {"path_to_db": str(path_to_target_db)}
@pytest.fixture
def sqlite_sample_target(sqlite_target_test_config):
"""Get a sample target object."""
return SQLiteTarget(config=sqlite_target_test_config)
@pytest.fixture
def sqlite_sample_target_hard_delete(sqlite_target_test_config):
"""Get a sample target object with hard_delete disabled."""
return SQLiteTarget(config={**sqlite_target_test_config, "hard_delete": True})
@pytest.fixture
def sqlite_sample_target_batch(sqlite_target_test_config):
"""Get a sample target object with hard_delete disabled."""
conf = sqlite_target_test_config
return SQLiteTarget(config=conf)
# SQLite Target Tests
def test_sync_sqlite_to_sqlite(
sqlite_sample_tap: SQLTap,
sqlite_sample_target: SQLTarget,
sqlite_sample_db_catalog: Catalog,
):
"""End-to-end-to-end test for SQLite tap and target.
Test performs the following actions:
- Extract sample data from SQLite tap.
- Load data to SQLite target.
- Extract data again from the target DB using the SQLite tap.
- Confirm the STDOUT from the original sample DB matches with the
STDOUT from the re-tapped target DB.
"""
orig_stdout, _, _, _ = tap_to_target_sync_test(
sqlite_sample_tap,
sqlite_sample_target,
)
orig_stdout.seek(0)
tapped_config = dict(sqlite_sample_target.config)
tapped_target = SQLiteTap(
config=tapped_config,
catalog=sqlite_sample_db_catalog.to_dict(),
)
new_stdout, _ = tap_sync_test(tapped_target)
orig_stdout.seek(0)
orig_lines = orig_stdout.readlines()
new_lines = new_stdout.readlines()
assert len(orig_lines) > 0, "Orig tap output should not be empty."
assert len(new_lines) > 0, "(Re-)tapped target output should not be empty."
assert len(orig_lines) == len(new_lines)
line_num = 0
for line_num, orig_out, new_out in zip(
range(len(orig_lines)),
orig_lines,
new_lines,
):
try:
orig_json = json.loads(orig_out)
except json.JSONDecodeError as e:
msg = f"Could not parse JSON in orig line {line_num}: {orig_out}"
raise RuntimeError(msg) from e
try:
tapped_json = json.loads(new_out)
except json.JSONDecodeError as e:
msg = f"Could not parse JSON in new line {line_num}: {new_out}"
raise RuntimeError(msg) from e
assert (
tapped_json["type"] == orig_json["type"]
), f"Mismatched message type on line {line_num}."
if tapped_json["type"] == "SCHEMA":
assert (
tapped_json["schema"]["properties"].keys()
== orig_json["schema"]["properties"].keys()
)
if tapped_json["type"] == "RECORD":
assert tapped_json["stream"] == orig_json["stream"]
assert tapped_json["record"] == orig_json["record"]
assert line_num > 0, "No lines read."
def test_sqlite_schema_addition(sqlite_sample_target: SQLTarget):
"""Test that SQL-based targets attempt to create new schema.
It should attempt to create a schema if one is included in stream name,
e.g. "schema_name-table_name".
"""
schema_name = f"test_schema_{str(uuid4()).split('-')[-1]}"
table_name = f"zzz_tmp_{str(uuid4()).split('-')[-1]}"
test_stream_name = f"{schema_name}-{table_name}"
schema_message = {
"type": "SCHEMA",
"stream": test_stream_name,
"schema": {
"type": "object",
"properties": {"col_a": th.StringType().to_dict()},
},
}
tap_output = "\n".join(
json.dumps(msg)
for msg in [
schema_message,
{
"type": "RECORD",
"stream": test_stream_name,
"record": {"col_a": "samplerow1"},
},
]
)
# sqlite doesn't support schema creation
with pytest.raises(sa.exc.OperationalError) as excinfo:
target_sync_test(
sqlite_sample_target,
input=StringIO(tap_output),
finalize=True,
)
# check the target at least tried to create the schema
assert excinfo.value.statement == f"CREATE SCHEMA {schema_name}"
def test_sqlite_column_addition(sqlite_sample_target: SQLTarget):
"""End-to-end-to-end test for SQLite tap and target.
Test performs the following actions:
- Load a dataset with 1 column.
- Load a dataset with 2 columns.
"""
test_tbl = f"zzz_tmp_{str(uuid4()).split('-')[-1]}"
props_a: dict[str, dict] = {"col_a": th.StringType().to_dict()}
props_b = deepcopy(props_a)
props_b["col_b"] = th.IntegerType().to_dict()
schema_msg_a, schema_msg_b = (
{
"type": "SCHEMA",
"stream": test_tbl,
"schema": {
"type": "object",
"properties": props,
},
}
for props in [props_a, props_b]
)
tap_output_a = "\n".join(
json.dumps(msg)
for msg in [
schema_msg_a,
{"type": "RECORD", "stream": test_tbl, "record": {"col_a": "samplerow1"}},
]
)
tap_output_b = "\n".join(
json.dumps(msg)
for msg in [
schema_msg_b,
{
"type": "RECORD",
"stream": test_tbl,
"record": {"col_a": "samplerow2", "col_b": 2},
},
]
)
target_sync_test(sqlite_sample_target, input=StringIO(tap_output_a), finalize=True)
target_sync_test(sqlite_sample_target, input=StringIO(tap_output_b), finalize=True)
def test_sqlite_activate_version(
sqlite_sample_target: SQLTarget,
sqlite_sample_target_hard_delete: SQLTarget,
):
"""Test handling the activate_version message for the SQLite target.
Test performs the following actions:
- Sends an activate_version message for a table that doesn't exist (which should
have no effect)
"""
test_tbl = f"zzz_tmp_{str(uuid4()).split('-')[-1]}"
schema_msg = {
"type": "SCHEMA",
"stream": test_tbl,
"schema": th.PropertiesList(th.Property("col_a", th.StringType())).to_dict(),
}
tap_output = "\n".join(
json.dumps(msg)
for msg in [
schema_msg,
{"type": "ACTIVATE_VERSION", "stream": test_tbl, "version": 12345},
{
"type": "RECORD",
"stream": test_tbl,
"record": {"col_a": "samplerow1"},
"version": 12345,
},
]
)
target_sync_test(sqlite_sample_target, input=StringIO(tap_output), finalize=True)
target_sync_test(
sqlite_sample_target_hard_delete,
input=StringIO(tap_output),
finalize=True,
)
def test_sqlite_column_morph(sqlite_sample_target: SQLTarget):
"""End-to-end-to-end test for SQLite tap and target.
Test performs the following actions:
- Load a column as an int.
- Send a new column definition to redefine as string.
- Ensure redefinition raises NotImplementedError, since column ALTERs are not
supported by SQLite.
"""
test_tbl = f"zzz_tmp_{str(uuid4()).split('-')[-1]}"
props_a: dict[str, dict] = {"col_a": th.IntegerType().to_dict()}
props_b: dict[str, dict] = {"col_a": th.StringType().to_dict()}
schema_msg_a, schema_msg_b = (
{
"type": "SCHEMA",
"stream": test_tbl,
"schema": {
"type": "object",
"properties": props,
},
}
for props in [props_a, props_b]
)
tap_output_a = "\n".join(
json.dumps(msg)
for msg in [
schema_msg_a,
{"type": "RECORD", "stream": test_tbl, "record": {"col_a": 123}},
]
)
tap_output_b = "\n".join(
json.dumps(msg)
for msg in [
schema_msg_b,
{
"type": "RECORD",
"stream": test_tbl,
"record": {"col_a": "row-number-2"},
},
]
)
target_sync_test(sqlite_sample_target, input=StringIO(tap_output_a), finalize=True)
with pytest.raises(NotImplementedError):
# SQLite does not support altering column types.
target_sync_test(
sqlite_sample_target,
input=StringIO(tap_output_b),
finalize=True,
)
def test_sqlite_process_batch_message(
sqlite_target_test_config: dict,
sqlite_sample_target_batch: SQLiteTarget,
):
"""Test handling the batch message for the SQLite target.
Test performs the following actions:
- Sends a batch message for a table that doesn't exist (which should
have no effect)
"""
schema_message = {
"type": "SCHEMA",
"stream": "users",
"key_properties": ["id"],
"schema": {
"required": ["id"],
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": ["null", "string"]},
},
},
}
batch_message = {
"type": "BATCH",
"stream": "users",
"encoding": {"format": "jsonl", "compression": "gzip"},
"manifest": [
"file://tests/core/resources/batch.1.jsonl.gz",
"file://tests/core/resources/batch.2.jsonl.gz",
],
}
tap_output = "\n".join([json.dumps(schema_message), json.dumps(batch_message)])
target_sync_test(
sqlite_sample_target_batch,
input=StringIO(tap_output),
finalize=True,
)
db = sqlite3.connect(sqlite_target_test_config["path_to_db"])
cursor = db.cursor()
cursor.execute("SELECT COUNT(*) as count FROM users")
assert cursor.fetchone()[0] == 4
def test_sqlite_process_batch_parquet(
sqlite_target_test_config: dict,
sqlite_sample_target_batch: SQLiteTarget,
):
"""Test handling a Parquet batch message for the SQLite target."""
config = {
**sqlite_target_test_config,
"batch_config": {
"encoding": {"format": "parquet", "compression": "gzip"},
"batch_size": 100,
},
}
schema_message = {
"type": "SCHEMA",
"stream": "continents",
"key_properties": ["id"],
"schema": {
"required": ["id"],
"type": "object",
"properties": {
"code": {"type": "string"},
"name": {"type": "string"},
},
},
}
batch_message = {
"type": "BATCH",
"stream": "continents",
"encoding": {"format": "parquet", "compression": "gzip"},
"manifest": [
"file://tests/core/resources/continents.parquet.gz",
],
}
tap_output = "\n".join([json.dumps(schema_message), json.dumps(batch_message)])
target_sync_test(
sqlite_sample_target_batch,
input=StringIO(tap_output),
finalize=True,
)
db = sqlite3.connect(config["path_to_db"])
cursor = db.cursor()
cursor.execute("SELECT COUNT(*) as count FROM continents")
assert cursor.fetchone()[0] == 7
def test_sqlite_column_no_morph(sqlite_sample_target: SQLTarget):
"""End-to-end-to-end test for SQLite tap and target.
Test performs the following actions:
- Load a column as a string.
- Send a new column definition to redefine as int.
- Ensure int value can still insert.
"""
test_tbl = f"zzz_tmp_{str(uuid4()).split('-')[-1]}"
props_a: dict[str, dict] = {"col_a": th.StringType().to_dict()}
props_b: dict[str, dict] = {"col_a": th.IntegerType().to_dict()}
schema_msg_a, schema_msg_b = (
{
"type": "SCHEMA",
"stream": test_tbl,
"schema": {
"type": "object",
"properties": props,
},
}
for props in [props_a, props_b]
)
tap_output_a = "\n".join(
json.dumps(msg)
for msg in [
schema_msg_a,
{"type": "RECORD", "stream": test_tbl, "record": {"col_a": "123"}},
]
)
tap_output_b = "\n".join(
json.dumps(msg)
for msg in [
schema_msg_b,
{
"type": "RECORD",
"stream": test_tbl,
"record": {"col_a": 456},
},
]
)
target_sync_test(sqlite_sample_target, input=StringIO(tap_output_a), finalize=True)
# Int should be inserted as string.
target_sync_test(sqlite_sample_target, input=StringIO(tap_output_b), finalize=True)
def test_record_with_missing_properties(
sqlite_sample_target: SQLTarget,
):
"""Test handling of records with missing properties."""
tap_output = "\n".join(
json.dumps(msg)
for msg in [
{
"type": "SCHEMA",
"stream": "test_stream",
"schema": {
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
},
},
"key_properties": ["id"],
},
{
"type": "RECORD",
"stream": "test_stream",
"record": {"id": 1},
},
]
)
target_sync_test(sqlite_sample_target, input=StringIO(tap_output), finalize=True)
@pytest.mark.parametrize(
"stream_name,schema,key_properties,expected_dml",
[
(
"test_stream",
{
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
},
},
[],
dedent(
"""\
INSERT INTO test_stream
(id, name)
VALUES (:id, :name)""",
),
),
],
ids=[
"no_key_properties",
],
)
def test_sqlite_generate_insert_statement(
sqlite_sample_target: SQLiteTarget,
stream_name: str,
schema: dict,
key_properties: list,
expected_dml: str,
):
sink = SQLiteSink(
sqlite_sample_target,
stream_name=stream_name,
schema=schema,
key_properties=key_properties,
)
dml = sink.generate_insert_statement(
sink.full_table_name,
sink.schema,
)
assert dml == expected_dml
def test_hostile_to_sqlite(
sqlite_sample_target: SQLTarget,
sqlite_target_test_config: dict,
):
tap = SampleTapHostile()
tap_to_target_sync_test(tap, sqlite_sample_target)
# check if stream table was created
db = sqlite3.connect(sqlite_target_test_config["path_to_db"])
cursor = db.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = [res[0] for res in cursor.fetchall()]
assert "hostile_property_names_stream" in tables
# check if columns were conformed
cursor.execute(
dedent(
"""
SELECT
p.name as columnName
FROM sqlite_master m
left outer join pragma_table_info((m.name)) p
on m.name <> p.name
where m.name = 'hostile_property_names_stream'
;
""",
),
)
columns = {res[0] for res in cursor.fetchall()}
assert columns == {
"name_with_spaces",
"nameiscamelcase",
"name_with_dashes",
"name_with_dashes_and_mixed_cases",
"gname_starts_with_number",
"fname_starts_with_number",
"hname_starts_with_number",
"name_with_emoji_",
}
def test_overwrite_load_method(
sqlite_target_test_config: dict,
):
sqlite_target_test_config["load_method"] = "overwrite"
target = SQLiteTarget(config=sqlite_target_test_config)
test_tbl = f"zzz_tmp_{str(uuid4()).split('-')[-1]}"
schema_msg = {
"type": "SCHEMA",
"stream": test_tbl,
"schema": {
"type": "object",
"properties": {"col_a": th.StringType().to_dict()},
},
}
tap_output_a = "\n".join(
json.dumps(msg)
for msg in [
schema_msg,
{"type": "RECORD", "stream": test_tbl, "record": {"col_a": "123"}},
]
)
# Assert
db = sqlite3.connect(sqlite_target_test_config["path_to_db"])
cursor = db.cursor()
target_sync_test(target, input=StringIO(tap_output_a), finalize=True)
cursor.execute(f"SELECT col_a FROM {test_tbl} ;") # noqa: S608
records = [res[0] for res in cursor.fetchall()]
assert records == ["123"]
tap_output_b = "\n".join(
json.dumps(msg)
for msg in [
schema_msg,
{"type": "RECORD", "stream": test_tbl, "record": {"col_a": "456"}},
]
)
target = SQLiteTarget(config=sqlite_target_test_config)
target_sync_test(target, input=StringIO(tap_output_b), finalize=True)
cursor.execute(f"SELECT col_a FROM {test_tbl} ;") # noqa: S608
records = [res[0] for res in cursor.fetchall()]
assert records == ["456"]