|
1 | 1 | import argparse |
2 | | -from unittest.mock import MagicMock |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | +import io |
| 4 | +import sys |
3 | 5 |
|
4 | 6 | from tabcmd.commands.site.list_command import ListCommand |
5 | 7 | from tabcmd.commands.site.list_sites_command import ListSiteCommand |
| 8 | +from tabcmd.execution.localize import set_client_locale |
6 | 9 |
|
7 | 10 | import unittest |
8 | 11 | from unittest import mock |
|
13 | 16 | fake_item.name = "fake-name" |
14 | 17 | fake_item.id = "fake-id" |
15 | 18 | fake_item.extract_encryption_mode = "ENFORCED" |
| 19 | +fake_item.owner_id = "fake-owner" |
| 20 | +fake_item.content_url = "fake-url" |
| 21 | + |
| 22 | +fake_view = mock.MagicMock() |
| 23 | +fake_view.name = "fake-view" |
16 | 24 |
|
17 | 25 | getter = MagicMock() |
18 | 26 | getter.get = MagicMock("get", return_value=([fake_item], 1)) |
19 | | - |
20 | | -mock_args = argparse.Namespace() |
21 | | -mock_args.logging_level = "INFO" |
| 27 | +getter.all = MagicMock("all", return_value=[fake_item]) |
22 | 28 |
|
23 | 29 |
|
24 | 30 | @mock.patch("tabcmd.commands.auth.session.Session.create_session") |
25 | 31 | @mock.patch("tableauserverclient.Server") |
26 | 32 | class ListingTests(unittest.TestCase): |
| 33 | + @staticmethod |
| 34 | + def _set_up_session(mock_session, mock_server): |
| 35 | + mock_session.return_value = mock_server |
| 36 | + assert mock_session is not None |
| 37 | + mock_session.assert_not_called() |
| 38 | + global mock_args |
| 39 | + mock_args = argparse.Namespace(logging_level="DEBUG") |
| 40 | + # set values for things that should always have a default |
| 41 | + # should refactor so this can be automated |
| 42 | + mock_args.continue_if_exists = False |
| 43 | + mock_args.project_name = None |
| 44 | + mock_args.parent_project_path = None |
| 45 | + mock_args.parent_path = None |
| 46 | + mock_args.timeout = None |
| 47 | + mock_args.username = None |
| 48 | + mock_args.name = True |
| 49 | + mock_args.owner = None |
| 50 | + mock_args.address = None |
| 51 | + mock_args.machine = False |
| 52 | + mock_args.get_extract_encryption_mode = False |
| 53 | + mock_args.details = False |
| 54 | + |
27 | 55 | def test_list_sites(self, mock_server, mock_session): |
| 56 | + ListingTests._set_up_session(mock_session, mock_server) |
28 | 57 | mock_server.sites = getter |
29 | | - mock_args.get_extract_encryption_mode = False |
30 | | - mock_session.return_value = mock_server |
31 | 58 | out_value = ListSiteCommand.run_command(mock_args) |
32 | 59 |
|
33 | 60 | def test_list_content(self, mock_server, mock_session): |
| 61 | + ListingTests._set_up_session(mock_session, mock_server) |
34 | 62 | mock_server.flows = getter |
35 | 63 | mock_args.content = "flows" |
36 | | - mock_session.return_value = mock_server |
37 | 64 | out_value = ListCommand.run_command(mock_args) |
38 | 65 |
|
39 | 66 | def test_list_wb_details(self, mock_server, mock_session): |
| 67 | + ListingTests._set_up_session(mock_session, mock_server) |
40 | 68 | mock_server.workbooks = getter |
| 69 | + mock_server.workbooks.populate_views = MagicMock() |
| 70 | + fake_item.views = [fake_view] |
41 | 71 | mock_args.content = "workbooks" |
42 | 72 | mock_session.return_value = mock_server |
43 | 73 | mock_args.details = True |
44 | 74 | out_value = ListCommand.run_command(mock_args) |
45 | 75 |
|
46 | 76 | def test_list_datasources(self, mock_server, mock_session): |
| 77 | + ListingTests._set_up_session(mock_session, mock_server) |
47 | 78 | mock_server.datasources = getter |
48 | 79 | mock_args.content = "datasources" |
49 | 80 | mock_session.return_value = mock_server |
50 | 81 | mock_args.details = True |
51 | 82 | out_value = ListCommand.run_command(mock_args) |
52 | 83 |
|
53 | 84 | def test_list_projects(self, mock_server, mock_session): |
| 85 | + ListingTests._set_up_session(mock_session, mock_server) |
54 | 86 | mock_server.projects = getter |
55 | 87 | mock_args.content = "projects" |
56 | 88 | mock_session.return_value = mock_server |
57 | 89 | mock_args.details = True |
58 | 90 | out_value = ListCommand.run_command(mock_args) |
| 91 | + |
| 92 | + |
| 93 | +class ListCommandFunctionalTests(unittest.TestCase): |
| 94 | + """Test that ListCommand properly uses localized strings in different scenarios""" |
| 95 | + |
| 96 | + @patch("tabcmd.commands.site.list_command._") |
| 97 | + @patch("tabcmd.commands.auth.session.Session") |
| 98 | + @patch("tabcmd.execution.logger_config.log") |
| 99 | + def test_show_header_with_all_options(self, mock_log, mock_session, mock_translate): |
| 100 | + """Test header generation with all display options enabled""" |
| 101 | + # Mock the translation function to return the actual English strings |
| 102 | + def translate_side_effect(key): |
| 103 | + translations = { |
| 104 | + "tabcmd.listing.header.id": "ID", |
| 105 | + "tabcmd.listing.header.name": "NAME", |
| 106 | + "tabcmd.listing.header.owner": "OWNER", |
| 107 | + "tabcmd.listing.header.url": "URL", |
| 108 | + "tabcmd.listing.header.children": "CHILDREN", |
| 109 | + } |
| 110 | + return translations.get(key, key) |
| 111 | + |
| 112 | + mock_translate.side_effect = translate_side_effect |
| 113 | + |
| 114 | + mock_args = argparse.Namespace(name=True, owner=True, address=True, details=True) |
| 115 | + |
| 116 | + # Test workbooks (should include all headers) |
| 117 | + header = ListCommand.show_header(mock_args, "workbooks") |
| 118 | + self.assertIn("ID", header) |
| 119 | + self.assertIn("NAME", header) |
| 120 | + self.assertIn("OWNER", header) |
| 121 | + self.assertIn("URL", header) |
| 122 | + self.assertIn("CHILDREN", header) |
| 123 | + |
| 124 | + # Test datasources (should include URL but not CHILDREN) |
| 125 | + header = ListCommand.show_header(mock_args, "datasources") |
| 126 | + self.assertIn("ID", header) |
| 127 | + self.assertIn("NAME", header) |
| 128 | + self.assertIn("OWNER", header) |
| 129 | + self.assertIn("URL", header) |
| 130 | + self.assertNotIn("CHILDREN", header) |
| 131 | + |
| 132 | + # Test projects (should not include URL or CHILDREN) |
| 133 | + header = ListCommand.show_header(mock_args, "projects") |
| 134 | + self.assertIn("ID", header) |
| 135 | + self.assertIn("NAME", header) |
| 136 | + self.assertIn("OWNER", header) |
| 137 | + self.assertNotIn("URL", header) |
| 138 | + self.assertNotIn("CHILDREN", header) |
| 139 | + |
| 140 | + @patch("tabcmd.commands.site.list_command._") |
| 141 | + @patch("tabcmd.commands.auth.session.Session") |
| 142 | + @patch("tabcmd.execution.logger_config.log") |
| 143 | + def test_show_header_minimal_options(self, mock_log, mock_session, mock_translate): |
| 144 | + """Test header generation with minimal options""" |
| 145 | + # Mock the translation function |
| 146 | + mock_translate.return_value = "ID" |
| 147 | + |
| 148 | + mock_args = argparse.Namespace(name=False, owner=False, address=False, details=False) |
| 149 | + |
| 150 | + header = ListCommand.show_header(mock_args, "workbooks") |
| 151 | + self.assertEqual(header, "ID") |
| 152 | + |
| 153 | + @patch("tabcmd.commands.site.list_command._") |
| 154 | + @patch("tableauserverclient.Server") |
| 155 | + def test_format_children_listing_workbooks(self, mock_server, mock_translate): |
| 156 | + """Test children listing format for workbooks""" |
| 157 | + # Mock the translation function |
| 158 | + mock_translate.return_value = "VIEWS: [" |
| 159 | + |
| 160 | + mock_args = argparse.Namespace(details=True) |
| 161 | + |
| 162 | + # Mock workbook item with views - create proper mock objects with string names |
| 163 | + view1 = MagicMock() |
| 164 | + view1.name = "View1" |
| 165 | + view2 = MagicMock() |
| 166 | + view2.name = "View2" |
| 167 | + |
| 168 | + mock_item = MagicMock() |
| 169 | + mock_item.views = [view1, view2] |
| 170 | + |
| 171 | + # Mock server populate_views method |
| 172 | + mock_server.workbooks.populate_views = MagicMock() |
| 173 | + |
| 174 | + result = ListCommand.format_children_listing(mock_args, mock_server, "workbooks", mock_item) |
| 175 | + |
| 176 | + self.assertIn("VIEWS: ", result) |
| 177 | + self.assertIn("View1", result) |
| 178 | + self.assertIn("View2", result) |
| 179 | + mock_server.workbooks.populate_views.assert_called_once_with(mock_item) |
| 180 | + |
| 181 | + @patch("tableauserverclient.Server") |
| 182 | + def test_format_children_listing_non_workbooks(self, mock_server): |
| 183 | + """Test children listing returns empty for non-workbook content types""" |
| 184 | + mock_args = argparse.Namespace(details=True) |
| 185 | + mock_item = MagicMock() |
| 186 | + |
| 187 | + result = ListCommand.format_children_listing(mock_args, mock_server, "datasources", mock_item) |
| 188 | + self.assertEqual(result, "") |
| 189 | + |
| 190 | + result = ListCommand.format_children_listing(mock_args, mock_server, "projects", mock_item) |
| 191 | + self.assertEqual(result, "") |
| 192 | + |
| 193 | + @patch("tableauserverclient.Server") |
| 194 | + def test_format_children_listing_no_details(self, mock_server): |
| 195 | + """Test children listing returns empty when details=False""" |
| 196 | + mock_args = argparse.Namespace(details=False) |
| 197 | + mock_item = MagicMock() |
| 198 | + |
| 199 | + result = ListCommand.format_children_listing(mock_args, mock_server, "workbooks", mock_item) |
| 200 | + self.assertEqual(result, "") |
| 201 | + |
| 202 | + |
| 203 | +class LocalizedStringKeysTests(unittest.TestCase): |
| 204 | + """Test that ListCommand calls the correct localization keys""" |
| 205 | + |
| 206 | + @patch("tabcmd.commands.site.list_command._") |
| 207 | + def test_show_header_datasources_with_url(self, mock_translate): |
| 208 | + """Test that datasources headers include URL when address=True""" |
| 209 | + mock_translate.return_value = "TRANSLATED" |
| 210 | + |
| 211 | + mock_args = argparse.Namespace( |
| 212 | + name=True, owner=True, address=True, details=True # With address=True, datasources should show URL |
| 213 | + ) |
| 214 | + |
| 215 | + ListCommand.show_header(mock_args, "datasources") |
| 216 | + |
| 217 | + # Verify that URL key IS called for datasources (but not CHILDREN) |
| 218 | + expected_calls = [ |
| 219 | + mock.call("tabcmd.listing.header.id"), |
| 220 | + mock.call("tabcmd.listing.header.name"), |
| 221 | + mock.call("tabcmd.listing.header.owner"), |
| 222 | + mock.call("tabcmd.listing.header.url"), # Should be called for datasources too |
| 223 | + # Note: tabcmd.listing.header.children should NOT be called (only for workbooks) |
| 224 | + ] |
| 225 | + mock_translate.assert_has_calls(expected_calls, any_order=True) |
| 226 | + |
| 227 | + # Verify CHILDREN key was not called (only for workbooks) |
| 228 | + all_calls = [call[0][0] for call in mock_translate.call_args_list] |
| 229 | + self.assertNotIn("tabcmd.listing.header.children", all_calls) |
| 230 | + |
| 231 | + def test_show_header_structure_without_mocking(self): |
| 232 | + """Test the basic structure of show_header without mocking translations""" |
| 233 | + mock_args = argparse.Namespace(name=False, owner=False, address=False, details=False) |
| 234 | + |
| 235 | + # This should return just the ID header (even if it's the localization key) |
| 236 | + header = ListCommand.show_header(mock_args, "workbooks") |
| 237 | + |
| 238 | + # The result should be a single string (not contain commas when no options are set) |
| 239 | + self.assertNotIn(",", header) |
| 240 | + self.assertTrue(isinstance(header, str)) |
| 241 | + self.assertGreater(len(header), 0) |
0 commit comments