|
| 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 | +@ddt.ddt |
| 12 | +class EntityFormatterTestCase(test_base.CoriolisBaseTestCase): |
| 13 | + """Test suite for the Coriolis Entity Formatter.""" |
| 14 | + |
| 15 | + def setUp(self): |
| 16 | + def _get_formatted_data(self, obj): |
| 17 | + return mock.sentinel.data |
| 18 | + setattr(formatter.EntityFormatter, '_get_formatted_data', |
| 19 | + _get_formatted_data) |
| 20 | + super(EntityFormatterTestCase, self).setUp() |
| 21 | + self.format = formatter.EntityFormatter() |
| 22 | + self.format.columns = mock.sentinel.columns |
| 23 | + |
| 24 | + def test_get_sorted_list(self): |
| 25 | + obj_list = ["obj2", "obj1"] |
| 26 | + |
| 27 | + result = self.format._get_sorted_list(obj_list) |
| 28 | + |
| 29 | + self.assertEqual( |
| 30 | + obj_list, |
| 31 | + result |
| 32 | + ) |
| 33 | + |
| 34 | + @mock.patch.object(formatter.EntityFormatter, '_get_generic_columns') |
| 35 | + @mock.patch.object(formatter.EntityFormatter, '_get_sorted_list') |
| 36 | + @mock.patch.object(formatter.EntityFormatter, '_get_generic_data') |
| 37 | + def test_list_objects( |
| 38 | + self, |
| 39 | + mock_get_generic_data, |
| 40 | + mock_get_sorted_list, |
| 41 | + mock_get_generic_columns, |
| 42 | + ): |
| 43 | + obj_list = ["obj2", "obj1"] |
| 44 | + mock_get_sorted_list.return_value = ["obj1", "obj2"] |
| 45 | + mock_get_generic_data.side_effect = [ |
| 46 | + mock.sentinel.generic_data_1, |
| 47 | + mock.sentinel.generic_data_2 |
| 48 | + ] |
| 49 | + mock_get_generic_columns.return_value = mock.sentinel.columns |
| 50 | + |
| 51 | + result = self.format.list_objects(obj_list) |
| 52 | + |
| 53 | + self.assertEqual( |
| 54 | + ( |
| 55 | + mock.sentinel.columns, |
| 56 | + mock.sentinel.generic_data_1, |
| 57 | + mock.sentinel.generic_data_2 |
| 58 | + ), |
| 59 | + ( |
| 60 | + result[0], |
| 61 | + next(result[1]), |
| 62 | + next(result[1]) |
| 63 | + ) |
| 64 | + ) |
| 65 | + mock_get_sorted_list.assert_called_once_with(obj_list) |
| 66 | + mock_get_generic_data.assert_has_calls([ |
| 67 | + mock.call("obj1"), |
| 68 | + mock.call("obj2") |
| 69 | + ]) |
| 70 | + |
| 71 | + def test_get_generic_data(self): |
| 72 | + obj = mock.Mock() |
| 73 | + |
| 74 | + result = self.format._get_generic_data(obj) |
| 75 | + |
| 76 | + self.assertEqual( |
| 77 | + mock.sentinel.data, |
| 78 | + result |
| 79 | + ) |
| 80 | + |
| 81 | + def test_get_generic_columns(self): |
| 82 | + result = self.format._get_generic_columns() |
| 83 | + |
| 84 | + self.assertEqual( |
| 85 | + mock.sentinel.columns, |
| 86 | + result |
| 87 | + ) |
| 88 | + |
| 89 | + def testget_formatted_entity(self): |
| 90 | + obj = mock.Mock() |
| 91 | + |
| 92 | + result = self.format.get_formatted_entity(obj) |
| 93 | + |
| 94 | + self.assertEqual( |
| 95 | + (mock.sentinel.columns, mock.sentinel.data), |
| 96 | + result |
| 97 | + ) |
| 98 | + |
| 99 | + @ddt.data( |
| 100 | + { |
| 101 | + "current_value": 3, |
| 102 | + "max_value": 9, |
| 103 | + "percent_format": "{:.0f}%", |
| 104 | + "expected_result": "33%" |
| 105 | + }, |
| 106 | + { |
| 107 | + "current_value": 3, |
| 108 | + "max_value": 9, |
| 109 | + "percent_format": "{:.2f}%", |
| 110 | + "expected_result": "33.33%" |
| 111 | + }, |
| 112 | + { |
| 113 | + "current_value": 0, |
| 114 | + "max_value": 9, |
| 115 | + "percent_format": "{:.0f}%", |
| 116 | + "expected_result": None |
| 117 | + }, |
| 118 | + { |
| 119 | + "current_value": 3, |
| 120 | + "max_value": None, |
| 121 | + "percent_format": "{:.0f}%", |
| 122 | + "expected_result": None |
| 123 | + } |
| 124 | + ) |
| 125 | + def test_get_percent_string(self, data): |
| 126 | + result = self.format._get_percent_string( |
| 127 | + data["current_value"], |
| 128 | + data["max_value"], |
| 129 | + data["percent_format"] |
| 130 | + ) |
| 131 | + |
| 132 | + self.assertEqual( |
| 133 | + data["expected_result"], |
| 134 | + result |
| 135 | + ) |
| 136 | + |
| 137 | + @mock.patch.object(formatter.EntityFormatter, '_get_percent_string') |
| 138 | + def test_format_progress_update(self, mock_get_percent_string): |
| 139 | + progress_update = { |
| 140 | + "current_step": "mock_current_step", |
| 141 | + "total_steps": "mock_total_steps", |
| 142 | + "created_at": "mock_created_at", |
| 143 | + "message": "mock_message", |
| 144 | + } |
| 145 | + mock_get_percent_string.return_value = "mock_percent_string" |
| 146 | + |
| 147 | + result = self.format._format_progress_update(progress_update) |
| 148 | + |
| 149 | + self.assertEqual( |
| 150 | + "mock_created_at [mock_percent_string] mock_message", |
| 151 | + result |
| 152 | + ) |
0 commit comments