|
| 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 | + @mock.patch.object(formatter.EntityFormatter, '_get_generic_columns') |
| 42 | + def test_list_objects( |
| 43 | + self, |
| 44 | + mock_get_generic_columns, |
| 45 | + ): |
| 46 | + obj_list = ["obj2", "obj1"] |
| 47 | + mock_get_generic_columns.return_value = mock.sentinel.columns |
| 48 | + |
| 49 | + result = self.format.list_objects(obj_list) |
| 50 | + |
| 51 | + self.assertEqual( |
| 52 | + ( |
| 53 | + mock.sentinel.columns, |
| 54 | + ["obj2", "obj1"] |
| 55 | + ), |
| 56 | + ( |
| 57 | + result[0], |
| 58 | + list(result[1]) |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + def test_get_generic_data(self): |
| 63 | + obj = mock.Mock() |
| 64 | + |
| 65 | + result = self.format._get_generic_data(obj) |
| 66 | + |
| 67 | + self.assertEqual( |
| 68 | + obj, |
| 69 | + result |
| 70 | + ) |
| 71 | + |
| 72 | + def test_get_generic_columns(self): |
| 73 | + result = self.format._get_generic_columns() |
| 74 | + |
| 75 | + self.assertEqual( |
| 76 | + ["column_1", "column_2"], |
| 77 | + result |
| 78 | + ) |
| 79 | + |
| 80 | + @mock.patch.object(formatter.EntityFormatter, '_get_generic_columns') |
| 81 | + def test_get_formatted_entity(self, mock_get_generic_columns): |
| 82 | + obj = mock.Mock() |
| 83 | + mock_get_generic_columns.return_value = mock.sentinel.columns |
| 84 | + |
| 85 | + result = self.format.get_formatted_entity(obj) |
| 86 | + |
| 87 | + self.assertEqual( |
| 88 | + (["column_1", "column_2"], obj), |
| 89 | + result |
| 90 | + ) |
| 91 | + |
| 92 | + @ddt.data( |
| 93 | + { |
| 94 | + "current_value": 3, |
| 95 | + "max_value": 9, |
| 96 | + "percent_format": "{:.0f}%", |
| 97 | + "expected_result": "33%" |
| 98 | + }, |
| 99 | + { |
| 100 | + "current_value": 3, |
| 101 | + "max_value": 9, |
| 102 | + "percent_format": "{:.2f}%", |
| 103 | + "expected_result": "33.33%" |
| 104 | + }, |
| 105 | + { |
| 106 | + "current_value": 0, |
| 107 | + "max_value": 9, |
| 108 | + "percent_format": "{:.0f}%", |
| 109 | + "expected_result": None |
| 110 | + }, |
| 111 | + { |
| 112 | + "current_value": 3, |
| 113 | + "max_value": None, |
| 114 | + "percent_format": "{:.0f}%", |
| 115 | + "expected_result": None |
| 116 | + } |
| 117 | + ) |
| 118 | + def test_get_percent_string(self, data): |
| 119 | + result = self.format._get_percent_string( |
| 120 | + data["current_value"], |
| 121 | + data["max_value"], |
| 122 | + data["percent_format"] |
| 123 | + ) |
| 124 | + |
| 125 | + self.assertEqual( |
| 126 | + data["expected_result"], |
| 127 | + result |
| 128 | + ) |
| 129 | + |
| 130 | + @mock.patch.object(formatter.EntityFormatter, '_get_percent_string') |
| 131 | + def test_format_progress_update(self, mock_get_percent_string): |
| 132 | + progress_update = { |
| 133 | + "current_step": "mock_current_step", |
| 134 | + "total_steps": "mock_total_steps", |
| 135 | + "created_at": "mock_created_at", |
| 136 | + "message": "mock_message", |
| 137 | + } |
| 138 | + mock_get_percent_string.return_value = "mock_percent_string" |
| 139 | + |
| 140 | + result = self.format._format_progress_update(progress_update) |
| 141 | + |
| 142 | + self.assertEqual( |
| 143 | + "mock_created_at [mock_percent_string] mock_message", |
| 144 | + result |
| 145 | + ) |
0 commit comments