generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding recordlinker.utils.path for project_root and read_json
- Loading branch information
1 parent
e3e8d6b
commit 5fcfa5c
Showing
8 changed files
with
67 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +0,0 @@ | ||
import json | ||
import pathlib | ||
|
||
|
||
def project_root() -> pathlib.Path: | ||
""" | ||
Returns the path to the project root directory. | ||
""" | ||
root = pathlib.Path(__file__).resolve() | ||
while root.name != "recordlinker": | ||
if root.parent == root: | ||
raise FileNotFoundError("recordlinker project root not found.") | ||
root = root.parent | ||
return root | ||
|
||
|
||
def read_json(path: str) -> dict: | ||
""" | ||
Loads a JSON file. | ||
""" | ||
if not pathlib.Path(path).is_absolute(): | ||
# if path is relative, append to the project root | ||
path = str(pathlib.Path(project_root(), path)) | ||
with open(path, "r") as fobj: | ||
return json.load(fobj) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import json | ||
import pathlib | ||
|
||
|
||
def project_root() -> pathlib.Path: | ||
""" | ||
Returns the path to the project root directory. | ||
""" | ||
root = pathlib.Path(__file__).resolve() | ||
while root.name != "recordlinker": | ||
if root.parent == root: | ||
raise FileNotFoundError("recordlinker project root not found.") | ||
root = root.parent | ||
return root | ||
|
||
|
||
def read_json(path: str) -> dict: | ||
""" | ||
Loads a JSON file. | ||
""" | ||
if not pathlib.Path(path).is_absolute(): | ||
# if path is relative, append to the project root | ||
path = str(pathlib.Path(project_root(), path)) | ||
with open(path, "r") as fobj: | ||
return json.load(fobj) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pathlib | ||
import tempfile | ||
import unittest.mock | ||
|
||
import pytest | ||
|
||
from recordlinker.utils import path as utils | ||
|
||
|
||
def test_project_root(): | ||
root = utils.project_root() | ||
assert root.name == "recordlinker" | ||
|
||
|
||
def test_project_root_not_found(): | ||
with unittest.mock.patch("pathlib.Path.resolve") as mock_resolve: | ||
mock_resolve.return_value = pathlib.Path("/") | ||
with pytest.raises(FileNotFoundError): | ||
utils.project_root() | ||
|
||
|
||
def test_read_json_relative(): | ||
tmp = utils.project_root() / "test.json" | ||
with open(tmp, "w") as fobj: | ||
fobj.write('{"key": "value"}') | ||
assert utils.read_json("test.json") == {"key": "value"} | ||
tmp.unlink() | ||
|
||
|
||
def test_read_json_absolute(): | ||
tmp = tempfile.NamedTemporaryFile(suffix=".json") | ||
with open(tmp.name, "w") as fobj: | ||
fobj.write('{"key": "value"}') | ||
assert utils.read_json(tmp.name) == {"key": "value"} | ||
tmp.close() |