Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test runner switched to Pytest #62

Merged
merged 14 commits into from
Sep 1, 2021
11 changes: 11 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[run]
omit =
# omit anything in a .local directory anywhere
*/.local/*
# omit tests
tests/*
# omit Tox
.tox/*

[html]
directory = coverage_report
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ jobs:
run: tox -e copyright-check
- name: Manifest check
run: tox -e check-manifest
- name: Unit tests
- name: Tests and coverage
run: tox -e test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ dist/

.DS_Store
*/.DS_Store

# Coverage related files and folders
.coverage
coverage_report/
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ pylint:

.PHONY: test
test:
python -m unittest discover -s .
coverage run -m pytest $(PYCOSM_TESTS_DIR) --doctest-modules --ignore $(PYCOSM_TESTS_DIR)/vulture_whitelist.py
coverage report
coverage html

####################
### License and copyright checks
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ profile = black
ignore =
Makefile
.pylintrc
.coveragerc
1 change: 0 additions & 1 deletion tests/clients/dummy_contract.wasm

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
from google.protobuf.json_format import ParseDict

from cosmpy.clients.cosmwasm_client import CosmWasmClient
from cosmpy.protos.cosmos.auth.v1beta1.auth_pb2 import BaseAccount
from cosmpy.protos.cosmos.auth.v1beta1.query_pb2 import QueryAccountResponse
from cosmpy.protos.cosmos.bank.v1beta1.query_pb2 import QueryBalanceResponse
from cosmpy.protos.cosmwasm.wasm.v1beta1.query_pb2 import (
QuerySmartContractStateResponse,
)
from tests.helpers import MockRestClient


Expand Down Expand Up @@ -69,28 +67,32 @@ def test_query_account_data():
"sequence": "1",
}
}
expected_response = ParseDict(content, QueryAccountResponse())
account_response = ParseDict(content, QueryAccountResponse())

account = BaseAccount()
if account_response.account.Is(BaseAccount.DESCRIPTOR):
account_response.account.Unpack(account)

mock_rest_client = MockRestClient(json.dumps(content))
wasm_client = CosmWasmClient(mock_rest_client)
response = wasm_client.query_account_data("account")
response = wasm_client.query_account_data("address")

assert response == expected_response
assert response == account
assert mock_rest_client.last_base_url == "/cosmos/auth/v1beta1/accounts/address"

@staticmethod
def test_query_contract_state():
"""Test query contract state for the positive result."""

raw_content = b'{\n "data": {"balance":"1"}\n}'
expected_response = QuerySmartContractStateResponse(data=b'{"balance": "1"}')
raw_content = b'{"data": {"balance":"1"}}'
expected_response = {"balance": "1"}

mock_rest_client = MockRestClient(raw_content)
wasm_client = CosmWasmClient(mock_rest_client)
response = wasm_client.query_contract_state("account", "denom")
response = wasm_client.query_contract_state("fetchcontractaddress", {})

assert response == expected_response
assert (
mock_rest_client.last_base_url
== "/wasm/v1beta1/contract/fetchcontractaddress/smart/e30=?"
== "/wasm/v1beta1/contract/fetchcontractaddress/smart/e30="
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import base64
import gzip
import json
import tempfile
import unittest
from typing import Optional
from unittest.mock import patch
Expand Down Expand Up @@ -245,17 +246,18 @@ def test_get_packed_exec_msg(self):

def test_get_packed_store_msg(self):
"""Test correct generation of packed store msg."""
msg = self.signing_wasm_client.get_packed_store_msg(
ADDRESS_PK, CONTRACT_FILENAME
)
with tempfile.NamedTemporaryFile(suffix=CONTRACT_FILENAME) as tmp:
tmp.write(CONTRACT_BYTECODE)
tmp.flush()
msg = self.signing_wasm_client.get_packed_store_msg(ADDRESS_PK, tmp.name)

msg_dict = MessageToDict(msg)
assert len(msg_dict) == 3
assert msg_dict["@type"] == "/cosmwasm.wasm.v1beta1.MsgStoreCode"
assert msg_dict["sender"] == str(ADDRESS_PK)
zipped_bytecode: bytes = base64.b64decode(msg_dict["wasmByteCode"])
original_bytecode: bytes = gzip.decompress(zipped_bytecode)
assert original_bytecode == CONTRACT_BYTECODE
self.assertEqual(original_bytecode, CONTRACT_BYTECODE)

def test_generate_tx(self):
"""Test correct generation of Tx."""
Expand Down Expand Up @@ -394,7 +396,11 @@ def test_deploy_contract(self):
self.signing_wasm_client.tx_client = mock_tx_client

with patch.object(self.signing_wasm_client, "get_code_id", mock_get_code_id):
result = self.signing_wasm_client.deploy_contract(CONTRACT_FILENAME)
with tempfile.NamedTemporaryFile(suffix=CONTRACT_FILENAME) as tmp:
tmp.write(CONTRACT_BYTECODE)
tmp.flush()
result = self.signing_wasm_client.deploy_contract(tmp.name)

assert result == CODE_ID

# Reconstruct original Tx from last tx request bytes
Expand Down
4 changes: 3 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ skipsdist = True
skip_install = True
deps =
-rrequirements-dev.txt
commands = python -m unittest discover -s .
commands =
coverage run -m pytest tests --doctest-modules --ignore tests/vulture_whitelist.py
coverage report

[testenv:liccheck]
skipsdist = True
Expand Down