Skip to content

Commit 1ed38ac

Browse files
committed
Add unit tests for coriolisclient.cli.formatter module
1 parent 5be9769 commit 1ed38ac

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Copyright 2024 Cloudbase Solutions Srl
2+
# All Rights Reserved.
3+
4+
import ddt
5+
from unittest import mock
6+
7+
from coriolisclient.cli import formatter
8+
from coriolisclient.tests import test_base
9+
10+
11+
class TestEntityFormattter(formatter.EntityFormatter):
12+
13+
def __init__(self):
14+
self.columns = [
15+
"column_1",
16+
"column_2"
17+
]
18+
19+
def _get_formatted_data(self, obj):
20+
return obj
21+
22+
23+
@ddt.ddt
24+
class TestEntityFormattterTestCase(test_base.CoriolisBaseTestCase):
25+
"""Test suite for the Coriolis Entity Formatter."""
26+
27+
def setUp(self):
28+
super(TestEntityFormattterTestCase, self).setUp()
29+
self.format = TestEntityFormattter()
30+
31+
def test_get_sorted_list(self):
32+
obj_list = ["obj2", "obj1"]
33+
34+
result = self.format._get_sorted_list(obj_list)
35+
36+
self.assertEqual(
37+
obj_list,
38+
result
39+
)
40+
41+
def test_list_objects(self):
42+
obj_list = ["obj2", "obj1"]
43+
44+
(columns, data) = self.format.list_objects(obj_list)
45+
46+
self.assertEqual(
47+
(
48+
self.format.columns,
49+
["obj2", "obj1"]
50+
),
51+
(
52+
columns,
53+
list(data)
54+
)
55+
)
56+
57+
def test_get_generic_data(self):
58+
obj = mock.Mock()
59+
60+
result = self.format._get_generic_data(obj)
61+
62+
self.assertEqual(
63+
obj,
64+
result
65+
)
66+
67+
def test_get_generic_columns(self):
68+
result = self.format._get_generic_columns()
69+
70+
self.assertEqual(
71+
self.format.columns,
72+
result
73+
)
74+
75+
def test_get_formatted_entity(self):
76+
obj = mock.Mock()
77+
78+
(columns, data) = self.format.get_formatted_entity(obj)
79+
80+
self.assertEqual(
81+
(self.format.columns, obj),
82+
(columns, data)
83+
)
84+
85+
@ddt.data(
86+
{
87+
"current_value": 3,
88+
"max_value": 9,
89+
"percent_format": "{:.0f}%",
90+
"expected_result": "33%"
91+
},
92+
{
93+
"current_value": 3,
94+
"max_value": 9,
95+
"percent_format": "{:.2f}%",
96+
"expected_result": "33.33%"
97+
},
98+
{
99+
"current_value": 0,
100+
"max_value": 9,
101+
"percent_format": "{:.0f}%",
102+
"expected_result": None
103+
},
104+
{
105+
"current_value": 3,
106+
"max_value": None,
107+
"percent_format": "{:.0f}%",
108+
"expected_result": None
109+
}
110+
)
111+
def test_get_percent_string(self, data):
112+
result = self.format._get_percent_string(
113+
data["current_value"],
114+
data["max_value"],
115+
percent_format=data["percent_format"]
116+
)
117+
118+
self.assertEqual(
119+
data["expected_result"],
120+
result
121+
)
122+
123+
@mock.patch.object(formatter.EntityFormatter, '_get_percent_string')
124+
def test_format_progress_update(self, mock_get_percent_string):
125+
progress_update = {
126+
"current_step": "mock_current_step",
127+
"total_steps": "mock_total_steps",
128+
"created_at": "mock_created_at",
129+
"message": "mock_message",
130+
}
131+
mock_get_percent_string.return_value = "mock_percent_string"
132+
133+
result = self.format._format_progress_update(progress_update)
134+
135+
self.assertEqual(
136+
"mock_created_at [mock_percent_string] mock_message",
137+
result
138+
)
139+
140+
@mock.patch.object(formatter.EntityFormatter, '_get_percent_string')
141+
def test_format_progress_update_no_percent_string(
142+
self,
143+
mock_get_percent_string
144+
):
145+
progress_update = {
146+
"current_step": "mock_current_step",
147+
"total_steps": "mock_total_steps",
148+
"created_at": "mock_created_at",
149+
"message": "mock_message",
150+
}
151+
mock_get_percent_string.return_value = None
152+
153+
result = self.format._format_progress_update(progress_update)
154+
155+
self.assertEqual(
156+
"mock_created_at mock_message",
157+
result
158+
)

0 commit comments

Comments
 (0)