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:
  - pdm.lock
  - pyproject.toml
  - src/bee_py/bee.py
  - src/bee_py/utils/http.py
  - src/bee_py/utils/type.py
  - tests/conftest.py
  - tests/unit/test_bee.py
  • Loading branch information
Aviksaikat committed Dec 11, 2023
1 parent d38a55e commit 8ead00c
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 28 deletions.
16 changes: 8 additions & 8 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies = [
"websocket-client>=1.6.4",
"swarm-cid-py>=0.1.2",
"eth-pydantic-types>=0.1.0a3",
"web3>=6.12.0",
]
requires-python = ">=3.9"
readme = "README.md"
Expand Down
7 changes: 6 additions & 1 deletion src/bee_py/bee.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ def __get_request_options_for_call(
dict: The merged request options.
"""
if options:
if isinstance(options, BeeRequestOptions):
print("---------***", type(options))
print(type(self.request_options))
options = options.model_dump()
self.request_options = self.request_options.model_dump()
return {**self.request_options, **options}
else:
return self.request_options
Expand Down Expand Up @@ -275,7 +280,7 @@ def download_data(
"""

assert_request_options(options)
assert_reference(reference)
assert_reference_or_ens(reference)

return bytes_api.download(self.__get_request_options_for_call(options), reference)

Expand Down
2 changes: 1 addition & 1 deletion src/bee_py/utils/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def http(options: Union[BeeRequestOptions, dict], config: dict) -> requests.Resp
**options,
}
request_config = maybe_run_on_request_hook(options, request_config)
if "http" not in request_config:
if "http" not in request_config["url"]:
msg = f"Invalid URL: {request_config['url']}"
raise TypeError(msg)
response = requests.request(**request_config)
Expand Down
21 changes: 9 additions & 12 deletions src/bee_py/utils/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def assert_reference_or_ens(value: Any) -> None:

if is_hex_string(value):
assert_reference(value)
return

if not is_valid_ens_name(value):
msg = "ReferenceOrEns is not valid Reference, but also not valid ENS domain."
Expand Down Expand Up @@ -167,26 +166,24 @@ def assert_request_options(options: Any, name: str = "RequestOptions") -> None:
msg = f"Options must be an instance of BeeRequestOptions or dictionary. Got: {type(options)}"
raise TypeError(msg)

if isinstance(options, BeeRequestOptions):
options = options.model_dump()
if isinstance(options, dict):
options = BeeRequestOptions.model_validate(options)

if options.get("retry", None):
if options.retry:
if (
not isinstance(
options.get(
"retry",
),
options.retry,
int,
)
or options.get("retry", None) < 0
or options.retry < 0
):
msg = f"{name}.retry has to be a non-negative integer!"
raise BeeArgumentError(msg, options.get("retry"))
raise BeeArgumentError(msg, options.retry)

if options.get("timeout", None):
if not isinstance(options.get("timeout"), int) or options.get("timeout", None) < 0:
if options.timeout:
if not isinstance(options.timeout, int) or options.timeout < 0:
msg = f"{name}.timeout has to be a non-negative integer!"
raise BeeArgumentError(msg, options.get("timeout"))
raise BeeArgumentError(msg, options.timeout)


def assert_upload_options(value: Any, name: str = "UploadOptions") -> None:
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,8 @@ def test_chunk_encrypted_reference_cid() -> str:
@pytest.fixture
def test_batch_id() -> str:
return "ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338"


@pytest.fixture
def test_chunk_hash_str() -> str:
return "ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338"
45 changes: 39 additions & 6 deletions tests/unit/test_bee.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
("0x634fb5a872396d9693e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd", TypeError),
]

request_options_assertion_data = [
request_options_assertions = [
(1, TypeError),
(True, TypeError),
([], TypeError),
Expand All @@ -52,16 +52,35 @@
({"timeout": {}}, pydantic.ValidationError),
({"timeout": []}, pydantic.ValidationError),
({"timeout": -1}, BeeArgumentError),
({"retry": "plur"}, BeeArgumentError),
({"retry": "plur"}, pydantic.ValidationError),
({"retry": True}, TypeError),
({"retry": {}}, TypeError),
({"retry": []}, TypeError),
({"retry": {}}, pydantic.ValidationError),
({"retry": []}, pydantic.ValidationError),
({"retry": -1}, BeeArgumentError),
]

data_assertions = [(1, TypeError), (True, TypeError), (None, TypeError), (lambda: {}, TypeError), ({}, TypeError)]


reference_or_ens_test_data = [
(1, TypeError),
(True, TypeError),
({}, TypeError),
(None, TypeError),
([], TypeError),
# Not an valid hexstring (ZZZ)
("ZZZfb5a872396d9693e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd", TypeError),
# Prefixed hexstring is not accepted
("0x634fb5a872396d9693e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd", TypeError),
# Length mismatch
("4fb5a872396d9693e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd", TypeError),
# ENS with invalid characters
("", TypeError),
("some space.eth", TypeError),
("http://example.eth", TypeError),
]


upload_options_assertions = [
(1, TypeError),
(True, TypeError),
Expand Down Expand Up @@ -147,7 +166,7 @@ def test_cid_encrypted_references(

assert reference.tag_uid == int(test_tag_id)

assert reference.cid() == test_chunk_encrypted_reference_cid
assert str(reference.cid()) == test_chunk_encrypted_reference_cid


@pytest.mark.parametrize("input_value, expected_error_type", batch_id_assertion_data)
Expand All @@ -157,7 +176,7 @@ def test_upload_data_batch_id_assertion(input_value, expected_error_type):
bee.upload_data(input_value, "")


@pytest.mark.parametrize("input_value, expected_error_type", request_options_assertion_data)
@pytest.mark.parametrize("input_value, expected_error_type", request_options_assertions)
def test_upload_data_request_options_assertions_with_test_data(input_value, expected_error_type, test_batch_id):
with pytest.raises(expected_error_type):
bee = Bee(MOCK_SERVER_URL, input_value)
Expand All @@ -176,3 +195,17 @@ def test_upload_options_assertions(input_value, expected_error_type, test_batch_
bee = Bee(MOCK_SERVER_URL)
with pytest.raises(expected_error_type):
bee.upload_data(test_batch_id, "", input_value)


@pytest.mark.parametrize("input_value, expected_error_type", reference_or_ens_test_data)
def test_download_data_reference_or_ens_assertions(input_value, expected_error_type):
bee = Bee(MOCK_SERVER_URL)
with pytest.raises(expected_error_type):
bee.download_data(input_value)


@pytest.mark.parametrize("input_value, expected_error_type", request_options_assertions)
def test_download_data_request_options_assertions(input_value, expected_error_type, test_chunk_hash_str):
bee = Bee(MOCK_SERVER_URL)
with pytest.raises(expected_error_type):
bee.download_data(test_chunk_hash_str, input_value)

0 comments on commit 8ead00c

Please sign in to comment.