From 3e5a204d199a528732f6c1f3029ff0c66041dc29 Mon Sep 17 00:00:00 2001 From: Andrey Berezin Date: Wed, 16 Nov 2022 17:29:49 +0300 Subject: [PATCH] Refactor balance tests Signed-off-by: Andrey Berezin --- .../testsuites/payment/test_balance.py | 110 ++++++++++-------- 1 file changed, 60 insertions(+), 50 deletions(-) diff --git a/pytest_tests/testsuites/payment/test_balance.py b/pytest_tests/testsuites/payment/test_balance.py index ac52e3bb..ff4f6e4b 100644 --- a/pytest_tests/testsuites/payment/test_balance.py +++ b/pytest_tests/testsuites/payment/test_balance.py @@ -1,20 +1,13 @@ import logging import os -import uuid import allure import pytest import yaml -from common import ( - ASSETS_DIR, - FREE_STORAGE, - NEOFS_CLI_EXEC, - NEOFS_ENDPOINT, - WALLET_CONFIG, - WALLET_PASS, -) +from common import FREE_STORAGE, NEOFS_CLI_EXEC, NEOFS_ENDPOINT, WALLET_CONFIG from neofs_testlib.cli import NeofsCli -from neofs_testlib.utils.wallet import get_last_address_from_wallet, init_wallet +from neofs_testlib.shell import CommandResult, Shell +from wallet import WalletFactory, WalletFile logger = logging.getLogger("NeoLogger") DEPOSIT_AMOUNT = 30 @@ -24,51 +17,30 @@ DEPOSIT_AMOUNT = 30 @pytest.mark.payments @pytest.mark.skipif(FREE_STORAGE, reason="Test only works on public network with paid storage") class TestBalanceAccounting: - @pytest.fixture(autouse=True) - def prepare_two_wallets(self, prepare_wallet_and_deposit): - self.user_wallet = prepare_wallet_and_deposit - self.address = get_last_address_from_wallet(self.user_wallet, WALLET_PASS) - another_wallet = os.path.join(os.getcwd(), ASSETS_DIR, f"{str(uuid.uuid4())}.json") - init_wallet(another_wallet, WALLET_PASS) - self.another_address = get_last_address_from_wallet(another_wallet, WALLET_PASS) + @pytest.fixture(scope="class") + def main_wallet(self, wallet_factory: WalletFactory) -> WalletFile: + return wallet_factory.create_wallet() - @allure.title("Test balance request with wallet and address") - def test_balance_wallet_address(self, client_shell): - cli = NeofsCli(client_shell, NEOFS_CLI_EXEC, WALLET_CONFIG) - result = cli.accounting.balance( - wallet=self.user_wallet, - rpc_endpoint=NEOFS_ENDPOINT, - address=self.address, - ) - assert int(result.stdout.rstrip()) == DEPOSIT_AMOUNT + @pytest.fixture(scope="class") + def other_wallet(self, wallet_factory: WalletFactory) -> WalletFile: + return wallet_factory.create_wallet() - @allure.title("Test balance request with wallet only") - def test_balance_wallet(self, client_shell): - cli = NeofsCli(client_shell, NEOFS_CLI_EXEC, WALLET_CONFIG) - result = cli.accounting.balance(wallet=self.user_wallet, rpc_endpoint=NEOFS_ENDPOINT) - assert int(result.stdout.rstrip()) == DEPOSIT_AMOUNT + @pytest.fixture(scope="class") + def cli(self, client_shell: Shell) -> NeofsCli: + return NeofsCli(client_shell, NEOFS_CLI_EXEC, WALLET_CONFIG) - @allure.title("Test balance request with wallet and wrong address") - def test_balance_wrong_address(self, client_shell): - with pytest.raises(Exception, match="address option must be specified and valid"): - cli = NeofsCli(client_shell, NEOFS_CLI_EXEC, WALLET_CONFIG) - cli.accounting.balance( - wallet=self.user_wallet, - rpc_endpoint=NEOFS_ENDPOINT, - address=self.another_address, + @allure.step("Check deposit amount") + def check_amount(self, result: CommandResult) -> None: + amount_str = result.stdout.rstrip() + + try: + amount = int(amount_str) + except Exception as ex: + pytest.fail( + f"Amount parse error, should be parsable as int({DEPOSIT_AMOUNT}), but given {amount_str}: {ex}" ) - @allure.title("Test balance request with config file") - def test_balance_api(self, prepare_tmp_dir, client_shell): - config_file = self.write_api_config( - config_dir=prepare_tmp_dir, endpoint=NEOFS_ENDPOINT, wallet=self.user_wallet - ) - logger.info(f"Config with API endpoint: {config_file}") - - cli = NeofsCli(client_shell, NEOFS_CLI_EXEC, config_file=config_file) - result = cli.accounting.balance() - - assert int(result.stdout.rstrip()) == DEPOSIT_AMOUNT + assert amount == DEPOSIT_AMOUNT @staticmethod @allure.step("Write config with API endpoint") @@ -84,3 +56,41 @@ class TestBalanceAccounting: with open(api_config_file, "w") as file: yaml.dump(api_config, file) return api_config_file + + @allure.title("Test balance request with wallet and address") + def test_balance_wallet_address(self, main_wallet: WalletFile, cli: NeofsCli): + result = cli.accounting.balance( + wallet=main_wallet.path, + rpc_endpoint=NEOFS_ENDPOINT, + address=main_wallet.get_address(), + ) + + self.check_amount(result) + + @allure.title("Test balance request with wallet only") + def test_balance_wallet(self, main_wallet: WalletFile, cli: NeofsCli): + result = cli.accounting.balance(wallet=main_wallet.path, rpc_endpoint=NEOFS_ENDPOINT) + self.check_amount(result) + + @allure.title("Test balance request with wallet and wrong address") + def test_balance_wrong_address( + self, main_wallet: WalletFile, other_wallet: WalletFile, cli: NeofsCli + ): + with pytest.raises(Exception, match="address option must be specified and valid"): + cli.accounting.balance( + wallet=main_wallet.path, + rpc_endpoint=NEOFS_ENDPOINT, + address=other_wallet.get_address(), + ) + + @allure.title("Test balance request with config file") + def test_balance_api(self, prepare_tmp_dir: str, main_wallet: WalletFile, client_shell: Shell): + config_file = self.write_api_config( + config_dir=prepare_tmp_dir, endpoint=NEOFS_ENDPOINT, wallet=main_wallet.path + ) + logger.info(f"Config with API endpoint: {config_file}") + + cli = NeofsCli(client_shell, NEOFS_CLI_EXEC, config_file=config_file) + result = cli.accounting.balance() + + self.check_amount(result)