Skip to content

Commit

Permalink
Inject get_data for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
gaffney2010 committed Dec 9, 2024
1 parent 5695620 commit 0af5524
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
12 changes: 8 additions & 4 deletions axelrod/load_data_.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pathlib
import pkgutil
from typing import Dict, List, Text, Tuple
from typing import Callable, Dict, List, Optional, Tuple


def axl_filename(path: pathlib.Path) -> pathlib.Path:
Expand All @@ -20,14 +20,18 @@ def axl_filename(path: pathlib.Path) -> pathlib.Path:
return axl_path / path


def load_file(filename: str, directory: str) -> List[List[str]]:
def load_file(
filename: str,
directory: str,
get_data: Callable[[str, str], Optional[bytes]] = pkgutil.get_data,
) -> List[List[str]]:
"""Loads a data file stored in the Axelrod library's data subdirectory,
likely for parameters for a strategy."""

path = str(pathlib.Path(directory) / filename)
data_bytes = pkgutil.get_data(__name__, path)
data_bytes = get_data(__name__, path)
if data_bytes is None:
raise FileNotFoundError(path)
raise FileNotFoundError(f"Some loader issue for path {path}")
data = data_bytes.decode("UTF-8", "replace")

rows = []
Expand Down
9 changes: 8 additions & 1 deletion axelrod/tests/unit/test_load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,12 @@ def test_axl_filename(self):

def test_raise_error_if_file_empty(self):
path = pathlib.Path("not/a/file.py")
with self.assertRaisesRegex(FileNotFoundError, str(path)):
with self.assertRaises(FileNotFoundError):
load_file(path, ".")

def test_raise_error_if_something(self):
dirname = os.path.dirname(__file__)
path = os.path.join(dirname, "../../strategies/titfortat.py")
bad_loader = lambda _, __: None
with self.assertRaises(FileNotFoundError):
load_file(path, ".", bad_loader)

0 comments on commit 0af5524

Please sign in to comment.