forked from microsoft/fabric-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fab_output.py
More file actions
184 lines (147 loc) · 6.19 KB
/
test_fab_output.py
File metadata and controls
184 lines (147 loc) · 6.19 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""Unit tests for FabricCLIOutput."""
import json
from enum import Enum
import pytest
from fabric_cli.core import fab_constant
from fabric_cli.core.fab_output import FabricCLIOutput, OutputResult, OutputStatus
class TestEnum(str, Enum):
A = "a"
B = "b"
C = "c"
def test_output_result_json_success():
"""Test OutputResult successfully handles properties and json conversion."""
# Test 1: With all fields
result = OutputResult(
data={"test": "data"},
hidden_data=[TestEnum.A, TestEnum.B],
message="test message",
)
assert result.data == [{"test": "data"}]
assert result.hidden_data == ["a", "b"]
assert result.message == "test message"
result_dict = result.to_dict()
assert result_dict["data"] == [{"test": "data"}]
assert result_dict["hidden_data"] == ["a", "b"]
assert result_dict["message"] == "test message"
# Test 2: Without optional fields
result = OutputResult(data={"test": "data"}, hidden_data=None, message=None)
assert result.data == [{"test": "data"}]
assert result.hidden_data is None
assert result.message is None
result_dict = result.to_dict()
assert result_dict["data"] == [{"test": "data"}]
assert "hidden_data" not in result_dict
assert "message" not in result_dict
# Test 3: With empty data
result = OutputResult(data=[], hidden_data=None, message=None)
assert result.get_data_keys() == []
# Test 4: With data containing keys
result = OutputResult(
data=[{"key1": "val1", "key2": "val2"}], hidden_data=None, message=None
)
assert set(result.get_data_keys()) == {"key1", "key2"}
def test_fabric_cli_output_json_success():
"""Test output successfully converts to JSON with different fields."""
# Test 1: With data and hidden_data, without message
output = FabricCLIOutput(
command="test",
status=OutputStatus.Success,
data={"test": "data"},
hidden_data=[TestEnum.A, TestEnum.B],
)
json_output = json.loads(output.to_json())
assert "message" not in json_output["result"]
assert json_output["command"] == "test"
assert json_output["result"]["data"] == [{"test": "data"}]
assert json_output["result"]["hidden_data"] == ["a", "b"]
# Test 2: With only message
output = FabricCLIOutput(
command="test", status=OutputStatus.Success, message="test message"
)
json_output = json.loads(output.to_json())
assert json_output["result"]["message"] == "test message"
assert "data" not in json_output["result"]
assert "hidden_data" not in json_output["result"]
def test_fabric_cli_output_json_failure():
"""Test output to JSON fails with invalid data."""
# Setup
output = FabricCLIOutput(
command="test", output_format_type=None, data=object() # Un-serializable object
)
# Verify
from fabric_cli.core.fab_exceptions import FabricCLIError
with pytest.raises(FabricCLIError) as ex:
output.to_json()
ex.value.status_code == fab_constant.WARNING_INVALID_JSON_FORMAT
ex.value.message == fab_constant.ERROR_INVALID_JSON
def test_output_result_success():
# Test 1: with None values
result = OutputResult(data=None, hidden_data=None, message=None)
assert result.data is None
assert result.hidden_data is None
assert result.message is None
# Test 2: hidden_data with empty list
result = OutputResult(data={"test": "data"}, hidden_data=[], message=None)
assert result.hidden_data == []
# Test 3: hidden_data sorting
result = OutputResult(
data={"test": "data"},
hidden_data=[TestEnum.C, TestEnum.A, TestEnum.B],
message=None,
)
assert result.hidden_data == ["a", "b", "c"]
def test_fabric_cli_output_show_headers():
"""Test show_headers property is handled correctly."""
# Test with show_headers True
output = FabricCLIOutput(data={"test": "data"}, show_headers=True)
assert output.show_headers is True
# Test with show_headers False (default)
output = FabricCLIOutput(data={"test": "data"})
assert output.show_headers is False
def test_fabric_cli_output_format_type_success():
"""Test output_format_type property is handled successfully."""
# Test with format type set
output = FabricCLIOutput(output_format_type="json")
assert output.output_format_type == "json"
# Test with no format type
output = FabricCLIOutput()
assert output.output_format_type is None
def test_fabric_cli_output_error_handling_success():
"""Test error handling is successful with different status conditions."""
# Test with failed status and error code
output = FabricCLIOutput(
status=OutputStatus.Failure,
error_code="TEST_ERROR",
message="Test error message",
)
json_output = json.loads(output.to_json())
assert json_output["status"] == OutputStatus.Failure
assert json_output["result"]["error_code"] == "TEST_ERROR"
assert json_output["result"]["message"] == "Test error message"
# Test error code not present in success status
output = FabricCLIOutput(
status=OutputStatus.Success,
error_code="TEST_ERROR", # Should be ignored in success status
)
json_output = json.loads(output.to_json())
assert "error_code" not in json_output
# Test default error_code when no error code is provided in failed status
output = FabricCLIOutput(
status=OutputStatus.Failure,
message="Test error message",
)
json_output = json.loads(output.to_json())
assert json_output["result"]["error_code"] == "UnexpectedError"
def test_fabric_cli_output_show_key_value_list_success():
"""Test show_key_value_list property is handled correctly."""
# Test with show_key_value_list True
output = FabricCLIOutput(data={"test": "data"}, show_key_value_list=True)
assert output.show_key_value_list is True
# Test with show_key_value_list False (default)
output = FabricCLIOutput(data={"test": "data"})
assert output.show_key_value_list is False
# Test with explicit False
output = FabricCLIOutput(data={"test": "data"}, show_key_value_list=False)
assert output.show_key_value_list is False