Skip to content

Commit ce02a05

Browse files
committed
Removed incorrect Bool.new() staticmethod
Added deserialization Enum unit test
1 parent 6a7cc67 commit ce02a05

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

scalecodec/types.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,6 @@ def example_value(self, _recursion_level: int = 0, max_recursion: int = TYPE_DEC
135135

136136
class Bool(ScalePrimitive):
137137

138-
@classmethod
139-
def new(cls):
140-
return ScaleType(type_def=cls())
141-
142138
def decode(self, data: ScaleBytes) -> bool:
143139

144140
bool_data = data.get_next_bytes(1)
@@ -898,8 +894,10 @@ def _encode(self, value: Union[str, bytes]) -> ScaleBytes:
898894
def serialize(self, value: bytes) -> str:
899895
return f'0x{value.hex()}'
900896

901-
def deserialize(self, value: str) -> bytes:
902-
return bytes.fromhex(value[2:])
897+
def deserialize(self, value: Union[str, bytes]) -> bytes:
898+
if type(value) is str:
899+
value = bytes.fromhex(value[2:])
900+
return value
903901

904902
def example_value(self, _recursion_level: int = 0, max_recursion: int = TYPE_DECOMP_MAX_RECURSIVE):
905903
return f'0x{str(self.byte_count).zfill(2) * self.byte_count}'

test/__init__.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Python SCALE Codec Library
22
#
3-
# Copyright 2018-2020 Stichting Polkascan (Polkascan Foundation).
3+
# Copyright 2018-2024 Stichting Polkascan (Polkascan Foundation).
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -13,13 +13,4 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16-
import json
17-
import os
1816

19-
20-
def load_json_file(file_path: str) -> dict:
21-
22-
with open(os.path.abspath(file_path), 'r') as fp:
23-
data = fp.read()
24-
25-
return json.loads(data)

test/test_enum.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
class TestEnum(unittest.TestCase):
2424

25-
def test_enum(self):
25+
def test_enum_encode_decode(self):
2626
scale_obj = Enum(Bool=Bool(), Number=U32, None_=None).new()
2727
value = {'Bool': True}
2828

@@ -45,6 +45,21 @@ def test_enum(self):
4545

4646
self.assertEqual(value, scale_obj.value)
4747

48+
def test_enum_deserialize(self):
49+
scale_obj = Enum(Bool=Bool(), Number=U32, None_=None).new()
50+
51+
scale_obj.deserialize({'Bool': True})
52+
self.assertEqual(('Bool', Bool().new(value=True)), scale_obj.value_object)
53+
54+
scale_obj.deserialize({'Number': 1})
55+
self.assertEqual(('Number', U32.new(value=1)), scale_obj.value_object)
56+
57+
scale_obj.deserialize({'None': None})
58+
self.assertEqual(('None', None), scale_obj.value_object)
59+
60+
scale_obj.deserialize('None')
61+
self.assertEqual(('None', None), scale_obj.value_object)
62+
4863

4964
if __name__ == '__main__':
5065
unittest.main()

0 commit comments

Comments
 (0)