Skip to content

Commit

Permalink
Use static foobar test file instead of ephemeral test file
Browse files Browse the repository at this point in the history
  • Loading branch information
collindutter committed Apr 16, 2024
1 parent dc67155 commit a4a8ad9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
6 changes: 4 additions & 2 deletions griptape/utils/file_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import griptape.utils as utils
from concurrent import futures
from typing import Optional


def load_file(path: str) -> bytes:
Expand All @@ -15,7 +16,7 @@ def load_file(path: str) -> bytes:
return f.read()


def load_files(paths: list[str]) -> dict[str, bytes]:
def load_files(paths: list[str], futures_executor: Optional[futures.ThreadPoolExecutor] = None) -> dict[str, bytes]:
"""Load multiple files concurrently and return a dictionary of their content.
Args:
Expand All @@ -25,7 +26,8 @@ def load_files(paths: list[str]) -> dict[str, bytes]:
A dictionary where the keys are a hash of the path and the values are the content of the files.
"""

futures_executor = futures.ThreadPoolExecutor()
if futures_executor is None:
futures_executor = futures.ThreadPoolExecutor()

return utils.execute_futures_dict(
{utils.str_to_hash(str(path)): futures_executor.submit(load_file, path) for path in paths}
Expand Down
26 changes: 26 additions & 0 deletions tests/resources/foobar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar

foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar

foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar

foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar

foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar

foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar

foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar

foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar

foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar

foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar

foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar

foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar

foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar

15 changes: 8 additions & 7 deletions tests/unit/utils/test_file_utils.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from griptape.loaders import TextLoader
from griptape import utils
from concurrent import futures
from tests.mocks.mock_embedding_driver import MockEmbeddingDriver

MAX_TOKENS = 50


class TestFileUtils:
def test_load_file(self):
file = utils.load_file("tests/resources/test.txt")
file = utils.load_file("tests/resources/foobar.txt")

assert file.decode("utf-8").startswith("foobar foobar foobar")
assert len(file.decode("utf-8")) == 4563

def test_load_files(self):
sources = ["tests/resources/test.txt", "tests/resources/test.txt", "tests/resources/small.png"]
files = utils.load_files(sources)
sources = ["tests/resources/foobar.txt", "tests/resources/foobar.txt", "tests/resources/small.png"]
files = utils.load_files(sources, futures_executor=futures.ThreadPoolExecutor(max_workers=1))
assert len(files) == 2

test_file = files[utils.str_to_hash("tests/resources/test.txt")]
test_file = files[utils.str_to_hash("tests/resources/foobar.txt")]
assert len(test_file) == 4563
assert test_file.decode("utf-8").startswith("foobar foobar foobar")

Expand All @@ -26,20 +27,20 @@ def test_load_files(self):
assert small_file[:8] == b"\x89PNG\r\n\x1a\n"

def test_load_file_with_loader(self):
file = utils.load_file("tests/resources/test.txt")
file = utils.load_file("tests/resources/foobar.txt")
artifacts = TextLoader(max_tokens=MAX_TOKENS, embedding_driver=MockEmbeddingDriver()).load(file)

assert len(artifacts) == 39
assert isinstance(artifacts, list)
assert artifacts[0].value.startswith("foobar foobar foobar")

def test_load_files_with_loader(self):
sources = ["tests/resources/test.txt"]
sources = ["tests/resources/foobar.txt"]
files = utils.load_files(sources)
loader = TextLoader(max_tokens=MAX_TOKENS, embedding_driver=MockEmbeddingDriver())
collection = loader.load_collection(list(files.values()))

test_file_artifacts = collection[loader.to_key(files[utils.str_to_hash("tests/resources/test.txt")])]
test_file_artifacts = collection[loader.to_key(files[utils.str_to_hash("tests/resources/foobar.txt")])]
assert len(test_file_artifacts) == 39
assert isinstance(test_file_artifacts, list)
assert test_file_artifacts[0].value.startswith("foobar foobar foobar")

0 comments on commit a4a8ad9

Please sign in to comment.