From f758904ab863a2d00361702ab32dc80d4dd25798 Mon Sep 17 00:00:00 2001 From: jreadey Date: Thu, 2 Nov 2023 15:50:35 -0700 Subject: [PATCH] updates for fixed utf8 attribute values --- hsds/attr_sn.py | 5 +++ tests/integ/attr_test.py | 67 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/hsds/attr_sn.py b/hsds/attr_sn.py index da78f4cb..d7c865f9 100755 --- a/hsds/attr_sn.py +++ b/hsds/attr_sn.py @@ -649,6 +649,7 @@ async def PUT_AttributeValue(request): raise HTTPBadRequest(reason=msg) np_shape = getShapeDims(attr_shape) + log.debug(f"np_shape: {np_shape}") type_json = dn_json["type"] np_dtype = createDataType(type_json) # np datatype @@ -697,6 +698,10 @@ async def PUT_AttributeValue(request): # convert to JSON for transmission to DN data = arr.tolist() value = bytesArrayToList(data) + if attr_shape["class"] == "H5S_SCALAR": + # just send the value, not a list + value = value[0] + else: try: body = await request.json() diff --git a/tests/integ/attr_test.py b/tests/integ/attr_test.py index 2dbc1e57..b4aa5f83 100644 --- a/tests/integ/attr_test.py +++ b/tests/integ/attr_test.py @@ -600,6 +600,7 @@ def testPutFixedUTF8String(self): rsp = self.session.get(req, headers=headers) self.assertEqual(rsp.status_code, 200) rspJson = json.loads(rsp.text) + print(rspJson) self.assertTrue("hrefs" in rspJson) self.assertTrue("value" in rspJson) self.assertEqual(rspJson["value"], text) @@ -614,6 +615,70 @@ def testPutFixedUTF8String(self): self.assertTrue("charSet" in type_json) self.assertEqual(type_json["charSet"], "H5T_CSET_UTF8") + def testPutFixedUTF8StringBinary(self): + # Test PUT value for 1d attribute with fixed length UTF-8 string in binary + print("testPutFixedUTF8StringBinary", self.base_domain) + + headers = helper.getRequestHeaders(domain=self.base_domain) + req = self.endpoint + "/" + + # Get root uuid + rsp = self.session.get(req, headers=headers) + self.assertEqual(rsp.status_code, 200) + rspJson = json.loads(rsp.text) + root_uuid = rspJson["root"] + helper.validateId(root_uuid) + + # create attr with json + character_text = "this is the chinese character for the number eight: \u516b" + + binary_text = bytearray(character_text, "UTF-8") + byte_length = len(binary_text) + + fixed_str_type = { + "charSet": "H5T_CSET_UTF8", + "class": "H5T_STRING", + "length": byte_length, # Null byte explicitly included + "strPad": "H5T_STR_NULLTERM", + } + + scalar_shape = {"class": "H5S_SCALAR"} + data = {"type": fixed_str_type, "shape": scalar_shape} + attr_name = "fixed_unicode_str_attr_binary" + req = self.endpoint + "/groups/" + root_uuid + "/attributes/" + attr_name + rsp = self.session.put(req, data=json.dumps(data), headers=headers) + self.assertEqual(rsp.status_code, 201) + + # write to attr in binary + attr_name = "fixed_unicode_str_attr_binary" + req = self.endpoint + "/groups/" + root_uuid + "/attributes/" + attr_name + "/value" + headers["Content-Type"] = "application/octet-stream" + rsp = self.session.put(req, data=binary_text, headers=headers) + self.assertEqual(rsp.status_code, 200) + + # read attr + headers["Content-Type"] = "application/json" + req = self.endpoint + "/groups/" + root_uuid + "/attributes/" + attr_name + + rsp = self.session.get(req, headers=headers) + self.assertEqual(rsp.status_code, 200) + rspJson = json.loads(rsp.text) + print(rspJson) + self.assertTrue("hrefs" in rspJson) + self.assertTrue("value" in rspJson) + print(f"Retrieved UTF8 string: {rspJson['value']}") + self.assertEqual(rspJson["value"], character_text) + self.assertTrue("type" in rspJson) + type_json = rspJson["type"] + self.assertTrue("class" in type_json) + self.assertEqual(type_json["class"], "H5T_STRING") + self.assertTrue("length" in type_json) + self.assertEqual(type_json["length"], byte_length) + self.assertTrue("strPad" in type_json) + self.assertEqual(type_json["strPad"], "H5T_STR_NULLTERM") + self.assertTrue("charSet" in type_json) + self.assertEqual(type_json["charSet"], "H5T_CSET_UTF8") + def testPutVLenString(self): # Test PUT value for 1d attribute with variable length string types print("testPutVLenString", self.base_domain) @@ -1340,7 +1405,7 @@ def testPutAttributeBinaryValue(self): rsp = self.session.put(req, data=data, headers=headers_bin_req) self.assertEqual(rsp.status_code, 200) - # try writing to few bytes, should fail + # try writing too few bytes, should fail data = bytearray(extent) for i in range(extent): data[i] = 255