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/bee.py
  - src/bee_py/utils/collection.py
  - src/bee_py/utils/type.py
  - tests/conftest.py
  - tests/unit/test_bee.py
  • Loading branch information
Aviksaikat committed Dec 12, 2023
1 parent a602c2d commit c7b57b0
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 22 deletions.
14 changes: 10 additions & 4 deletions src/bee_py/bee.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,7 @@ def upload_data(
assert_upload_options(options)
return bytes_api.upload(request_options, data, postage_batch_id, options)

def download_data(
self, reference: Union[ReferenceOrENS, str], options: Optional[BeeRequestOptions] = None
) -> bytes:
def download_data(self, reference: Union[ReferenceOrENS, str], options: Optional[BeeRequestOptions] = None) -> Data:
"""
Download data as a byte array.
Expand Down Expand Up @@ -431,6 +429,8 @@ def download_file(
Returns:
FileData
"""
if options:
assert_request_options(options)

assert_reference_or_ens(reference)
reference = make_reference_or_ens(reference, ReferenceType.MANIFEST)
Expand Down Expand Up @@ -477,7 +477,7 @@ def upload_files(
Args:
postage_batch_id (str): The Postage Batch ID to use for uploading the data.
file_list (FileList | File[]): A FileList or a list of File objects to be uploaded.
file_list (os.PathLike | str): A FileList or a list of File objects to be uploaded.
options (CollectionUploadOptions): Additional options for the upload,
such as tag, encryption, pinning, and request options.
request_options (BeeRequestOptions): Options that affect the request behavior.
Expand All @@ -487,6 +487,12 @@ def upload_files(
Returns:
UploadResultWithCid
"""

assert_batch_id(postage_batch_id)

if request_options:
assert_request_options(request_options)

if options:
assert_collection_upload_options(options)

Expand Down
7 changes: 3 additions & 4 deletions src/bee_py/utils/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ def assert_collection(data: Any):
raise ValueError(msg)


def _make_filepath(file: Union[os.PathLike, str]) -> str:
def _make_filepath(file: Union[os.PathLike, str, Path]) -> str:
"""
Extracts the filename from the provided file path.
Args:
file (Union[os.PathLike, str]): The file path.
file (Union[os.PathLike, str, Path]): The file path.
Returns:
str: The extracted filename.
"""
if isinstance(file, str):
file = Path(file)

if file.is_file():
return file.name

Expand All @@ -57,7 +56,7 @@ def make_collection_from_file_list(file_list: list[Union[os.PathLike, str]]) ->
file_data = open(file_path, "rb").read()

collection.append(
CollectionEntry.model_validateect(
CollectionEntry.model_validate(
{
"path": filename,
"data": bytearray(file_data),
Expand Down
8 changes: 6 additions & 2 deletions src/bee_py/utils/type.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import IO, Any, Union

from ens.utils import is_valid_ens_name
from ens.utils import is_valid_ens_name # type: ignore
from swarm_cid import ReferenceType, decode_cid, encode_reference

from bee_py.types.type import (
Expand All @@ -11,6 +11,7 @@
REFERENCE_HEX_LENGTH,
AllTagsOptions,
BeeRequestOptions,
CollectionUploadOptions,
FeedType,
FileUploadOptions,
ReferenceOrENS,
Expand Down Expand Up @@ -239,7 +240,7 @@ def assert_file_upload_options(value: Any, name: str = "FileUploadOptions") -> N

assert_upload_options(value, name)
if isinstance(value, dict):
if value.get("size", None) and isinstance(value.get("size"), int):
if value.get("size", None) and isinstance(value.get("size"), bool):
msg = "size property in FileUploadOptions has to be number or None!"
raise TypeError(msg)

Expand Down Expand Up @@ -276,6 +277,9 @@ def assert_collection_upload_options(value: Any, name: str = "CollectionUploadOp

assert_upload_options(value, name)

if isinstance(value, dict):
value = CollectionUploadOptions.model_validate(value)

options = value

if options.index_document:
Expand Down
24 changes: 18 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import os
import random
from pathlib import Path
from unittest.mock import MagicMock, patch
from typing import Union
from unittest.mock import MagicMock

import pytest

Expand Down Expand Up @@ -77,7 +78,7 @@ def bee_peer_debug_url():


@pytest.fixture
def get_data_folder() -> str:
def get_data_folder() -> Path:
return DATA_FOLDER


Expand Down Expand Up @@ -122,7 +123,7 @@ def bee_debug_peer_ky_options(bee_peer_debug_url) -> dict:


@pytest.fixture
def read_local_bee_stamp() -> str:
def read_local_bee_stamp() -> Union[str, bool]:
with open(BEE_DATA_FILE) as f:
stamp = json.loads(f.read())
if stamp["BEE_POSTAGE"]:
Expand All @@ -131,7 +132,7 @@ def read_local_bee_stamp() -> str:


@pytest.fixture
def read_local_bee_peer_stamp() -> str:
def read_local_bee_peer_stamp() -> Union[str, bool]:
with open(BEE_DATA_FILE) as f:
stamp = json.loads(f.read())
if stamp["BEE_PEER_POSTAGE"]:
Expand Down Expand Up @@ -246,7 +247,7 @@ def invalid_reference() -> str:


@pytest.fixture
def test_collection() -> dict:
def test_collection() -> list[dict]:
return [
{
"path": "0",
Expand All @@ -271,7 +272,7 @@ def _method(soc_hash):
return response
except ValueError as e:
try:
response.status_code == 400 # noqa: B015
response.code == 400 # noqa: B015
return response
except: # noqa: E722
raise e # noqa: B904
Expand Down Expand Up @@ -337,3 +338,14 @@ def test_batch_id() -> str:
@pytest.fixture
def test_chunk_hash_str() -> str:
return "ca6357a08e317d15ec560fef34e4c45f8f19f01c372aa70f1da72bfa7f1a4338"


@pytest.fixture(scope="session")
def create_blank_temp_files(tmp_path_factory):
# Create a temporary directory
temp_dir = tmp_path_factory.mktemp("temp")

# Create 2-3 temporary files in the temporary directory
temp_files = [temp_dir / f"temp_file_{i}.txt" for i in range(2, 5)]

return temp_files
66 changes: 60 additions & 6 deletions tests/unit/test_bee.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from bee_py.bee import Bee
from bee_py.feed.topic import make_topic_from_string
from bee_py.feed.type import FeedType

# from bee_py.feed.type import FeedType
from bee_py.types.type import (
CHUNK_SIZE,
SPAN_SIZE,
Expand Down Expand Up @@ -59,7 +60,13 @@
({"retry": -1}, BeeArgumentError),
]

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


reference_or_ens_test_data = [
Expand All @@ -81,7 +88,7 @@
]


upload_options_assertions = [
upload_options_assertions: list[tuple] = [
(1, TypeError),
(True, TypeError),
([], TypeError),
Expand All @@ -102,7 +109,7 @@
]


file_data_assertions = [
file_data_assertions: list[tuple] = [
(1, TypeError),
(True, TypeError),
(None, TypeError),
Expand All @@ -113,12 +120,12 @@
]


file_upload_option_assertions = [
file_upload_option_assertions: list[tuple] = [
({"content_type": True}, pydantic.ValidationError),
({"content_type": 1}, pydantic.ValidationError),
({"content_type": {}}, pydantic.ValidationError),
({"content_type": []}, pydantic.ValidationError),
({"size": "plur"}, TypeError),
({"size": "plur"}, pydantic.ValidationError),
({"size": True}, TypeError),
({"size": {}}, pydantic.ValidationError),
({"size": []}, pydantic.ValidationError),
Expand Down Expand Up @@ -302,6 +309,7 @@ def test_download_readable_data_request_options_assertion(input_value, expected_
bee.download_readable_data(test_chunk_hash, input_value)


# * upload_file
@pytest.mark.parametrize("input_value, expected_error_type", batch_id_assertion_data)
def test_upload_file_batch_id_assertion(input_value, expected_error_type):
bee = Bee(MOCK_SERVER_URL)
Expand Down Expand Up @@ -336,3 +344,49 @@ def test_upload_file_file_upload_options_assertion(input_value, expected_error_t
bee = Bee(MOCK_SERVER_URL)
with pytest.raises(expected_error_type):
bee.upload_file(test_batch_id, "", None, input_value)


# * download_file
@pytest.mark.parametrize("input_value, expected_error_type", reference_or_ens_test_data)
def test_downalod_file_reference_or_ens_assertion(input_value, expected_error_type):
bee = Bee(MOCK_SERVER_URL)
with pytest.raises(expected_error_type):
bee.download_file(input_value)


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


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


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


# * upload_files
@pytest.mark.parametrize("input_value, expected_error_type", batch_id_assertion_data)
def test_upload_files_batch_id_assertion(input_value, expected_error_type, create_fake_file):
bee = Bee(MOCK_SERVER_URL)
with pytest.raises(expected_error_type):
bee.upload_files(input_value, [create_fake_file])


# TODO:
# @pytest.mark.parametrize("input_value, expected_error_type", file_upload_option_assertions)
# def test_upload_files_upload_options_assertion(input_value, expected_error_type, create_fake_file, test_batch_id):
# bee = Bee(MOCK_SERVER_URL)
# with pytest.raises(expected_error_type):
# bee.upload_files(test_batch_id, [create_fake_file], input_value)

0 comments on commit c7b57b0

Please sign in to comment.