-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_target_sqlite.py
487 lines (410 loc) · 17 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
import pytest
import os
from jsonschema import ValidationError
from sqlalchemy import create_engine, inspect, text
from target_sqlite.target_sqlite import TargetSQLite
def load_stream(filename):
myDir = os.path.dirname(os.path.abspath(__file__))
stream = os.path.join(myDir, "data_files", filename)
with open(stream) as f:
return [line for line in f]
@pytest.fixture(scope="class")
def sqlite_engine(config):
db_file = f'{config["database"]}.db'
try:
yield create_engine(f"sqlite:///{db_file}", future=True)
finally:
os.remove(db_file)
class TestTargetSQLite:
def test_record_before_schema(self, config):
test_stream = "record_before_schema.stream"
target = TargetSQLite(config)
stream = load_stream(test_stream)
with pytest.raises(Exception) as excinfo:
for line in stream:
target.process_line(line)
assert "encountered before a corresponding schema" in str(excinfo.value)
def test_invalid_schema(self, config):
test_stream = "invalid_schema.stream"
target = TargetSQLite(config)
stream = load_stream(test_stream)
with pytest.raises(ValidationError) as excinfo:
for line in stream:
target.process_line(line)
assert "Not supported schema" in str(excinfo.value)
def test_record_missing_key_property(self, config, sqlite_engine):
test_stream = "record_missing_key_property.stream"
target = TargetSQLite(config)
stream = load_stream(test_stream)
with pytest.raises(ValidationError) as excinfo:
for line in stream:
target.process_line(line)
assert "id" in str(excinfo.value)
# Drop the Test Tables
for stream, loader in target.loaders.items():
loader.table.drop(loader.engine)
def test_record_missing_required_property(self, config, sqlite_engine):
test_stream = "record_missing_required_property.stream"
target = TargetSQLite(config)
stream = load_stream(test_stream)
with pytest.raises(ValidationError) as excinfo:
for line in stream:
target.process_line(line)
assert "'id' is a required property" in str(excinfo.value)
# Drop the Test Tables
for stream, loader in target.loaders.items():
loader.table.drop(loader.engine)
@pytest.mark.slow
def test_camelcase(self, config, sqlite_engine):
# The expected results to compare
expected_results = {
"state": None,
"tables": ["test_camelcase"],
"columns": {
"test_camelcase": ["id", "client_name", config["timestamp_column"]]
},
"total_records": {"test_camelcase": 2},
}
test_stream = "camelcase.stream"
self.integration_test(
config, sqlite_engine, expected_results, test_stream, drop_schema=False
)
# We also need to test that the record has data in the camelcased field
with sqlite_engine.begin() as connection:
item_query = text("SELECT client_name FROM test_camelcase")
item_result = connection.execute(item_query).fetchone()
assert item_result[0] == "Gitter Windows Desktop App"
@pytest.mark.slow
def test_special_chars_in_attributes(self, config, sqlite_engine):
# The expected results to compare
expected_results = {
"state": None,
"tables": ["test_special_chars_in_attributes"],
"columns": {
"test_special_chars_in_attributes": [
"_id",
"d__env",
"d__agent_type",
"d__agent_os_version",
config["timestamp_column"],
]
},
"total_records": {"test_special_chars_in_attributes": 1},
}
test_stream = "special_chars_in_attributes.stream"
self.integration_test(config, sqlite_engine, expected_results, test_stream)
@pytest.mark.slow
def test_optional_attributes(self, config, sqlite_engine):
# The expected results to compare
expected_results = {
"state": {"test_optional_attributes": 4},
"tables": ["test_optional_attributes"],
"columns": {
"test_optional_attributes": [
"id",
"optional",
config["timestamp_column"],
]
},
"total_records": {"test_optional_attributes": 4},
}
test_stream = "optional_attributes.stream"
self.integration_test(config, sqlite_engine, expected_results, test_stream)
@pytest.mark.slow
def test_schema_no_properties(self, config, sqlite_engine):
# The expected results to compare
expected_results = {
"state": None,
"tables": [
"test_object_schema_with_properties",
"test_object_schema_no_properties",
],
"columns": {
"test_object_schema_with_properties": [
"object_store__id",
"object_store__metric",
config["timestamp_column"],
],
"test_object_schema_no_properties": [
"object_store",
config["timestamp_column"],
],
},
"total_records": {
"test_object_schema_with_properties": 2,
"test_object_schema_no_properties": 2,
},
}
test_stream = "schema_no_properties.stream"
self.integration_test(
config, sqlite_engine, expected_results, test_stream, drop_schema=False
)
# We also need to test that the proper data records were stored
with sqlite_engine.connect() as connection:
query = text(
"SELECT COUNT(*) "
" FROM test_object_schema_with_properties "
" WHERE object_store__id = 1 AND object_store__metric = 187"
)
result = connection.execute(query).fetchone()
assert result[0] == 1
query = text("""
SELECT COUNT(*)
FROM test_object_schema_no_properties
WHERE object_store = '{"id": 1, "metric": 1}'
""")
result = connection.execute(query).fetchone()
assert result[0] == 1
# Drop the Test Tables
with sqlite_engine.begin() as connection:
connection.execute(text("DROP TABLE test_object_schema_with_properties"))
connection.execute(text("DROP TABLE test_object_schema_no_properties"))
@pytest.mark.slow
def test_schema_updates(self, config, sqlite_engine):
# The expected results to compare
expected_results = {
"state": {"test_schema_updates": 6},
"tables": ["test_schema_updates"],
"columns": {
"test_schema_updates": [
"id",
"a1",
"a2",
"a3",
"a4__id",
"a4__value",
"a5",
"a6",
config["timestamp_column"],
]
},
"total_records": {"test_schema_updates": 6},
}
test_stream = "schema_updates.stream"
self.integration_test(config, sqlite_engine, expected_results, test_stream)
@pytest.mark.slow
def test_multiple_state_messages(self, capsys, config, sqlite_engine):
# The expected results to compare
expected_results = {
"state": {
"test_multiple_state_messages_a": 5,
"test_multiple_state_messages_b": 6,
},
"tables": [
"test_multiple_state_messages_a",
"test_multiple_state_messages_b",
],
"columns": {
"test_multiple_state_messages_a": [
"id",
"metric",
config["timestamp_column"],
],
"test_multiple_state_messages_b": [
"id",
"metric",
config["timestamp_column"],
],
},
"total_records": {
"test_multiple_state_messages_a": 6,
"test_multiple_state_messages_b": 6,
},
}
test_stream = "multiple_state_messages.stream"
updated_config = {**config, "batch_size": 3}
self.integration_test(
updated_config, sqlite_engine, expected_results, test_stream
)
# Check that the expected State messages where flushed
expected_stdout = [
'{"test_multiple_state_messages_a": 1, "test_multiple_state_messages_b": 0}',
'{"test_multiple_state_messages_a": 3, "test_multiple_state_messages_b": 2}',
'{"test_multiple_state_messages_a": 5, "test_multiple_state_messages_b": 6}',
"",
]
captured = capsys.readouterr()
assert captured.out == "\n".join(expected_stdout)
@pytest.mark.slow
def test_relational_data(self, config, sqlite_engine):
# Start with a simple initial insert for everything
# The expected results to compare
expected_results = {
"state": {"test_users": 5, "test_locations": 3, "test_user_in_location": 3},
"tables": ["test_users", "test_locations", "test_user_in_location"],
"columns": {
"test_users": ["id", "name", config["timestamp_column"]],
"test_locations": ["id", "name", config["timestamp_column"]],
"test_user_in_location": [
"id",
"user_id",
"location_id",
"info__weather",
"info__mood",
config["timestamp_column"],
],
},
"total_records": {
"test_users": 5,
"test_locations": 3,
"test_user_in_location": 3,
},
}
test_stream = "user_location_data.stream"
# We are not dropping the schema after the first integration test
# in order to also test UPSERTing records to SQLite
self.integration_test(
config, sqlite_engine, expected_results, test_stream, drop_schema=False
)
# And then test Upserting (Combination of Updating already available
# rows and inserting a couple new rows)
expected_results["state"] = {
"test_users": 13,
"test_locations": 8,
"test_user_in_location": 14,
}
expected_results["total_records"] = {
"test_users": 8,
"test_locations": 5,
"test_user_in_location": 5,
}
test_stream = "user_location_upsert_data.stream"
self.integration_test(
config, sqlite_engine, expected_results, test_stream, drop_schema=True
)
@pytest.mark.slow
def test_no_primary_keys(self, config, sqlite_engine):
# The expected results to compare
expected_results = {
"state": {"test_no_pk": 3},
"tables": ["test_no_pk"],
"columns": {"test_no_pk": ["id", "metric", config["timestamp_column"]]},
"total_records": {"test_no_pk": 3},
}
test_stream = "no_primary_keys.stream"
# We are not dropping the schema after the first integration test
# in order to also test APPENDING records when no PK is defined
self.integration_test(
config, sqlite_engine, expected_results, test_stream, drop_schema=False
)
# And then test Upserting
# The Total Records in this case should be 8 (5+3) due to the records
# being appended and not UPSERTed
expected_results["state"] = {"test_no_pk": 5}
expected_results["total_records"] = {"test_no_pk": 8}
test_stream = "no_primary_keys_append.stream"
self.integration_test(
config, sqlite_engine, expected_results, test_stream, drop_schema=True
)
def test_duplicate_records(self, config, sqlite_engine):
# The expected results to compare
expected_results = {
"state": {"test_duplicate_records": 2},
"tables": ["test_duplicate_records"],
"columns": {
"test_duplicate_records": ["id", "metric", config["timestamp_column"]]
},
"total_records": {"test_duplicate_records": 2},
}
test_stream = "duplicate_records.stream"
self.integration_test(config, sqlite_engine, expected_results, test_stream)
@pytest.mark.slow
def test_array_data(self, config, sqlite_engine):
# The expected results to compare
expected_results = {
"state": {"test_carts": 4},
"tables": ["test_carts"],
"columns": {"test_carts": ["id", "fruits", config["timestamp_column"]]},
"total_records": {"test_carts": 4},
}
test_stream = "array_data.stream"
self.integration_test(
config,
sqlite_engine,
expected_results,
test_stream,
drop_schema=False,
)
# We also need to test that the proper data records were stored
with sqlite_engine.connect() as connection:
query = text("""
SELECT json_array_length(fruits) AS size
FROM test_carts
ORDER BY id
""")
result = connection.execute(query).fetchall()
assert result[0][0] == 3
assert result[1][0] == 2
assert result[2][0] == 1
assert result[3][0] == 4
# Drop the Test Table
with sqlite_engine.begin() as connection:
connection.execute(text("DROP TABLE test_carts"))
@pytest.mark.slow
def test_encoded_string_data(self, config, sqlite_engine):
# The expected results to compare
expected_results = {
"state": {
"test_strings": 11,
"test_strings_in_objects": 11,
"test_strings_in_arrays": 6,
},
"tables": [
"test_strings",
"test_strings_in_objects",
"test_strings_in_arrays",
],
"columns": {
"test_strings": ["id", "info", config["timestamp_column"]],
"test_strings_in_objects": [
"id",
"info__name",
"info__value",
config["timestamp_column"],
],
"test_strings_in_arrays": ["id", "strings", config["timestamp_column"]],
},
"total_records": {
"test_strings": 11,
"test_strings_in_objects": 11,
"test_strings_in_arrays": 6,
},
}
test_stream = "encoded_strings.stream"
self.integration_test(config, sqlite_engine, expected_results, test_stream)
def integration_test(
self, config, sqlite_engine, expected, stream_file, drop_schema=True
):
try:
# Create the TargetSQLite and fully run it using the user_location_data
target = TargetSQLite(config)
stream = load_stream(stream_file)
for line in stream:
target.process_line(line)
target.flush_all_cached_records()
# Check that the final state is the expected one
assert target.last_emitted_state == expected["state"]
# Check that the requested schema has been created
inspector = inspect(sqlite_engine)
all_table_names = inspector.get_table_names()
with sqlite_engine.begin() as connection:
for table in expected["tables"]:
# Check that the Table has been created in SQLite
assert table in all_table_names
# Check that the Table created has the requested attributes
db_columns = []
columns = inspector.get_columns(table)
for column in columns:
db_columns.append(column["name"].lower())
assert column["name"].lower() in expected["columns"][table]
for column in expected["columns"][table]:
assert column in db_columns
# Check that the correct number of rows were inserted
query = text(f"SELECT COUNT(*) FROM {table}")
results = connection.execute(query).fetchone()
assert results[0] == expected["total_records"][table]
finally:
# Drop the Test Tables
if drop_schema:
for stream, loader in target.loaders.items():
loader.table.drop(loader.engine)