Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit 8745af1

Browse files
committed
add unittests for utils.read_file
1 parent 230c788 commit 8745af1

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

tests/test_utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,5 +404,30 @@ def test_is_json(self):
404404
self.assertFalse(is_json(test_json))
405405

406406

407+
class TestReadFile(unittest.TestCase):
408+
409+
def test_read_file(self):
410+
from WmAgentScripts.utils import read_file
411+
test_json = {
412+
"first": {"a": "A"},
413+
"second": "B"
414+
}
415+
test_data = "test data"
416+
417+
with patch('WmAgentScripts.utils.sendLog', return_value=None):
418+
mock_open_json = mock_open(read_data=json.dumps(test_json))
419+
with patch('__builtin__.open', mock_open_json):
420+
content = json.loads(read_file('filename.json'))
421+
self.assertDictEqual(content, test_json)
422+
423+
mock_open_file = mock_open(read_data=test_data)
424+
with patch('__builtin__.open', mock_open_file):
425+
content = read_file('filename')
426+
self.assertEqual(content, test_data)
427+
428+
textcontent = read_file('filename.json')
429+
self.assertEqual(textcontent, '{}')
430+
431+
407432
if __name__ == '__main__':
408433
unittest.main()

0 commit comments

Comments
 (0)