Skip to content

Commit

Permalink
test: refactor call method in integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Den authored and Den committed Sep 10, 2024
1 parent 942121f commit 9c5488a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 125 deletions.
15 changes: 15 additions & 0 deletions tests/common/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from eth_account import Account

from tests.common.transactions import sign_transaction
from tests.common.transactions import encode_transaction_data


load_dotenv()

Expand Down Expand Up @@ -43,6 +45,19 @@ def get_transaction_by_id(transaction_id: str):
return raw_response.json()


def call_contract_method(
contract_address: str,
from_account: Account,
method_name: str,
method_args: list,
):
params_as_string = json.dumps(method_args)
encoded_data = encode_transaction_data([method_name, params_as_string])
return post_request_localhost(
payload("call", contract_address, from_account.address, encoded_data)
).json()


def send_transaction(
account: Account,
contract_address: str,
Expand Down
48 changes: 13 additions & 35 deletions tests/integration/contract_examples/test_llm_erc20.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# tests/e2e/test_storage.py

import json

from tests.common.request import (
deploy_intelligent_contract,
send_transaction,
Expand All @@ -22,7 +19,7 @@
)

from tests.common.accounts import create_new_account
from tests.common.transactions import encode_transaction_data
from tests.common.request import call_contract_method

TOKEN_TOTAL_SUPPLY = 1000
TRANSFER_AMOUNT = 100
Expand Down Expand Up @@ -60,11 +57,9 @@ def test_llm_erc20():
########################################
######### GET Initial State ############
########################################
params_as_string = json.dumps([])
encoded_data = encode_transaction_data(["get_balances", params_as_string])
contract_state_1 = post_request_localhost(
payload("call", contract_address, from_account_a.address, encoded_data)
).json()
contract_state_1 = call_contract_method(
contract_address, from_account_a, "get_balances", []
)
assert has_success_status(contract_state_1)
assert contract_state_1["result"]["data"][from_account_a.address] == str(
TOKEN_TOTAL_SUPPLY
Expand All @@ -85,12 +80,9 @@ def test_llm_erc20():
assert_dict_struct(transaction_response_call_1, call_contract_function_response)

# Get Updated State
params_as_string = json.dumps([])
encoded_data = encode_transaction_data(["get_balances", params_as_string])

contract_state_2_1 = post_request_localhost(
payload("call", contract_address, from_account_a.address, encoded_data)
).json()
contract_state_2_1 = call_contract_method(
contract_address, from_account_a, "get_balances", []
)
assert has_success_status(contract_state_2_1)
assert (
contract_state_2_1["result"]["data"][from_account_a.address]
Expand All @@ -102,30 +94,16 @@ def test_llm_erc20():
)

# Get Updated State
params_as_string = json.dumps([from_account_a.address])
encoded_data = encode_transaction_data(["get_balance_of", params_as_string])
contract_state_2_2 = post_request_localhost(
payload(
"call",
contract_address,
from_account_a.address,
encoded_data,
)
).json()
contract_state_2_2 = call_contract_method(
contract_address, from_account_a, "get_balance_of", [from_account_a.address]
)
assert has_success_status(contract_state_2_2)
assert contract_state_2_2["result"]["data"] == TOKEN_TOTAL_SUPPLY - TRANSFER_AMOUNT

# Get Updated State
params_as_string = json.dumps([from_account_b.address])
encoded_data = encode_transaction_data(["get_balance_of", params_as_string])
contract_state_2_3 = post_request_localhost(
payload(
"call",
contract_address,
from_account_a.address,
encoded_data,
)
).json()
contract_state_2_3 = call_contract_method(
contract_address, from_account_a, "get_balance_of", [from_account_b.address]
)
assert has_success_status(contract_state_2_3)
assert contract_state_2_3["result"]["data"] == TRANSFER_AMOUNT

Expand Down
61 changes: 22 additions & 39 deletions tests/integration/contract_examples/test_log_indexer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# tests/e2e/test_storage.py

import json

from tests.common.request import (
deploy_intelligent_contract,
send_transaction,
Expand All @@ -22,7 +19,7 @@
)

from tests.common.accounts import create_new_account
from tests.common.transactions import encode_transaction_data
from tests.common.request import call_contract_method

TOKEN_TOTAL_SUPPLY = 1000
TRANSFER_AMOUNT = 100
Expand Down Expand Up @@ -56,11 +53,9 @@ def test_log_indexer():
# ##########################################
# ##### Get closest vector when empty ######
# ##########################################
params_as_string = json.dumps(["I like mango"])
encoded_data = encode_transaction_data(["get_closest_vector", params_as_string])
closest_vector_log_0 = post_request_localhost(
payload("call", contract_address, from_account.address, encoded_data)
).json()
closest_vector_log_0 = call_contract_method(
contract_address, from_account, "get_closest_vector", ["I like mango"]
)
assert has_success_status(closest_vector_log_0)
assert closest_vector_log_0["result"]["data"] is None

Expand All @@ -79,23 +74,19 @@ def test_log_indexer():
# ########################################
# ##### Get closest vector to log 0 ######
# ########################################
params_as_string = json.dumps(["I like mango"])
encoded_data = encode_transaction_data(["get_closest_vector", params_as_string])
closest_vector_log_0 = post_request_localhost(
payload("call", contract_address, from_account.address, encoded_data)
).json()
closest_vector_log_0 = call_contract_method(
contract_address, from_account, "get_closest_vector", ["I like mango"]
)
assert has_success_status(closest_vector_log_0)
assert float(closest_vector_log_0["result"]["data"]["similarity"]) > 0.86
assert float(closest_vector_log_0["result"]["data"]["similarity"]) < 0.87

# ########################################
# ######### Get log 0 metadata ###########
# ########################################
params_as_string = json.dumps([0])
encoded_data = encode_transaction_data(["get_vector_metadata", params_as_string])
metadata_log_0 = post_request_localhost(
payload("call", contract_address, from_account.address, encoded_data)
).json()
metadata_log_0 = call_contract_method(
contract_address, from_account, "get_vector_metadata", [0]
)
assert has_success_status(metadata_log_0)
assert metadata_log_0["result"]["data"] == {"log_id": 0}

Expand All @@ -113,11 +104,9 @@ def test_log_indexer():
# ########################################
# ##### Get closest vector to log 1 ######
# ########################################
params_as_string = json.dumps(["I like carrots"])
encoded_data = encode_transaction_data(["get_closest_vector", params_as_string])
closest_vector_log_1 = post_request_localhost(
payload("call", contract_address, from_account.address, encoded_data)
).json()
closest_vector_log_1 = call_contract_method(
contract_address, from_account, "get_closest_vector", ["I like carrots"]
)
assert has_success_status(closest_vector_log_1)
assert float(closest_vector_log_1["result"]["data"]["similarity"]) == 1

Expand All @@ -135,11 +124,9 @@ def test_log_indexer():
# ########################################
# ###### Get closest vector to log 0 #####
# ########################################
params_as_string = json.dumps(["I like mango a lot"])
encoded_data = encode_transaction_data(["get_closest_vector", params_as_string])
closest_vector_log_0_2 = post_request_localhost(
payload("call", contract_address, from_account.address, encoded_data)
).json()
closest_vector_log_0_2 = call_contract_method(
contract_address, from_account, "get_closest_vector", ["I like mango a lot"]
)
assert has_success_status(closest_vector_log_0_2)
assert float(closest_vector_log_0_2["result"]["data"]["similarity"]) > 0.85
assert float(closest_vector_log_0_2["result"]["data"]["similarity"]) < 0.86
Expand All @@ -158,11 +145,9 @@ def test_log_indexer():
# ########################################
# ##### Get closest vector to log 0 ######
# ########################################
params_as_string = json.dumps(["I like to eat mango"])
encoded_data = encode_transaction_data(["get_closest_vector", params_as_string])
closest_vector_log_0_3 = post_request_localhost(
payload("call", contract_address, from_account.address, encoded_data)
).json()
closest_vector_log_0_3 = call_contract_method(
contract_address, from_account, "get_closest_vector", ["I like to eat mango"]
)
assert has_success_status(closest_vector_log_0_3)
assert float(closest_vector_log_0_3["result"]["data"]["similarity"]) > 0.50
assert float(closest_vector_log_0_3["result"]["data"]["similarity"]) < 0.51
Expand All @@ -181,11 +166,9 @@ def test_log_indexer():
assert has_success_status(transaction_response_add_log_2)

# Check if new item got id 2
params_as_string = json.dumps(["This is the third log"])
encoded_data = encode_transaction_data(["get_closest_vector", params_as_string])
closest_vector_log_2 = post_request_localhost(
payload("call", contract_address, from_account.address, encoded_data)
).json()
closest_vector_log_2 = call_contract_method(
contract_address, from_account, "get_closest_vector", ["This is the third log"]
)
assert has_success_status(closest_vector_log_2)
assert float(closest_vector_log_2["result"]["data"]["similarity"]) > 0.99
assert closest_vector_log_2["result"]["data"]["id"] == 2
Expand Down
20 changes: 7 additions & 13 deletions tests/integration/contract_examples/test_storage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# tests/e2e/test_storage.py
import json

from tests.common.request import (
deploy_intelligent_contract,
send_transaction,
Expand All @@ -21,7 +19,7 @@
)

from tests.common.accounts import create_new_account
from tests.common.transactions import encode_transaction_data
from tests.common.request import call_contract_method

INITIAL_STATE = "a"
UPDATED_STATE = "b"
Expand Down Expand Up @@ -56,11 +54,9 @@ def test_storage():
contract_address = call_method_response_deploy["result"]["data"]["contract_address"]

# Get Initial State
params_as_string = json.dumps([])
encoded_data = encode_transaction_data(["get_storage", params_as_string])
contract_state_1 = post_request_localhost(
payload("call", contract_address, from_account.address, encoded_data)
).json()
contract_state_1 = call_contract_method(
contract_address, from_account, "get_storage", []
)
assert has_success_status(contract_state_1)
assert contract_state_1["result"]["data"] == INITIAL_STATE

Expand All @@ -75,11 +71,9 @@ def test_storage():
assert_dict_struct(transaction_response_call_1, call_contract_function_response)

# Get Updated State
params_as_string = json.dumps([])
encoded_data = encode_transaction_data(["get_storage", params_as_string])
contract_state_2 = post_request_localhost(
payload("call", contract_address, from_account.address, encoded_data)
).json()
contract_state_2 = call_contract_method(
contract_address, from_account, "get_storage", []
)
assert has_success_status(contract_state_2)
assert contract_state_2["result"]["data"] == UPDATED_STATE

Expand Down
63 changes: 25 additions & 38 deletions tests/integration/contract_examples/test_user_storage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# tests/e2e/test_storage.py
import json

from tests.common.request import (
deploy_intelligent_contract,
send_transaction,
Expand All @@ -22,7 +20,7 @@
)

from tests.common.accounts import create_new_account
from tests.common.transactions import encode_transaction_data
from tests.common.request import call_contract_method

INITIAL_STATE_USER_A = "user_a_initial_state"
UPDATED_STATE_USER_A = "user_a_updated_state"
Expand Down Expand Up @@ -61,11 +59,9 @@ def test_user_storage():
########################################
######### GET Initial State ############
########################################
params_as_string = json.dumps([])
encoded_data = encode_transaction_data(["get_complete_storage", params_as_string])
contract_state_1 = post_request_localhost(
payload("call", contract_address, from_account_a.address, encoded_data)
).json()
contract_state_1 = call_contract_method(
contract_address, from_account_a, "get_complete_storage", []
)
assert has_success_status(contract_state_1)
assert len(contract_state_1["result"]["data"]) == 0

Expand All @@ -81,28 +77,22 @@ def test_user_storage():
assert_dict_struct(transaction_response_call_1, call_contract_function_response)

# Get Updated State
params_as_string = json.dumps([])
encoded_data = encode_transaction_data(["get_complete_storage", params_as_string])
contract_state_2_1 = post_request_localhost(
payload("call", contract_address, from_account_a.address, encoded_data)
).json()
contract_state_2_1 = call_contract_method(
contract_address, from_account_a, "get_complete_storage", []
)
assert has_success_status(contract_state_2_1)
assert (
contract_state_2_1["result"]["data"][from_account_a.address]
== INITIAL_STATE_USER_A
)

# Get Updated State
params_as_string = json.dumps([from_account_a.address])
encoded_data = encode_transaction_data(["get_account_storage", params_as_string])
contract_state_2_2 = post_request_localhost(
payload(
"call",
contract_address,
from_account_a.address,
encoded_data,
)
).json()
contract_state_2_2 = call_contract_method(
contract_address,
from_account_a,
"get_account_storage",
[from_account_a.address],
)
assert has_success_status(contract_state_2_2)
assert contract_state_2_2["result"]["data"] == INITIAL_STATE_USER_A

Expand All @@ -118,11 +108,9 @@ def test_user_storage():
assert_dict_struct(transaction_response_call_2, call_contract_function_response)

# Get Updated State
params_as_string = json.dumps([])
encoded_data = encode_transaction_data(["get_complete_storage", params_as_string])
contract_state_3 = post_request_localhost(
payload("call", contract_address, from_account_a.address, encoded_data)
).json()
contract_state_3 = call_contract_method(
contract_address, from_account_a, "get_complete_storage", []
)
assert has_success_status(contract_state_3)
assert (
contract_state_3["result"]["data"][from_account_a.address]
Expand All @@ -145,11 +133,9 @@ def test_user_storage():
assert_dict_struct(transaction_response_call_3, call_contract_function_response)

# Get Updated State
params_as_string = json.dumps([])
encoded_data = encode_transaction_data(["get_complete_storage", params_as_string])
contract_state_4_1 = post_request_localhost(
payload("call", contract_address, from_account_a.address, encoded_data)
).json()
contract_state_4_1 = call_contract_method(
contract_address, from_account_a, "get_complete_storage", []
)
assert has_success_status(contract_state_4_1)
assert (
contract_state_4_1["result"]["data"][from_account_a.address]
Expand All @@ -161,11 +147,12 @@ def test_user_storage():
)

# Get Updated State
params_as_string = json.dumps([from_account_b.address])
encoded_data = encode_transaction_data(["get_account_storage", params_as_string])
contract_state_4_2 = post_request_localhost(
payload("call", contract_address, from_account_a.address, encoded_data)
).json()
contract_state_4_2 = call_contract_method(
contract_address,
from_account_a,
"get_account_storage",
[from_account_b.address],
)
assert has_success_status(contract_state_4_2)
assert contract_state_4_2["result"]["data"] == INITIAL_STATE_USER_B

Expand Down

0 comments on commit 9c5488a

Please sign in to comment.