-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile_loading.test.py
More file actions
36 lines (29 loc) · 1.2 KB
/
Copy pathprofile_loading.test.py
File metadata and controls
36 lines (29 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import tempfile
import unittest
from unittest import mock
import logging
import json
from invoice import _load_profile
class LoadProfileTests(unittest.TestCase):
def test_missing_file_returns_empty_profile(self):
with tempfile.TemporaryDirectory() as td:
self.assertEqual(_load_profile(td), {})
def test_invalid_json_logs_error(self):
with tempfile.TemporaryDirectory() as td:
path = os.path.join(td, "institution.json")
with open(path, "w", encoding="utf-8") as fh:
fh.write("{invalid")
with self.assertLogs(level="ERROR") as cm:
with self.assertRaises(json.JSONDecodeError):
_load_profile(td)
self.assertIn("Failed to parse", cm.output[0])
def test_os_error_logs_error(self):
with tempfile.TemporaryDirectory() as td:
with mock.patch("builtins.open", side_effect=PermissionError("denied")):
with self.assertLogs(level="ERROR") as cm:
with self.assertRaises(OSError):
_load_profile(td)
self.assertIn("Unable to read", cm.output[0])
if __name__ == "__main__":
unittest.main()