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 4, 2024
1 parent 587a1cb commit e1a1e2a
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/bee_py/types/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,9 +634,9 @@ class PostageBatchBuckets(BaseModel):


class PostageBatchOptions(BaseModel):
label: Optional[str]
label: Optional[str] = Field(None)
gas_price: Optional[str] = Field(default="", alias="gasPrice")
immutable_flag: Optional[bool]
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: 4 additions & 0 deletions src/bee_py/utils/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
FeedType,
FileUploadOptions,
JsonFeedOptions,
PostageBatchOptions,
Reference,
ReferenceOrENS,
ReferenceResponse,
Expand Down Expand Up @@ -402,6 +403,9 @@ def assert_postage_batch_options(value: Any, name: str = "PostageBatchOptions")

assert_request_options(options, name)

if isinstance(options, dict):
options = PostageBatchOptions.model_validate(value)

if options.gas_price:
if not isinstance(options.gas_price, int) or options.gas_price < 0:
msg = "gasPrice must be a non-negative integer"
Expand Down
104 changes: 103 additions & 1 deletion tests/unit/test_bee_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
)

TRANSACTION_HASH = "36b7efd913ca4cf880b8eeac5093fa27b0825906c600685b6abdd6566e6cfe8f"

CASHOUT_RESPONSE = {"transactionHash": TRANSACTION_HASH}

BATCH_ID = "36b7efd913ca4cf880b8eeac5093fa27b0825906c600685b6abdd6566e6cfe8f"
BATCH_RESPONSE = {
"batchID": BATCH_ID,
}

MOCK_SERVER_URL = "http://localhost:12345/"

# * Endpoints
Expand Down Expand Up @@ -63,6 +67,23 @@
("", TypeError),
]

transaction_options_assertions: list[tuple] = [
(1, TypeError),
(True, TypeError),
([], TypeError),
("string", TypeError),
({"gasPrice": "plur"}, TypeError),
({"gasPrice": True}, TypeError),
({"gasPrice": {}}, TypeError),
({"gasPrice": []}, TypeError),
({"gasPrice": -1}, TypeError),
({"gasLimit": "plur"}, TypeError),
({"gasLimit": True}, TypeError),
({"gasLimit": {}}, TypeError),
({"gasLimit": []}, TypeError),
({"gasLimit": -1}, TypeError),
]


@pytest.mark.parametrize(
"url",
Expand Down Expand Up @@ -336,3 +357,84 @@ def test_deposit_tokens_throw_error_if_passed_wrong_gas_price_as_input(input_val
bee = BeeDebug(MOCK_SERVER_URL)
with pytest.raises(expected_error_type):
assert bee.deposit_tokens("1", input_value)


@pytest.mark.parametrize("input_value, expected_error_type", request_options_assertions)
def test_retrieve_extended_tag(input_value, expected_error_type):
with pytest.raises(expected_error_type):
bee = BeeDebug(MOCK_SERVER_URL, input_value)
bee.retrieve_extended_tag(0, input_value)


@pytest.mark.parametrize(
"input_value, expected_error_type",
[
("", TypeError),
([], TypeError),
({}, TypeError),
(None, TypeError),
({"total": True}, TypeError),
({"total": "asdf"}, TypeError),
({"total": None}, TypeError),
(-1, ValueError),
],
)
def test_throw_exception_for_bad_tag(input_value, expected_error_type):
bee = BeeDebug(MOCK_SERVER_URL)
with pytest.raises(expected_error_type):
bee.retrieve_extended_tag(input_value)


@pytest.mark.parametrize("input_value, expected_error_type", request_options_assertions)
def test_get_stake(input_value, expected_error_type):
with pytest.raises(expected_error_type):
bee = BeeDebug(MOCK_SERVER_URL, input_value)
bee.get_stake(input_value)


@pytest.mark.parametrize("input_value, expected_error_type", request_options_assertions)
def test_deposit_stake(input_value, expected_error_type):
with pytest.raises(expected_error_type):
bee = BeeDebug(MOCK_SERVER_URL, input_value)
bee.deposit_stake("100000000000000000", None, input_value)


@pytest.mark.parametrize("input_value, expected_error_type", transaction_options_assertions)
def test_deposit_stake_transaction_assertions(input_value, expected_error_type):
bee = BeeDebug(MOCK_SERVER_URL)
with pytest.raises(expected_error_type):
bee.deposit_stake("100000000000000000", None, input_value)


@pytest.mark.parametrize("input_value, expected_error_type", request_options_assertions)
def test_create_postage_batch(input_value, expected_error_type):
with pytest.raises(expected_error_type):
bee = BeeDebug(MOCK_SERVER_URL, input_value)
bee.create_postage_batch("10", 17, input_value)


@pytest.mark.parametrize("input_value, expected_error_type", transaction_options_assertions)
def test_create_postage_batch_transaction_assertions(input_value, expected_error_type):
bee = BeeDebug(MOCK_SERVER_URL)
with pytest.raises(expected_error_type):
bee.create_postage_batch("10", 17, None, input_value)


def test_no_headers_if_no_create_postage_batch_is_set(requests_mock):
url = "http://localhost:12345/stamps/10/17"

requests_mock.post(url, json=BATCH_ID)

bee = BeeDebug(MOCK_SERVER_URL)

assert bee.create_postage_batch("10", 17, {"waitForUsable": False}) == BATCH_ID


def test_no_headers_if_create_postage_batch_is_set(requests_mock):
url = "http://localhost:12345/stamps/10/17"

requests_mock.post(url, headers={"gas-price": "100000000000"}, json=BATCH_ID)

bee = BeeDebug(MOCK_SERVER_URL)

assert bee.create_postage_batch("10", 17, {"waitForUsable": False, "gasPrice": "100"}) == BATCH_ID

0 comments on commit e1a1e2a

Please sign in to comment.