-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(UTILITIES): validation, file mixins
- Loading branch information
1 parent
1bd8bb5
commit cae4262
Showing
27 changed files
with
443 additions
and
106 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
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
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,17 @@ | ||
"""Test harness for the Job Spec file test classes.""" | ||
|
||
import os | ||
from pathlib import Path | ||
from unittest import TestCase, mock | ||
|
||
from ...utilities.mixins import json_file | ||
|
||
|
||
class SpecFileTestHarness(TestCase): | ||
"""Test harness for Job Spec file test classes.""" | ||
|
||
def setUp(self) -> None: | ||
self.json_reader = json_file.JSONFileReader() | ||
self.mock_spec_file_location = Path("spec.json") | ||
self.mock_workspace = mock.Mock() | ||
self.fixtures_folder = Path(os.path.dirname(__file__)) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,39 @@ | ||
"""JSONFile mixin classes.""" | ||
|
||
import json | ||
from pathlib import Path | ||
from typing import Any, Union | ||
|
||
|
||
class JSONFileReader: | ||
"""JSONFileReader mixin class.""" | ||
|
||
encoding = "utf-8" | ||
|
||
def load_json_file(self, json_file_location: Union[Path, str]) -> Any: | ||
"""Load a JSON file from the filesystem and return it as a Python object. | ||
:param json_file_location: The path to the source file. | ||
:returns: The loaded JSON object. | ||
""" | ||
|
||
with open(json_file_location, encoding=self.encoding) as fhandle: | ||
json_file_content = json.load(fhandle) | ||
return json_file_content | ||
|
||
|
||
class JSONFileWriter: | ||
"""JSONFileWriter mixin class.""" | ||
|
||
encoding = "utf-8" | ||
|
||
def write_json_file( | ||
self, python_object: Any, json_file_location: Union[Path, str] | ||
) -> None: | ||
"""Write a Python object to the filesystem as JSON. | ||
:param python_object: The python object to write to file as JSON. | ||
:param json_file_location: The path to the destination file. | ||
""" | ||
with open(json_file_location, "w", encoding=self.encoding) as file_handle: | ||
json.dump(python_object, file_handle) |
Empty file.
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,74 @@ | ||
"""Test the JSONFile mixin classes.""" | ||
|
||
import json | ||
from unittest import TestCase, mock | ||
|
||
from .. import json_file | ||
|
||
JSON_FILE_MODULE = json_file.__name__ | ||
|
||
|
||
@mock.patch(JSON_FILE_MODULE + '.json') | ||
@mock.patch('builtins.open') | ||
class JSONFileReaderTest(TestCase): | ||
"""Test the JSONFileReader mixin class.""" | ||
|
||
def setUp(self) -> None: | ||
self.instance = json_file.JSONFileReader() | ||
self.mock_object = { | ||
"mock": "object" | ||
} | ||
self.mock_json = json.dumps(self.mock_object) | ||
self.mock_context = mock.Mock() | ||
|
||
def test_load_json_file_return_value( | ||
self, | ||
m_open: mock.Mock, | ||
m_json: mock.Mock, | ||
) -> None: | ||
mock_path = "/mock/path" | ||
self.mock_context.return_value = self.mock_json | ||
m_open.return_value.__enter__.return_value = self.mock_context | ||
m_json.load.return_value = self.mock_object | ||
|
||
result = self.instance.load_json_file(mock_path) | ||
self.assertEqual(result, self.mock_object) | ||
|
||
def test_load_json_file_call( | ||
self, | ||
m_open: mock.Mock, | ||
m_json: mock.Mock, | ||
) -> None: | ||
mock_path = "/mock/path" | ||
m_open.return_value.__enter__.return_value = self.mock_context | ||
|
||
self.instance.load_json_file(mock_path) | ||
|
||
m_json.load.assert_called_once_with(self.mock_context) | ||
|
||
|
||
@mock.patch(JSON_FILE_MODULE + '.json') | ||
@mock.patch('builtins.open') | ||
class JSONFileWriterTest(TestCase): | ||
"""Test the JSONFileReader mixin class.""" | ||
|
||
def setUp(self) -> None: | ||
self.instance = json_file.JSONFileWriter() | ||
self.mock_object = { | ||
"mock": "object" | ||
} | ||
self.mock_json = json.dumps(self.mock_object) | ||
self.mock_context = mock.Mock() | ||
|
||
def test_write_json_file_call( | ||
self, | ||
m_open: mock.Mock, | ||
m_json: mock.Mock, | ||
) -> None: | ||
mock_path = "/mock/path" | ||
self.mock_context.return_value = self.mock_json | ||
m_open.return_value.__enter__.return_value = self.mock_context | ||
|
||
self.instance.write_json_file(self.mock_object, mock_path) | ||
|
||
m_json.dump.assert_called_once_with(self.mock_object, self.mock_context) |
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,51 @@ | ||
"""Test the TextFile mixin classes.""" | ||
|
||
from unittest import TestCase, mock | ||
|
||
from .. import text_file | ||
|
||
TEXT_FILE_MODULE = text_file.__name__ | ||
|
||
|
||
@mock.patch('builtins.open') | ||
class TextFileReaderTest(TestCase): | ||
"""Test the TextFileReader mixin class.""" | ||
|
||
def setUp(self) -> None: | ||
self.instance = text_file.TextFileReader() | ||
self.mock_context = mock.Mock() | ||
self.mock_path = "/mock/path" | ||
self.mock_text = "I am a mock string." | ||
|
||
def test_read_text_file_return_value(self, m_open: mock.Mock) -> None: | ||
m_open.return_value.__enter__.return_value.read.return_value = \ | ||
self.mock_text | ||
result = self.instance.read_text_file(self.mock_path) | ||
self.assertEqual(result, self.mock_text) | ||
|
||
def test_read_text_file_call(self, m_open: mock.Mock) -> None: | ||
self.instance.read_text_file(self.mock_path) | ||
m_open.assert_called_once_with( | ||
self.mock_path, encoding=text_file.TextFileReader.encoding | ||
) | ||
m_open.return_value.__enter__.return_value.read.assert_called_once_with() | ||
|
||
|
||
@mock.patch('builtins.open') | ||
class TextFileWriterTest(TestCase): | ||
"""Test the TextFileWriter class.""" | ||
|
||
def setUp(self) -> None: | ||
self.instance = text_file.TextFileWriter() | ||
self.mock_context = mock.Mock() | ||
self.mock_path = "/mock/path" | ||
self.mock_text = "I am a mock string." | ||
|
||
def test_write_text_file_call(self, m_open: mock.Mock) -> None: | ||
self.instance.write_text_file(self.mock_text, self.mock_path) | ||
m_open.assert_called_once_with( | ||
self.mock_path, "w", encoding=text_file.TextFileReader.encoding | ||
) | ||
m_open.return_value.__enter__.return_value.write.assert_called_once_with( | ||
self.mock_text | ||
) |
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,38 @@ | ||
"""TextFile mixin classes.""" | ||
|
||
from pathlib import Path | ||
from typing import Union | ||
|
||
|
||
class TextFileReader: | ||
"""TextFileReader mixin class.""" | ||
|
||
encoding = "utf-8" | ||
|
||
def read_text_file(self, text_file_location: Union[Path, str]) -> str: | ||
"""Load a text file from the file system and return it as a string. | ||
:param text_file_location: The path to the source file. | ||
:returns: The loaded string object. | ||
""" | ||
with open(text_file_location, encoding=self.encoding) as file_handle: | ||
data = file_handle.read() | ||
return data | ||
|
||
|
||
class TextFileWriter: | ||
"""TextFileWriter mixin class.""" | ||
|
||
encoding = "utf-8" | ||
|
||
def write_text_file( | ||
self, text_file_content: str, text_file_location: Union[Path, str] | ||
) -> None: | ||
"""Load a text file from the file system and return it as a string. | ||
:param text_file_content: The content to write to the file. | ||
:param text_file_location: The path to the source file. | ||
:returns: The loaded string object. | ||
""" | ||
with open(text_file_location, "w", encoding=self.encoding) as file_handle: | ||
file_handle.write(text_file_content) |
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
Oops, something went wrong.