Skip to content

[Feature] Add Tox and Pytest packages for testing and utilities unit tests #69

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[pytest]
pythonpath = src/
testpaths = tests
python_files = test_*.py
python_classes = Test
python_functions = test
4 changes: 3 additions & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ web3==5.31.3
websockets==9.1
yarl==1.8.2
zipp==3.11.0
websocket-client==1.5.1
websocket-client==1.5.1
pytest==7.3.1
tox==4.5.1
Empty file added tests/__init__.py
Empty file.
140 changes: 140 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
from enum import Enum

import pytest
from web3 import Web3

from firefly_exchange_client.utilities import (address_to_bytes32,
bn_to_bytes8, config_logging,
current_unix_timestamp,
default_enum_value,
default_value, extract_enums,
extract_query, hash_string,
random_number, strip_hex_prefix)


@pytest.mark.parametrize(
"input_string, expected_output",
[
("0x123", "123"),
("123", "123"),
("0xABCD", "ABCD"),
],
)
def test_strip_hex_prefix(input_string, expected_output):
"""
Test case to verify strip_hex_prefix function.
It checks if the hex prefix '0x' is correctly stripped from the input string.
"""
assert strip_hex_prefix(input_string) == expected_output


def test_address_to_bytes32():
"""
Test case to verify address_to_bytes32 function.
It checks if the address is correctly converted to bytes32 format.
"""
address = "0x123"
expected_output = "0x000000000000000000000000123"
assert address_to_bytes32(address) == expected_output


def test_hash_string():
"""
Test case to verify hash_string function.
It checks if the input string is correctly hashed using soliditySha3.
"""
value = "example"
expected_output = Web3.soliditySha3(["string"], [value]).hex()
assert hash_string(value) == expected_output


def test_bn_to_bytes8():
"""
Test case to verify bn_to_bytes8 function.
It checks if the input value is correctly converted to bytes8 format.
"""
value = 123
expected_output = f"0x{'0'*16}{hex(value)[2:]}".encode("utf-8")
assert bn_to_bytes8(value) == expected_output


def test_default_value():
"""
Test case to verify default_value function.
It checks if the function correctly returns the default value when the key is not present in the dictionary.
"""
data = {"key1": "value1", "key2": "value2"}
key = "key3"
default_val = "default"
assert default_value(data, key, default_val) == default_val


def test_default_enum_value():
"""
Test case to verify default_enum_value function.
It checks if the function correctly returns the default enum value when the key is not present in the dictionary.
"""

class MyEnum(Enum):
VALUE1 = 1
VALUE2 = 2

data = {"key1": MyEnum.VALUE1, "key2": MyEnum.VALUE2}
key = "key3"
default_val = MyEnum.VALUE1
assert default_enum_value(data, key, default_val) == default_val.value


def test_current_unix_timestamp():
"""
Test case to verify current_unix_timestamp function.
It checks if the function returns an integer value representing the current UNIX timestamp.
"""
timestamp = current_unix_timestamp()
assert isinstance(timestamp, int)


def test_random_number():
"""
Test case to verify random_number function.
It checks if the function returns a random number within the specified range.
"""
max_range = 100
number = random_number(max_range)
assert isinstance(number, int)
assert number >= current_unix_timestamp()
assert number <= current_unix_timestamp() + max_range * 2


def test_extract_query():
"""
Test case to verify extract_query function.
It checks if the function correctly extracts and formats query parameters from a dictionary.
"""
value = {"param1": "value1", "param2": "value2"}
expected_output = "param1=value1&param2=value2"
assert extract_query(value) == expected_output


def test_extract_enums():
"""
Test case to verify extract_enums function.
It checks if the function correctly extracts enum values from a dictionary based on the provided enum list.
"""

class MyEnum(Enum):
VALUE1 = 1
VALUE2 = 2

params = {
"param1": MyEnum.VALUE1.value,
"param2": [MyEnum.VALUE2.value, MyEnum.VALUE1.value],
"param3": "value3",
}
enums = [MyEnum.VALUE1, MyEnum.VALUE2]
expected_output = {
"param1": MyEnum.VALUE1.value,
"param2": [MyEnum.VALUE2.value, MyEnum.VALUE1.value],
"param3": "value3",
}
assert extract_enums(params, enums) == expected_output
8 changes: 8 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tox]
envlist=py38, py39

[testenv]
deps =
pytest
commands =
pytest