Skip to content

Commit

Permalink
[refactor] hyperledger-iroha#2398: Add integration tests for query fi…
Browse files Browse the repository at this point in the history
…lters

Signed-off-by: Shanin Roman <[email protected]>
  • Loading branch information
Erigara committed Jul 26, 2023
1 parent 1e55a40 commit e9ce820
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 100 deletions.
39 changes: 16 additions & 23 deletions client_cli/pytests/src/client_cli/client_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
import json
import subprocess
from time import sleep, time
from time import sleep, monotonic
from typing import Callable

import allure
Expand Down Expand Up @@ -48,36 +48,21 @@ def __exit__(self, exc_type, exc_val, exc_tb):
"""
self.reset()

def wait_for(self, expected: str, actual_call: Callable[[], str], timeout=None):
def wait_for(self, condition: Callable[[], bool], timeout=None):
"""
Wait for a certain condition to be met, specified by the expected and actual values.
:param expected: The expected value.
:type expected: str
:param actual: The actual value.
:type actual: str
:param condition: Condition that should be met in given time.
:type condition: Callable[[], bool]
:param timeout: Maximum time to wait for the condition to be met, defaults to None.
:type timeout: int, optional
"""
timeout = timeout or self._timeout
start_time = time()
actual = actual_call()
while expected not in actual:
if time() - start_time > timeout:
allure.attach(
json.dumps(actual),
name='actual',
attachment_type=allure.attachment_type.JSON)
allure.attach(
json.dumps(expected),
name='expected',
attachment_type=allure.attachment_type.JSON)
raise TimeoutError(f"Expected '{expected}' "
f"to be in '{actual}' "
f"after waiting for '{timeout}' seconds.")
start_time = monotonic()
while not condition():
if monotonic() - start_time > timeout:
raise TimeoutError(f"Expected condition to be satisfied after waiting for '{timeout}' seconds.")
sleep(1)
actual = actual_call()
assert expected in actual

def reset(self):
"""
Expand Down Expand Up @@ -255,6 +240,14 @@ def execute(self):
text=True
) as process:
self.stdout, self.stderr = process.communicate()
allure.attach(
self.stdout,
name='stdout',
attachment_type=allure.attachment_type.TEXT)
allure.attach(
self.stderr,
name='stderr',
attachment_type=allure.attachment_type.TEXT)
except Exception as exception:
raise RuntimeError(
f"Error executing command: {command}. "
Expand Down
43 changes: 30 additions & 13 deletions client_cli/pytests/src/client_cli/have.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@
This module contains functions for checking expected results in tests.
"""

import json
import allure

from src.client_cli import client_cli, iroha, match

def expected_in_actual(expected, actual) -> bool:
allure.attach(
json.dumps(actual),
name='actual',
attachment_type=allure.attachment_type.JSON)
allure.attach(
json.dumps(expected),
name='expected',
attachment_type=allure.attachment_type.JSON)

return expected in actual

def domain(expected):
"""
Expand All @@ -12,9 +26,10 @@ def domain(expected):
:param expected: The expected domain object.
:return: True if the domain is present, False otherwise.
"""
return match.iroha_have_domain(
expected=expected,
actual=lambda: iroha.list_filter(f'{{"Identifiable": {{"Is": "{expected}"}}}}').domains())
def domain_in_domains() -> bool:
domains = iroha.list_filter(f'{{"Identifiable": {{"Is": "{expected}"}}}}').domains()
return expected_in_actual(expected, domains)
return client_cli.wait_for(domain_in_domains)


def account(expected):
Expand All @@ -24,9 +39,10 @@ def account(expected):
:param expected: The expected account object.
:return: True if the account is present, False otherwise.
"""
return match.iroha_have_account(
expected=expected,
actual=lambda: iroha.list_filter(f'{{"Identifiable": {{"Is": "{expected}"}}}}').accounts())
def account_in_accounts() -> bool:
accounts = iroha.list_filter(f'{{"Identifiable": {{"Is": "{expected}"}}}}').accounts()
return expected_in_actual(expected, accounts)
return client_cli.wait_for(account_in_accounts)


def asset_definition(expected):
Expand All @@ -37,10 +53,10 @@ def asset_definition(expected):
:return: True if the asset definition is present, False otherwise.
"""
expected_domain = expected.split('#')[1]
return match.iroha_have_asset_definition(
expected=expected,
actual=lambda: iroha.list_filter(f'{{"Identifiable": {{"Is": "{expected_domain}"}}}}').asset_definitions())

def asset_definition_in_asset_definitions() -> bool:
asset_definitions = iroha.list_filter(f'{{"Identifiable": {{"Is": "{expected_domain}"}}}}').asset_definitions()
return expected_in_actual(expected, asset_definitions)
return client_cli.wait_for(asset_definition_in_asset_definitions)

def asset(expected):
"""
Expand All @@ -49,9 +65,10 @@ def asset(expected):
:param expected: The expected asset object.
:return: True if the asset is present, False otherwise.
"""
return match.iroha_have_asset(
expected=expected,
actual=lambda: iroha.list_filter(f'{{"Identifiable": {{"Is": "{expected}"}}}}').assets())
def asset_in_assets() -> bool:
assets = iroha.list_filter(f'{{"Identifiable": {{"Is": "{expected}"}}}}').assets()
return expected_in_actual(expected, assets)
return client_cli.wait_for(asset_in_assets)


def error(expected):
Expand Down
54 changes: 0 additions & 54 deletions client_cli/pytests/src/client_cli/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,8 @@
This module provides helper functions for matching expected and actual values in Iroha objects.
"""

from typing import Callable
import allure

from src.client_cli import client_cli


def wait_for(expected, actual):
"""
Waits for the expected value to be present in the actual value.
:param expected: The expected value.
:param actual: The actual value.
"""
client_cli.wait_for(expected, actual)


def iroha_have_domain(expected: str, actual: str):
"""
Checks if Iroha has the expected domain.
:param expected: The expected domain.
:param actual: The actual domain list.
"""
wait_for(expected, actual)


def iroha_have_account(expected: str, actual: str):
"""
Checks if Iroha has the expected account.
:param expected: The expected account.
:param actual: The actual account list.
"""
wait_for(expected, actual)


def iroha_have_asset_definition(expected: str, actual: Callable[[], str]):
"""
Checks if Iroha has the expected asset definition.
:param expected: The expected asset definition.
:param actual: The actual asset definition list.
"""
wait_for(expected, actual)


def iroha_have_asset(expected: str, actual: str):
"""
Checks if Iroha has the expected asset.
:param expected: The expected asset.
:param actual: The actual asset list.
"""
wait_for(expected, actual)


def client_cli_have_error(expected: str, actual: str):
"""
Checks if the command-line client has the expected error.
Expand Down
51 changes: 51 additions & 0 deletions client_cli/pytests/test/accounts/test_accounts_query_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import json
import allure

from src.client_cli import iroha, iroha, client_cli

# using existing account to have at least one account in response
def test_filter_by_domain(GIVEN_new_one_existence_account):
def condition():
domain = GIVEN_new_one_existence_account.domain
with allure.step(
f'WHEN client_cli query accounts '
f'in the "{domain}" domain'):
accounts = iroha.list_filter(f'{{"Identifiable": {{"EndsWith": "@{domain}"}}}}').accounts()
with allure.step(
f'THEN Iroha should return only accounts with this domain'):
allure.attach(
json.dumps(accounts),
name='accounts',
attachment_type=allure.attachment_type.JSON)
return accounts and all(account.endswith(domain) for account in accounts)
client_cli.wait_for(condition)

def test_filter_by_account_name(GIVEN_new_one_existence_account):
def condition():
name = GIVEN_new_one_existence_account.name
with allure.step(
f'WHEN client_cli query accounts with name "{name}"'):
accounts = iroha.list_filter(f'{{"Identifiable": {{"StartsWith": "{name}@"}}}}').accounts()
with allure.step(
f'THEN Iroha should return only accounts with this name'):
allure.attach(
json.dumps(accounts),
name='accounts',
attachment_type=allure.attachment_type.JSON)
return accounts and all(account.startswith(name) for account in accounts)
client_cli.wait_for(condition)

def test_filter_by_account_id(GIVEN_new_one_existence_account):
def condition():
account_id = GIVEN_new_one_existence_account.name + "@" + GIVEN_new_one_existence_account.domain
with allure.step(
f'WHEN client_cli query accounts with account id "{account_id}"'):
accounts = iroha.list_filter(f'{{"Identifiable": {{"Is": "{account_id}"}}}}').accounts()
with allure.step(
f'THEN Iroha should return only accounts with this id'):
allure.attach(
json.dumps(accounts),
name='accounts',
attachment_type=allure.attachment_type.JSON)
return accounts and all(account == account_id for account in accounts)
client_cli.wait_for(condition)
54 changes: 54 additions & 0 deletions client_cli/pytests/test/assets/test_assets_query_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import json
import allure

from src.client_cli import iroha, iroha, client_cli

# using existing account with asset to have at least one in response
def test_filter_by_domain(GIVEN_currently_account_quantity_with_two_quantity_of_asset):
def condition():
domain = GIVEN_currently_account_quantity_with_two_quantity_of_asset.domain
with allure.step(
f'WHEN client_cli query assets'
f'in the "{domain}" domain'):
assets = iroha.list_filter(
f'{{"Or": [{{"Identifiable": {{"Contains": "#{domain}#"}}}}, {{"And": [{{"Identifiable": {{"Contains": "##"}}}}, {{"Identifiable": {{"EndsWith": "@{domain}"}}}}]}}]}}'
).assets()
with allure.step(
f'THEN Iroha should return only assets with this domain'):
allure.attach(
json.dumps(assets),
name='assets',
attachment_type=allure.attachment_type.JSON)
return assets and all(f'#{domain}#' in asset or ('##' in asset and f"@{domain}" in asset) for asset in assets)
client_cli.wait_for(condition)


def test_filter_by_asset_name(GIVEN_currently_account_quantity_with_two_quantity_of_asset):
def condition():
name = GIVEN_currently_account_quantity_with_two_quantity_of_asset.name
with allure.step(
f'WHEN client_cli query assets with name "{name}"'):
assets = iroha.list_filter(f'{{"Identifiable": {{"StartsWith": "{name}#"}}}}').assets()
with allure.step(
f'THEN Iroha should return only assets with this name'):
allure.attach(
json.dumps(assets),
name='assets',
attachment_type=allure.attachment_type.JSON)
return assets and all(asset.startswith(name) for asset in assets)
client_cli.wait_for(condition)

def test_filter_by_asset_id(GIVEN_currently_authorized_account, GIVEN_currently_account_quantity_with_two_quantity_of_asset):
def condition():
asset_id = GIVEN_currently_account_quantity_with_two_quantity_of_asset.name + "##" + GIVEN_currently_authorized_account.name + "@" + GIVEN_currently_authorized_account.domain
with allure.step(
f'WHEN client_cli query assets with asset id "{asset_id}"'):
assets = iroha.list_filter(f'{{"Identifiable": {{"Is": "{asset_id}"}}}}').assets()
with allure.step(
f'THEN Iroha should return only assets with this id'):
allure.attach(
json.dumps(assets),
name='assets',
attachment_type=allure.attachment_type.JSON)
return assets and all(asset == asset_id for asset in assets)
client_cli.wait_for(condition)
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ def test_register_asset_with_empty_name(

@allure.label('sdk_test_id', 'register_asset_with_not_existing_domain')
def test_register_asset_with_not_existing_domain(
GIVEN_fake_name,
GIVEN_not_existing_name,
GIVEN_quantity_value_type,
GIVEN_fake_asset_name):
with allure.step(
f'WHEN client_cli tries to register an asset definition with not existing domain'):
client_cli.register().asset().definition(asset=GIVEN_fake_asset_name, domain=GIVEN_fake_name,
client_cli.register().asset().definition(asset=GIVEN_fake_asset_name, domain=GIVEN_not_existing_name,
value_type=GIVEN_quantity_value_type)
with allure.step(
f'THEN client_cli should have the error'):
Expand Down
13 changes: 5 additions & 8 deletions client_cli/pytests/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ def GIVEN_existence_domain_with_uppercase_letter(
@pytest.fixture()
def GIVEN_new_one_existence_account(GIVEN_new_one_existence_domain, GIVEN_public_key):
"""Fixture to create and register an existing account."""
name = fake_name()
account = Account(
name=fake_name(),
name=name,
domain=GIVEN_new_one_existence_domain.name,
public_key=GIVEN_public_key)
name = fake_name()
with allure.step(f'GIVEN the account "{name}" in the "{GIVEN_new_one_existence_domain.name}" domain'):
client_cli.register().account(
account=account.name,
Expand All @@ -72,8 +72,7 @@ def GIVEN_currently_account_quantity_with_two_quantity_of_asset(
asset_def = AssetDefinition(name=GIVEN_fake_asset_name,
domain=GIVEN_currently_authorized_account.domain,
value_type=GIVEN_quantity_value_type)
name = fake_name()
with allure.step(f'GIVEN the asset_definition "{name}" '
with allure.step(f'GIVEN the asset_definition "{GIVEN_fake_asset_name}" '
f'in the "{GIVEN_currently_authorized_account.domain}" domain'):
client_cli.register().asset().definition(
asset=asset_def.name,
Expand All @@ -97,8 +96,7 @@ def GIVEN_existence_asset_definition_with_quantity_value_type(
asset_def = AssetDefinition(name=GIVEN_fake_asset_name,
domain=GIVEN_new_one_existence_domain.name,
value_type=GIVEN_quantity_value_type)
name = fake_name()
with allure.step(f'GIVEN the asset_definition "{name}" '
with allure.step(f'GIVEN the asset_definition "{GIVEN_fake_asset_name}" '
f'in the "{GIVEN_new_one_existence_domain.name}" domain'):
client_cli.register().asset().definition(asset=asset_def.name,
domain=asset_def.domain,
Expand All @@ -114,8 +112,7 @@ def GIVEN_existence_asset_definition_with_store_value_type(
asset_def = AssetDefinition(name=GIVEN_fake_asset_name,
domain=GIVEN_new_one_existence_domain.name,
value_type=GIVEN_store_value_type)
name = fake_name()
with allure.step(f'GIVEN the asset_definition "{name}" '
with allure.step(f'GIVEN the asset_definition "{GIVEN_fake_asset_name}" '
f'in the "{GIVEN_new_one_existence_domain.name}" domain'):
client_cli.register().asset().definition(asset=asset_def.name,
domain=asset_def.domain,
Expand Down
19 changes: 19 additions & 0 deletions client_cli/pytests/test/domains/test_domains_query_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import json
import allure

from src.client_cli import iroha, iroha, client_cli

def test_filter_by_domain(GIVEN_new_one_existence_domain):
def condition():
domain_name = GIVEN_new_one_existence_domain.name
with allure.step(
f'WHEN client_cli query domains filtered by name "{domain_name}"'):
domains = iroha.list_filter(f'{{"Identifiable": {{"Is": "{domain_name}"}}}}').domains()
with allure.step(
f'THEN Iroha should return only return domains with "{domain_name}" name'):
allure.attach(
json.dumps(domains),
name='domains',
attachment_type=allure.attachment_type.JSON)
return domains and all(domain == domain_name for domain in domains)
client_cli.wait_for(condition)
Binary file modified configs/peer/validator.wasm
Binary file not shown.

0 comments on commit e9ce820

Please sign in to comment.