Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Parametrize block number test #238

Merged
merged 6 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -139,16 +139,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