From 55e8a01e664f006ca395f2b2bb1bf8b1377f28f3 Mon Sep 17 00:00:00 2001 From: Zilehuda Tariq Date: Mon, 15 May 2023 21:39:43 +0500 Subject: [PATCH 1/6] Added Tox and Pytest packages for testing --- requirements.in | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/requirements.in b/requirements.in index acbdc0c..ad7276e 100644 --- a/requirements.in +++ b/requirements.in @@ -46,4 +46,6 @@ web3==5.31.3 websockets==9.1 yarl==1.8.2 zipp==3.11.0 -websocket-client==1.5.1 \ No newline at end of file +websocket-client==1.5.1 +pytest==7.3.1 +tox==4.5.1 From ed32b3d84f722762fe89d90180292a42ac5ece1b Mon Sep 17 00:00:00 2001 From: Zilehuda Tariq Date: Mon, 15 May 2023 21:42:01 +0500 Subject: [PATCH 2/6] Created pytest.ini for pytest config --- pytest.ini | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 pytest.ini diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..f09ebf2 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,6 @@ +[pytest] +pythonpath = src/ +testpaths = tests +python_files = test_*.py +python_classes = Test +python_functions = test From ea5dbb406d0445b48956ae8f63c6c585e85814bf Mon Sep 17 00:00:00 2001 From: Zilehuda Tariq Date: Mon, 15 May 2023 21:42:13 +0500 Subject: [PATCH 3/6] Created Tox.ini for tox config --- tox.ini | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 tox.ini diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..fbbc70b --- /dev/null +++ b/tox.ini @@ -0,0 +1,8 @@ +[tox] +envlist=py38, py39 + +[testenv] +deps = + pytest +commands = + pytest \ No newline at end of file From ee6c9ba897bc2af2d6e76e19b4c1097b2409d23c Mon Sep 17 00:00:00 2001 From: Zilehuda Tariq Date: Mon, 15 May 2023 21:42:58 +0500 Subject: [PATCH 4/6] Added unit tests for utilities --- tests/__init__.py | 0 tests/test_utilities.py | 133 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_utilities.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_utilities.py b/tests/test_utilities.py new file mode 100644 index 0000000..bd5034a --- /dev/null +++ b/tests/test_utilities.py @@ -0,0 +1,133 @@ +import pytest +from enum import Enum +from datetime import datetime +from web3 import Web3 +from firefly_exchange_client.utilities import ( + strip_hex_prefix, + address_to_bytes32, + hash_string, + bn_to_bytes8, + default_value, + default_enum_value, + current_unix_timestamp, + random_number, + extract_query, + extract_enums, + config_logging, +) + + +@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¶m2=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 From 6b45c4e7ab516571447d0a0ea563e8fef4d3e94a Mon Sep 17 00:00:00 2001 From: Zilehuda Tariq Date: Mon, 15 May 2023 21:44:06 +0500 Subject: [PATCH 5/6] format the test_utilities --- tests/test_utilities.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index bd5034a..d239039 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -59,7 +59,7 @@ def test_bn_to_bytes8(): 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') + expected_output = f"0x{'0'*16}{hex(value)[2:]}".encode("utf-8") assert bn_to_bytes8(value) == expected_output @@ -79,9 +79,11 @@ 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 @@ -124,10 +126,20 @@ 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"} + + 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"} + expected_output = { + "param1": MyEnum.VALUE1.value, + "param2": [MyEnum.VALUE2.value, MyEnum.VALUE1.value], + "param3": "value3", + } assert extract_enums(params, enums) == expected_output From 2bf4c2d4474bef44d50847b0dc15e54d04ac0c2d Mon Sep 17 00:00:00 2001 From: Zilehuda Tariq Date: Tue, 16 May 2023 14:10:39 +0500 Subject: [PATCH 6/6] formatted the code, removed non-required import --- tests/test_utilities.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index d239039..9f1b475 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -1,20 +1,15 @@ -import pytest from enum import Enum -from datetime import datetime + +import pytest from web3 import Web3 -from firefly_exchange_client.utilities import ( - strip_hex_prefix, - address_to_bytes32, - hash_string, - bn_to_bytes8, - default_value, - default_enum_value, - current_unix_timestamp, - random_number, - extract_query, - extract_enums, - config_logging, -) + +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(