Skip to content

Commit

Permalink
Add some unit tests. Bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed May 8, 2024
1 parent fa9d6dc commit 1e810dd
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 3 deletions.
2 changes: 1 addition & 1 deletion multiversx_sdk/abi/parts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PartsHolder:
Both functionalities (building and reading) are kept within this single abstraction, for convenience.
"""

def __init__(self, parts: List[bytes] = []):
def __init__(self, parts: List[bytes]):
"""
Creates a new PartsHolder, which has the given parts.
Focus is on the first part, if any, or "beyond the last part" otherwise.
Expand Down
2 changes: 1 addition & 1 deletion multiversx_sdk/abi/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def serialize(self, input_values: List[Any]) -> str:
return self._encode_parts(parts)

def serialize_to_parts(self, input_values: List[Any]) -> List[bytes]:
parts_holder = PartsHolder()
parts_holder = PartsHolder([])
self._do_serialize(parts_holder, input_values)
return parts_holder.get_parts()

Expand Down
128 changes: 128 additions & 0 deletions multiversx_sdk/abi/serializer_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import pytest

from multiversx_sdk.abi.serializer import Serializer
from multiversx_sdk.abi.values_multi import *
from multiversx_sdk.abi.values_single import *


def test_serialize():
serializer = Serializer(parts_separator="@", pub_key_length=32)

# u8
data = serializer.serialize([
U8Value(0x42)
])

assert data == "42"

# u16
data = serializer.serialize([
U16Value(0x4243)
])

assert data == "4243"

# u8, u16
data = serializer.serialize([
U8Value(0x42),
U16Value(0x4243),
])

assert data == "42@4243"

# optional (missing)
data = serializer.serialize([
U8Value(0x42),
InputOptionalValue(),
])

assert data == "42"

# optional (provided)
data = serializer.serialize([
U8Value(0x42),
InputOptionalValue(U8Value(0x43)),
])

assert data == "42@43"

# optional: should err because optional must be last
with pytest.raises(ValueError, match="^an optional value must be last among input values$"):
serializer.serialize([
InputOptionalValue(U8Value(0x43)),
U8Value(0x42),
])

# multi<u8, u16, u32>
data = serializer.serialize([
InputMultiValue([
U8Value(0x42),
U16Value(0x4243),
U32Value(0x42434445),
]),
])

assert data == "42@4243@42434445"

# u8, multi<u8, u16, u32>
data = serializer.serialize([
U8Value(0x42),
InputMultiValue([
U8Value(0x42),
U16Value(0x4243),
U32Value(0x42434445),
]),
])

assert data == "42@42@4243@42434445"

# multi<multi<u8, u16>, multi<u8, u16>>
data = serializer.serialize([
InputMultiValue([
InputMultiValue([
U8Value(0x42),
U16Value(0x4243),
]),
InputMultiValue([
U8Value(0x44),
U16Value(0x4445),
]),
]),
])

assert data == "42@4243@44@4445"

# variadic, of different types
data = serializer.serialize([
InputVariadicValues([
U8Value(0x42),
U16Value(0x4243),
]),
])

# For now, the serializer does not perform such a strict type check.
# Although doable, it would be slightly complex and, if done, might be even dropped in the future
# (with respect to the decoder that is embedded in Rust-based smart contracts).
assert data == "42@4243"

# variadic<u8>, u8: should err because variadic must be last
with pytest.raises(ValueError, match="^variadic values must be last among input values$"):
serializer.serialize([
InputVariadicValues([
U8Value(0x42),
U8Value(0x43),
]),
U8Value(0x44),
])

# u8, variadic<u8>
data = serializer.serialize([
U8Value(0x41),
InputVariadicValues([
U8Value(0x42),
U8Value(0x43),
]),
])

assert data == "41@42@43"

2 changes: 1 addition & 1 deletion multiversx_sdk/abi/values_multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, items: List[Any], item_creator: Callable[[], Any]):


class InputOptionalValue:
def __init__(self, value: Any):
def __init__(self, value: Any = None):
self.value = value


Expand Down

0 comments on commit 1e810dd

Please sign in to comment.