-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtest_postgres.py
More file actions
952 lines (825 loc) · 36 KB
/
test_postgres.py
File metadata and controls
952 lines (825 loc) · 36 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
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
# Copyright 2025 Collate
# Licensed under the Collate Community License, Version 1.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# https://github.com/open-metadata/OpenMetadata/blob/main/ingestion/LICENSE
# 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.
"""
Test Postgres using the topology
"""
import types
from unittest import TestCase
from unittest.mock import MagicMock, patch
from sqlalchemy.types import VARCHAR
from metadata.generated.schema.entity.data.database import Database
from metadata.generated.schema.entity.data.databaseSchema import DatabaseSchema
from metadata.generated.schema.entity.data.table import (
Column,
Constraint,
DataType,
TableType,
)
from metadata.generated.schema.entity.services.databaseService import (
DatabaseConnection,
DatabaseService,
DatabaseServiceType,
)
from metadata.generated.schema.metadataIngestion.workflow import (
OpenMetadataWorkflowConfig,
)
from metadata.generated.schema.type.entityReference import EntityReference
from metadata.generated.schema.type.filterPattern import FilterPattern
from metadata.ingestion.source.database.common_pg_mappings import (
GEOMETRY,
POINT,
POLYGON,
)
from metadata.ingestion.source.database.postgres.metadata import PostgresSource
from metadata.ingestion.source.database.postgres.usage import PostgresUsageSource
from metadata.ingestion.source.database.postgres.utils import get_postgres_version
mock_postgres_config = {
"source": {
"type": "postgres",
"serviceName": "local_postgres1",
"serviceConnection": {
"config": {
"type": "Postgres",
"username": "username",
"authType": {
"password": "password",
},
"hostPort": "localhost:5432",
"database": "postgres",
"sslMode": "verify-ca",
"sslConfig": {
"caCertificate": "CA certificate content",
},
}
},
"sourceConfig": {
"config": {
"type": "DatabaseMetadata",
}
},
},
"sink": {
"type": "metadata-rest",
"config": {},
},
"workflowConfig": {
"openMetadataServerConfig": {
"hostPort": "http://localhost:8585/api",
"authProvider": "openmetadata",
"securityConfig": {"jwtToken": "postgres"},
}
},
}
mock_postgres_usage_config = {
"source": {
"type": "postgres-usage",
"serviceName": "local_postgres1",
"serviceConnection": {
"config": {
"type": "Postgres",
"username": "username",
"authType": {
"password": "password",
},
"hostPort": "localhost:5432",
"database": "postgres",
}
},
"sourceConfig": {
"config": {
"type": "DatabaseUsage",
"queryLogDuration": 1,
}
},
},
"sink": {
"type": "metadata-rest",
"config": {},
},
"workflowConfig": {
"openMetadataServerConfig": {
"hostPort": "http://localhost:8585/api",
"authProvider": "openmetadata",
"securityConfig": {"jwtToken": "postgres"},
}
},
}
mock_postgres_usage_config_custom_source = {
"source": {
"type": "postgres-usage",
"serviceName": "local_postgres1",
"serviceConnection": {
"config": {
"type": "Postgres",
"username": "username",
"authType": {
"password": "password",
},
"hostPort": "localhost:5432",
"database": "postgres",
"queryStatementSource": "my_schema.custom_pg_stat_statements",
}
},
"sourceConfig": {
"config": {
"type": "DatabaseUsage",
"queryLogDuration": 1,
}
},
},
"sink": {
"type": "metadata-rest",
"config": {},
},
"workflowConfig": {
"openMetadataServerConfig": {
"hostPort": "http://localhost:8585/api",
"authProvider": "openmetadata",
"securityConfig": {"jwtToken": "postgres"},
}
},
}
MOCK_DATABASE_SERVICE = DatabaseService(
id="85811038-099a-11ed-861d-0242ac120002",
name="postgres_source",
connection=DatabaseConnection(),
serviceType=DatabaseServiceType.Postgres,
)
MOCK_DATABASE = Database(
id="2aaa012e-099a-11ed-861d-0242ac120002",
name="118146679784",
fullyQualifiedName="postgres_source.default",
displayName="118146679784",
description="",
service=EntityReference(
id="85811038-099a-11ed-861d-0242ac120002",
type="databaseService",
),
)
MOCK_DATABASE_SCHEMA = DatabaseSchema(
id="2aaa012e-099a-11ed-861d-0242ac120056",
name="default",
fullyQualifiedName="postgres_source.118146679784.default",
displayName="default",
description="",
database=EntityReference(
id="2aaa012e-099a-11ed-861d-0242ac120002",
type="database",
),
service=EntityReference(
id="2aaa012e-099a-11ed-861d-0242ac120002",
type="database",
),
)
MOCK_COLUMN_VALUE = [
{
"name": "username",
"type": VARCHAR(),
"nullable": True,
"default": None,
"autoincrement": False,
"system_data_type": "varchar(50)",
"comment": None,
},
{
"name": "geom_c",
"type": GEOMETRY(),
"nullable": True,
"default": None,
"autoincrement": False,
"system_data_type": "geometry",
"comment": None,
},
{
"name": "point_c",
"type": POINT(),
"nullable": True,
"default": None,
"autoincrement": False,
"system_data_type": "point",
"comment": None,
},
{
"name": "polygon_c",
"type": POLYGON(),
"nullable": True,
"default": None,
"autoincrement": False,
"comment": None,
"system_data_type": "polygon",
},
]
EXPECTED_COLUMN_VALUE = [
Column(
name="username",
displayName=None,
dataType=DataType.VARCHAR,
arrayDataType=None,
dataLength=1,
precision=None,
scale=None,
dataTypeDisplay="varchar(50)",
description=None,
fullyQualifiedName=None,
tags=None,
constraint=Constraint.NULL,
ordinalPosition=None,
jsonSchema=None,
children=None,
customMetrics=None,
profile=None,
),
Column(
name="geom_c",
displayName=None,
dataType=DataType.GEOMETRY,
arrayDataType=None,
dataLength=1,
precision=None,
scale=None,
dataTypeDisplay="geometry",
description=None,
fullyQualifiedName=None,
tags=None,
constraint=Constraint.NULL,
ordinalPosition=None,
jsonSchema=None,
children=None,
customMetrics=None,
profile=None,
),
Column(
name="point_c",
displayName=None,
dataType=DataType.GEOMETRY,
arrayDataType=None,
dataLength=1,
precision=None,
scale=None,
dataTypeDisplay="point",
description=None,
fullyQualifiedName=None,
tags=None,
constraint=Constraint.NULL,
ordinalPosition=None,
jsonSchema=None,
children=None,
customMetrics=None,
profile=None,
),
Column(
name="polygon_c",
displayName=None,
dataType=DataType.GEOMETRY,
arrayDataType=None,
dataLength=1,
precision=None,
scale=None,
dataTypeDisplay="polygon",
description=None,
fullyQualifiedName=None,
tags=None,
constraint=Constraint.NULL,
ordinalPosition=None,
jsonSchema=None,
children=None,
customMetrics=None,
profile=None,
),
]
class PostgresUnitTest(TestCase):
@patch(
"metadata.ingestion.source.database.common_db_source.CommonDbSourceService.test_connection"
)
def __init__(self, methodName, test_connection) -> None:
super().__init__(methodName)
test_connection.return_value = False
self.config = OpenMetadataWorkflowConfig.model_validate(mock_postgres_config)
self.postgres_source = PostgresSource.create(
mock_postgres_config["source"],
self.config.workflowConfig.openMetadataServerConfig,
)
self.postgres_source.context.get().__dict__[
"database_service"
] = MOCK_DATABASE_SERVICE.name.root
self.postgres_source.context.get().__dict__[
"database"
] = MOCK_DATABASE.name.root
self.postgres_source.context.get().__dict__[
"database_schema"
] = MOCK_DATABASE_SCHEMA.name.root
self.usage_config = OpenMetadataWorkflowConfig.model_validate(
mock_postgres_usage_config
)
with patch(
"metadata.ingestion.source.database.postgres.usage.PostgresUsageSource.test_connection"
):
self.postgres_usage_source = PostgresUsageSource.create(
mock_postgres_usage_config["source"],
self.usage_config.workflowConfig.openMetadataServerConfig,
)
def test_datatype(self):
inspector = types.SimpleNamespace()
inspector.get_columns = (
lambda table_name, schema_name, table_type, db_name: MOCK_COLUMN_VALUE
)
inspector.get_pk_constraint = lambda table_name, schema_name: []
inspector.get_unique_constraints = lambda table_name, schema_name: []
inspector.get_foreign_keys = lambda table_name, schema_name: []
result, _, _ = self.postgres_source.get_columns_and_constraints(
"public", "user", "postgres", inspector, TableType.Regular
)
for i, _ in enumerate(EXPECTED_COLUMN_VALUE):
self.assertEqual(result[i], EXPECTED_COLUMN_VALUE[i])
def test_get_stored_procedures(self):
"""
Test fetching stored procedures with filter
"""
self.postgres_source.source_config.includeStoredProcedures = True
self.postgres_source.source_config.storedProcedureFilterPattern = FilterPattern(
excludes=["sp_exclude"]
)
self.postgres_source.context.get().__dict__["database"] = "test_db"
self.postgres_source.context.get().__dict__["database_schema"] = "test_schema"
mock_engine = MagicMock()
self.postgres_source.engine = mock_engine
# Mock rows
row1 = MagicMock()
row1._mapping = {
"procedure_name": "sp_include",
"schema_name": "test_schema",
"definition": "def1",
"language": "SQL",
"procedure_type": "PROCEDURE",
}
row2 = MagicMock()
row2._mapping = {
"procedure_name": "sp_exclude",
"schema_name": "test_schema",
"definition": "def2",
"language": "SQL",
"procedure_type": "PROCEDURE",
}
# PostgreSQL get_stored_procedures calls _get_stored_procedures_internal twice
# once for procedures and once for functions
mock_result_proc = MagicMock()
mock_result_proc.all.return_value = [row1, row2]
mock_result_func = MagicMock()
mock_result_func.all.return_value = []
mock_conn = MagicMock()
mock_conn.execute.side_effect = [mock_result_proc, mock_result_func]
mock_engine.connect.return_value.__enter__ = MagicMock(return_value=mock_conn)
mock_engine.connect.return_value.__exit__ = MagicMock(return_value=False)
results = list(self.postgres_source.get_stored_procedures())
self.assertEqual(len(results), 1)
self.assertEqual(results[0].name, "sp_include")
def test_get_version_info(self):
mock_engine = MagicMock()
mock_conn = MagicMock()
mock_engine.connect.return_value.__enter__ = MagicMock(return_value=mock_conn)
mock_engine.connect.return_value.__exit__ = MagicMock(return_value=False)
mock_conn.execute.return_value.all.return_value = [["110016"]]
self.assertEqual("110016", get_postgres_version(mock_engine))
mock_conn.execute.return_value.all.return_value = [["90624"]]
self.assertEqual("90624", get_postgres_version(mock_engine))
mock_conn.execute.return_value.all.return_value = [[]]
self.assertIsNone(get_postgres_version(mock_engine))
@patch("sqlalchemy.engine.base.Engine")
@patch(
"metadata.ingestion.source.database.common_db_source.CommonDbSourceService.connection"
)
def test_close_connection(self, engine, connection):
connection.return_value = True
self.postgres_source.close()
def test_query_statement_source_default(self):
"""Test that default query statement source is pg_stat_statements"""
# Mock the engine with time column check
mock_engine = MagicMock()
mock_conn = MagicMock()
mock_conn.__enter__ = MagicMock(return_value=mock_conn)
mock_conn.__exit__ = MagicMock(return_value=None)
mock_conn.execute.return_value = [("total_exec_time",)]
mock_engine.connect.return_value = mock_conn
self.postgres_usage_source.engine = mock_engine
sql_statement = self.postgres_usage_source.get_sql_statement()
# Verify default pg_stat_statements is used
self.assertIn("pg_stat_statements s", sql_statement)
def test_query_statement_source_custom(self):
"""Test that custom query statement source is used when configured"""
with patch(
"metadata.ingestion.source.database.postgres.usage.PostgresUsageSource.test_connection"
):
custom_config = OpenMetadataWorkflowConfig.model_validate(
mock_postgres_usage_config_custom_source
)
custom_usage_source = PostgresUsageSource.create(
mock_postgres_usage_config_custom_source["source"],
custom_config.workflowConfig.openMetadataServerConfig,
)
# Mock the engine with time column check
mock_engine = MagicMock()
mock_conn = MagicMock()
mock_conn.__enter__ = MagicMock(return_value=mock_conn)
mock_conn.__exit__ = MagicMock(return_value=None)
mock_conn.execute.return_value = [("total_exec_time",)]
mock_engine.connect.return_value = mock_conn
custom_usage_source.engine = mock_engine
sql_statement = custom_usage_source.get_sql_statement()
# Verify custom source is used
self.assertIn("my_schema.custom_pg_stat_statements s", sql_statement)
# Verify default source is NOT used (check for the default without the custom schema prefix)
self.assertNotIn("FROM\n pg_stat_statements s", sql_statement)
def test_mark_deleted_schemas_enabled(self):
"""Test mark deleted schemas when the config is enabled"""
# Create a mock source config with the required attributes
mock_source_config = MagicMock()
mock_source_config.markDeletedSchemas = True
self.postgres_source.source_config = mock_source_config
# Mock the context to have database information
self.postgres_source.context.get().__dict__["database"] = "test_db"
self.postgres_source.context.get().__dict__["database_service"] = "test_service"
# Mock the schema entity source state
self.postgres_source.schema_entity_source_state = {"test_schema_fqn"}
# Mock the _get_filtered_schema_names method
with patch.object(
self.postgres_source, "_get_filtered_schema_names"
) as mock_filtered_schemas:
mock_filtered_schemas.return_value = [
"test_schema_fqn",
"another_schema_fqn",
]
# Mock the delete_entity_from_source function
with patch(
"metadata.ingestion.source.database.database_service.delete_entity_from_source"
) as mock_delete:
mock_delete.return_value = iter([])
# Call the method
result = list(self.postgres_source.mark_schemas_as_deleted())
# Verify that delete_entity_from_source was called with correct parameters
mock_delete.assert_called_once()
call_args = mock_delete.call_args
self.assertEqual(call_args[1]["entity_type"], DatabaseSchema)
self.assertEqual(call_args[1]["mark_deleted_entity"], True)
self.assertEqual(
call_args[1]["params"], {"database": "test_service.test_db"}
)
# Verify the entity_source_state contains both processed and filtered schemas
expected_source_state = {
"test_schema_fqn",
"test_schema_fqn",
"another_schema_fqn",
}
self.assertEqual(
call_args[1]["entity_source_state"], expected_source_state
)
def test_mark_deleted_schemas_disabled(self):
"""Test mark deleted schemas when the config is disabled"""
# Create a mock source config with the required attributes
mock_source_config = MagicMock()
mock_source_config.markDeletedSchemas = False
self.postgres_source.source_config = mock_source_config
# Call the method
result = list(self.postgres_source.mark_schemas_as_deleted())
# Verify that no deletion operations are performed
self.assertEqual(result, [])
def test_mark_deleted_schemas_no_database_context(self):
"""Test mark deleted schemas when no database is in context"""
# Create a mock source config with the required attributes
mock_source_config = MagicMock()
mock_source_config.markDeletedSchemas = True
self.postgres_source.source_config = mock_source_config
# Remove database from context
self.postgres_source.context.get().__dict__.pop("database", None)
# Call the method and expect ValueError
with self.assertRaises(ValueError) as context:
list(self.postgres_source.mark_schemas_as_deleted())
self.assertIn("No Database found in the context", str(context.exception))
def test_mark_deleted_databases_enabled(self):
"""Test mark deleted databases when the config is enabled"""
# Create a mock source config with the required attributes
mock_source_config = MagicMock()
mock_source_config.markDeletedDatabases = True
self.postgres_source.source_config = mock_source_config
# Mock the context to have database service information
self.postgres_source.context.get().__dict__["database_service"] = "test_service"
# Mock the database entity source state
self.postgres_source.database_entity_source_state = {"test_db_fqn"}
# Mock the _get_filtered_database_names method
with patch.object(
self.postgres_source, "_get_filtered_database_names"
) as mock_filtered_dbs:
mock_filtered_dbs.return_value = ["test_db", "another_db"]
# Mock the delete_entity_from_source function
with patch(
"metadata.ingestion.source.database.database_service.delete_entity_from_source"
) as mock_delete:
mock_delete.return_value = iter([])
# Call the method
result = list(self.postgres_source.mark_databases_as_deleted())
# Verify that delete_entity_from_source was called with correct parameters
mock_delete.assert_called_once()
call_args = mock_delete.call_args
self.assertEqual(call_args[1]["entity_type"], Database)
self.assertEqual(call_args[1]["mark_deleted_entity"], True)
self.assertEqual(call_args[1]["params"], {"service": "test_service"})
# Verify the entity_source_state contains both processed and filtered databases
expected_source_state = {
"test_db_fqn",
"test_service.test_db",
"test_service.another_db",
}
self.assertEqual(
call_args[1]["entity_source_state"], expected_source_state
)
def test_mark_deleted_databases_disabled(self):
"""Test mark deleted databases when the config is disabled"""
# Create a mock source config with the required attributes
mock_source_config = MagicMock()
mock_source_config.markDeletedDatabases = False
self.postgres_source.source_config = mock_source_config
# Call the method
result = list(self.postgres_source.mark_databases_as_deleted())
# Verify that no deletion operations are performed
self.assertEqual(result, [])
def test_mark_deleted_schemas_with_schema_filter_pattern(self):
"""Test mark deleted schemas with schema filter pattern applied"""
# Create a mock source config with the required attributes
mock_source_config = MagicMock()
mock_source_config.markDeletedSchemas = True
self.postgres_source.source_config = mock_source_config
# Mock the context to have database information
self.postgres_source.context.get().__dict__["database"] = "test_db"
self.postgres_source.context.get().__dict__["database_service"] = "test_service"
# Mock the schema entity source state with some existing schemas
self.postgres_source.schema_entity_source_state = {
"test_service.test_db.schema1",
"test_service.test_db.schema2",
}
# Mock the _get_filtered_schema_names method to return filtered schemas
with patch.object(
self.postgres_source, "_get_filtered_schema_names"
) as mock_filtered_schemas:
mock_filtered_schemas.return_value = ["test_service.test_db.schema1"]
# Mock the delete_entity_from_source function
with patch(
"metadata.ingestion.source.database.database_service.delete_entity_from_source"
) as mock_delete:
mock_delete.return_value = iter([])
# Call the method
result = list(self.postgres_source.mark_schemas_as_deleted())
# Verify that delete_entity_from_source was called
mock_delete.assert_called_once()
call_args = mock_delete.call_args
# Verify the entity_source_state contains both processed and filtered schemas
expected_source_state = {
"test_service.test_db.schema1",
"test_service.test_db.schema2",
"test_service.test_db.schema1",
}
self.assertEqual(
call_args[1]["entity_source_state"], expected_source_state
)
def test_mark_deleted_databases_with_database_filter_pattern(self):
"""Test mark deleted databases with database filter pattern applied"""
# Create a mock source config with the required attributes
mock_source_config = MagicMock()
mock_source_config.markDeletedDatabases = True
self.postgres_source.source_config = mock_source_config
# Mock the context to have database service information
self.postgres_source.context.get().__dict__["database_service"] = "test_service"
# Mock the database entity source state with some existing databases
self.postgres_source.database_entity_source_state = {
"test_service.db1",
"test_service.db2",
}
# Mock the _get_filtered_database_names method to return filtered databases
with patch.object(
self.postgres_source, "_get_filtered_database_names"
) as mock_filtered_dbs:
mock_filtered_dbs.return_value = ["db1"]
# Mock the delete_entity_from_source function
with patch(
"metadata.ingestion.source.database.database_service.delete_entity_from_source"
) as mock_delete:
mock_delete.return_value = iter([])
# Call the method
result = list(self.postgres_source.mark_databases_as_deleted())
# Verify that delete_entity_from_source was called
mock_delete.assert_called_once()
call_args = mock_delete.call_args
# Verify the entity_source_state contains both processed and filtered databases
expected_source_state = {
"test_service.db1",
"test_service.db2",
"test_service.db1",
}
self.assertEqual(
call_args[1]["entity_source_state"], expected_source_state
)
def test_mark_deleted_schemas_empty_source_state(self):
"""Test mark deleted schemas with empty source state"""
# Create a mock source config with the required attributes
mock_source_config = MagicMock()
mock_source_config.markDeletedSchemas = True
self.postgres_source.source_config = mock_source_config
# Mock the context to have database information
self.postgres_source.context.get().__dict__["database"] = "test_db"
self.postgres_source.context.get().__dict__["database_service"] = "test_service"
# Mock empty schema entity source state
self.postgres_source.schema_entity_source_state = set()
# Mock the _get_filtered_schema_names method
with patch.object(
self.postgres_source, "_get_filtered_schema_names"
) as mock_filtered_schemas:
mock_filtered_schemas.return_value = ["test_service.test_db.schema1"]
# Mock the delete_entity_from_source function
with patch(
"metadata.ingestion.source.database.database_service.delete_entity_from_source"
) as mock_delete:
mock_delete.return_value = iter([])
# Call the method
result = list(self.postgres_source.mark_schemas_as_deleted())
# Verify that delete_entity_from_source was called with only filtered schemas
mock_delete.assert_called_once()
call_args = mock_delete.call_args
expected_source_state = {"test_service.test_db.schema1"}
self.assertEqual(
call_args[1]["entity_source_state"], expected_source_state
)
def test_mark_deleted_databases_empty_source_state(self):
"""Test mark deleted databases with empty source state"""
# Create a mock source config with the required attributes
mock_source_config = MagicMock()
mock_source_config.markDeletedDatabases = True
self.postgres_source.source_config = mock_source_config
# Mock the context to have database service information
self.postgres_source.context.get().__dict__["database_service"] = "test_service"
# Mock empty database entity source state
self.postgres_source.database_entity_source_state = set()
# Mock the _get_filtered_database_names method
with patch.object(
self.postgres_source, "_get_filtered_database_names"
) as mock_filtered_dbs:
mock_filtered_dbs.return_value = ["db1"]
# Mock the delete_entity_from_source function
with patch(
"metadata.ingestion.source.database.database_service.delete_entity_from_source"
) as mock_delete:
mock_delete.return_value = iter([])
# Call the method
result = list(self.postgres_source.mark_databases_as_deleted())
# Verify that delete_entity_from_source was called with only filtered databases
mock_delete.assert_called_once()
call_args = mock_delete.call_args
expected_source_state = {"test_service.db1"}
self.assertEqual(
call_args[1]["entity_source_state"], expected_source_state
)
def test_mark_deleted_schemas_exception_handling(self):
"""Test mark deleted schemas exception handling"""
# Create a mock source config with the required attributes
mock_source_config = MagicMock()
mock_source_config.markDeletedSchemas = True
self.postgres_source.source_config = mock_source_config
# Mock the context to have database information
self.postgres_source.context.get().__dict__["database"] = "test_db"
self.postgres_source.context.get().__dict__["database_service"] = "test_service"
# Mock the schema entity source state
self.postgres_source.schema_entity_source_state = {"test_schema_fqn"}
# Mock the _get_filtered_schema_names method to raise an exception
with patch.object(
self.postgres_source, "_get_filtered_schema_names"
) as mock_filtered_schemas:
mock_filtered_schemas.side_effect = Exception("Test exception")
# Call the method and expect it to handle the exception gracefully
with self.assertRaises(Exception):
list(self.postgres_source.mark_schemas_as_deleted())
def test_mark_deleted_databases_exception_handling(self):
"""Test mark deleted databases exception handling"""
# Create a mock source config with the required attributes
mock_source_config = MagicMock()
mock_source_config.markDeletedDatabases = True
self.postgres_source.source_config = mock_source_config
# Mock the context to have database service information
self.postgres_source.context.get().__dict__["database_service"] = "test_service"
# Mock the database entity source state
self.postgres_source.database_entity_source_state = {"test_db_fqn"}
# Mock the _get_filtered_database_names method to raise an exception
with patch.object(
self.postgres_source, "_get_filtered_database_names"
) as mock_filtered_dbs:
mock_filtered_dbs.side_effect = Exception("Test exception")
# Call the method and expect it to handle the exception gracefully
with self.assertRaises(Exception):
list(self.postgres_source.mark_databases_as_deleted())
def test_mark_deleted_schemas_with_multiple_schemas(self):
"""Test mark deleted schemas with multiple schemas in source state"""
# Create a mock source config with the required attributes
mock_source_config = MagicMock()
mock_source_config.markDeletedSchemas = True
self.postgres_source.source_config = mock_source_config
# Mock the context to have database information
self.postgres_source.context.get().__dict__["database"] = "test_db"
self.postgres_source.context.get().__dict__["database_service"] = "test_service"
# Mock the schema entity source state with multiple schemas
self.postgres_source.schema_entity_source_state = {
"test_service.test_db.schema1",
"test_service.test_db.schema2",
"test_service.test_db.schema3",
}
# Mock the _get_filtered_schema_names method
with patch.object(
self.postgres_source, "_get_filtered_schema_names"
) as mock_filtered_schemas:
mock_filtered_schemas.return_value = [
"test_service.test_db.schema1",
"test_service.test_db.schema2",
]
# Mock the delete_entity_from_source function
with patch(
"metadata.ingestion.source.database.database_service.delete_entity_from_source"
) as mock_delete:
mock_delete.return_value = iter([])
# Call the method
result = list(self.postgres_source.mark_schemas_as_deleted())
# Verify that delete_entity_from_source was called
mock_delete.assert_called_once()
call_args = mock_delete.call_args
# Verify the entity_source_state contains all schemas
expected_source_state = {
"test_service.test_db.schema1",
"test_service.test_db.schema2",
"test_service.test_db.schema3",
"test_service.test_db.schema1",
"test_service.test_db.schema2",
}
self.assertEqual(
call_args[1]["entity_source_state"], expected_source_state
)
def test_mark_deleted_databases_with_multiple_databases(self):
"""Test mark deleted databases with multiple databases in source state"""
# Create a mock source config with the required attributes
mock_source_config = MagicMock()
mock_source_config.markDeletedDatabases = True
self.postgres_source.source_config = mock_source_config
# Mock the context to have database service information
self.postgres_source.context.get().__dict__["database_service"] = "test_service"
# Mock the database entity source state with multiple databases
self.postgres_source.database_entity_source_state = {
"test_service.db1",
"test_service.db2",
"test_service.db3",
}
# Mock the _get_filtered_database_names method
with patch.object(
self.postgres_source, "_get_filtered_database_names"
) as mock_filtered_dbs:
mock_filtered_dbs.return_value = ["db1", "db2"]
# Mock the delete_entity_from_source function
with patch(
"metadata.ingestion.source.database.database_service.delete_entity_from_source"
) as mock_delete:
mock_delete.return_value = iter([])
# Call the method
result = list(self.postgres_source.mark_databases_as_deleted())
# Verify that delete_entity_from_source was called
mock_delete.assert_called_once()
call_args = mock_delete.call_args
# Verify the entity_source_state contains all databases
expected_source_state = {
"test_service.db1",
"test_service.db2",
"test_service.db3",
"test_service.db1",
"test_service.db2",
}
self.assertEqual(
call_args[1]["entity_source_state"], expected_source_state
)
class TestPostgresCommonMappings(TestCase):
"""Verify extended type entries in the shared PostgreSQL ischema_names map."""
def test_tid_type_registered(self):
"""'tid' must be present in the PostgreSQL ischema_names after common_pg_mappings is loaded."""
# common_pg_mappings registers types as a side-effect of module import
from sqlalchemy.dialects.postgresql.base import (
ischema_names as pg_ischema_names,
)
import metadata.ingestion.source.database.common_pg_mappings # noqa: F401
self.assertIn("tid", pg_ischema_names)
def test_tid_maps_to_string(self):
"""'tid' must map to a String-compatible SQLAlchemy type."""
from sqlalchemy import String as SqlAlchemyString
from sqlalchemy.dialects.postgresql.base import (
ischema_names as pg_ischema_names,
)
import metadata.ingestion.source.database.common_pg_mappings # noqa: F401
tid_type = pg_ischema_names["tid"]
self.assertIs(tid_type, SqlAlchemyString)