Skip to content

Commit

Permalink
Update and modify files for the project
Browse files Browse the repository at this point in the history
- Updated files:
  - src/bee_py/types/type.py
  - src/bee_py/utils/type.py
  - tests/unit/test_bee_debug.py
  • Loading branch information
Aviksaikat committed Jan 5, 2024
1 parent 666ad86 commit b423da5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/bee_py/types/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ class PostageBatchBuckets(BaseModel):

class PostageBatchOptions(BaseModel):
label: Optional[str] = Field(None)
gas_price: Optional[str] = Field(default="", alias="gasPrice")
gas_price: Optional[Union[str, int]] = Field(default="", alias="gasPrice")
immutable_flag: Optional[bool] = Field(False)
wait_for_usable: Optional[bool] = Field(default=True, alias="waitForUsable")
wait_for_usable_timeout: Optional[int] = Field(default=120, alias="waitForUsableTimeout")
Expand Down
4 changes: 3 additions & 1 deletion src/bee_py/utils/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ def assert_postage_batch_options(value: Any, name: str = "PostageBatchOptions")
options = PostageBatchOptions.model_validate(value)

if options.gas_price:
options.gas_price = int(options.gas_price)
if not isinstance(options.gas_price, int) or options.gas_price < 0:
msg = "gasPrice must be a non-negative integer"
raise ValueError(msg)
Expand Down Expand Up @@ -492,7 +493,8 @@ def assert_transaction_hash(transaction_hash: Any, name: str = "TransactionHash"
raise TypeError(msg)

if not is_prefixed_hex_string(transaction_hash):
raise TypeError(f"Invalid transaction hash. Expected hex string got: {transaction_hash}")
msg = f"Invalid transaction hash. Expected hex string got: {transaction_hash}"
raise TypeError(msg)

# Hash is 64 long + '0x' prefix = 66
if len(transaction_hash) != PUBKEY_HEX_LENGTH:
Expand Down
38 changes: 27 additions & 11 deletions tests/unit/test_bee_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
request_options_assertions: list[tuple] = [
(1, TypeError),
(True, TypeError),
# ([], TypeError),
(lambda: {}, TypeError),
("string", TypeError),
({"timeout": "plur"}, pydantic.ValidationError),
Expand Down Expand Up @@ -456,9 +455,8 @@ def test_no_headers_if_create_postage_batch_is_set(requests_mock):
@pytest.mark.parametrize(
"input_value, expected_error_type",
[
({"immutable_flag": "asd"}, TypeError),
({"immutable_flag": -1}, TypeError),
({"immutable_flag": "true"}, TypeError),
({"immutable_flag": "asd"}, pydantic.ValidationError),
({"immutable_flag": -1}, pydantic.ValidationError),
],
)
def test_throw_error_if_wrong_immutable_input(input_value, expected_error_type):
Expand All @@ -467,23 +465,23 @@ def test_throw_error_if_wrong_immutable_input(input_value, expected_error_type):
bee.create_postage_batch("10", 17, input_value)


@pytest.mark.parametrize("input_value, expected_error_type", [(-1, TypeError), (15, TypeError)])
@pytest.mark.parametrize("input_value, expected_error_type", [(-1, ValueError), (15, BeeArgumentError)])
def test_throw_error_if_too_small_depth(input_value, expected_error_type):
bee = BeeDebug(MOCK_SERVER_URL)
with pytest.raises(TypeError):
with pytest.raises(expected_error_type):
bee.create_postage_batch("10", input_value)


@pytest.mark.parametrize("input_value, expected_error_type", [("-10", TypeError), ("0", TypeError)])
@pytest.mark.parametrize("input_value, expected_error_type", [("-10", ValueError), ("0", ValueError)])
def test_throw_error_if_too_small_amount(input_value, expected_error_type):
bee = BeeDebug(MOCK_SERVER_URL)
with pytest.raises(TypeError):
with pytest.raises(expected_error_type):
bee.create_postage_batch(input_value, 17)


def test_throw_error_if_too_big_depth():
bee = BeeDebug(MOCK_SERVER_URL)
with pytest.raises(TypeError):
with pytest.raises(BeeArgumentError):
bee.create_postage_batch("10", 256)


Expand All @@ -501,15 +499,33 @@ def test_get_postage_batch_transaction_assertions(input_value, expected_error_ty
bee.get_postage_batch(input_value)


@pytest.mark.parametrize("input_value, expected_error_type", request_options_assertions)
@pytest.mark.parametrize(
"input_value, expected_error_type",
[
(1, TypeError),
(True, TypeError),
(lambda: {}, TypeError),
("string", TypeError),
({"timeout": "plur"}, pydantic.ValidationError),
({"timeout": True}, TypeError),
({"timeout": {}}, pydantic.ValidationError),
({"timeout": []}, pydantic.ValidationError),
({"timeout": -1}, TypeError),
({"retry": "plur"}, TypeError),
({"retry": True}, TypeError),
({"retry": {}}, TypeError),
({"retry": []}, TypeError),
({"retry": -1}, TypeError),
],
)
def test_get_postage_batch_buckets(input_value, expected_error_type, test_batch_id):
with pytest.raises(expected_error_type):
bee = BeeDebug(MOCK_SERVER_URL, input_value)
bee.get_postage_batch_buckets(input_value, test_batch_id)


@pytest.mark.parametrize("input_value, expected_error_type", batch_id_assertion_data)
def test_get_postage_batch_buckets_batch_id_assertion(input_value, expected_error_type, test_batch_id):
def test_get_postage_batch_buckets_batch_id_assertion(input_value, expected_error_type):
bee = BeeDebug(MOCK_SERVER_URL)
with pytest.raises(expected_error_type):
bee.get_postage_batch_buckets(input_value)

0 comments on commit b423da5

Please sign in to comment.