diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a6e197c..1b987d6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to the Zowe Client Python SDK will be documented in this file. +## Recent Changes + +### Enhancements + +### Bug Fixes + +- Fixed a bug on `create` in `Datasets` where the target dataset gets created with a different block size when `like` is specified [#295] (https://github.com/zowe/zowe-client-python-sdk/issues/295) + ## `1.0.0-dev17` ### Enhancements diff --git a/src/zos_files/zowe/zos_files_for_zowe_sdk/datasets.py b/src/zos_files/zowe/zos_files_for_zowe_sdk/datasets.py index 32f8230a..7b02401f 100644 --- a/src/zos_files/zowe/zos_files_for_zowe_sdk/datasets.py +++ b/src/zos_files/zowe/zos_files_for_zowe_sdk/datasets.py @@ -374,8 +374,8 @@ def create(self, dataset_name, options: Optional[DatasetOption] = None): if options.like is None: if options.primary is None or options.lrecl is None: - self.logger.error("If 'like' is not specified, you must specify 'primary' or 'lrecl'.") - raise ValueError("If 'like' is not specified, you must specify 'primary' or 'lrecl'.") + self.logger.error("If 'like' is not specified, you must specify 'primary' and 'lrecl'.") + raise ValueError("If 'like' is not specified, you must specify 'primary' and 'lrecl'.") if options.dirblk is not None: if options.dsorg == "PS": if options.dirblk != 0: @@ -385,6 +385,12 @@ def create(self, dataset_name, options: Optional[DatasetOption] = None): if options.dirblk == 0: self.logger.error("Can't allocate empty directory blocks.") raise ValueError + else: + dsn_attr = self.list(options.like, return_attributes=True)["items"] + for dsn in dsn_attr: + if dsn["dsname"] == options.like.upper(): + options.blksize = int(dsn["blksz"]) + break custom_args = self._create_custom_request_arguments() custom_args["url"] = "{}ds/{}".format(self._request_endpoint, self._encode_uri_component(dataset_name)) diff --git a/tests/unit/files/datasets/test_create.py b/tests/unit/files/datasets/test_create.py index f5d1dfeb..2f477f4d 100644 --- a/tests/unit/files/datasets/test_create.py +++ b/tests/unit/files/datasets/test_create.py @@ -27,6 +27,22 @@ def test_create_data_set_accept_valid_recfm(self, mock_send_request): Files(self.test_profile).create_data_set("DSNAME123", options=option) mock_send_request.assert_called() + @mock.patch("requests.Session.send") + def test_create_data_set_with_like(self, mock_send_request: mock.Mock): + """Test if create dataset does accept all accepted record formats""" + + option = DatasetOption(like="test") + + response1 = mock.Mock( + headers={"Content-Type": "application/octet-stream"}, + status_code=200, + content={"items": [{"dsname": "test", "blksz": 123}]}, + ) + response2 = mock.Mock(headers={"Content-Type": "application/json"}, status_code=201) + mock_send_request.side_effect = [response1, response2] + Files(self.test_profile).create_data_set("DSNAME123", options=option) + self.assertEqual(mock_send_request.call_count, 2) + def test_create_data_set_does_not_accept_invalid_recfm(self): """Test if create dataset raises an error for invalid record formats""" with self.assertRaises(KeyError): @@ -38,7 +54,7 @@ def test_create_data_set_raises_error_without_required_arguments(self): option = DatasetOption(alcunit="CYL", dsorg="PO", primary=1, dirblk=25, recfm="FB", blksize=6160) with self.assertRaises(ValueError) as e_info: obj = Files(self.test_profile).create_data_set("DSNAME123", options=option) - self.assertEqual(str(e_info.exception), "If 'like' is not specified, you must specify 'primary' or 'lrecl'.") + self.assertEqual(str(e_info.exception), "If 'like' is not specified, you must specify 'primary' and 'lrecl'.") def test_create_data_set_raises_error_with_invalid_arguments_parameterized(self): """Test not providing valid arguments raises an error""" @@ -117,7 +133,7 @@ def test_create_data_set_parameterized(self): with self.assertRaises(ValueError) as e_info: files_test_profile.create_data_set("DSN", test_case[0]) self.assertEqual( - str(e_info.exception), "If 'like' is not specified, you must specify 'primary' or 'lrecl'." + str(e_info.exception), "If 'like' is not specified, you must specify 'primary' and 'lrecl'." ) @mock.patch("requests.Session.send")