def test_run_task__revert_path_already_exists(self): with temporary_dir() as path, temporary_dir() as revert_path: project_config = BaseProjectConfig( BaseGlobalConfig(), config={"noyaml": True} ) task_config = TaskConfig( {"options": {"path": path, "revert_path": revert_path}} ) task = CreateUnmanagedEESrc(project_config, task_config) with self.assertRaises(TaskOptionsError): task()
def test_freeze(self): with temporary_dir() as path: os.mkdir(".git") os.makedirs("unpackaged/test") task = create_task(DeployBundles, {"path": path + "/unpackaged"}) step = StepSpec(1, "deploy_bundles", task.task_config, None) steps = task.freeze(step) self.assertEqual( [ { "is_required": True, "kind": "metadata", "name": "Deploy unpackaged/test", "path": "deploy_bundles.test", "step_num": "1.1", "task_class": "cumulusci.tasks.salesforce.UpdateDependencies", "task_config": { "options": { "dependencies": [ { "ref": task.project_config.repo_commit, "repo_name": "TestRepo", "repo_owner": "TestOwner", "subfolder": "unpackaged/test", } ] } }, } ], steps, )
def test_run_task__path_not_found(self): with temporary_dir() as path: pass task = create_task(DeployBundles, {"path": path}) task._get_api = mock.Mock() task() task._get_api.assert_not_called()
def test_create_org_uses_org_def_email(self, Command): out = b"Successfully created scratch org: ORG_ID, username: USERNAME" Command.return_value = p = mock.Mock( stdout=io.BytesIO(out), stderr=io.BytesIO(b""), returncode=0 ) config = ScratchOrgConfig( { "config_file": "tmp.json", "set_password": True, "email_address": "*****@*****.**", }, "test", ) config.generate_password = mock.Mock() with temporary_dir(): with open("tmp.json", "w") as f: f.write('{"adminEmail": "*****@*****.**"}') config.create_org() p.run.assert_called_once() self.assertEqual(config.config["org_id"], "ORG_ID") self.assertEqual(config.config["username"], "USERNAME") self.assertIn("date_created", config.config) config.generate_password.assert_called_once() self.assertTrue(config.config["created"]) self.assertEqual(config.scratch_org_type, "workspace") self.assertNotIn("*****@*****.**", Command.call_args[0][0])
def test_run_task(self): with temporary_dir() as d: os.mkdir(".git") os.mkdir("src") meta_xml_path = os.path.join(d, "src", "test-meta.xml") with open(meta_xml_path, "w") as f: f.write( """<?xml version="1.0" encoding="UTF-8"?> <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>1.0</apiVersion> </ApexClass> """ ) project_config = BaseProjectConfig( BaseGlobalConfig(), config={"noyaml": True} ) task_config = TaskConfig({"options": {"version": "43.0"}}) task = UpdateApi(project_config, task_config) task() with open(meta_xml_path, "r") as f: result = f.read() self.assertEqual( """<?xml version="1.0" encoding="UTF-8"?> <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>43.0</apiVersion> </ApexClass> """, result, )
def test_run_task(self): src_path = os.path.join( __location__, "package_metadata", "namespaced_report_folder" ) with open(os.path.join(src_path, "package.xml"), "r") as f: expected = f.read() with temporary_dir() as path: output_path = os.path.join(path, "package.xml") project_config = BaseProjectConfig( BaseGlobalConfig(), { "project": { "package": {"name": "Test Package", "api_version": "36.0"} } }, ) task_config = TaskConfig( {"options": {"path": src_path, "output": output_path, "managed": True}} ) org_config = OrgConfig({}, "test") task = UpdatePackageXml(project_config, task_config, org_config) task() with open(output_path, "r") as f: result = f.read() self.assertEqual(expected, result)
def test_generates_record_type_and_business_process(self): task = create_task( EnsureRecordTypes, { "record_type_developer_name": "NPSP_Default", "record_type_label": "NPSP Default", "sobject": "Opportunity", }, ) task.sf = mock.Mock() task.sf.Opportunity = mock.Mock() task.sf.Opportunity.describe = mock.Mock( return_value=OPPORTUNITY_DESCRIBE_NO_RTS ) task._infer_requirements() with temporary_dir(): task._build_package() with open(os.path.join("objects", "Opportunity.object"), "r") as f: opp_contents = f.read() self.assertMultiLineEqual(OPPORTUNITY_METADATA, opp_contents) with open(os.path.join("package.xml"), "r") as f: pkg_contents = f.read() self.assertMultiLineEqual(PACKAGE_XML, pkg_contents)
def test_call(self): with temporary_dir() as d: repo = mock.Mock(spec=Repository) repo.owner = "SalesforceFoundation" repo.name = "TestRepo" repo.tree = mock.Mock() repo.tree.return_value = Tree( { "url": "string", "sha": "tree-ish-hash", "tree": [ { "type": "tree", "mode": "100644", "path": "dir", "sha": "bogus1", }, { "type": "blob", "mode": "100644", "path": "file_outside_dir", "sha": "bogus2", }, { "type": "blob", "mode": "100644", "path": os.path.join("dir", "unchanged"), "sha": hashlib.sha1(b"blob 0\0").hexdigest(), }, { "type": "blob", "mode": "100644", "path": os.path.join("dir", "modified"), "sha": "bogus3", }, { "type": "blob", "mode": "100644", "path": os.path.join("dir", "removed"), "sha": "bogus4", }, ], }, None, ) commit = CommitDir(repo) os.mkdir("dir") with open("unchanged", "w") as f: f.write("") with open("modified", "w") as f: f.write("modified") with open("new", "w") as f: f.write("new") with open(".hidden", "w") as f: pass commit(d, "master", "dir", dry_run=True) commit(d, "master", "dir", commit_message="msg") repo.create_commit.assert_called_once()
def test_project_init_already_initted(self): with temporary_dir(): os.mkdir(".git") with open("cumulusci.yml", "w"): pass # create empty file with self.assertRaises(click.ClickException): run_click_command(cci.project_init)
def test_get_api__skip_clean_meta_xml(self): with temporary_dir() as path: touch("package.xml") task = create_task(Deploy, {"path": path, "clean_meta_xml": False}) api = task._get_api() zf = zipfile.ZipFile(io.BytesIO(base64.b64decode(api.package_zip)), "r") self.assertIn("package.xml", zf.namelist())
def test_repo_commit_no_repo_branch(self): config = BaseProjectConfig(BaseGlobalConfig()) with temporary_dir() as d: os.mkdir(os.path.join(d, ".git")) with open(os.path.join(d, ".git", "HEAD"), "w") as f: f.write("abcdef") self.assertIsNone(config.repo_commit)
def test_get_api_no_changes(self): with temporary_dir() as path: task = create_task(BaseUninstallMetadata, {"path": path}) api = mock.Mock() task._get_destructive_changes = mock.Mock(return_value=None) task.api_class = mock.Mock(return_value=api) task() api.assert_not_called()
def test_run_task(self): with temporary_dir() as d: os.mkdir(".git") os.mkdir("src") meta_xml_path = os.path.join(d, "src", "test-meta.xml") with open(meta_xml_path, "w") as f: f.write( """<?xml version="1.0" encoding="UTF-8"?> <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> <packageVersions> <namespace>npsp</namespace> <majorNumber>1</majorNumber> <minorNumber>0</minorNumber> </packageVersions> <packageVersions> <namespace>npe01</namespace> <majorNumber>1</majorNumber> <minorNumber>0</minorNumber> </packageVersions> </ApexClass> """ ) project_config = BaseProjectConfig( BaseGlobalConfig(), config={"noyaml": True} ) project_config.get_static_dependencies = mock.Mock( return_value=[ { "namespace": "npsp", "version": "3.0", "dependencies": [{"namespace": "npe01", "version": "1.1"}], } ] ) task_config = TaskConfig() task = UpdateDependencies(project_config, task_config) task() with open(meta_xml_path, "r") as f: result = f.read() self.assertEqual( """<?xml version="1.0" encoding="UTF-8"?> <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> <packageVersions> <namespace>npsp</namespace> <majorNumber>3</majorNumber> <minorNumber>0</minorNumber> </packageVersions> <packageVersions> <namespace>npe01</namespace> <majorNumber>1</majorNumber> <minorNumber>1</minorNumber> </packageVersions> </ApexClass> """, result, )
def test_run_task(self): with temporary_dir() as path: os.mkdir("bundle") with open(os.path.join(path, "file"), "w"): pass task = create_task(UninstallLocalBundles, {"path": path}) task._get_api = mock.Mock() task() task._get_api.assert_called_once()
def test_get_api__managed(self): with temporary_dir() as path: touch("package.xml") task = create_task( Deploy, {"path": path, "namespace_inject": "ns", "unmanaged": False} ) api = task._get_api() zf = zipfile.ZipFile(io.BytesIO(base64.b64decode(api.package_zip)), "r") self.assertIn("package.xml", zf.namelist())
def test_parse_item(self): with temporary_dir() as path: item_path = os.path.join(path, "Test") os.mkdir(item_path) with open(os.path.join(item_path, ".hidden"), "w"): pass with open(os.path.join(item_path, "Test.object"), "w"): pass parser = MetadataFolderParser("TestMDT", path, "object", delete=False) self.assertEqual(["Test", "Test/Test"], parser._parse_item("Test"))
def test_run_task(self): with temporary_dir() as path: project_config = create_project_config() project_config.config["project"]["package"]["name"] = "TestPackage" task = create_task(RetrievePackaged, {"path": path}, project_config) zf = zipfile.ZipFile(io.BytesIO(), "w") zf.writestr("TestPackage/testfile", "test") task.api_class = mock.Mock(return_value=mock.Mock(return_value=zf)) task() self.assertTrue(os.path.exists(os.path.join(path, "testfile")))
def test_parse_item(self): with temporary_dir() as path: item_path = os.path.join(path, "Test") os.mkdir(item_path) with open(os.path.join(item_path, ".hidden"), "w"): pass # subitems should be ignored with open(os.path.join(item_path, "Test.object"), "w"): pass parser = BundleParser("TestMDT", path, "object", delete=False) self.assertEqual(["Test"], parser._parse_item("Test"))
def test_run_task__no_changes(self): with temporary_dir() as path: task = create_task(RetrieveChanges, {"path": path}) task._init_task() messages = [] task.tooling = mock.Mock() task.tooling.query_all.return_value = {"totalSize": 0, "records": []} task.logger = mock.Mock() task.logger.info = messages.append task._run_task() self.assertIn("No changes to retrieve", messages)
def test_findRename(self): with utils.temporary_dir() as d: path = os.path.join(d, "foo") with open(path, "w") as f: f.write("aa") logger = mock.Mock() utils.findRename("foo", "bar", d, logger) logger.info.assert_called_once() self.assertEqual(os.listdir(d), ["bar"])
def test_call__no_changes(self): with temporary_dir() as d: repo = mock.Mock(spec=Repository) repo.tree = mock.Mock( return_value=Tree( {"url": "string", "sha": "tree-ish-hash", "tree": []}, None ) ) commit = CommitDir(repo) commit(d, "master", commit_message="msg") repo.create_commit.assert_not_called()
def test_get_api(self): with temporary_dir() as path: with open(os.path.join(path, "package.xml"), "w") as f: f.write("PACKAGE") task = create_task( RetrieveUnpackaged, {"path": path, "package_xml": "package.xml", "api_version": "43.0"}, ) task.api_class = mock.Mock() task._get_api() self.assertEqual("PACKAGE", task.api_class.call_args[0][1])
def test_render_xml__managed(self): with temporary_dir() as path: generator = PackageXmlGenerator( path, "43.0", "Test Package", managed=True, install_class="Install", uninstall_class="Uninstall", ) result = generator() self.assertEqual(EXPECTED_MANAGED, result)
def test_repo_commit_packed_refs(self): config = BaseProjectConfig(BaseGlobalConfig()) with temporary_dir() as d: os.system("git init") with open(os.path.join(d, ".git", "packed-refs"), "w") as f: f.write("# pack-refs with: peeled fully-peeled sorted\n") f.write("#\n") f.write( "8ce67f4519190cd1ec9785105168e21b9599bc27 refs/remotes/origin/master\n" ) self.assertIsNotNone(config.repo_commit)
def _run_task(self): if self.options["connect"]: self._validate_connect_service() with temporary_dir() as tempdir: self.tempdir = tempdir self.options["command"] += " -d {}".format(self.tempdir) self._build_package() super(CreateConnectedApp, self)._run_task() if self.options["connect"]: self._connect_service()
def test_findReplaceRegex(self): with utils.temporary_dir() as d: path = os.path.join(d, "test") with open(path, "w") as f: f.write("aa") logger = mock.Mock() utils.findReplaceRegex(r"\w", "x", d, "*", logger) logger.info.assert_called_once() with open(path, "r") as f: result = f.read() self.assertEqual(result, "xx")
def test_call__error_creating_tree(self): with temporary_dir() as d: repo = mock.Mock(spec=Repository) repo.create_tree.return_value = None repo.tree = mock.Mock( return_value=Tree( {"url": "string", "sha": "tree-ish-hash", "tree": []}, None ) ) with open("new", "w") as f: f.write("new") commit = CommitDir(repo) with self.assertRaises(GithubException): commit(d, "master", commit_message="msg")
def test_process_namespace(self): with temporary_dir() as path: task = create_task( BaseRetrieveMetadata, { "path": path, "namespace_inject": "ns", "namespace_tokenize": "ns", "namespace_strip": "ns", }, ) zf = zipfile.ZipFile(io.BytesIO(), "w") result = task._process_namespace(zf) self.assertIsInstance(result, zipfile.ZipFile)
def test_scratch_info_username_not_found(self, Command): Command.return_value = mock.Mock( stderr=io.BytesIO(b"error"), stdout=io.BytesIO(b"out"), returncode=0 ) config = ScratchOrgConfig( {"config_file": "tmp.json", "email_address": "*****@*****.**"}, "test" ) with temporary_dir(): with open("tmp.json", "w") as f: f.write("{}") with self.assertRaises(ScratchOrgException): config.scratch_info
def test_parser__missing_name(self): with temporary_dir() as path: with open(os.path.join(path, "Test.test"), "w") as f: f.write( """<?xml version='1.0' encoding='utf-8'?> <root xmlns="http://soap.sforce.com/2006/04/metadata"> <test /> </root>""" ) parser = MetadataXmlElementParser( "TestMDT", path, "test", delete=False, item_xpath="./sf:test" ) with self.assertRaises(MissingNameElementError): parser()
def test_create_org_command_error(self, Command): Command.return_value = mock.Mock( stdout=io.BytesIO(b""), stderr=io.BytesIO(b"scratcherror"), returncode=1 ) config = ScratchOrgConfig( {"config_file": "tmp.json", "email_address": "*****@*****.**"}, "test" ) with temporary_dir(): with open("tmp.json", "w") as f: f.write("{}") with self.assertRaises(ScratchOrgException) as ctx: config.create_org() self.assertIn("scratcherror", str(ctx.error))
def test_get_destructive_changes(self, PackageXmlGenerator): with temporary_dir() as path: project_config = create_project_config() project_config.config["project"]["package"]["namespace"] = "ns" task = create_task( UninstallLocalNamespacedBundles, { "path": path, "managed": True, "filename_token": "%TOKEN%" }, project_config, ) PackageXmlGenerator.return_value = "%TOKEN%" self.assertEqual("ns__", task._get_destructive_changes())
def test_call__error_creating_commit(self): with temporary_dir() as d: repo = mock.Mock(spec=Repository) repo.create_commit.return_value = None repo.tree = mock.Mock(return_value=Tree( { "url": "string", "sha": "tree-ish-hash", "tree": [] }, None)) with open("new", "w") as f: f.write("new") commit = CommitDir(repo) with self.assertRaises(GithubException): commit(d, "master", commit_message="msg")
def test_process_text_in_directory__skips_binary(self): contents = b"\x9c%%%NAMESPACE%%%" with utils.temporary_dir(): with open("test", "wb") as f: f.write(contents) def process(name, content): return name, "" utils.process_text_in_directory(".", process) # assert contents were untouched with open("test", "rb") as f: result = f.read() assert contents == result
def test_run_task(self): with temporary_dir() as revert_path: with open(os.path.join(revert_path, "file"), "w") as f: pass path = os.path.join(os.path.dirname(revert_path), os.path.basename(revert_path) + "_orig") project_config = BaseProjectConfig(BaseGlobalConfig()) task_config = TaskConfig( {"options": { "path": path, "revert_path": revert_path }}) task = RevertUnmanagedEESrc(project_config, task_config) task() self.assertTrue(os.path.exists(os.path.join(path, "file")))
def test_get_api__skip_clean_meta_xml(self): with temporary_dir() as path: touch("package.xml") task = create_task( Deploy, { "path": path, "clean_meta_xml": False, "unmanaged": True, }, ) api = task._get_api() zf = zipfile.ZipFile(io.BytesIO(base64.b64decode(api.package_zip)), "r") self.assertIn("package.xml", zf.namelist())
def test_package_name_urlencoding(self): api_version = "36.0" package_name = "Test & Package" expected = '<?xml version="1.0" encoding="UTF-8"?>\n' expected += '<Package xmlns="http://soap.sforce.com/2006/04/metadata">\n' expected += " <fullName>Test %26 Package</fullName>\n" expected += " <version>{}</version>\n".format(api_version) expected += "</Package>" with temporary_dir() as path: generator = PackageXmlGenerator(path, api_version, package_name) package_xml = generator() self.assertEqual(package_xml, expected)
def test_parser(self): with temporary_dir() as path: with open(os.path.join(path, "custom.labels"), "w") as f: f.write( """<?xml version='1.0' encoding='utf-8'?> <root xmlns="http://soap.sforce.com/2006/04/metadata"> <labels> <fullName>TestLabel</fullName> </labels> </root>""" ) parser = CustomLabelsParser( "CustomLabels", path, "labels", False, item_xpath="./sf:labels" ) self.assertEqual(["TestLabel"], parser._parse_item("custom.labels"))
def test_create_table_modern_id_mapping(self): mapping_file = os.path.join(os.path.dirname(__file__), "mapping_v2.yml") content = parse_from_yaml(mapping_file) account_mapping = content["Insert Contacts"] with temporary_dir() as d: tmp_db_path = os.path.join(d, "temp.db") engine, metadata = create_db_file(tmp_db_path) t = create_table(account_mapping, metadata) assert t.name == "contacts" assert isinstance(t.columns["id"].type, Integer) assert isinstance(t.columns["first_name"].type, Unicode) assert isinstance(t.columns["last_name"].type, Unicode) assert isinstance(t.columns["email"].type, Unicode)
def test_init_options__sfdx_format(self, sfdx): with temporary_dir(): project_config = create_project_config() project_config.project__source_format = "sfdx" with open("sfdx-project.json", "w") as f: json.dump( { "packageDirectories": [{ "path": "force-app", "default": True }] }, f) task = create_task(RetrieveChanges, {}, project_config) assert not task.md_format assert task.options["path"] == "force-app"
def test_run_task(self, removeXmlElement): with temporary_dir() as path: revert_path = os.path.join(os.path.dirname(path), os.path.basename(path) + "_revert") project_config = BaseProjectConfig(BaseGlobalConfig(), config={"noyaml": True}) task_config = TaskConfig( {"options": { "path": path, "revert_path": revert_path }}) task = CreateUnmanagedEESrc(project_config, task_config) task() removeXmlElement.assert_called_once_with("availableFields", path, "*.object")
def test_run(self): api = mock.Mock() api.endpoint = "http://api" api.create_insert_job.side_effect = ["1", "3"] api.post_batch.side_effect = ["2", "4"] api.is_batch_done.side_effect = [False, True, True] api.headers.return_value = {} responses.add( method="GET", url="http://api/job/1/batch/2/result", body=b"Id\n1", status=200, ) responses.add( method="GET", url="https://example.com/services/data/vNone/query/?q=SELECT+Id+FROM+RecordType+WHERE+SObjectType%3D%27Account%27AND+DeveloperName+%3D+%27HH_Account%27+LIMIT+1", body=json.dumps({"records": [{"Id": "1"}]}), status=200, ) responses.add( method="GET", url="http://api/job/3/batch/4/result", body=b"Id\n1", status=200, ) base_path = os.path.dirname(__file__) db_path = os.path.join(base_path, "testdata.db") mapping_path = os.path.join(base_path, "mapping.yml") with temporary_dir() as d: tmp_db_path = os.path.join(d, "testdata.db") shutil.copyfile(db_path, tmp_db_path) task = _make_task( bulkdata.LoadData, { "options": { "database_url": "sqlite:///{}".format(tmp_db_path), "mapping": mapping_path, } }, ) task.bulk = api task() contact = task.session.query(task.tables["contacts"]).one() self.assertEquals("1", contact.sf_id) task.session.close()
def test_run_task(self,): task = create_task( ReportPushFailures, options={"request_id": "123", "ignore_errors": "IgnoreMe"}, ) def _init_class(): task.sf = mock.Mock() task.sf.query_all.side_effect = [ { "done": True, "totalSize": 2, "records": [ error_record(ErrorTitle="IgnoreMe"), error_record(gack=True), { "attributes": {"type": "job"}, "SubscriberOrganizationKey": "00Dxxx000000001", }, ], }, { "done": True, "totalSize": 1, "records": [ { "OrgKey": "00Dxxx000000001", "OrgName": "Test Org", "OrgType": "Sandbox", "OrgStatus": "Demo", "InstanceName": "CSxx", } ], }, ] task._init_class = _init_class with temporary_dir(): task() self.assertEqual(2, task.sf.query_all.call_count) self.assertTrue( os.path.isfile(task.result), "the result file does not exist" ) with open(task.result, "r") as f: reader = csv.DictReader(f) rows = list(reader) self.assertEqual(len(rows), 2) self.assertEqual(rows[1]["Stacktrace Id"], "-4532")
def test_run_task(self): with temporary_dir() as d: with open("dev.json", "w") as f: json.dump( { "settings": { "orgPreferenceSettings": {"s1DesktopEnabled": True}, "communitiesSettings": {"enableNetworksEnabled": True}, } }, f, ) path = os.path.join(d, "dev.json") task_options = {"definition_file": path} task = create_task(DeployOrgSettings, task_options) task.api_class = Mock() task() package_zip = task.api_class.call_args[0][1] zf = zipfile.ZipFile(io.BytesIO(base64.b64decode(package_zip)), "r") assert ( readtext(zf, "package.xml") == """<?xml version="1.0" encoding="UTF-8"?> <Package xmlns="http://soap.sforce.com/2006/04/metadata"> <types> <members>*</members> <name>Settings</name> </types> <version>47.0</version> </Package>""" ) assert ( readtext(zf, "settings/OrgPreference.settings") == """<?xml version="1.0" encoding="UTF-8"?> <OrgPreferenceSettings xmlns="http://soap.sforce.com/2006/04/metadata"> <preferences> <settingName>S1DesktopEnabled</settingName> <settingValue>True</settingValue> </preferences> </OrgPreferenceSettings>""" ) assert ( readtext(zf, "settings/Communities.settings") == """<?xml version="1.0" encoding="UTF-8"?> <CommunitiesSettings xmlns="http://soap.sforce.com/2006/04/metadata"> <enableNetworksEnabled>True</enableNetworksEnabled> </CommunitiesSettings>""" )
def test_run__v2__person_accounts_enabled(self, query_op_mock): base_path = os.path.dirname(__file__) mapping_path = os.path.join(base_path, self.mapping_file_v2) mock_describe_calls() with temporary_dir() as d: tmp_db_path = os.path.join(d, "testdata.db") task = _make_task( ExtractData, { "options": { "database_url": f"sqlite:///{tmp_db_path}", "mapping": mapping_path, } }, ) task.bulk = mock.Mock() task.sf = mock.Mock() task.org_config._is_person_accounts_enabled = True mock_query_households = MockBulkQueryOperation( sobject="Account", api_options={}, context=task, query="SELECT Id, Name, IsPersonAccount FROM Account", ) mock_query_contacts = MockBulkQueryOperation( sobject="Contact", api_options={}, context=task, query="SELECT Id, FirstName, LastName, Email, IsPersonAccount, AccountId FROM Contact", ) mock_query_households.results = [["1", "TestHousehold", "false"]] mock_query_contacts.results = [ ["2", "First", "Last", "*****@*****.**", "true", "1"] ] query_op_mock.side_effect = [mock_query_households, mock_query_contacts] task() with create_engine(task.options["database_url"]).connect() as conn: household = next(conn.execute("select * from households")) assert household.name == "TestHousehold" assert household.IsPersonAccount == "false" assert household.record_type == "HH_Account" contact = next(conn.execute("select * from contacts")) assert contact.household_id == "1" assert contact.IsPersonAccount == "true"
def _run_task(self): # Find or create Version if not self.dry_run: product = self._find_product() version = self._find_or_create_version(product) # Check out the specified tag repo_owner = self.project_config.repo_owner repo_name = self.project_config.repo_name gh = self.project_config.get_github_api() repo = gh.repository(repo_owner, repo_name) tag = self.options["tag"] commit_sha = repo.tag(repo.ref("tags/" + tag).object.sha).object.sha self.logger.info("Downloading commit {} of {} from GitHub".format( commit_sha, repo.full_name)) zf = download_extract_github(gh, repo_owner, repo_name, ref=commit_sha) with temporary_dir() as project_dir: zf.extractall(project_dir) project_config = BaseProjectConfig( self.project_config.global_config_obj, repo_info={ "root": project_dir, "owner": repo_owner, "name": repo_name, "url": self.project_config.repo_url, "branch": tag, "commit": commit_sha, }, ) project_config.set_keychain(self.project_config.keychain) # Create each plan for plan_name, plan_config in self.plan_configs.items(): steps = self._freeze_steps(project_config, plan_config) self.logger.debug("Prepared steps:\n" + json.dumps(steps, indent=4)) if not self.dry_run: self._publish_plan(product, version, plan_name, plan_config, steps) # Update version to set is_listed=True if not self.dry_run: self._call_api( "PATCH", "/versions/{}".format(version["id"]), json={"is_listed": True}, ) self.logger.info("Published Version {}".format(version["url"]))
def test_download_extract_zip_to_target(self): with utils.temporary_dir() as d: f = io.BytesIO() with zipfile.ZipFile(f, "w") as zf: zf.writestr("test", "test") f.seek(0) zipbytes = f.read() responses.add( method=responses.GET, url="http://test", body=zipbytes, content_type="application/zip", ) utils.download_extract_zip("http://test", target=d) assert "test" in os.listdir(d)
def test_remove_xml_element_directory(self): with utils.temporary_dir() as d: path = os.path.join(d, "test.xml") with open(path, "w") as f: f.write( '<?xml version="1.0" ?>' '<root xmlns="http://soap.sforce.com/2006/04/metadata">' "<tag>text</tag></root>") utils.remove_xml_element_directory("tag", d, "*") with open(path, "r") as f: result = f.read() expected = """<?xml version='1.0' encoding='UTF-8'?> <root xmlns="http://soap.sforce.com/2006/04/metadata" />""" assert expected == result
def local_github_checkout(repo_owner, repo_name, commit_ish=None): with temporary_dir() as repo_root: # pretend it's a git clone to satisfy cci os.mkdir(".git") repo = get_github_api_for_repo(None, repo_owner, repo_name) if commit_ish is None: commit_ish = repo.repository(repo_owner, repo_name).default_branch zip_file = download_extract_github(repo, repo_owner, repo_name, ref=commit_ish) zip_file.extractall(repo_root) yield repo_root
def test_create_table_legacy_oid_mapping(self): mapping_file = os.path.join(os.path.dirname(__file__), "mapping_v1.yml") with open(mapping_file, "r") as fh: content = yaml.safe_load(fh) account_mapping = content["Insert Contacts"] with temporary_dir() as d: tmp_db_path = os.path.join(d, "temp.db") engine, metadata = create_db_file(tmp_db_path) t = create_table(account_mapping, metadata) assert t.name == "contacts" assert isinstance(t.columns["sf_id"].type, Unicode) assert isinstance(t.columns["first_name"].type, Unicode) assert isinstance(t.columns["last_name"].type, Unicode) assert isinstance(t.columns["email"].type, Unicode)
def test_get_api__additional_options(self): with temporary_dir() as path: touch("package.xml") task = create_task( Deploy, { "path": path, "test_level": "RunSpecifiedTests", "specified_tests": "TestA,TestB", "unmanaged": False, }, ) api = task._get_api() assert api.run_tests == ["TestA", "TestB"] assert api.test_level == "RunSpecifiedTests"
def test_scratch_info_username_not_found(self, Command): Command.return_value = mock.Mock(stderr=io.BytesIO(b"error"), stdout=io.BytesIO(b"out"), returncode=0) config = ScratchOrgConfig( { "config_file": "tmp.json", "email_address": "*****@*****.**" }, "test") with temporary_dir(): with open("tmp.json", "w") as f: f.write("{}") with self.assertRaises(ScratchOrgException): config.scratch_info
def test_download_extract_zip_to_target(self): with utils.temporary_dir() as d: f = io.BytesIO() with zipfile.ZipFile(f, 'w') as zf: zf.writestr('test', 'test') f.seek(0) zipbytes = f.read() responses.add( method=responses.GET, url='http://test', body=zipbytes, content_type='application/zip', ) utils.download_extract_zip('http://test', target=d) self.assertIn('test', os.listdir(d))
def _run_task(self): self._infer_requirements() if not self.options["generate_record_type"]: self.logger.info( "Record types already enabled for {} in target org; no action needed" .format(self.options["sobject"])) return with temporary_dir() as tempdir: self._build_package() d = self._deploy(self.project_config, self.task_config, self.org_config, path=tempdir) d()
def test_run_task__additional_dependencies(self): project_config = create_project_config() project_config.keychain.get_service = Mock() project_config.project__package__name = "Project" project_config.project__name = "Project" project_config.project__package__namespace = "test" project_config.project__dependencies = [{"github": "http://example"}] task = create_task( GenerateDataDictionary, {"additional_dependencies": [{ "github": "http://test" }]}, project_config=project_config, ) task.get_repo = Mock() release = Mock() release.draft = False release.prerelease = False release.tag_name = "release/1.1" task.get_repo.return_value.releases.return_value = [release] task._get_repo_dependencies = Mock(return_value=[1, 2]) task._walk_releases = Mock() with temporary_dir(): task._run_task() task._get_repo_dependencies.assert_has_calls([ call([{ "github": "http://test" }], include_beta=False), call(project_config.project__dependencies, include_beta=False), ]) task._walk_releases.assert_has_calls([ call( Package( task.get_repo.return_value, project_config.project__package__name, "test__", "release/", )), call(1), call(2), call(1), call(2), ])
def test_fetch(self): responses.add( method=responses.GET, url=self.repo_api_url, json=self._get_expected_repo(owner="TestOwner", name="TestRepo"), ) responses.add( "GET", "https://api.github.com/repos/TestOwner/TestRepo/releases/latest", json=self._get_expected_release("release/1.0"), ) responses.add( "GET", "https://api.github.com/repos/TestOwner/TestRepo/git/refs/tags/release/1.0", json=self._get_expected_tag_ref("release/1.0", "tag_sha"), ) f = io.BytesIO() zf = zipfile.ZipFile(f, "w") zfi = zipfile.ZipInfo("toplevel/") zf.writestr(zfi, "") zf.writestr( "toplevel/cumulusci.yml", yaml.dump({ "project": { "package": { "name_managed": "Test Product", "namespace": "ns" } } }), ) zf.close() responses.add( "GET", "https://api.github.com/repos/TestOwner/TestRepo/zipball/tag_sha", body=f.getvalue(), content_type="application/zip", ) source = GitHubSource( self.project_config, {"github": "https://github.com/TestOwner/TestRepo.git"}) with temporary_dir() as d: project_config = source.fetch() assert isinstance(project_config, BaseProjectConfig) assert project_config.repo_root == os.path.join( os.path.realpath(d), ".cci", "projects", "TestRepo", "tag_sha")
def _get_destructive_changes(self, path=None): self.logger.info( "Retrieving metadata in package {} from target org".format( self.options["package"])) packaged = self._retrieve_packaged() path = os.path.abspath(self.options["path"]) with temporary_dir() as tempdir: packaged.extractall(tempdir) destructive_changes = self._package_xml_diff( os.path.join(path, "package.xml"), os.path.join(tempdir, "package.xml")) self.logger.info("Deleting metadata in package {} from target org". format(self.options["package"]) if destructive_changes else "No metadata found to delete") return destructive_changes
def test_project_init_tasks(self, click): """Verify that the generated cumulusci.yml file is readable and has the proper robot task""" with temporary_dir(): os.mkdir(".git") click.prompt.side_effect = ( "testproj", # project_name "testpkg", # package_name "testns", # package_namespace "43.0", # api_version "mdapi", # source_format "3", # extend other URL "https://github.com/SalesforceFoundation/Cumulus", # github_url "default", # git_default_branch "work/", # git_prefix_feature "uat/", # git_prefix_beta "rel/", # git_prefix_release "%_TEST%", # test_name_match ) click.confirm.side_effect = (True, True ) # is managed? # extending? run_click_command(cci.project_init) # verify we can load the generated yml cli_runtime = CliRuntime(load_keychain=False) # ...and verify it has the expected tasks config = cli_runtime.project_config.config_project expected_tasks = { "robot": { "options": { "suites": u"robot/testproj/tests", "options": { "outputdir": "robot/testproj/results" }, } }, "robot_testdoc": { "options": { "path": "robot/testproj/tests", "output": "robot/testproj/doc/testproj_tests.html", } }, } self.assertDictEqual(config["tasks"], expected_tasks)
def _convert_sfdx_format(self, path, name): orig_path = path with contextlib.ExitStack() as stack: if not pathlib.Path(path, "package.xml").exists(): self.logger.info("Converting from sfdx to mdapi format") path = stack.enter_context(temporary_dir(chdir=False)) args = ["-r", str(orig_path), "-d", path] if name: args += ["-n", name] sfdx( "force:source:convert", args=args, capture_output=False, check_return=True, ) yield path
def test_get_api(self): with temporary_dir() as path: touch("package.xml") task = create_task( Deploy, { "path": path, "namespace_tokenize": "ns", "namespace_inject": "ns", "namespace_strip": "ns", }, ) api = task._get_api() zf = zipfile.ZipFile(io.BytesIO(base64.b64decode(api.package_zip)), "r") self.assertIn("package.xml", zf.namelist())