Skip to content
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

test_: services approach #6171

Merged
merged 1 commit into from
Dec 9, 2024
Merged
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
7 changes: 5 additions & 2 deletions tests-functional/clients/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from conftest import option
from json import JSONDecodeError


class RpcClient:

def __init__(self, rpc_url, client=requests.Session()):
Expand Down Expand Up @@ -43,7 +44,9 @@ def verify_is_json_rpc_error(self, response):
self._check_decode_and_key_errors_in_response(response, "error")

@retry(stop=stop_after_delay(10), wait=wait_fixed(0.5), reraise=True)
def rpc_request(self, method, params=[], request_id=13, url=None):
def rpc_request(self, method, params=None, request_id=13, url=None):
if params is None:
params = []
url = url if url else self.rpc_url
data = {"jsonrpc": "2.0", "method": method, "id": request_id}
if params:
Expand All @@ -59,7 +62,7 @@ def rpc_request(self, method, params=[], request_id=13, url=None):
logging.info(f"Got response: {response.content}")
return response

def rpc_valid_request(self, method, params=[], _id=None, url=None):
def rpc_valid_request(self, method, params=None, _id=None, url=None):
response = self.rpc_request(method, params, _id, url)
self.verify_is_valid_json_rpc_response(response, _id)
return response
Expand Down
12 changes: 12 additions & 0 deletions tests-functional/clients/services/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from clients.rpc import RpcClient


class Service:
def __init__(self, client: RpcClient, name: str):
assert name is not ""
self.rpc_client = client
self.name = name

def rpc_request(self, method: str, params=None):
full_method_name = f"{self.name}_{method}"
return self.rpc_client.rpc_request(full_method_name, params)
11 changes: 11 additions & 0 deletions tests-functional/clients/services/wallet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from clients.rpc import RpcClient
from clients.services.service import Service


class WalletService(Service):
def __init__(self, client: RpcClient):
super().__init__(client, "wallet")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I like this approach

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1


def get_balances_at_by_chain(self, chains: list, addresses: list, tokens: list):
params = [chains, addresses, tokens]
return self.rpc_request("getBalancesByChain", params)
9 changes: 4 additions & 5 deletions tests-functional/clients/status_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from constants import user_1, DEFAULT_DISPLAY_NAME



class StatusBackend(RpcClient, SignalClient):

def __init__(self, await_signals=[], url=None):
Expand All @@ -27,7 +26,6 @@ def __init__(self, await_signals=[], url=None):
self.ws_url = f"{url}".replace("http", "ws")
self.rpc_url = f"{url}/statusgo/CallRPC"


RpcClient.__init__(self, self.rpc_url)
SignalClient.__init__(self, self.ws_url, await_signals)

Expand Down Expand Up @@ -83,7 +81,8 @@ def create_account_and_login(self, data_dir="/", display_name=DEFAULT_DISPLAY_NA
}
return self.api_valid_request(method, data)

def restore_account_and_login(self, data_dir="/",display_name=DEFAULT_DISPLAY_NAME, user=user_1):
def restore_account_and_login(self, data_dir="/",display_name=DEFAULT_DISPLAY_NAME, user=user_1,
network_id=31337):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe let's add 31337 to constants, something like
DEFAULT_NETWORK_ID

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fbarbu15 100% agree with you, It should definitely be done
But it's better do it outside of this PR not to mess up things. 31337 is duplicated in quite many places already.

method = "RestoreAccountAndLogin"
data = {
"rootDataDir": data_dir,
Expand All @@ -95,10 +94,10 @@ def restore_account_and_login(self, data_dir="/",display_name=DEFAULT_DISPLAY_NA
"logEnabled": True,
"logLevel": "DEBUG",
"testNetworksEnabled": False,
"networkId": 31337,
"networkId": network_id,
"networksOverride": [
{
"ChainID": 31337,
"ChainID": network_id,
"ChainName": "Anvil",
"DefaultRPCURL": "http://anvil:8545",
"RPCURL": "http://anvil:8545",
Expand Down
6 changes: 4 additions & 2 deletions tests-functional/tests/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pytest

from clients.services.wallet import WalletService
from clients.signals import SignalClient, SignalType
from clients.status_backend import RpcClient, StatusBackend
from conftest import option
Expand All @@ -28,15 +29,16 @@ class StatusBackendTestCase:
SignalType.NODE_LOGIN.value
]

network_id = 31337
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then you don't need to define it here again


def setup_class(self):
self.rpc_client = StatusBackend(await_signals=self.await_signals)
self.wallet_service = WalletService(self.rpc_client)

self.rpc_client.init_status_backend()
self.rpc_client.restore_account_and_login()
self.rpc_client.wait_for_login()

self.network_id = 31337


class WalletTestCase(StatusBackendTestCase):

Expand Down
Loading