Skip to content

Commit 0af5524

Browse files
committed
Inject get_data for testing
1 parent 5695620 commit 0af5524

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

axelrod/load_data_.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pathlib
22
import pkgutil
3-
from typing import Dict, List, Text, Tuple
3+
from typing import Callable, Dict, List, Optional, Tuple
44

55

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

2222

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

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

3337
rows = []

axelrod/tests/unit/test_load_data.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,12 @@ def test_axl_filename(self):
1818

1919
def test_raise_error_if_file_empty(self):
2020
path = pathlib.Path("not/a/file.py")
21-
with self.assertRaisesRegex(FileNotFoundError, str(path)):
21+
with self.assertRaises(FileNotFoundError):
2222
load_file(path, ".")
23+
24+
def test_raise_error_if_something(self):
25+
dirname = os.path.dirname(__file__)
26+
path = os.path.join(dirname, "../../strategies/titfortat.py")
27+
bad_loader = lambda _, __: None
28+
with self.assertRaises(FileNotFoundError):
29+
load_file(path, ".", bad_loader)

0 commit comments

Comments
 (0)