Skip to content

Commit

Permalink
add component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ribeirojose committed Apr 2, 2024
1 parent 21fbd4e commit 7e56f1c
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 18 deletions.
5 changes: 5 additions & 0 deletions cow_py/codegen/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .abi_handler import ABIHandler

__all__ = [
"ABIHandler",
]
2 changes: 2 additions & 0 deletions cow_py/codegen/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from cow_py.codegen.components.contract_loader import ContractLoader
from cow_py.codegen.components.base_mixin import BaseMixin
from cow_py.codegen.components.get_abi_file import get_abi_file
from cow_py.codegen.components.templates import partials

__all__ = [
"BaseContract",
Expand All @@ -12,4 +13,5 @@
"ContractLoader",
"BaseMixin",
"get_abi_file",
"partials",
]
56 changes: 38 additions & 18 deletions cow_py/codegen/solidity_converter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Constants
import re
from typing import Optional


SOLIDITY_TO_PYTHON_TYPES = {
"address": "str",
"bool": "bool",
Expand Down Expand Up @@ -66,23 +69,40 @@ def convert_type(cls, solidity_type: str, internal_type: str) -> str:
Returns:
str: The Python type equivalent to the given Solidity type.
"""
if internal_type and "enum" in internal_type:
return internal_type.split("enum")[-1].split(".")[-1].strip()
if "[]" in solidity_type:
base_type = solidity_type.replace("[]", "")
return f'List[{SOLIDITY_TO_PYTHON_TYPES.get(base_type, "Any")}]'
elif "[" in solidity_type and "]" in solidity_type:
base_type = solidity_type.split("[")[0]
return f'List[{SOLIDITY_TO_PYTHON_TYPES.get(base_type, "Any")}]'
if re.search(r"enum", internal_type) or (re.search(r"enum", solidity_type)):
return cls._extract_enum_name(internal_type, solidity_type)
elif solidity_type == "tuple":
return cls._get_struct_name(internal_type)
elif "[" in solidity_type and "]" in solidity_type:
array_size = solidity_type[
solidity_type.index("[") + 1 : solidity_type.index("]")
]
base_type = solidity_type.split("[")[0]
if array_size:
return f'Tuple[{", ".join([SOLIDITY_TO_PYTHON_TYPES.get(base_type, "Any")] * int(array_size))}]'
else:
else:
return cls._convert_array_or_basic_type(solidity_type)

@staticmethod
def _extract_enum_name(
internal_type: Optional[str], solidity_type: Optional[str] = None
) -> str:
if internal_type and re.search(r"enum", internal_type):
return internal_type.replace("enum ", "").replace(".", "_")
elif solidity_type and re.search(r"enum", solidity_type):
return solidity_type.replace("enum ", "").replace(".", "_")
raise SolidityConverterError(f"Invalid internal type for enum: {internal_type}")

@staticmethod
def _convert_array_or_basic_type(solidity_type: str) -> str:
array_match = re.match(r"(.+?)(\[\d*\])", solidity_type)
if array_match:
base_type, array_size = array_match.groups()
if array_size == "[]":
return f'List[{SOLIDITY_TO_PYTHON_TYPES.get(base_type, "Any")}]'
return SOLIDITY_TO_PYTHON_TYPES.get(solidity_type, "Any")
else:
size = int(array_size[1:-1])
return f'Tuple[{", ".join([SOLIDITY_TO_PYTHON_TYPES.get(base_type, "Any")] * size)}]'
else:
return SOLIDITY_TO_PYTHON_TYPES.get(solidity_type, "Any")

@staticmethod
def _get_struct_name(internal_type: str) -> str:
if not internal_type or "struct " not in internal_type:
raise SolidityConverterError(
f"Invalid internal type for struct: {internal_type}"
)
return internal_type.replace("struct ", "").replace(".", "_").replace("[]", "")
21 changes: 21 additions & 0 deletions tests/codegen/components/test_base_contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# tests/codegen/components/test_base_contract.py


def test_base_contract_singleton():
# Test that BaseContract follows the singleton pattern
pass


def test_base_contract_function_exists_in_abi():
# Test that BaseContract._function_exists_in_abi correctly checks function existence
pass


def test_base_contract_event_exists_in_abi():
# Test that BaseContract._event_exists_in_abi correctly checks event existence
pass


def test_base_contract_getattr():
# Test that BaseContract.__getattr__ correctly handles attribute access
pass
11 changes: 11 additions & 0 deletions tests/codegen/components/test_contract_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# tests/codegen/components/test_contract_factory.py


def test_contract_factory_get_contract_class():
# Test that ContractFactory.get_contract_class correctly retrieves or creates contract class
pass


def test_contract_factory_create():
# Test that ContractFactory.create correctly creates an instance of the contract class
pass
45 changes: 45 additions & 0 deletions tests/codegen/test_abi_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,48 @@
)
def test_to_python_conventional_name(input_name, expected_output):
assert to_python_conventional_name(input_name) == expected_output


def test_compile_partial():
# Test that compile_partial correctly compiles a partial template
pass


def test_get_filename_without_extension():
# Test that get_filename_without_extension correctly removes the extension
pass


def test_get_template_file():
# Test that _get_template_file returns the correct template file path
pass


def test_get_partials_files():
# Test that _get_partials_files returns the correct list of partial files
pass


def test_abi_handler_generate():
# Test that ABIHandler.generate correctly generates Python code from an ABI
pass


def test_abi_handler_prepare_template_data():
# Test that ABIHandler._prepare_template_data correctly processes the ABI
pass


def test_abi_handler_process_parameters():
# Test that ABIHandler._process_parameters correctly processes function parameters
pass


def test_abi_handler_process_function():
# Test that ABIHandler._process_function correctly processes a function item
pass


def test_abi_handler_render_template():
# Test that ABIHandler._render_template correctly renders the template with data
pass
83 changes: 83 additions & 0 deletions tests/codegen/test_solidity_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import pytest
from cow_py.codegen.solidity_converter import SolidityConverter, SolidityConverterError


def test_solidity_converter_get_struct_name():
internal_type = "struct MyStruct"
expected_result = "MyStruct"
result = SolidityConverter._get_struct_name(internal_type)
assert result == expected_result


def test_solidity_converter_get_struct_name_invalid_internal_type():
internal_type = "uint256"
with pytest.raises(SolidityConverterError):
SolidityConverter._get_struct_name(internal_type)


def test_solidity_converter_convert_type_enum():
solidity_type = "enum MyEnum"
internal_type = ""
expected_result = "MyEnum"
result = SolidityConverter.convert_type(solidity_type, internal_type)
assert result == expected_result


def test_solidity_converter_convert_type_array():
solidity_type = "uint256[]"
internal_type = ""
expected_result = "List[int]"
result = SolidityConverter.convert_type(solidity_type, internal_type)
assert result == expected_result


def test_solidity_converter_convert_type_tuple():
solidity_type = "tuple"
internal_type = "struct MyStruct"
expected_result = "MyStruct"
result = SolidityConverter.convert_type(solidity_type, internal_type)
assert result == expected_result


def test_solidity_converter_convert_type_fixed_size_array():
solidity_type = "uint256[3]"
internal_type = ""
expected_result = "Tuple[int, int, int]"
result = SolidityConverter.convert_type(solidity_type, internal_type)
assert result == expected_result


def test_solidity_converter_convert_type_unknown_type():
solidity_type = "unknown_type"
internal_type = ""
expected_result = "Any"
result = SolidityConverter.convert_type(solidity_type, internal_type)
assert result == expected_result


def test_solidity_converter_extract_enum_name():
internal_type = "enum MyEnum.Option"
expected_result = "MyEnum_Option"
result = SolidityConverter._extract_enum_name(internal_type)
assert result == expected_result


def test_solidity_converter_convert_array_or_basic_type_dynamic_array():
solidity_type = "address[]"
expected_result = "List[str]"
result = SolidityConverter._convert_array_or_basic_type(solidity_type)
assert result == expected_result


def test_solidity_converter_convert_array_or_basic_type_fixed_size_array():
solidity_type = "bool[5]"
expected_result = "Tuple[bool, bool, bool, bool, bool]"
result = SolidityConverter._convert_array_or_basic_type(solidity_type)
assert result == expected_result


def test_solidity_converter_convert_array_or_basic_type_basic_type():
solidity_type = "bytes32"
expected_result = "HexBytes"
result = SolidityConverter._convert_array_or_basic_type(solidity_type)
assert result == expected_result

0 comments on commit 7e56f1c

Please sign in to comment.