|
1 | | -from unittest.mock import ANY, MagicMock, mock_open, patch |
| 1 | +from unittest.mock import ANY, MagicMock, Mock, mock_open, patch |
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 |
|
|
11 | 11 |
|
12 | 12 |
|
13 | 13 | class TestCheckComponents: |
| 14 | + @patch("os.path.exists", return_value=True) |
| 15 | + @patch("os.remove") |
| 16 | + @patch("os.path.isdir", return_value=True) |
| 17 | + @patch("os.listdir", return_value=["package.xml"]) |
| 18 | + @patch("os.path.join", side_effect=lambda *args: "/".join(args)) |
| 19 | + @patch("cumulusci.core.sfdx.convert_sfdx_source") |
| 20 | + @patch( |
| 21 | + "cumulusci.tasks.salesforce.check_components.CheckComponents._is_plan", |
| 22 | + return_value=False, |
| 23 | + ) |
| 24 | + @patch( |
| 25 | + "cumulusci.tasks.salesforce.check_components.CheckComponents._freeze_steps", |
| 26 | + return_value=[], |
| 27 | + ) |
| 28 | + @patch( |
| 29 | + "cumulusci.tasks.salesforce.check_components.CheckComponents._collect_components_from_paths", |
| 30 | + return_value=[{"Type1": ["Comp1"]}, []], |
| 31 | + ) |
| 32 | + @patch( |
| 33 | + "cumulusci.tasks.salesforce.check_components.CheckComponents._get_api_object_responce", |
| 34 | + return_value=[], |
| 35 | + ) |
| 36 | + @patch( |
| 37 | + "builtins.open", |
| 38 | + new_callable=mock_open, |
| 39 | + read_data=""" |
| 40 | + <Package xmlns="http://soap.sforce.com/2006/04/metadata"> |
| 41 | + <types> |
| 42 | + <members>Delivery</members> |
| 43 | + <name>ApexClass</name> |
| 44 | + </types> |
| 45 | + <types> |
| 46 | + <members>Delivery__c</members> |
| 47 | + <name>CustomObject</name> |
| 48 | + </types> |
| 49 | + <version>58.0</version> |
| 50 | + </Package> |
| 51 | + """, |
| 52 | + ) |
| 53 | + @patch("cumulusci.utils.xml.metadata_tree.parse") |
| 54 | + @patch( |
| 55 | + "cumulusci.utils.xml.metadata_tree.parse_package_xml_types", |
| 56 | + return_value={"Type2": ["Comp2"]}, |
| 57 | + ) |
| 58 | + def test_get_repo_existing_components( |
| 59 | + self, |
| 60 | + mock_metadata_parse, |
| 61 | + mock_open_file, |
| 62 | + mock_convert_sfdx_source, |
| 63 | + mock_path_join, |
| 64 | + mock_listdir, |
| 65 | + mock_isdir, |
| 66 | + mock_remove, |
| 67 | + mock_path_exists, |
| 68 | + mock_is_plan, |
| 69 | + mock_freeze_steps, |
| 70 | + mock_get_api_object, |
| 71 | + mock_parse, |
| 72 | + mock_collect_components, |
| 73 | + ): |
| 74 | + org_config = Mock(scratch=True, config={}) |
| 75 | + org_config.username = "test_user" |
| 76 | + org_config.org_id = "test_org_id" |
| 77 | + self.org_config = Mock(return_value=("test", org_config)) |
| 78 | + project_config = create_project_config() |
| 79 | + flow_config = { |
| 80 | + "test": { |
| 81 | + "steps": { |
| 82 | + 1: { |
| 83 | + "flow": "test2", |
| 84 | + } |
| 85 | + } |
| 86 | + }, |
| 87 | + "test2": { |
| 88 | + "steps": { |
| 89 | + 1: { |
| 90 | + "task": "deploy", |
| 91 | + "options": {"path": "force-app/main/default"}, |
| 92 | + } |
| 93 | + } |
| 94 | + }, |
| 95 | + } |
| 96 | + plan_config = { |
| 97 | + "title": "Test Install", |
| 98 | + "slug": "install", |
| 99 | + "tier": "primary", |
| 100 | + "steps": {1: {"flow": "test"}}, |
| 101 | + } |
| 102 | + project_config.config["plans"] = { |
| 103 | + "Test Install": plan_config, |
| 104 | + } |
| 105 | + project_config.config["flows"] = flow_config |
| 106 | + |
| 107 | + task = create_task(CheckComponents, {"name": "test2"}) |
| 108 | + task.deploy_paths = ["test"] |
| 109 | + |
| 110 | + (components, response_messages) = task.get_repo_existing_components("test2") |
| 111 | + assert "Type1" in components |
| 112 | + assert "Type2" in components |
| 113 | + assert "Comp1" in components["Type1"] |
| 114 | + assert "Comp2" in components["Type2"] |
| 115 | + |
| 116 | + @patch("os.path.exists", return_value=True) |
| 117 | + @patch("os.remove") |
| 118 | + @patch("os.path.isdir", return_value=True) |
| 119 | + @patch("os.listdir", return_value=["package.xml"]) |
| 120 | + @patch("os.path.join", side_effect=lambda *args: "/".join(args)) |
| 121 | + @patch("cumulusci.core.sfdx.convert_sfdx_source") |
| 122 | + @patch( |
| 123 | + "cumulusci.tasks.salesforce.check_components.CheckComponents._is_plan", |
| 124 | + return_value=False, |
| 125 | + ) |
| 126 | + @patch( |
| 127 | + "cumulusci.tasks.salesforce.check_components.CheckComponents._freeze_steps", |
| 128 | + return_value=[], |
| 129 | + ) |
| 130 | + @patch( |
| 131 | + "cumulusci.tasks.salesforce.check_components.CheckComponents._collect_components_from_paths", |
| 132 | + return_value=[{"Type1": ["Comp1"]}, []], |
| 133 | + ) |
| 134 | + @patch( |
| 135 | + "cumulusci.tasks.salesforce.check_components.CheckComponents._get_api_object_responce", |
| 136 | + return_value=[], |
| 137 | + ) |
| 138 | + @patch( |
| 139 | + "builtins.open", |
| 140 | + new_callable=mock_open, |
| 141 | + read_data=""" |
| 142 | + <Package xmlns="http://soap.sforce.com/2006/04/metadata"> |
| 143 | + <types> |
| 144 | + <members>Delivery</members> |
| 145 | + <name>ApexClass</name> |
| 146 | + </types> |
| 147 | + <types> |
| 148 | + <members>Delivery__c</members> |
| 149 | + <name>CustomObject</name> |
| 150 | + </types> |
| 151 | + <version>58.0</version> |
| 152 | + </Package> |
| 153 | + """, |
| 154 | + ) |
| 155 | + @patch("cumulusci.utils.xml.metadata_tree.parse") |
| 156 | + @patch( |
| 157 | + "cumulusci.utils.xml.metadata_tree.parse_package_xml_types", |
| 158 | + return_value={"Type2": ["Comp2"]}, |
| 159 | + ) |
| 160 | + def test_get_repo_existing_components_paths_paramter( |
| 161 | + self, |
| 162 | + mock_metadata_parse, |
| 163 | + mock_open_file, |
| 164 | + mock_convert_sfdx_source, |
| 165 | + mock_path_join, |
| 166 | + mock_listdir, |
| 167 | + mock_isdir, |
| 168 | + mock_remove, |
| 169 | + mock_path_exists, |
| 170 | + mock_is_plan, |
| 171 | + mock_freeze_steps, |
| 172 | + mock_get_api_object, |
| 173 | + mock_parse, |
| 174 | + mock_collect_components, |
| 175 | + ): |
| 176 | + org_config = Mock(scratch=True, config={}) |
| 177 | + org_config.username = "test_user" |
| 178 | + org_config.org_id = "test_org_id" |
| 179 | + self.org_config = Mock(return_value=("test", org_config)) |
| 180 | + project_config = create_project_config() |
| 181 | + flow_config = { |
| 182 | + "test": { |
| 183 | + "steps": { |
| 184 | + 1: { |
| 185 | + "flow": "test2", |
| 186 | + } |
| 187 | + } |
| 188 | + }, |
| 189 | + "test2": { |
| 190 | + "steps": { |
| 191 | + 1: { |
| 192 | + "task": "deploy", |
| 193 | + "options": {"path": "force-app/main/default"}, |
| 194 | + } |
| 195 | + } |
| 196 | + }, |
| 197 | + } |
| 198 | + plan_config = { |
| 199 | + "title": "Test Install", |
| 200 | + "slug": "install", |
| 201 | + "tier": "primary", |
| 202 | + "steps": {1: {"flow": "test"}}, |
| 203 | + } |
| 204 | + project_config.config["plans"] = { |
| 205 | + "Test Install": plan_config, |
| 206 | + } |
| 207 | + project_config.config["flows"] = flow_config |
| 208 | + |
| 209 | + task = create_task(CheckComponents, {"name": "test2"}) |
| 210 | + task.deploy_paths = ["test"] |
| 211 | + |
| 212 | + (components, response_messages) = task.get_repo_existing_components("", "src") |
| 213 | + assert "Type1" in components |
| 214 | + assert "Type2" in components |
| 215 | + assert "Comp1" in components["Type1"] |
| 216 | + assert "Comp2" in components["Type2"] |
| 217 | + |
14 | 218 | @patch("os.path.exists", return_value=True) |
15 | 219 | @patch("os.remove") |
16 | 220 | @patch("os.path.isdir", return_value=True) |
|
0 commit comments