From b2f9af56ab95a76b0db4f93a15026abbc8d2d99a Mon Sep 17 00:00:00 2001 From: alexstroke1 Date: Mon, 18 Nov 2024 10:15:18 +0100 Subject: [PATCH] refactor conftest.py Signed-off-by: Far --- pytests/iroha_cli_tests/test/conftest.py | 199 +++++++++++++---------- 1 file changed, 113 insertions(+), 86 deletions(-) diff --git a/pytests/iroha_cli_tests/test/conftest.py b/pytests/iroha_cli_tests/test/conftest.py index b4650eea14..e9754c1658 100644 --- a/pytests/iroha_cli_tests/test/conftest.py +++ b/pytests/iroha_cli_tests/test/conftest.py @@ -6,6 +6,8 @@ import allure # type: ignore import pytest +from typing import Any, Generator, List + from ..common.consts import ValueTypes from ..common.helpers import ( fake_asset_name, @@ -27,7 +29,7 @@ # General fixtures @pytest.fixture(scope="session", autouse=True) -def before_all(): +def before_all() -> Generator[None, None, None]: """Initial setup for all test sessions. This fixture generates configurations based on peers and is automatically used for every test session.""" @@ -36,7 +38,7 @@ def before_all(): @pytest.fixture(scope="function", autouse=True) -def before_each(): +def before_each() -> Generator[None, None, None]: """Fixture to set up and reset the iroha_cli state.""" allure.dynamic.label("sdk", "Client CLI") allure.dynamic.label("owner", "astrokov") @@ -46,7 +48,7 @@ def before_each(): # Fixtures for creating objects (domains, accounts, asset definitions, assets) @pytest.fixture() -def GIVEN_registered_domain(): +def GIVEN_registered_domain() -> Domain: """Fixture to create and register a domain.""" domain = Domain(fake_name()) with allure.step(f"GIVEN a registered domain {domain.name}"): @@ -55,8 +57,10 @@ def GIVEN_registered_domain(): @pytest.fixture() -def GIVEN_registered_domain_with_uppercase_letter(GIVEN_registered_domain): - """Fixture to create and register a domain, but with uppercase letter.""" +def GIVEN_registered_domain_with_uppercase_letter( + GIVEN_registered_domain: Domain, +) -> Domain: + """Fixture to create and register a domain, but with an uppercase letter.""" domain = GIVEN_registered_domain domain.name = name_with_uppercase_letter(domain.name) with allure.step(f"GIVEN a registered domain {domain.name}"): @@ -65,11 +69,13 @@ def GIVEN_registered_domain_with_uppercase_letter(GIVEN_registered_domain): @pytest.fixture() -def GIVEN_registered_account(GIVEN_registered_domain, GIVEN_public_key): - """Fixture to create an account.""" +def GIVEN_registered_account( + GIVEN_registered_domain: Domain, GIVEN_public_key: str +) -> Account: + """Fixture to create and register an account.""" account = Account(signatory=GIVEN_public_key, domain=GIVEN_registered_domain.name) with allure.step( - f'GIVEN the account "{GIVEN_public_key}" in the "{GIVEN_registered_domain.name}" domain' + f'GIVEN the account "{account.signatory}" in the "{account.domain}" domain' ): iroha_cli.register().account(signatory=account.signatory, domain=account.domain) return account @@ -77,9 +83,9 @@ def GIVEN_registered_account(GIVEN_registered_domain, GIVEN_public_key): @pytest.fixture() def GIVEN_registered_account_granted_with_CanSetParameters( - GIVEN_registered_account, GIVEN_currently_authorized_account -): - """Fixture to create an account granted with CanSetParameter.""" + GIVEN_registered_account: Account, GIVEN_currently_authorized_account: Account +) -> Account: + """Fixture to grant the account with CanSetParameters permission.""" with allure.step( f'GIVEN "{GIVEN_registered_account}" granted with permission CanSetParameters' ): @@ -90,9 +96,9 @@ def GIVEN_registered_account_granted_with_CanSetParameters( @pytest.fixture() -def GIVEN_currently_authorized_account(): +def GIVEN_currently_authorized_account() -> Account: """Fixture to get the currently authorized account.""" - account: Account = Account( + account = Account( signatory=config.account_signatory, domain=config.account_domain, ) @@ -100,25 +106,30 @@ def GIVEN_currently_authorized_account(): f'GIVEN the currently authorized account "{account.signatory}" ' f'in the "{account.domain}" domain' ): - return account + pass + return account @pytest.fixture() def GIVEN_currently_account_quantity_with_two_quantity_of_asset( - GIVEN_currently_authorized_account, GIVEN_numeric_type, GIVEN_fake_asset_name -): - """Fixture to get the currently authorized account asset""" + GIVEN_currently_authorized_account: Account, + GIVEN_numeric_type: str, + GIVEN_fake_asset_name: str, +) -> Asset: + """Fixture to get the currently authorized account's asset with a quantity of 2.""" asset_def = AssetDefinition( name=GIVEN_fake_asset_name, domain=GIVEN_currently_authorized_account.domain, type_=GIVEN_numeric_type, ) asset = Asset( - definition=asset_def, value="2", account=GIVEN_currently_authorized_account + definition=asset_def, + value="2", + account=GIVEN_currently_authorized_account, ) name = fake_name() with allure.step( - f'GIVEN the asset_definition "{name}" ' + f'GIVEN the asset definition "{name}" ' f'in the "{GIVEN_currently_authorized_account.domain}" domain' ): iroha_cli.register().asset_definition( @@ -136,11 +147,14 @@ def GIVEN_currently_account_quantity_with_two_quantity_of_asset( @pytest.fixture() def GIVEN_numeric_asset_for_account( - request, GIVEN_numeric_type, GIVEN_fake_asset_name, GIVEN_numeric_value -): - """Fixture to get an asset for a given account and domain with specified quantity.""" - account, domain = request.param.split("@") - account = Account(signatory=account, domain=domain) + request: Any, + GIVEN_numeric_type: str, + GIVEN_fake_asset_name: str, + GIVEN_numeric_value: str, +) -> Asset: + """Fixture to get an asset for a given account and domain with a specified quantity.""" + account_str, domain = request.param.split("@") + account = Account(signatory=account_str, domain=domain) asset_def = AssetDefinition( name=GIVEN_fake_asset_name, domain=domain, type_=GIVEN_numeric_type @@ -150,7 +164,7 @@ def GIVEN_numeric_asset_for_account( ) with allure.step( - f'GIVEN the asset_definition "{asset_def.name}" ' f'in the "{domain}" domain' + f'GIVEN the asset definition "{asset_def.name}" in the "{domain}" domain' ): iroha_cli.register().asset_definition( asset=asset.definition.name, @@ -168,17 +182,19 @@ def GIVEN_numeric_asset_for_account( @pytest.fixture() def GIVEN_registered_asset_definition_with_numeric_type( - GIVEN_registered_domain, GIVEN_numeric_type, GIVEN_fake_asset_name -): - """Fixture to create and register an asset definition with numeric value type.""" + GIVEN_registered_domain: Domain, + GIVEN_numeric_type: str, + GIVEN_fake_asset_name: str, +) -> AssetDefinition: + """Fixture to create and register an asset definition with a numeric value type.""" asset_def = AssetDefinition( name=GIVEN_fake_asset_name, domain=GIVEN_registered_domain.name, type_=GIVEN_numeric_type, ) with allure.step( - f'GIVEN the asset_definition "{GIVEN_fake_asset_name}" ' - f'in the "{GIVEN_registered_domain.name}" domain' + f'GIVEN the asset definition "{asset_def.name}" ' + f'in the "{asset_def.domain}" domain' ): iroha_cli.register().asset_definition( asset=asset_def.name, @@ -190,13 +206,11 @@ def GIVEN_registered_asset_definition_with_numeric_type( @pytest.fixture() def GIVEN_minted_asset_quantity( - GIVEN_registered_asset_definition_with_numeric_type, - GIVEN_registered_account, - GIVEN_numeric_value, -): - """Fixture to create and return an asset with a specified quantity. - It takes a registered asset definition, a registered account, and a numeric value. - """ + GIVEN_registered_asset_definition_with_numeric_type: AssetDefinition, + GIVEN_registered_account: Account, + GIVEN_numeric_value: str, +) -> Asset: + """Fixture to create and return an asset with a specified quantity.""" asset = Asset( account=GIVEN_registered_account, definition=GIVEN_registered_asset_definition_with_numeric_type, @@ -212,143 +226,156 @@ def GIVEN_minted_asset_quantity( @pytest.fixture() def GIVEN_registered_asset_definition_with_store_type( - GIVEN_registered_domain, GIVEN_store_type, GIVEN_fake_asset_name -): - """Fixture to create and register an asset definition with store value type.""" + GIVEN_registered_domain: Domain, GIVEN_store_type: str, GIVEN_fake_asset_name: str +) -> AssetDefinition: + """Fixture to create and register an asset definition with a store value type.""" asset_def = AssetDefinition( name=GIVEN_fake_asset_name, domain=GIVEN_registered_domain.name, type_=GIVEN_store_type, ) with allure.step( - f'GIVEN the asset_definition "{GIVEN_fake_asset_name}" ' - f'in the "{GIVEN_registered_domain.name}" domain' + f'GIVEN the asset definition "{asset_def.name}" ' + f'in the "{asset_def.domain}" domain' ): iroha_cli.register().asset_definition( asset=asset_def.name, domain=asset_def.domain, - type=asset_def.type, + type_=asset_def.type_, ) return asset_def # Fixtures for generating various types of data (strings, keys, names, etc.) @pytest.fixture() -def GIVEN_fake_name(): +def GIVEN_fake_name() -> str: """Fixture to provide a fake name.""" name = fake_name() with allure.step(f'GIVEN a "{name}" name'): - return name + pass + return name @pytest.fixture() -def GIVEN_fake_asset_name(): +def GIVEN_fake_asset_name() -> str: """Fixture to provide a fake asset name.""" asset_name = fake_asset_name() with allure.step(f'GIVEN a "{asset_name}" asset'): - return asset_name + pass + return asset_name @pytest.fixture() -def GIVEN_not_existing_name(): +def GIVEN_not_existing_name() -> str: """Fixture to provide a non-existing name.""" name = not_existing_name() - with allure.step(f"GIVEN an non-existing {name}"): - return name + with allure.step(f"GIVEN a non-existing {name}"): + pass + return name @pytest.fixture() -def GIVEN_public_key(): +def GIVEN_public_key() -> str: """Fixture to provide a public key.""" public_key = generate_public_key() with allure.step(f"GIVEN a public key {public_key}"): - return public_key + pass + return public_key @pytest.fixture() -def GIVEN_random_character(): - """Fixture to provide a random character from the ASCII letters.""" +def GIVEN_random_character() -> str: + """Fixture to provide a random character from ASCII letters.""" letter = random.choice(string.ascii_letters) - with allure.step(f'GIVEN a "{letter}" name'): - return letter + with allure.step(f'GIVEN a "{letter}" character'): + pass + return letter @pytest.fixture() -def GIVEN_random_invalid_base64_character(): - """Fixture to provide a random invalid base64 character - (not a-z,A-Z,0-9,+,/,=). - """ +def GIVEN_random_invalid_base64_character() -> str: + """Fixture to provide a random invalid base64 character.""" invalid_chars = [ ch for ch in string.printable if not (ch.isalpha() or ch.isdigit() or ch in ["=", "+", "/"]) ] letter = random.choice(invalid_chars) - with allure.step(f'GIVEN a "{letter}" name'): - return letter + with allure.step(f'GIVEN an invalid base64 character "{letter}"'): + pass + return letter -# Fixtures for providing specific values or conditions (e.g., name length, string with spaces) +# Fixtures for providing specific values or conditions (e.g., name length, strings with spaces) @pytest.fixture() def GIVEN_key_with_invalid_character_in_key( - GIVEN_public_key, GIVEN_random_invalid_base64_character -): + GIVEN_public_key: str, GIVEN_random_invalid_base64_character: str +) -> str: """Fixture to provide a public key with an invalid character.""" invalid_key = key_with_invalid_character_in_key( GIVEN_public_key, GIVEN_random_invalid_base64_character ) with allure.step(f'GIVEN an invalid key "{invalid_key}"'): - return invalid_key + pass + return invalid_key @pytest.fixture() -def GIVEN_numeric_type(): +def GIVEN_numeric_type() -> str: """Fixture to provide a numeric value type.""" type_ = ValueTypes.NUMERIC.value with allure.step(f'GIVEN a "{type_}" value type'): - return type_ + pass + return type_ @pytest.fixture() -def GIVEN_store_type(): +def GIVEN_store_type() -> str: """Fixture to provide a store value type.""" type_ = ValueTypes.STORE.value with allure.step(f'GIVEN a "{type_}" value type'): - return type_ + pass + return type_ @pytest.fixture() -def GIVEN_numeric_value(): - """Fixture to provide a random numeric value based on the given value type.""" +def GIVEN_numeric_value() -> str: + """Fixture to provide a random numeric value.""" rand_int = str((random.getrandbits(96)) - 1) return rand_int @pytest.fixture() -def GIVEN_128_length_name(): +def GIVEN_128_length_name() -> str: + """Fixture to provide a string with 128 characters.""" ident = generate_random_string_without_reserved_chars(128) - with allure.step(f'GIVEN a name with 128 length "{ident}"'): - return ident + with allure.step(f'GIVEN a name with 128 characters "{ident}"'): + pass + return ident @pytest.fixture() -def GIVEN_129_length_name(): +def GIVEN_129_length_name() -> str: + """Fixture to provide a string with 129 characters.""" ident = generate_random_string_without_reserved_chars(129) - with allure.step(f'GIVEN a name with 129 length "{ident}"'): - return ident + with allure.step(f'GIVEN a name with 129 characters "{ident}"'): + pass + return ident @pytest.fixture() -def GIVEN_string_with_reserved_character(): - """Fixture to provide a random string with reserved characters.""" +def GIVEN_string_with_reserved_character() -> str: + """Fixture to provide a string with reserved characters.""" new_string = generate_random_string_with_reserved_char() - with allure.step(f'GIVEN a "{new_string}" string'): - return new_string + with allure.step(f'GIVEN a string with reserved characters "{new_string}"'): + pass + return new_string @pytest.fixture() -def GIVEN_string_with_whitespaces(): - """Fixture to provide a random string with whitespaces.""" +def GIVEN_string_with_whitespaces() -> str: + """Fixture to provide a string with whitespaces.""" new_string = generate_random_string_with_whitespace() - with allure.step(f'GIVEN a "{new_string}" string'): - return new_string + with allure.step(f'GIVEN a string with whitespaces "{new_string}"'): + pass + return new_string