-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some basic unit tests for functions in util. Need to fill in some additional documentation/context for tests.
- Loading branch information
Showing
1 changed file
with
96 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import logging | ||
import pytest | ||
import yaml | ||
import textwrap | ||
from datetime import datetime | ||
from synorchestrator import util | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def test_heredoc(): | ||
test_string = util.heredoc( | ||
''' | ||
Mock string: | ||
{value} | ||
''', {'value': 'mock_val'} | ||
) | ||
|
||
mock_string = "Mock string:\nmock_val\n" | ||
assert(test_string == textwrap.dedent(mock_string)) | ||
|
||
|
||
def test_get_yaml(tmpdir): | ||
mock_string = """ | ||
section: | ||
key: {} | ||
""" | ||
mock_file = tmpdir.join('mock.yaml') | ||
mock_file.write(textwrap.dedent(mock_string)) | ||
|
||
test_object = util.get_yaml(str(mock_file)) | ||
mock_object = {'section': {'key': {}}} | ||
|
||
assert(test_object == mock_object) | ||
|
||
|
||
def test_save_yaml(tmpdir): | ||
mock_object = {'section': {'key': {}}} | ||
|
||
mock_file = tmpdir.join('mock.yaml') | ||
|
||
util.save_yaml(str(mock_file), mock_object) | ||
|
||
mock_string = "section:\n key: {}\n" | ||
|
||
assert(mock_file.read() == mock_string) | ||
|
||
|
||
def test_get_json(tmpdir): | ||
mock_string = """ | ||
{ | ||
"section": { | ||
"key": {} | ||
} | ||
} | ||
""" | ||
mock_file = tmpdir.join('mock.json') | ||
mock_file.write(textwrap.dedent(mock_string)) | ||
|
||
test_object = util.get_json(str(mock_file)) | ||
mock_object = {'section': {'key': {}}} | ||
|
||
assert(test_object == mock_object) | ||
|
||
|
||
def test_save_json(tmpdir): | ||
mock_object = {'section': {'key': {}}} | ||
|
||
mock_file = tmpdir.join('mock.json') | ||
|
||
util.save_json(str(mock_file), mock_object) | ||
|
||
mock_string = """{ | ||
"section": { | ||
"key": {} | ||
} | ||
}""" | ||
|
||
assert(mock_file.read() == textwrap.dedent(mock_string)) | ||
|
||
|
||
def test_ctime2datetime(): | ||
mock_string = 'Sun Jan 01 00:00:00 2000' | ||
|
||
test_datetime = util.ctime2datetime(mock_string) | ||
|
||
assert(test_datetime == datetime(2000, 1, 1, 0, 0)) | ||
|
||
|
||
def test_convert_timedelta(): | ||
mock_duration = datetime(2000, 1, 1, 1, 1) - datetime(2000, 1, 1, 0, 0) | ||
|
||
test_string = util.convert_timedelta(mock_duration) | ||
|
||
assert(test_string == '1h:1m:0s') |