diff --git a/Pipfile.lock b/Pipfile.lock index ccb514bc..6661e9b1 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -186,11 +186,11 @@ }, "google-api-python-client": { "hashes": [ - "sha256:8375489232823f44c601196a960505e03ec58c95ddb6415c6b1d1d76b468f8ba", - "sha256:87af2462b922c976291fcd0d062a0011d4ac41b11cee04089aa9577dd7b44ae3" + "sha256:20bc8f711883f9f8f96d96bc44b40ad0e2b0a9ef7df53e7c738948584fddd1c2", + "sha256:26402b51b21480e5f2de2650f4dc41d19db031ce28d26047f30dad0803fbf27d" ], "markers": "python_version >= '3.6'", - "version": "==2.15.0" + "version": "==2.17.0" }, "google-auth": { "hashes": [ diff --git a/tests/staking/tests.py b/tests/staking/tests.py index 961570ea..c67800b0 100644 --- a/tests/staking/tests.py +++ b/tests/staking/tests.py @@ -22,6 +22,8 @@ import json from unittest import TestCase +from google.protobuf.json_format import ParseDict + from pycosm.protos.cosmos.staking.v1beta1.query_pb2 import ( QueryDelegationRequest, QueryDelegationResponse, @@ -59,104 +61,566 @@ class StakingRestClientTestCase(TestCase): """Test case for StakingRestClient class.""" - @classmethod - def setUpClass(cls): - """Set up test case.""" - content = {} - + @staticmethod + def test_Validators(): + """Test Validators method.""" + content = { + "validators": [ + { + "operator_address": "string", + "jailed": True, + "status": "BOND_STATUS_UNSPECIFIED", + "tokens": "123", + "delegator_shares": "string", + "description": { + "moniker": "string", + "identity": "string", + "website": "string", + "security_contact": "string", + "details": "string", + }, + "unbonding_height": "1", + "unbonding_time": "2021-08-18T11:23:18.208Z", + "commission": { + "commission_rates": { + "rate": "0", + "max_rate": "1", + "max_change_rate": "1", + }, + "update_time": "2021-08-18T11:23:18.208Z", + }, + "min_self_delegation": "0", + } + ], + "pagination": {"next_key": "", "total": "1"}, + } mock_client = MockRestClient(json.dumps(content)) - cls.client = StakingRestClient(mock_client) - def test_Validators(self): - """Test Validators method.""" - request = QueryValidatorsRequest() - result = self.client.Validators(request) - self.assertIsInstance(result, QueryValidatorsResponse) + expected_response = ParseDict(content, QueryValidatorsResponse()) + + staking = StakingRestClient(mock_client) + + assert staking.Validators(QueryValidatorsRequest() == expected_response) + assert mock_client.last_base_url == "/cosmos/staking/v1beta1/validators" - def test_Validator(self): + @staticmethod + def test_Validator(): """Test Validator method.""" - request = QueryValidatorRequest(validator_addr="validator_addr") - result = self.client.Validator(request) - self.assertIsInstance(result, QueryValidatorResponse) - def test_ValidatorDelegations(self): + content = { + "validator": { + "operator_address": "string", + "jailed": True, + "status": "BOND_STATUS_UNSPECIFIED", + "tokens": "string", + "delegator_shares": "string", + "description": { + "moniker": "string", + "identity": "string", + "website": "string", + "security_contact": "string", + "details": "string", + }, + "unbonding_height": "0", + "unbonding_time": "2021-08-18T10:33:53.339Z", + "min_self_delegation": "string", + } + } + mock_client = MockRestClient(json.dumps(content)) + + expected_response = ParseDict(content, QueryValidatorResponse()) + + staking = StakingRestClient(mock_client) + + assert ( + staking.Validator(QueryValidatorRequest(validator_addr="validator_addr")) + == expected_response + ) + assert ( + mock_client.last_base_url + == "/cosmos/staking/v1beta1/validators/validator_addr" + ) + + @staticmethod + def test_ValidatorDelegations(): """Test ValidatorDelegations method.""" - request = QueryValidatorDelegationsRequest(validator_addr="validator_addr") - result = self.client.ValidatorDelegations(request) - self.assertIsInstance(result, QueryValidatorDelegationsResponse) - def test_ValidatorUnbondingDelegations(self): + content = { + "delegation_responses": [ + { + "delegation": { + "delegator_address": "fetchdelegator", + "validator_address": "fetchvalidator", + "shares": "123", + }, + "balance": {"denom": "atestfet", "amount": "12345"}, + } + ], + "pagination": {"next_key": "", "total": "0"}, + } + mock_client = MockRestClient(json.dumps(content)) + + expected_response = ParseDict(content, QueryValidatorDelegationsResponse()) + + staking = StakingRestClient(mock_client) + + assert ( + staking.ValidatorDelegations( + QueryValidatorDelegationsRequest(validator_addr="validator_addr") + ) + == expected_response + ) + assert ( + mock_client.last_base_url + == "/cosmos/staking/v1beta1/validators/validator_addr/delegations" + ) + + @staticmethod + def test_ValidatorUnbondingDelegations(): """Test ValidatorUnbondingDelegations method.""" - request = QueryValidatorUnbondingDelegationsRequest( - validator_addr="validator_addr" + + content = { + "unbonding_responses": [ + { + "delegator_address": "fetchddelegator", + "validator_address": "fetchvalidator", + "entries": [ + { + "creation_height": "123", + "completion_time": "2021-08-18T11:42:45.657Z", + "initial_balance": "12", + "balance": "123", + } + ], + } + ], + "pagination": {"next_key": "", "total": "0"}, + } + mock_client = MockRestClient(json.dumps(content)) + + expected_response = ParseDict( + content, QueryValidatorUnbondingDelegationsResponse() + ) + + staking = StakingRestClient(mock_client) + + assert ( + staking.ValidatorUnbondingDelegations( + QueryValidatorUnbondingDelegationsRequest( + validator_addr="validator_addr" + ) + ) + == expected_response + ) + assert ( + mock_client.last_base_url + == "/cosmos/staking/v1beta1/validators/validator_addr/unbonding_delegations" ) - result = self.client.ValidatorUnbondingDelegations(request) - self.assertIsInstance(result, QueryValidatorUnbondingDelegationsResponse) - def test_Delegation(self): + @staticmethod + def test_Delegation(): """Test Delegation method.""" - request = QueryDelegationRequest( - validator_addr="validator_addr", delegator_addr="delegator_addr" + + content = { + "delegation_response": { + "delegation": { + "delegator_address": "fetchdelegator", + "validator_address": "fetchvalidator", + "shares": "123", + }, + "balance": {"denom": "atestfet", "amount": "123"}, + } + } + mock_client = MockRestClient(json.dumps(content)) + + expected_response = ParseDict(content, QueryDelegationResponse()) + + staking = StakingRestClient(mock_client) + + assert ( + staking.Delegation( + QueryDelegationRequest( + validator_addr="validator_addr", delegator_addr="delegator_addr" + ) + ) + == expected_response + ) + assert ( + mock_client.last_base_url + == "/cosmos/staking/v1beta1/validators/validator_addr/delegations/delegator_addr" ) - result = self.client.Delegation(request) - self.assertIsInstance(result, QueryDelegationResponse) - def test_UnbondingDelegation(self): + @staticmethod + def test_UnbondingDelegation(): """Test UnbondingDelegation method.""" - request = QueryUnbondingDelegationRequest( - validator_addr="validator_addr", delegator_addr="delegator_addr" + + content = { + "unbond": { + "delegator_address": "fetchdelegator", + "validator_address": "fetchvalidator", + "entries": [ + { + "creation_height": "1", + "completion_time": "2021-08-18T11:55:13.614Z", + "initial_balance": "123", + "balance": "1234", + } + ], + } + } + mock_client = MockRestClient(json.dumps(content)) + + expected_response = ParseDict(content, QueryUnbondingDelegationResponse()) + + staking = StakingRestClient(mock_client) + + assert ( + staking.UnbondingDelegation( + QueryUnbondingDelegationRequest( + validator_addr="validator_addr", delegator_addr="delegator_addr" + ) + ) + == expected_response + ) + assert ( + mock_client.last_base_url + == "/cosmos/staking/v1beta1/validators/validator_addr/delegations/delegator_addr/unbonding_delegation" ) - result = self.client.UnbondingDelegation(request) - self.assertIsInstance(result, QueryUnbondingDelegationResponse) - def test_DelegatorDelegations(self): + @staticmethod + def test_DelegatorDelegations(): """Test DelegatorDelegations method.""" - request = QueryDelegatorDelegationsRequest(delegator_addr="delegator_addr") - result = self.client.DelegatorDelegations(request) - self.assertIsInstance(result, QueryDelegatorDelegationsResponse) - def test_DelegatorUnbondingDelegations(self): + content = { + "delegation_responses": [ + { + "delegation": { + "delegator_address": "fetchdelegator", + "validator_address": "fetchvalidator", + "shares": "123", + }, + "balance": {"denom": "atestfet", "amount": "123"}, + } + ], + "pagination": {"next_key": "", "total": "0"}, + } + mock_client = MockRestClient(json.dumps(content)) + + expected_response = ParseDict(content, QueryDelegatorDelegationsResponse()) + + staking = StakingRestClient(mock_client) + + assert ( + staking.DelegatorDelegations( + QueryDelegatorDelegationsRequest(delegator_addr="delegator_addr") + ) + == expected_response + ) + assert ( + mock_client.last_base_url + == "/cosmos/staking/v1beta1/delegations/delegator_addr" + ) + + @staticmethod + def test_DelegatorUnbondingDelegations(): """Test DelegatorUnbondingDelegations method.""" - request = QueryDelegatorUnbondingDelegationsRequest( - delegator_addr="delegator_addr" + + content = { + "unbonding_responses": [ + { + "delegator_address": "fetchdelegator", + "validator_address": "fetchvalidator", + "entries": [ + { + "creation_height": "123", + "completion_time": "2021-08-18T12:07:23.832Z", + "initial_balance": "123", + "balance": "1234", + } + ], + } + ], + "pagination": {"next_key": "", "total": "0"}, + } + + mock_client = MockRestClient(json.dumps(content)) + + expected_response = ParseDict( + content, QueryDelegatorUnbondingDelegationsResponse() + ) + + staking = StakingRestClient(mock_client) + + assert ( + staking.DelegatorUnbondingDelegations( + QueryDelegatorUnbondingDelegationsRequest( + delegator_addr="delegator_addr" + ) + ) + == expected_response + ) + assert ( + mock_client.last_base_url + == "/cosmos/staking/v1beta1/delegators/delegator_addr/unbonding_delegations" ) - result = self.client.DelegatorUnbondingDelegations(request) - self.assertIsInstance(result, QueryDelegatorUnbondingDelegationsResponse) - def test_Redelegations(self): + @staticmethod + def test_Redelegations(): """Test Redelegations method.""" - request = QueryRedelegationsRequest(delegator_addr="delegator_addr") - result = self.client.Redelegations(request) - self.assertIsInstance(result, QueryRedelegationsResponse) - def test_DelegatorValidators(self): + content = { + "redelegation_responses": [ + { + "redelegation": { + "delegator_address": "fetchdelegator", + "validator_src_address": "fetchsrc", + "validator_dst_address": "fetchdst", + "entries": [ + { + "creation_height": "123", + "completion_time": "2021-08-18T12:10:21.412Z", + "initial_balance": "123", + "shares_dst": "123", + } + ], + }, + "entries": [ + { + "redelegation_entry": { + "creation_height": "123", + "completion_time": "2021-08-18T12:10:21.412Z", + "initial_balance": "123", + "shares_dst": "123", + }, + "balance": "123", + } + ], + } + ], + "pagination": {"next_key": "", "total": "0"}, + } + + mock_client = MockRestClient(json.dumps(content)) + + expected_response = ParseDict(content, QueryRedelegationsResponse()) + + staking = StakingRestClient(mock_client) + + assert ( + staking.Redelegations( + QueryRedelegationsRequest(delegator_addr="delegator_addr") + ) + == expected_response + ) + assert ( + mock_client.last_base_url + == "/cosmos/staking/v1beta1/delegators/delegator_addr/redelegations" + ) + + @staticmethod + def test_DelegatorValidators(): """Test DelegatorValidators method.""" - request = QueryDelegatorValidatorsRequest(delegator_addr="delegator_addr") - result = self.client.DelegatorValidators(request) - self.assertIsInstance(result, QueryDelegatorValidatorsResponse) - def test_DelegatorValidator(self): + content = { + "validators": [ + { + "operator_address": "fetchoperator", + "jailed": True, + "status": "BOND_STATUS_UNSPECIFIED", + "tokens": "123", + "delegator_shares": "123", + "description": { + "moniker": "string", + "identity": "string", + "website": "string", + "security_contact": "string", + "details": "string", + }, + "unbonding_height": "123", + "unbonding_time": "2021-08-18T12:14:55.780Z", + "commission": { + "commission_rates": { + "rate": "123", + "max_rate": "1234", + "max_change_rate": "1234", + }, + "update_time": "2021-08-18T12:14:55.780Z", + }, + "min_self_delegation": "123", + } + ], + "pagination": {"next_key": "", "total": "0"}, + } + + mock_client = MockRestClient(json.dumps(content)) + + expected_response = ParseDict(content, QueryDelegatorValidatorsResponse()) + + staking = StakingRestClient(mock_client) + + assert ( + staking.DelegatorValidators( + QueryDelegatorValidatorsRequest(delegator_addr="delegator_addr") + ) + == expected_response + ) + assert ( + mock_client.last_base_url + == "/cosmos/staking/v1beta1/delegators/delegator_addr/validators" + ) + + @staticmethod + def test_DelegatorValidator(): """Test DelegatorValidator method.""" - request = QueryDelegatorValidatorRequest( - validator_addr="validator_addr", delegator_addr="delegator_addr" + + content = { + "validator": { + "operator_address": "fetchoperator", + "jailed": True, + "status": "BOND_STATUS_UNSPECIFIED", + "tokens": "123", + "delegator_shares": "123", + "description": { + "moniker": "string", + "identity": "string", + "website": "string", + "security_contact": "string", + "details": "string", + }, + "unbonding_height": "123", + "unbonding_time": "2021-08-18T12:17:33.141Z", + "commission": { + "commission_rates": { + "rate": "123", + "max_rate": "1234", + "max_change_rate": "1234", + }, + "update_time": "2021-08-18T12:17:33.141Z", + }, + "min_self_delegation": "123", + } + } + + mock_client = MockRestClient(json.dumps(content)) + + expected_response = ParseDict(content, QueryDelegatorValidatorResponse()) + + staking = StakingRestClient(mock_client) + + assert ( + staking.DelegatorValidator( + QueryDelegatorValidatorRequest( + validator_addr="validator_addr", delegator_addr="delegator_addr" + ) + ) + == expected_response + ) + assert ( + mock_client.last_base_url + == "/cosmos/staking/v1beta1/delegators/delegator_addr/validators/validator_addr" ) - result = self.client.DelegatorValidator(request) - self.assertIsInstance(result, QueryDelegatorValidatorResponse) - def test_HistoricalInfo(self): + @staticmethod + def test_HistoricalInfo(): """Test HistoricalInfo method.""" - request = QueryHistoricalInfoRequest(height=1) - result = self.client.HistoricalInfo(request) - self.assertIsInstance(result, QueryHistoricalInfoResponse) - def test_Pool(self): + content = { + "hist": { + "header": { + "version": {"block": "123", "app": "123"}, + "chain_id": "string", + "height": "123", + "time": "2021-08-18T12:19:32.225Z", + "last_block_id": { + "hash": "string", + "part_set_header": {"total": 0, "hash": "string"}, + }, + "last_commit_hash": "string", + "data_hash": "string", + "validators_hash": "string", + "next_validators_hash": "string", + "consensus_hash": "string", + "app_hash": "string", + "last_results_hash": "string", + "evidence_hash": "string", + "proposer_address": "string", + }, + "valset": [ + { + "operator_address": "fetchoperator", + "jailed": True, + "status": "BOND_STATUS_UNSPECIFIED", + "tokens": "123", + "delegator_shares": "123", + "description": { + "moniker": "string", + "identity": "string", + "website": "string", + "security_contact": "string", + "details": "string", + }, + "unbonding_height": "123", + "unbonding_time": "2021-08-18T12:19:32.225Z", + "commission": { + "commission_rates": { + "rate": "123", + "max_rate": "1234", + "max_change_rate": "1234", + }, + "update_time": "2021-08-18T12:19:32.225Z", + }, + "min_self_delegation": "123", + } + ], + } + } + + mock_client = MockRestClient(json.dumps(content)) + + expected_response = ParseDict(content, QueryHistoricalInfoResponse()) + + staking = StakingRestClient(mock_client) + + assert ( + staking.HistoricalInfo(QueryHistoricalInfoRequest(height=1)) + == expected_response + ) + assert mock_client.last_base_url == "/cosmos/staking/v1beta1/historical_info/1" + + @staticmethod + def test_Pool(): """Test Pool method.""" - request = QueryPoolRequest() - result = self.client.Pool(request) - self.assertIsInstance(result, QueryPoolResponse) - def test_Params(self): + content = {"pool": {"not_bonded_tokens": "123", "bonded_tokens": "123"}} + + mock_client = MockRestClient(json.dumps(content)) + + expected_response = ParseDict(content, QueryPoolResponse()) + + staking = StakingRestClient(mock_client) + + assert staking.Pool(QueryPoolRequest()) == expected_response + assert mock_client.last_base_url == "/cosmos/staking/v1beta1/pool" + + @staticmethod + def test_Params(): """Test Params method.""" - request = QueryParamsRequest() - result = self.client.Params(request) - self.assertIsInstance(result, QueryParamsResponse) + + content = { + "params": { + "unbonding_time": "123s", + "max_validators": 0, + "max_entries": 0, + "historical_entries": 0, + "bond_denom": "atestfet", + } + } + + mock_client = MockRestClient(json.dumps(content)) + + expected_response = ParseDict(content, QueryParamsResponse()) + + staking = StakingRestClient(mock_client) + + assert staking.Params(QueryParamsRequest()) == expected_response + assert mock_client.last_base_url == "/cosmos/staking/v1beta1/params"