Skip to content

Commit

Permalink
Fix 372 write file path (#374)
Browse files Browse the repository at this point in the history
* fix: check for directory prefix

* test: add test for directory prefix

* refactor: rename mock variable

* refactor: reformat code

* fix: add optional directory arg
  • Loading branch information
Louise-X10 authored Jun 20, 2023
1 parent 506b07f commit ddfff47
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
10 changes: 9 additions & 1 deletion src/aind_data_schema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import re
import urllib.parse
from enum import EnumMeta
from pathlib import Path
from typing import Optional

from pydantic import BaseModel, Extra, Field
Expand Down Expand Up @@ -112,17 +113,24 @@ def default_filename(cls):
name = cls._get_direct_subclass(cls).__name__
return re.sub(r"(?<!^)(?=[A-Z])", "_", name).lower() + ".json"

def write_standard_file(self, prefix=None):
def write_standard_file(self, output_directory: Optional[Path] = None, prefix=None):
"""
Writes schema to standard json file
Parameters
----------
output_directory:
optional Path object for output directory
prefix:
optional str for intended filepath with extra naming convention
"""
if prefix is None:
filename = self.default_filename()
else:
filename = str(prefix) + "_" + self.default_filename()

if output_directory is not None:
filename = output_directory / filename

with open(filename, "w") as f:
f.write(self.json(indent=3))
54 changes: 44 additions & 10 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,65 @@ def test_default_filename(self, mock_log):
)

@patch("builtins.open", new_callable=unittest.mock.mock_open())
def test_write_standard_file_no_prefix(self, mocked_file):
def test_write_standard_file_no_prefix(self, mock_open):
"""tests that standard file is named and written as expected with no prefix"""
p = Procedures.construct()
default_filename = p.default_filename()
json_contents = p.json(indent=3)
p.write_standard_file()

mocked_file.assert_called_once_with(default_filename, "w")
mocked_file.return_value.__enter__().write.assert_called_once_with(json_contents)
mock_open.assert_called_once_with(default_filename, "w")
mock_open.return_value.__enter__().write.assert_called_once_with(json_contents)

@patch("builtins.open", new_callable=unittest.mock.mock_open())
def test_write_standard_file_with_prefix(self, mocked_file):
"""tests that standard file is named and written as expected with prefix"""
def test_write_standard_file_with_prefix(self, mock_open):
"""tests that standard file is named and written as expected with filename prefix"""
p = Procedures.construct()
default_filename = p.default_filename()
json_contents = p.json(indent=3)
new_path = Path("foo") / "bar" / "aibs"
p.write_standard_file(new_path)
prefix = "aibs"
p.write_standard_file(prefix=prefix)

# It's expected that the file will be written to something like
# aibs_procedure.json
expected_file_path = str(prefix) + "_" + default_filename

mock_open.assert_called_once_with(expected_file_path, "w")
mock_open.return_value.__enter__().write.assert_called_once_with(json_contents)

@patch("builtins.open", new_callable=unittest.mock.mock_open())
def test_write_standard_file_with_output_directory(self, mock_open):
"""tests that standard file is named and written as expected with designated output directory"""
p = Procedures.construct()
default_filename = p.default_filename()
json_contents = p.json(indent=3)
new_path = Path("foo") / "bar"
p.write_standard_file(output_directory=new_path)

# It's expected that the file will be written to something like
# foo/bar/procedure.json
expected_file_path = new_path / default_filename

mock_open.assert_called_once_with(expected_file_path, "w")
mock_open.return_value.__enter__().write.assert_called_once_with(json_contents)

@patch("builtins.open", new_callable=unittest.mock.mock_open())
def test_write_standard_file_with_output_directory_and_prefix(self, mock_open):
"""tests that standard file is named and written as expected
with designated output directory and filename prefix"""
p = Procedures.construct()
default_filename = p.default_filename()
json_contents = p.json(indent=3)
new_path = Path("foo") / "bar"
prefix = "aibs"
p.write_standard_file(output_directory=new_path, prefix=prefix)

# It's expected that the file will be written to something like
# foo/bar/aibs_procedure.json
expected_file_path = str(new_path) + "_" + default_filename
expected_file_path = new_path / (prefix + "_" + default_filename)

mocked_file.assert_called_once_with(expected_file_path, "w")
mocked_file.return_value.__enter__().write.assert_called_once_with(json_contents)
mock_open.assert_called_once_with(expected_file_path, "w")
mock_open.return_value.__enter__().write.assert_called_once_with(json_contents)


if __name__ == "__main__":
Expand Down

0 comments on commit ddfff47

Please sign in to comment.