From b75cc29569b5fc916c47fa3054f57f6948edb46d Mon Sep 17 00:00:00 2001 From: Jac Fitzgerald Date: Fri, 28 Jul 2023 16:12:15 -0700 Subject: [PATCH 1/2] fix log error on publish also add unit test --- .../publish_command.py | 2 +- tests/commands/test_publish_command.py | 96 +++++++++++++++++++ tests/commands/test_run_commands.py | 32 +------ 3 files changed, 99 insertions(+), 31 deletions(-) create mode 100644 tests/commands/test_publish_command.py diff --git a/tabcmd/commands/datasources_and_workbooks/publish_command.py b/tabcmd/commands/datasources_and_workbooks/publish_command.py index 99c4ea25..cb50be17 100644 --- a/tabcmd/commands/datasources_and_workbooks/publish_command.py +++ b/tabcmd/commands/datasources_and_workbooks/publish_command.py @@ -75,7 +75,7 @@ def run_command(args): new_workbook = TSC.WorkbookItem(project_id, name=args.name, show_tabs=args.tabbed) try: if creds: - logger.debug("Workbook credentials object: " + creds) + logger.debug("Workbook credentials object: " + str(creds)) new_workbook = server.workbooks.publish( new_workbook, args.filename, diff --git a/tests/commands/test_publish_command.py b/tests/commands/test_publish_command.py new file mode 100644 index 00000000..45b065a6 --- /dev/null +++ b/tests/commands/test_publish_command.py @@ -0,0 +1,96 @@ +import argparse +import unittest +from unittest.mock import * +import tableauserverclient as TSC + +from tabcmd.commands.auth import login_command +from tabcmd.commands.datasources_and_workbooks import ( + delete_command, + export_command, + get_url_command, + publish_command +) + + +from typing import List, NamedTuple, TextIO, Union +import io + +mock_args = argparse.Namespace() + +fake_item = MagicMock() +fake_item.name = "fake-name" +fake_item.id = "fake-id" +fake_item.pdf = b"/pdf-representation-of-view" +fake_item.extract_encryption_mode = "Disabled" + +fake_job = MagicMock() +fake_job.id = "fake-job-id" + +creator = MagicMock() +getter = MagicMock() +getter.get = MagicMock("get", return_value=([fake_item], 1)) +getter.publish = MagicMock("publish", return_value=fake_item) + + +@patch("tableauserverclient.Server") +@patch("tabcmd.commands.auth.session.Session.create_session") +class RunCommandsTest(unittest.TestCase): + @staticmethod + def _set_up_session(mock_session, mock_server): + mock_session.return_value = mock_server + assert mock_session is not None + mock_session.assert_not_called() + global mock_args + mock_args = argparse.Namespace(logging_level="DEBUG") + # set values for things that should always have a default + # should refactor so this can be automated + mock_args.continue_if_exists = False + mock_args.project_name = None + mock_args.parent_project_path = None + mock_args.parent_path = None + mock_args.timeout = None + mock_args.username = None + + def test_publish(self, mock_session, mock_server): + RunCommandsTest._set_up_session(mock_session, mock_server) + mock_args.overwrite = False + mock_args.filename = "existing_file.twbx" + mock_args.project_name = "project-name" + mock_args.parent_project_path = "projects" + mock_args.name = "" + mock_args.tabbed = True + mock_args.db_username = None + mock_args.oauth_username = None + mock_args.append = False + mock_args.replace = False + mock_args.thumbnail_username = None + mock_args.thumbnail_group = None + mock_server.projects = getter + publish_command.PublishCommand.run_command(mock_args) + mock_session.assert_called() + + + def test_publish_with_creds(self, mock_session, mock_server): + RunCommandsTest._set_up_session(mock_session, mock_server) + mock_args.overwrite = False + mock_args.append = True + mock_args.replace = False + + mock_args.filename = "existing_file.twbx" + mock_args.project_name = "project-name" + mock_args.parent_project_path = "projects" + mock_args.name = "" + mock_args.tabbed = True + + mock_args.db_username = "username" + mock_args.db_password = "oauth_u" + mock_args.save_db_password = True + mock_args.oauth_username = None + mock_args.embed = False + + mock_args.thumbnail_username = None + mock_args.thumbnail_group = None + + mock_server.projects = getter + publish_command.PublishCommand.run_command(mock_args) + mock_session.assert_called() diff --git a/tests/commands/test_run_commands.py b/tests/commands/test_run_commands.py index dbb9bf55..fe7a6b82 100644 --- a/tests/commands/test_run_commands.py +++ b/tests/commands/test_run_commands.py @@ -8,8 +8,7 @@ delete_command, export_command, get_url_command, - publish_command, - runschedule_command, + publish_command ) from tabcmd.commands.extracts import ( create_extracts_command, @@ -35,7 +34,7 @@ remove_users_command, delete_site_users_command, ) -from typing import List, NamedTuple, TextIO, Union +from typing import NamedTuple, TextIO, Union import io mock_args = argparse.Namespace() @@ -139,33 +138,6 @@ def test_get_workbook(self, mock_session, mock_server): get_url_command.GetUrl.run_command(mock_args) mock_session.assert_called() - def test_publish(self, mock_session, mock_server): - RunCommandsTest._set_up_session(mock_session, mock_server) - mock_args.overwrite = False - mock_args.filename = "existing_file.twbx" - mock_args.project_name = "project-name" - mock_args.parent_project_path = "projects" - mock_args.name = "" - mock_args.tabbed = True - mock_args.db_username = None - mock_args.oauth_username = None - mock_args.append = False - mock_args.replace = False - mock_args.thumbnail_username = None - mock_args.thumbnail_group = None - mock_server.projects = getter - publish_command.PublishCommand.run_command(mock_args) - mock_session.assert_called() - - @unittest.skip("target code not implemented yet") - def test_runschedule(self, mock_session, mock_server): - RunCommandsTest._set_up_session(mock_session, mock_server) - mock_server.schedules = getter - mock_args.schedule = "myschedule" - with self.assertRaises(SystemExit): - runschedule_command.RunSchedule.run_command(mock_args) - # mock_session.assert_called() - # extracts def test_create_extract(self, mock_session, mock_server): RunCommandsTest._set_up_session(mock_session, mock_server) From 3fe0ab587872a8e313c2b64c3cd17f0e216550dc Mon Sep 17 00:00:00 2001 From: Jac Fitzgerald Date: Fri, 28 Jul 2023 16:24:30 -0700 Subject: [PATCH 2/2] black --- tabcmd/commands/site/list_command.py | 1 - tests/commands/test_publish_command.py | 8 +------- tests/commands/test_run_commands.py | 7 +------ tests/commands/test_session.py | 2 ++ 4 files changed, 4 insertions(+), 14 deletions(-) diff --git a/tabcmd/commands/site/list_command.py b/tabcmd/commands/site/list_command.py index 76c67cbb..64e4fa48 100644 --- a/tabcmd/commands/site/list_command.py +++ b/tabcmd/commands/site/list_command.py @@ -18,7 +18,6 @@ class ListCommand(Server): "tabcmd_content_none": "No content found.", } - name: str = "list" description: str = "List content items of a specified type" diff --git a/tests/commands/test_publish_command.py b/tests/commands/test_publish_command.py index 45b065a6..9f91a02e 100644 --- a/tests/commands/test_publish_command.py +++ b/tests/commands/test_publish_command.py @@ -4,12 +4,7 @@ import tableauserverclient as TSC from tabcmd.commands.auth import login_command -from tabcmd.commands.datasources_and_workbooks import ( - delete_command, - export_command, - get_url_command, - publish_command -) +from tabcmd.commands.datasources_and_workbooks import delete_command, export_command, get_url_command, publish_command from typing import List, NamedTuple, TextIO, Union @@ -69,7 +64,6 @@ def test_publish(self, mock_session, mock_server): publish_command.PublishCommand.run_command(mock_args) mock_session.assert_called() - def test_publish_with_creds(self, mock_session, mock_server): RunCommandsTest._set_up_session(mock_session, mock_server) mock_args.overwrite = False diff --git a/tests/commands/test_run_commands.py b/tests/commands/test_run_commands.py index fe7a6b82..2efd4a75 100644 --- a/tests/commands/test_run_commands.py +++ b/tests/commands/test_run_commands.py @@ -4,12 +4,7 @@ import tableauserverclient as TSC from tabcmd.commands.auth import login_command, logout_command -from tabcmd.commands.datasources_and_workbooks import ( - delete_command, - export_command, - get_url_command, - publish_command -) +from tabcmd.commands.datasources_and_workbooks import delete_command, export_command, get_url_command, publish_command from tabcmd.commands.extracts import ( create_extracts_command, delete_extracts_command, diff --git a/tests/commands/test_session.py b/tests/commands/test_session.py index 44b20a8a..cb60260d 100644 --- a/tests/commands/test_session.py +++ b/tests/commands/test_session.py @@ -74,6 +74,7 @@ def _set_mocks_for_json_file_exists(mock_path, mock_json_lib, does_it_exist=True mock_json_lib.load.return_value = None return path + def _set_mock_file_content(mock_load, expected_content): mock_load.return_value = expected_content return mock_load @@ -120,6 +121,7 @@ def test_json_invalid(self, mock_open, mock_path, mock_json): test_session = Session() assert test_session.username is None + @mock.patch("getpass.getpass") class BuildCredentialsTests(unittest.TestCase): @classmethod