22
33import pytest
44
5+ from sentry .backup .dependencies import ImportKind , NormalizedModelName , PrimaryKeyMap
6+ from sentry .backup .helpers import ImportFlags
7+ from sentry .backup .scopes import ImportScope
8+ from sentry .monitors .types import DATA_SOURCE_CRON_MONITOR
59from sentry .workflow_engine .registry import data_source_type_registry
610from tests .sentry .workflow_engine .test_base import BaseWorkflowTest
711
@@ -18,3 +22,73 @@ def test_data_source_valid_type(self) -> None:
1822 data_source = self .create_data_source (type = "test" )
1923 assert data_source is not None
2024 assert data_source .type == "test"
25+
26+ def test_normalize_before_relocation_import (self ) -> None :
27+ """Test that normalize_before_relocation_import correctly maps source_id"""
28+ monitor = self .create_monitor (project = self .project )
29+ data_source = self .create_data_source (
30+ type = DATA_SOURCE_CRON_MONITOR ,
31+ source_id = str (monitor .id ),
32+ organization_id = self .organization .id ,
33+ )
34+
35+ old_monitor_pk = monitor .id
36+ new_monitor_pk = 9999
37+ old_data_source_id = data_source .id
38+ old_org_id = data_source .organization_id
39+
40+ # Create a PrimaryKeyMap that maps the old monitor ID to a new one
41+ pk_map = PrimaryKeyMap ()
42+ pk_map .insert (
43+ model_name = NormalizedModelName ("monitors.monitor" ),
44+ old = old_monitor_pk ,
45+ new = new_monitor_pk ,
46+ kind = ImportKind .Inserted ,
47+ )
48+ pk_map .insert (
49+ model_name = NormalizedModelName ("sentry.organization" ),
50+ old = old_org_id ,
51+ new = old_org_id ,
52+ kind = ImportKind .Inserted ,
53+ )
54+
55+ old_data_source_pk = data_source .normalize_before_relocation_import (
56+ pk_map , ImportScope .Organization , ImportFlags ()
57+ )
58+
59+ assert (
60+ old_data_source_pk == old_data_source_id
61+ ), f"Expected { old_data_source_id } , got { old_data_source_pk } "
62+ assert data_source .source_id == str (new_monitor_pk )
63+ assert data_source .pk is None
64+
65+ def test_normalize_before_relocation_import_missing_source (self ) -> None :
66+ """Test that normalize_before_relocation_import succeeds but doesn't update source_id if mapping not found"""
67+ monitor = self .create_monitor (project = self .project )
68+ data_source = self .create_data_source (
69+ type = DATA_SOURCE_CRON_MONITOR ,
70+ source_id = str (monitor .id ),
71+ organization_id = self .organization .id ,
72+ )
73+
74+ old_source_id = data_source .source_id
75+ old_data_source_id = data_source .id
76+ old_org_id = data_source .organization_id
77+
78+ # Create a PrimaryKeyMap without the monitor mapping
79+ pk_map = PrimaryKeyMap ()
80+ pk_map .insert (
81+ model_name = NormalizedModelName ("sentry.organization" ),
82+ old = old_org_id ,
83+ new = old_org_id ,
84+ kind = ImportKind .Inserted ,
85+ )
86+
87+ result = data_source .normalize_before_relocation_import (
88+ pk_map , ImportScope .Organization , ImportFlags ()
89+ )
90+
91+ # Should succeed but leave source_id unchanged
92+ assert result == old_data_source_id
93+ assert data_source .source_id == old_source_id
94+ assert data_source .pk is None
0 commit comments