-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_importer_endpoint.py
More file actions
77 lines (56 loc) · 3.24 KB
/
Copy pathtest_importer_endpoint.py
File metadata and controls
77 lines (56 loc) · 3.24 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Unit tests for validations of the ImporterEndpoint class."""
import unittest
from unittest.mock import MagicMock, patch
from pyflowintel.endpoints.importers import ImporterEndpoint
class TestImporterEndpointValidations(unittest.TestCase):
"""Unit tests for validations of the ImporterEndpoint class."""
def setUp(self):
self.mock_client = MagicMock()
self.endpoint = ImporterEndpoint(self.mock_client)
def test_build_endpoint_path(self):
self.assertEqual(self.endpoint.build_endpoint_path("/case"), "/importer/case")
self.assertEqual(self.endpoint.build_endpoint_path("/template"), "/importer/template")
# --- import_case ---
@patch("pyflowintel.endpoints.importers.utils.read_json")
def test_import_case_non_json_extension_raises(self, mock_read_json):
"""import_case propagates ValueError from read_json for invalid file extension."""
mock_read_json.side_effect = ValueError("File must have one of these extensions")
with self.assertRaises(ValueError):
self.endpoint.import_case("case.txt")
self.mock_client.post.assert_not_called()
@patch("pyflowintel.endpoints.importers.utils.read_json")
def test_import_case_missing_file_raises(self, mock_read_json):
"""import_case propagates ValueError from read_json when file does not exist."""
mock_read_json.side_effect = ValueError("File not found")
with self.assertRaises(ValueError):
self.endpoint.import_case("nonexistent.json")
self.mock_client.post.assert_not_called()
# --- import_template ---
@patch("pyflowintel.endpoints.importers.utils.read_json")
def test_import_template_success(self, mock_read_json):
"""import_template reads the file, posts to /importer/template, and returns the response."""
payload = {"title": "My Template", "tasks": []}
mock_read_json.return_value = payload
self.mock_client.post.return_value = {"message": "Template imported"}
result = self.endpoint.import_template("template.json")
mock_read_json.assert_called_once_with("template.json")
self.mock_client.post.assert_called_once_with(
"/importer/template", json_data=payload
)
self.assertEqual(result, {"message": "Template imported"})
@patch("pyflowintel.endpoints.importers.utils.read_json")
def test_import_template_non_json_extension_raises(self, mock_read_json):
"""import_template propagates ValueError from read_json for invalid file extension."""
mock_read_json.side_effect = ValueError("File must have one of these extensions")
with self.assertRaises(ValueError):
self.endpoint.import_template("template.yaml")
self.mock_client.post.assert_not_called()
@patch("pyflowintel.endpoints.importers.utils.read_json")
def test_import_template_missing_file_raises(self, mock_read_json):
"""import_template propagates ValueError from read_json when file does not exist."""
mock_read_json.side_effect = ValueError("File not found")
with self.assertRaises(ValueError):
self.endpoint.import_template("nonexistent.json")
self.mock_client.post.assert_not_called()
if __name__ == "__main__":
unittest.main()