Skip to content

Commit

Permalink
Parametrize block number test (0xSpaceShard#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikiw authored and internnos committed Aug 30, 2022
1 parent 2813d24 commit ef0f0e8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
Empty file modified scripts/format.sh
100755 → 100644
Empty file.
22 changes: 22 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Fixtures for tests
"""

from __future__ import annotations

from test.util import run_devnet_in_background, terminate_and_wait

import pytest


@pytest.fixture(name="run_devnet_in_background")
def fixture_run_devnet_in_background(request) -> None:
"""
Run devnet instance in background
"""
args = getattr(request, "param", [])
proc = run_devnet_in_background(*args)
try:
yield
finally:
terminate_and_wait(proc)
15 changes: 1 addition & 14 deletions test/rpc/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import typing

from test.util import load_file_content, run_devnet_in_background, terminate_and_wait
from test.util import load_file_content

from starkware.starknet.definitions import constants
from starkware.starknet.services.api.contract_class import ContractClass
Expand Down Expand Up @@ -138,16 +138,3 @@ def fixture_rpc_invoke_tx_common() -> dict:
"nonce": "0x00",
"type": "INVOKE",
}


@pytest.fixture(name="run_devnet_in_background")
def fixture_run_devnet_in_background(request) -> None:
"""
Run devnet instance in background
"""
args = getattr(request, "param", [])
proc = run_devnet_in_background(*args)
try:
yield
finally:
terminate_and_wait(proc)
45 changes: 30 additions & 15 deletions test/test_block_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,52 @@
Test block number
"""

import pytest

from .shared import ARTIFACTS_PATH, FAILING_CONTRACT_PATH, GENESIS_BLOCK_NUMBER
from .util import declare, devnet_in_background, deploy, call, invoke

BLOCK_NUMBER_CONTRACT_PATH = f"{ARTIFACTS_PATH}/block_number.cairo/block_number.json"
BLOCK_NUMBER_ABI_PATH = f"{ARTIFACTS_PATH}/block_number.cairo/block_number_abi.json"


@pytest.fixture(name="expected_hash")
def fixture_expected_hash(request):
"""
Fixture to return values of expected hash
"""
return request.param


def my_get_block_number(address: str):
"""Execute my_get_block_number on block_number.cairo contract deployed at `address`"""
return call(
function="my_get_block_number", address=address, abi_path=BLOCK_NUMBER_ABI_PATH
)


def base_workflow():
"""Used by test cases to perform the test"""
deploy_info = deploy(BLOCK_NUMBER_CONTRACT_PATH)
@pytest.mark.usefixtures("run_devnet_in_background")
@pytest.mark.parametrize(
"run_devnet_in_background, expected_hash",
[
([], "0x4f1ea446f67c1be47619444eae4d8118f6e017d0e6fe16e89b3df03da38606d"),
(["--lite-mode"], "0x0"),
],
indirect=True,
)
def test_block_number_incremented(expected_hash):
"""
Tests how block number is incremented in regular mode and lite mode.
In regular mode with salt "0x42" our expected hash is
0x4f1ea446f67c1be47619444eae4d8118f6e017d0e6fe16e89b3df03da38606d.
In lite mode we expect 0x0 transaction hash.
"""

deploy_info = deploy(BLOCK_NUMBER_CONTRACT_PATH, salt="0x42")
block_number_before = my_get_block_number(deploy_info["address"])

assert int(block_number_before) == GENESIS_BLOCK_NUMBER + 1
assert expected_hash == deploy_info["tx_hash"]

invoke(
function="write_block_number",
Expand All @@ -41,18 +68,6 @@ def base_workflow():
assert int(block_number_after) == GENESIS_BLOCK_NUMBER + 2


@devnet_in_background()
def test_block_number_incremented():
"""Tests how block number is incremented in regular mode"""
base_workflow()


@devnet_in_background("--lite-mode")
def test_block_number_incremented_in_lite_mode():
"""Tests compatibility with lite mode"""
base_workflow()


@devnet_in_background()
def test_block_number_incremented_on_declare():
"""Declare tx should increment get_block_number response"""
Expand Down

0 comments on commit ef0f0e8

Please sign in to comment.