Skip to content

Commit

Permalink
feat: add tests for agent code fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
futrime committed Feb 6, 2025
1 parent 73540f7 commit e731c1d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
1 change: 0 additions & 1 deletion agent_code_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def __init__(self, session: aiohttp.ClientSession):
Args:
session: The aiohttp client session initialized with the base URL of the API
"""

self._session = session

async def clean(self) -> None:
Expand Down
3 changes: 0 additions & 3 deletions base_agent_code_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class BaseAgentCodeFetcher(ABC):
@abstractmethod
async def clean(self) -> None:
"""Cleans up fetched resources."""
raise NotImplementedError

@abstractmethod
async def fetch(self, code_id: str) -> Path:
Expand All @@ -26,7 +25,6 @@ async def fetch(self, code_id: str) -> Path:
Returns:
The path to the tarball file where the code should be saved
"""
raise NotImplementedError

@abstractmethod
async def list(self) -> Dict[str, Path]:
Expand All @@ -35,4 +33,3 @@ async def list(self) -> Dict[str, Path]:
Returns:
A dictionary mapping code IDs to the paths of their corresponding tarball files
"""
raise NotImplementedError
89 changes: 89 additions & 0 deletions tests/test_agent_code_fetcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import shutil
from pathlib import Path
from unittest import IsolatedAsyncioTestCase

import aiohttp

import agent_code_fetcher

CODE_ID = "a09f660a-e0e6-41ac-b721-f8ece8e71f33"
HTTP_BASE_URL = "https://api.dev.saiblo.net"


class TestAgentCodeFetcher(IsolatedAsyncioTestCase):
_session: aiohttp.ClientSession

async def asyncSetUp(self) -> None:
shutil.rmtree(
Path("data"),
ignore_errors=True,
)

self._session = aiohttp.ClientSession(HTTP_BASE_URL)

def tearDown(self) -> None:
shutil.rmtree(
Path("data"),
ignore_errors=True,
)

async def test_clean_no_dir(self):
# Arrange.
fetcher = agent_code_fetcher.AgentCodeFetcher(self._session)

# Act.
await fetcher.clean()

# Assert.
self.assertTrue(Path("data/agent_code").is_dir())

async def test_clean_dir_exists(self):
# Arrange.
Path("data/agent_code").mkdir(parents=True, exist_ok=True)
fetcher = agent_code_fetcher.AgentCodeFetcher(self._session)

# Act.
await fetcher.clean()

# Assert.
self.assertTrue(
Path("data/agent_code").is_dir(),
)

async def test_fetch_cached(self):
# Arrange.
Path("data/agent_code").mkdir(parents=True, exist_ok=True)
path = Path("data/agent_code") / f"{CODE_ID}.tar"
path.touch()
fetcher = agent_code_fetcher.AgentCodeFetcher(self._session)

# Act.
result = await fetcher.fetch(CODE_ID)

# Assert.
self.assertEqual(path.absolute(), result)

async def test_fetch_not_cached(self):
# Arrange.
path = Path("data/agent_code") / f"{CODE_ID}.tar"
fetcher = agent_code_fetcher.AgentCodeFetcher(self._session)

# Act.
result = await fetcher.fetch(CODE_ID)

# Assert.
self.assertEqual(path.absolute(), result)
self.assertTrue(path.is_file())

async def test_list(self):
# Arrange.
Path("data/agent_code").mkdir(parents=True, exist_ok=True)
path = Path("data/agent_code") / f"{CODE_ID}.tar"
path.touch()
fetcher = agent_code_fetcher.AgentCodeFetcher(self._session)

# Act.
result = await fetcher.list()

# Assert.
self.assertEqual({CODE_ID: path.absolute()}, result)
6 changes: 6 additions & 0 deletions tests/test_path_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def test_get_agent_code_base_dir_path(self):
path_manager.get_agent_code_base_dir_path(),
)

def test_get_judge_replay_base_dir_path(self):
self.assertEqual(
Path.cwd() / "data/judge_replays",
path_manager.get_judge_replay_base_dir_path(),
)

def test_get_judge_result_base_dir_path(self):
self.assertEqual(
Path.cwd() / "data/judge_results",
Expand Down

0 comments on commit e731c1d

Please sign in to comment.