-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathcloudsql_test.py
More file actions
619 lines (519 loc) · 21.4 KB
/
cloudsql_test.py
File metadata and controls
619 lines (519 loc) · 21.4 KB
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
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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 unittest
from parameterized import parameterized
# pylint: disable=ungrouped-imports
try:
from apache_beam.transforms.enrichment_handlers.cloudsql import CloudSQLConnectionConfig
from apache_beam.transforms.enrichment_handlers.cloudsql import CloudSQLEnrichmentHandler
from apache_beam.transforms.enrichment_handlers.cloudsql import CustomQueryConfig
from apache_beam.transforms.enrichment_handlers.cloudsql import DatabaseTypeAdapter
from apache_beam.transforms.enrichment_handlers.cloudsql import ExternalSQLDBConnectionConfig
from apache_beam.transforms.enrichment_handlers.cloudsql import TableFieldsQueryConfig
from apache_beam.transforms.enrichment_handlers.cloudsql import TableFunctionQueryConfig
from apache_beam.transforms.enrichment_handlers.cloudsql_it_test import query_fn
from apache_beam.transforms.enrichment_handlers.cloudsql_it_test import where_clause_value_fn
except ImportError as e:
raise unittest.SkipTest(f'CloudSQL dependencies not installed: {str(e)}')
class TestCloudSQLEnrichment(unittest.TestCase):
def test_invalid_external_db_connection_params(self):
with self.assertRaises(ValueError) as context:
_ = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='',
port=5432,
user='',
password='',
db_id='')
self.assertIn("Database host cannot be empty", str(context.exception))
def test_invalid_cloudsql_db_connection_params(self):
with self.assertRaises(ValueError) as context:
_ = CloudSQLConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
instance_connection_uri='',
user='',
password='',
db_id='')
self.assertIn(
"Instance connection URI cannot be empty", str(context.exception))
@parameterized.expand([
# Empty TableFieldsQueryConfig.
(
lambda: TableFieldsQueryConfig(
table_id="", where_clause_template="", where_clause_fields=[]),
1,
2,
"must provide table_id and where_clause_template"
),
# Missing where_clause_template in TableFieldsQueryConfig.
(
lambda: TableFieldsQueryConfig(
table_id="table",
where_clause_template="",
where_clause_fields=["id"]),
2,
10,
"must provide table_id and where_clause_template"
),
# Invalid CustomQueryConfig with None query_fn.
(
lambda: CustomQueryConfig(query_fn=None), # type: ignore[arg-type]
2,
10,
"must provide a valid query_fn"
),
# Missing table_id in TableFunctionQueryConfig.
(
lambda: TableFunctionQueryConfig(
table_id="",
where_clause_template="id='{}'",
where_clause_value_fn=where_clause_value_fn),
2,
10,
"must provide table_id and where_clause_template"
),
# Missing where_clause_fields in TableFieldsQueryConfig.
(
lambda: TableFieldsQueryConfig(
table_id="table",
where_clause_template="id = '{}'",
where_clause_fields=[]),
1,
10,
"must provide non-empty where_clause_fields"
),
# Missing where_clause_value_fn in TableFunctionQueryConfig.
(
lambda: TableFunctionQueryConfig(
table_id="table",
where_clause_template="id = '{}'",
where_clause_value_fn=None), # type: ignore[arg-type]
1,
10,
"must provide where_clause_value_fn"
),
])
def test_invalid_query_config(
self, create_config, min_batch_size, max_batch_size, expected_error_msg):
"""Test that validation errors are raised for invalid query configs.
The test verifies both that the appropriate ValueError is raised and that
the error message contains the expected text.
"""
with self.assertRaises(ValueError) as context:
# Call the lambda to create the config.
query_config = create_config()
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='',
password='',
db_id='')
_ = CloudSQLEnrichmentHandler(
connection_config=connection_config,
query_config=query_config,
min_batch_size=min_batch_size,
max_batch_size=max_batch_size,
)
# Verify the error message contains the expected text.
self.assertIn(expected_error_msg, str(context.exception))
def test_valid_query_configs(self):
"""Test valid query configuration cases."""
# Valid TableFieldsQueryConfig.
table_fields_config = TableFieldsQueryConfig(
table_id="my_table",
where_clause_template="id = :id",
where_clause_fields=["id"])
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
handler1 = CloudSQLEnrichmentHandler(
connection_config=connection_config,
query_config=table_fields_config,
min_batch_size=1,
max_batch_size=10)
self.assertEqual(
handler1.query_template, "SELECT * FROM my_table WHERE id = :id")
# Valid TableFunctionQueryConfig.
table_function_config = TableFunctionQueryConfig(
table_id="my_table",
where_clause_template="id = :id",
where_clause_value_fn=where_clause_value_fn)
handler2 = CloudSQLEnrichmentHandler(
connection_config=connection_config,
query_config=table_function_config,
min_batch_size=1,
max_batch_size=10)
self.assertEqual(
handler2.query_template, "SELECT * FROM my_table WHERE id = :id")
# Valid CustomQueryConfig.
custom_config = CustomQueryConfig(query_fn=query_fn)
handler3 = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=custom_config)
# Verify that batching kwargs are empty for CustomQueryConfig.
self.assertEqual(handler3.batch_elements_kwargs(), {})
def test_custom_query_config_cache_key_error(self):
"""Test get_cache_key raises NotImplementedError with CustomQueryConfig."""
custom_config = CustomQueryConfig(query_fn=query_fn)
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
handler = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=custom_config)
# Create a dummy request.
import apache_beam as beam
request = beam.Row(id=1)
# Verify that get_cache_key raises NotImplementedError.
with self.assertRaises(NotImplementedError):
handler.get_cache_key(request)
def test_batch_elements_kwargs_include_max_batch_duration_secs(self):
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
query_config = TableFieldsQueryConfig(
table_id='my_table',
where_clause_template='id = :id',
where_clause_fields=['id'])
handler = CloudSQLEnrichmentHandler(
connection_config=connection_config,
query_config=query_config,
min_batch_size=2,
max_batch_size=10,
max_batch_duration_secs=0.5)
self.assertEqual(
handler.batch_elements_kwargs(),
{
'min_batch_size': 2,
'max_batch_size': 10,
'max_batch_duration_secs': 0.5,
})
def test_batch_elements_kwargs_omit_max_batch_duration_secs_by_default(self):
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
query_config = TableFieldsQueryConfig(
table_id='my_table',
where_clause_template='id = :id',
where_clause_fields=['id'])
handler = CloudSQLEnrichmentHandler(
connection_config=connection_config,
query_config=query_config,
min_batch_size=2,
max_batch_size=10)
self.assertEqual(
handler.batch_elements_kwargs(), {
'min_batch_size': 2,
'max_batch_size': 10,
})
def test_extract_parameter_names(self):
"""Test parameter extraction from SQL templates."""
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
config = TableFieldsQueryConfig(
table_id="users",
where_clause_template="id = :id",
where_clause_fields=["id"])
handler = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=config)
# Test simple parameter extraction.
self.assertEqual(handler._extract_parameter_names("id = :id"), ["id"])
# Test multiple parameters.
self.assertEqual(
handler._extract_parameter_names("id = :id AND name = :name"),
["id", "name"])
# Test no parameters.
self.assertEqual(
handler._extract_parameter_names("SELECT * FROM users"), [])
# Test complex query.
self.assertEqual(
handler._extract_parameter_names(
"age > :min_age AND city = :city AND status = :status"),
["min_age", "city", "status"])
def test_build_single_param_dict(self):
"""Test building parameter dictionaries for single requests."""
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
# Test TableFieldsQueryConfig.
table_config = TableFieldsQueryConfig(
table_id="users",
where_clause_template="id = :id AND name = :name",
where_clause_fields=["id", "name"])
handler1 = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=table_config)
result1 = handler1._build_single_param_dict([123, "John"])
self.assertEqual(result1, {"id": 123, "name": "John"})
# Test TableFunctionQueryConfig.
table_func_config = TableFunctionQueryConfig(
table_id="users",
where_clause_template="age > :min_age AND city = :city",
where_clause_value_fn=lambda row: [row.min_age, row.city])
handler2 = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=table_func_config)
result2 = handler2._build_single_param_dict([3, "NYC"])
self.assertEqual(result2, {"min_age": 3, "city": "NYC"})
def test_extract_values_from_request(self):
"""Test extracting values from requests based on query configuration."""
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
# Test TableFieldsQueryConfig.
table_config = TableFieldsQueryConfig(
table_id="users",
where_clause_template="id = :id AND name = :name",
where_clause_fields=["id", "name"])
handler1 = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=table_config)
import apache_beam as beam
request1 = beam.Row(id=123, name="John", age=30)
result1 = handler1._extract_values_from_request(request1)
self.assertEqual(result1, [123, "John"])
# Test TableFunctionQueryConfig.
def test_value_fn(row):
return [row.age, row.city]
table_func_config = TableFunctionQueryConfig(
table_id="users",
where_clause_template="age > :min_age AND city = :city",
where_clause_value_fn=test_value_fn)
handler2 = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=table_func_config)
request2 = beam.Row(age=25, city="NYC", name="Jane")
result2 = handler2._extract_values_from_request(request2)
self.assertEqual(result2, [25, "NYC"])
# Test missing field error.
request3 = beam.Row(age=30) # Missing name field.
with self.assertRaises(KeyError) as context:
handler1._extract_values_from_request(request3)
self.assertIn("where_clause_fields", str(context.exception))
def test_build_batch_query_single_request(self):
"""Test batch query building with single request returns original query."""
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
config = TableFieldsQueryConfig(
table_id="users",
where_clause_template="id = :id",
where_clause_fields=["id"])
handler = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=config)
import apache_beam as beam
requests = [beam.Row(id=1)]
# Single request should return original query template.
result = handler._build_batch_query(requests, batch_size=1)
self.assertEqual(result, "SELECT * FROM users WHERE id = :id")
def test_build_batch_query_multiple_requests(self):
"""Test batch query building with multiple requests creates OR clauses."""
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
config = TableFieldsQueryConfig(
table_id="users",
where_clause_template="id = :id AND name = :name",
where_clause_fields=["id", "name"])
handler = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=config)
import apache_beam as beam
requests = [beam.Row(id=1, name="Alice"), beam.Row(id=2, name="Bob")]
result = handler._build_batch_query(requests, batch_size=2)
expected = (
"SELECT * FROM users WHERE "
"(id = :batch_0_id AND name = :batch_0_name) OR "
"(id = :batch_1_id AND name = :batch_1_name)")
self.assertEqual(result, expected)
def test_build_parameters_dict_batch(self):
"""Test parameter dictionary building for batch requests."""
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
config = TableFieldsQueryConfig(
table_id="users",
where_clause_template="id = :id",
where_clause_fields=["id"])
handler = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=config)
import apache_beam as beam
requests = [beam.Row(id=1), beam.Row(id=2)]
result = handler._build_parameters_dict(requests, batch_size=2)
expected = {"batch_0_id": 1, "batch_1_id": 2}
self.assertEqual(result, expected)
def test_create_batch_clause(self):
"""Test batch clause creation with unique parameter names."""
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
config = TableFieldsQueryConfig(
table_id="users",
where_clause_template="id = :id AND name = :name",
where_clause_fields=["id", "name"])
handler = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=config)
result = handler._create_batch_clause(batch_index=0)
expected = "(id = :batch_0_id AND name = :batch_0_name)"
self.assertEqual(result, expected)
result2 = handler._create_batch_clause(batch_index=1)
expected2 = "(id = :batch_1_id AND name = :batch_1_name)"
self.assertEqual(result2, expected2)
def test_security_parameter_extraction_edge_cases(self):
"""Test parameter extraction with edge cases and security issues."""
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
config = TableFieldsQueryConfig(
table_id="users",
where_clause_template="id = :id",
where_clause_fields=["id"])
handler = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=config)
# Test empty template.
self.assertEqual(handler._extract_parameter_names(""), [])
# Test template with no parameters.
self.assertEqual(
handler._extract_parameter_names("SELECT * FROM users"), [])
# Test template with malformed parameters (should not match).
self.assertEqual(handler._extract_parameter_names("id = :"), [])
# Test template with numeric parameter names.
self.assertEqual(
handler._extract_parameter_names("col = :param_123"), ["param_123"])
# Test template with underscore parameter names.
self.assertEqual(
handler._extract_parameter_names("col = :user_id"), ["user_id"])
# Test duplicate parameter names.
result = handler._extract_parameter_names("id = :id OR id = :id")
# Note: re.findall returns all matches, so we'd get ["id", "id"].
self.assertEqual(result, ["id", "id"])
def test_build_single_param_dict_with_generic_names(self):
"""Test parameter dict building with generic names."""
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
# Test TableFunctionQueryConfig with generic parameter names.
config = TableFunctionQueryConfig(
table_id="users",
where_clause_template="id = :param_0 AND name = :param_1",
where_clause_value_fn=lambda row: [row.id, row.name])
handler = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=config)
result = handler._build_single_param_dict([123, "John"])
expected = {"param_0": 123, "param_1": "John"}
self.assertEqual(result, expected)
def test_duplicate_binding_parameter_names_handling(self):
"""Test handling of duplicate binding parameter names."""
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
# Test TableFunctionQueryConfig with duplicate parameter names.
config = TableFunctionQueryConfig(
table_id="users",
where_clause_template="id = :param AND name = :param",
where_clause_value_fn=lambda row: [row.id, row.name])
handler = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=config)
# Test that duplicate parameter names are made unique.
result = handler._build_single_param_dict([123, "John"])
expected = {"param_0": 123, "param_1": "John"}
self.assertEqual(result, expected)
# Test the helper method for template and params.
template = "id = :param AND name = :param"
values = [123, "John"]
updated_template, param_dict = handler._get_unique_template_and_params(
template, values)
expected_template = "id = :param_0 AND name = :param_1"
expected_params = {"param_0": 123, "param_1": "John"}
self.assertEqual(updated_template, expected_template)
self.assertEqual(param_dict, expected_params)
def test_unsupported_query_config_type_error(self):
"""Test that unsupported query config types raise ValueError."""
connection_config = ExternalSQLDBConnectionConfig(
db_adapter=DatabaseTypeAdapter.POSTGRESQL,
host='localhost',
port=5432,
user='user',
password='password',
db_id='db')
config = TableFieldsQueryConfig(
table_id="users",
where_clause_template="id = :id",
where_clause_fields=["id"])
handler = CloudSQLEnrichmentHandler(
connection_config=connection_config, query_config=config)
# Temporarily replace the config with an unsupported type.
handler._query_config = "unsupported_type"
import apache_beam as beam
request = beam.Row(id=1)
with self.assertRaises(ValueError) as context:
handler._extract_values_from_request(request)
self.assertIn(
"Unsupported query configuration type", str(context.exception))
if __name__ == '__main__':
unittest.main()