Skip to content

Commit

Permalink
run integration tests
Browse files Browse the repository at this point in the history
* the main happy path still does not work
* remove old pytest django's django_db_setup mock
* to run the test suite WEB3_PROVIDER_URI env var needs to be set
  • Loading branch information
kvbik committed Oct 31, 2023
1 parent 2784f6d commit a72e046
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 76 deletions.
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def run(self):
extras_require = {
'test': [
'pytest>=5',
'pytest-django>=3.5',
'celery>=5',
],
'lint': [
Expand Down
64 changes: 0 additions & 64 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
Team,
)
import pytest
from pytest_django.fixtures import (
_disable_native_migrations,
)

from pretix_eth.payment import Ethereum
from rest_framework.test import APIClient
Expand Down Expand Up @@ -137,67 +134,6 @@ def _get_order_and_payment(order_kwargs=None, payment_kwargs=None, info_data=Non
return _get_order_and_payment


@pytest.fixture(scope="function")
def django_db_setup(
request,
django_test_environment,
django_db_blocker,
django_db_use_migrations,
django_db_keepdb,
django_db_createdb,
django_db_modify_db_settings,
):
"""
Copied and pasted from here:
https://github.com/pytest-dev/pytest-django/blob/d2973e21c34d843115acdbccdd7a16cb2714f4d3/pytest_django/fixtures.py#L84
We override this fixture and give it a "function" scope as a hack to force
the test database to be re-created per test case. This causes the same
database ids to be used for payment records in each test run. Usage of the
`reset_sequences` flag provided by pytest-django isn't always sufficient
since it can depend on whether or not a particular database supports
sequence resets.
As an example of why sequence resets are necessary, tests in the
`tests.integration.test_confirm_payments` module will fail if database ids
are not reset per test function. This is because the test wallet on the
Goerli test net must hardcode payment ids into transactions and token
transfers. If payment ids don't deterministically begin at 1 per test
case, payment ids in the Goerli test wallet won't correctly correspond to
payment ids generated during test runs.
"""
from pytest_django.compat import setup_databases, teardown_databases

setup_databases_args = {}

if not django_db_use_migrations:
_disable_native_migrations()

if django_db_keepdb and not django_db_createdb:
setup_databases_args["keepdb"] = True

with django_db_blocker.unblock():
db_cfg = setup_databases(
verbosity=request.config.option.verbose,
interactive=False,
**setup_databases_args
)

def teardown_database():
with django_db_blocker.unblock():
try:
teardown_databases(db_cfg, verbosity=request.config.option.verbose)
except Exception as exc:
request.node.warn(
pytest.PytestWarning(
"Error when trying to teardown test databases: %r" % exc
)
)

if not django_db_keepdb:
request.addfinalizer(teardown_database)


def pytest_addoption(parser):
parser.addoption(
"--require-web3", action="store_true", default=False,
Expand Down
17 changes: 7 additions & 10 deletions tests/integration/test_confirm_payments.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


WEB3_PROVIDER_URI = os.environ.get("WEB3_PROVIDER_URI")
SINGLE_RECEIVER_ADDRESS = "0x0000000000000000000000000000000000000000"


def check_web3_provider(pytesconfig):
Expand Down Expand Up @@ -66,12 +67,10 @@ def make_order_payment(payment_info, provider, get_request_order_payment):
{
"currency": "ETH - Goerli",
"amount": int("1000", base=10),
"hex_address": "0xb84AC43014d60AE5dCe5d36975eE461f31e953d3",
}, # Has about 0.5 ETH
{
"currency": "DAI - Goerli",
"amount": int("1000", base=10),
"hex_address": "0x18FF3A11FAF05F83198A8724006975ce414872Bc",
}, # Has about 48 DAI
]

Expand All @@ -84,10 +83,9 @@ def test_confirm_payment_enough(
provider, event, get_request_order_payment, pytestconfig
):
check_web3_provider(pytestconfig)
provider.settings.set("NETWORK_RPC_URL",
{"Goerli_RPC_URL": WEB3_PROVIDER_URI})
provider.settings.set("SINGLE_RECEIVER_ADDRESS",
"0x0000000000000000000000000000000000000000")
provider.settings.set("NETWORK_RPC_URL", {"Goerli_RPC_URL": WEB3_PROVIDER_URI})
provider.settings.set("SINGLE_RECEIVER_ADDRESS", SINGLE_RECEIVER_ADDRESS)

payments = []
orders = []
for payment_info in TEST_ENOUGH_AMOUNT:
Expand Down Expand Up @@ -117,6 +115,7 @@ def test_confirm_payment_dry_run(
):
check_web3_provider(pytestconfig)
provider.settings.set("NETWORK_RPC_URL", {"Goerli_RPC_URL": WEB3_PROVIDER_URI})
provider.settings.set("SINGLE_RECEIVER_ADDRESS", SINGLE_RECEIVER_ADDRESS)

payments = []
orders = []
Expand Down Expand Up @@ -144,12 +143,10 @@ def test_confirm_payment_dry_run(
{
"currency": "ETH - Goerli",
"amount": int("99000000", base=10),
"hex_address": "0xDb9574bf428A612fe13BEFFeB7F4bD8C73BF2D88",
}, # Has about 10'000'000 wei
{
"currency": "DAI - Goerli",
"amount": int("99000000", base=10),
"hex_address": "0x3d5091A1652e215c71C755BCfA97A08AFC9d6CB0",
}, # Has about 32'035 wei
]

Expand All @@ -163,6 +160,7 @@ def test_confirm_payment_lower_amount(
):
check_web3_provider(pytestconfig)
provider.settings.set("NETWORK_RPC_URL", {"Goerli_RPC_URL": WEB3_PROVIDER_URI})
provider.settings.set("SINGLE_RECEIVER_ADDRESS", SINGLE_RECEIVER_ADDRESS)

payments = []
orders = []
Expand All @@ -188,12 +186,10 @@ def test_confirm_payment_lower_amount(
{
"currency": "DAI - Goerli",
"amount": int("1000", base=10),
"hex_address": "0xb84AC43014d60AE5dCe5d36975eE461f31e953d3",
}, # Has enough amount, but in ETH
{
"currency": "ETH - Goerli",
"amount": int("1000", base=10),
"hex_address": "0x18FF3A11FAF05F83198A8724006975ce414872Bc",
}, # Has enough amount, but in DAI
]

Expand All @@ -207,6 +203,7 @@ def test_confirm_payment_wrong_currency(
):
check_web3_provider(pytestconfig)
provider.settings.set("NETWORK_RPC_URL", {"Goerli_RPC_URL": WEB3_PROVIDER_URI})
provider.settings.set("SINGLE_RECEIVER_ADDRESS", SINGLE_RECEIVER_ADDRESS)

payments = []
orders = []
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exclude=venv,docs
usedevelop=True
commands=
core: pytest {posargs:tests/core}
integration: pytest {posargs:tests/integration}
integration: pytest --require-web3 {posargs:tests/integration}
extras=test
basepython=
py311: python3.11
Expand All @@ -22,6 +22,7 @@ basepython=
py38: python3.8
py37: python3.7
passenv=
WEB3_PROVIDER_URI
ETHERSCAN_API_KEY

[testenv:lint]
Expand Down

0 comments on commit a72e046

Please sign in to comment.