Skip to content

Commit

Permalink
change name
Browse files Browse the repository at this point in the history
  • Loading branch information
pritipsingh committed Feb 4, 2025
1 parent 55a6eae commit 6c7f05c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
28 changes: 14 additions & 14 deletions libs/agno/agno/document/reader/json_reader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from io import BytesIO
from pathlib import Path
from typing import List, Union
from typing import IO, Any, List, Union

from agno.document.base import Document
from agno.document.reader.base import Reader
Expand All @@ -13,20 +13,20 @@ class JSONReader(Reader):

chunk: bool = False

def read(self, file: Union[Path, BytesIO]) -> List[Document]:
def read(self, path: Union[Path, IO[Any]]) -> List[Document]:
try:
if isinstance(file, Path):
if not file.exists():
raise FileNotFoundError(f"Could not find file: {file}")
logger.info(f"Reading: {file}")
json_name = file.name.split(".")[0]
json_contents = json.loads(file.read_text("utf-8"))

elif isinstance(file, BytesIO):
logger.info("Reading file from BytesIO stream")
json_name = file.name.split(".")[0]
file.seek(0)
json_contents = json.load(file)
if isinstance(path, Path):
if not path.exists():
raise FileNotFoundError(f"Could not find file: {path}")
logger.info(f"Reading: {path}")
json_name = path.name.split(".")[0]
json_contents = json.loads(path.read_text("utf-8"))

elif isinstance(path, BytesIO):
logger.info(f"Reading uploaded file: {path.name}")
json_name = path.name.split(".")[0]
path.seek(0)
json_contents = json.load(path)

else:
raise ValueError("Unsupported file type. Must be Path or BytesIO.")
Expand Down
36 changes: 36 additions & 0 deletions libs/agno/tests/unit/playground/upload_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import json
import pytest
from io import BytesIO
from agno.document.reader.json_reader import JSONReader
from agno.document import Document

@pytest.fixture
def mock_json_file():
# Create a mock JSON file with content
json_data = '[{"name": "Document 1", "content": "Some content here"}]'
mock_file = BytesIO(json_data.encode('utf-8')) # Mock file with JSON content
mock_file.name = 'test_upload.json' # Name the file
mock_file.content_type = 'application/json' # Set the content type to JSON
return mock_file

def test_upload_json_file(mock_json_file):
"""Test uploading a JSON file."""
# Create a mock reader that simulates reading the file
reader = JSONReader()

# Read the contents from the mock JSON file
file_content = reader.read(mock_json_file)

# Check if the file content has the expected number of documents
assert len(file_content) == 1 # Only one document in the mock JSON

# Check if the first document is an instance of Document
assert isinstance(file_content[0], Document)

# Check the document's name
assert file_content[0].name == "test_upload" # Extracted name from the file name

# Check if the document content matches the expected structure
expected_content = {"name": "Document 1", "content": "Some content here"}
assert json.loads(file_content[0].content) == expected_content

0 comments on commit 6c7f05c

Please sign in to comment.