Skip to content

Commit

Permalink
Merge pull request #304 from zowe/Allocate-like
Browse files Browse the repository at this point in the history
Update create
  • Loading branch information
zFernand0 authored Jul 8, 2024
2 parents a9cebff + 017549f commit a903f49
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/zos_files/zowe/zos_files_for_zowe_sdk/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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))
Expand Down
20 changes: 18 additions & 2 deletions tests/unit/files/datasets/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"""
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit a903f49

Please sign in to comment.