Skip to content

Restructured unit tests #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions scalecodec/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def deserialize(self, value: bool) -> bool:
def example_value(self, _recursion_level: int = 0, max_recursion: int = TYPE_DECOMP_MAX_RECURSIVE):
return True


class NullType(ScaleTypeDef):

def decode(self, data: ScaleBytes) -> any:
Expand Down Expand Up @@ -798,7 +799,7 @@ def deserialize(self, value: list) -> list:
return [(self.key_def.deserialize(k), self.value_def.deserialize(v)) for k, v in value]


class Bytes(ScaleTypeDef):
class BytesDef(ScaleTypeDef):
"""
A variable collection of bytes, stored as an `Vec<u8>`
"""
Expand Down Expand Up @@ -844,7 +845,7 @@ def example_value(self, _recursion_level: int = 0, max_recursion: int = TYPE_DEC
return b'Bytes'


class String(Bytes):
class StringDef(BytesDef):
def decode(self, data: ScaleBytes) -> str:
value = super().decode(data)

Expand Down Expand Up @@ -900,8 +901,8 @@ def example_value(self, _recursion_level: int = 0, max_recursion: int = TYPE_DEC
return f'0x{str(self.byte_count).zfill(2) * self.byte_count}'


String = String()
Bytes = Bytes()
String = StringDef()
Bytes = BytesDef()
Type = String
Text = String
H256 = HashDef(256)
Expand Down
258 changes: 0 additions & 258 deletions scalecodec/utils/ss58.py

This file was deleted.

82 changes: 82 additions & 0 deletions test/test_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Python SCALE Codec Library
#
# Copyright 2018-2024 Stichting Polkascan (Polkascan Foundation).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import unittest

from scalecodec.base import ScaleBytes
from scalecodec.exceptions import ScaleEncodeException
from scalecodec.types import Array, U32, U8


class TestArray(unittest.TestCase):

def test_dynamic_fixed_array_type_decode(self):
obj = Array(U32, 1).new()
self.assertEqual([1], obj.decode(ScaleBytes("0x01000000")))

obj = Array(U32, 3).new()
self.assertEqual([1, 2, 3], obj.decode(ScaleBytes("0x010000000200000003000000")))

obj = Array(U32, 0).new()
self.assertEqual([], obj.decode(ScaleBytes(bytes())))

def test_dynamic_fixed_array_type_decode_u8(self):
obj = Array(U8, 65).new()
obj.decode(ScaleBytes(
"0xc42b82d02bce3202f6a05d4b06d1ad46963d3be36fd0528bbe90e7f7a4e5fcd38d14234b1c9fcee920d76cfcf43b4ed5dd718e357c2bc1aae3a642975207e67f01"
))
self.assertEqual(
"0xc42b82d02bce3202f6a05d4b06d1ad46963d3be36fd0528bbe90e7f7a4e5fcd38d14234b1c9fcee920d76cfcf43b4ed5dd718e357c2bc1aae3a642975207e67f01",
obj.value
)

def test_array_type_encode_u8(self):
obj = Array(U8, 2).new()
self.assertEqual('0x0102', str(obj.encode('0x0102')))
self.assertEqual('0x0102', str(obj.encode(b'\x01\x02')))
self.assertEqual('0x0102', str(obj.encode([1, 2])))

def test_array_type_encode(self):
obj = Array(U32, 2).new()
self.assertEqual('0x0100000002000000', str(obj.encode([1, 2])))

obj = Array(U8, 3).new()
self.assertEqual('0x010203', str(obj.encode('0x010203')))

def test_invalid_array_encode(self):
obj = Array(U8, 3).new()
self.assertRaises(ScaleEncodeException, obj.encode, '0x0102')

obj = Array(U32, 3).new()
self.assertRaises(ScaleEncodeException, obj.encode, '0x0102')

def test_array_u8(self):
obj = Array(U8, 4).new()

value = [1, 2, 3, 4]
data = obj.encode(value)

self.assertEqual(data, ScaleBytes('0x01020304'))

data.reset()
self.assertEqual('0x01020304', obj.decode(data))

self.assertEqual(obj.value_object, b'\x01\x02\x03\x04')


if __name__ == '__main__':
unittest.main()
Loading
Loading