forked from NREL/GEOPHIRES-X
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend GeophiresXTestCase from BaseTestCase
- Loading branch information
1 parent
e6fee57
commit ce5c5a3
Showing
2 changed files
with
58 additions
and
55 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,56 @@ | ||
import os.path | ||
import unittest | ||
|
||
|
||
class BaseTestCase(unittest.TestCase): | ||
maxDiff = None | ||
|
||
def _get_test_file_path(self, test_file_name): | ||
return os.path.join(os.path.abspath(os.path.dirname(__file__)), test_file_name) | ||
|
||
def _get_test_file_content(self, test_file_name): | ||
with open(self._get_test_file_path(test_file_name)) as f: | ||
return f.readlines() | ||
|
||
def _list_test_files_dir(self, test_files_dir: str): | ||
return os.listdir(self._get_test_file_path(test_files_dir)) | ||
|
||
def assertDictAlmostEqual(self, d1, d2, msg=None, places=7): | ||
""" | ||
https://stackoverflow.com/a/53081544/21380804 | ||
""" | ||
|
||
# check if both inputs are dicts | ||
self.assertIsInstance(d1, dict, 'First argument is not a dictionary') | ||
self.assertIsInstance(d2, dict, 'Second argument is not a dictionary') | ||
|
||
# check if both inputs have the same keys | ||
self.assertEqual(d1.keys(), d2.keys()) | ||
|
||
# check each key | ||
for key, value in d1.items(): | ||
if isinstance(value, dict): | ||
self.assertDictAlmostEqual(d1[key], d2[key], msg=msg, places=places) | ||
elif isinstance(value, list): | ||
self.assertListAlmostEqual(d1[key], d2[key], msg=msg, places=places) | ||
else: | ||
self.assertAlmostEqual(d1[key], d2[key], places=places, msg=msg) | ||
|
||
def assertListAlmostEqual(self, l1, l2, msg=None, places=7): | ||
# check if both inputs are dicts | ||
self.assertIsInstance(l1, list, 'First argument is not a list') | ||
self.assertIsInstance(l2, list, 'Second argument is not a list') | ||
|
||
# check if both inputs have the same keys | ||
self.assertEqual(len(l1), len(l2)) | ||
|
||
# check each key | ||
for i in range(len(l1)): | ||
v1 = l1[i] | ||
v2 = l2[i] | ||
if isinstance(v1, dict): | ||
self.assertDictAlmostEqual(v1, v2, msg=msg, places=places) | ||
elif isinstance(v1, list): | ||
self.assertListAlmostEqual(v1, v2, msg=msg, places=places) | ||
else: | ||
self.assertAlmostEqual(v1, v2, places=places, msg=msg) |
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