Skip to content

Commit

Permalink
fix-test: accept integers when expected type is float
Browse files Browse the repository at this point in the history
  • Loading branch information
mesudip committed Aug 1, 2024
1 parent a6c38ba commit 501ebe9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
14 changes: 14 additions & 0 deletions tests/govtool-backend/lib/assertions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

from typing import _TypedDictMeta

def assert_data_type(typed_dict:_TypedDictMeta, result_data):
for key, val in typed_dict.__annotations__.items():
if typed_dict.__annotations__[key] == float:
assert isinstance(
result_data[key],float
) or isinstance(result_data[key],int), f"{dir(typed_dict.__name__)}.{key} should be of type {typed_dict.__annotations__[key]} got {type(result_data[key])}"

else:
assert isinstance(
result_data[key], typed_dict.__annotations__[key]
), f"{dir(typed_dict.__name__)}.{key} should be of type {typed_dict.__annotations__[key]} got {type(result_data[key])}"
4 changes: 2 additions & 2 deletions tests/govtool-backend/test_cases/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

@pytest.fixture(scope="session")
def govtool_api():
base_url: str = os.environ.get("BASE_URL")
metrics_url: str = os.environ.get("METRICS_URL")
base_url: (str| None) = os.environ.get("BASE_URL")
metrics_url: (str| None) = os.environ.get("METRICS_URL")

if base_url is not None:
base_url = base_url[:-1] if base_url.endswith("/") else base_url
Expand Down
11 changes: 5 additions & 6 deletions tests/govtool-backend/test_cases/test_drep.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import List
from lib.assertions import assert_data_type
from models.TestData import Drep, VoteonProposal, Vote, Proposal, DrepInfo
import allure


def validate_drep_list(drep_list: [Drep]) -> bool:
def validate_drep_list(drep_list: List[Drep]) -> bool:
for item in drep_list:
if not isinstance(item, dict):
return False
Expand All @@ -13,7 +15,7 @@ def validate_drep_list(drep_list: [Drep]) -> bool:
return True


def validate_voteonproposal_list(voteonproposal_list: [VoteonProposal]) -> bool:
def validate_voteonproposal_list(voteonproposal_list: List[VoteonProposal]) -> bool:
for item in voteonproposal_list:
if not isinstance(item, dict):
return False
Expand All @@ -40,10 +42,7 @@ def validate_voteonproposal_list(voteonproposal_list: [VoteonProposal]) -> bool:


def validate_drep_info(drep):
for key, val in DrepInfo.__annotations__.items():
assert isinstance(
drep[key], DrepInfo.__annotations__[key]
), f"drepInfo.{key} should be of type {DrepInfo.__annotations__[key]} got {type(drep[key])}"
assert_data_type(DrepInfo,drep)


@allure.story("Drep")
Expand Down
18 changes: 4 additions & 14 deletions tests/govtool-backend/test_cases/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import allure

from lib.assertions import assert_data_type
from models.TestData import EpochParam, NetworkMetrics, TxStatus


def validate_epoch_param(epoch_param):
for key, val in EpochParam.__annotations__.items():
assert isinstance(
epoch_param[key], EpochParam.__annotations__[key]
), f"epochParam.{key} should be of type {EpochParam.__annotations__[key]} got {type(epoch_param[key])}"
assert_data_type(EpochParam,epoch_param)


def validate_network_metrics(network_metrics):
for key, val in NetworkMetrics.__annotations__.items():
assert isinstance(
network_metrics[key], NetworkMetrics.__annotations__[key]
), f"epochParam.{key} should be of type {NetworkMetrics.__annotations__[key]} got {type(network_metrics[key])}"


def validate_model(model, item):
for key, val in model.__annotations__.items():
assert isinstance(item[key], val), f"{model.__name__}.{key} should be of type {val} got {type(item[key])}"
assert_data_type(NetworkMetrics,network_metrics)


@allure.story("Misc")
Expand All @@ -37,4 +27,4 @@ def test_get_network_metrics(govtool_api):
@allure.story("Misc")
def test_get_transaction_status(govtool_api):
tx_status = govtool_api.get_transaction_status("ff" * 32).json()
validate_model(TxStatus, tx_status)
assert_data_type(TxStatus, tx_status)

0 comments on commit 501ebe9

Please sign in to comment.