Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proxy apps command line interface #1003

Merged
merged 18 commits into from
Jan 6, 2024
203 changes: 99 additions & 104 deletions tests/unit_tests/test_tethys_cli/test_proxyapps_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
add_proxyapp,
update_proxyapp,
list_proxyapps,
get_engine,
)

import unittest
Expand Down Expand Up @@ -35,58 +34,11 @@ def setUp(self):
def tearDown(self):
self.proxy_app.delete()

@mock.patch("tethys_cli.proxyapps_commands.write_error")
@mock.patch("tethys_cli.proxyapps_commands.read_settings")
def test_get_engine_no_db_settings_error(self, mock_settings, mock_write_error):
mock_settings.return_value = {}
get_engine()
mock_write_error.assert_called_with(
"No database settings defined in the portal_config.yml file"
)

@mock.patch("tethys_cli.proxyapps_commands.write_error")
@mock.patch("tethys_cli.proxyapps_commands.read_settings")
def test_get_engine_no_default_db_settings_error(
self, mock_settings, mock_write_error
):
mock_settings.return_value = {"DATABASES": {}}
get_engine()
mock_write_error.assert_called_with(
"No default database defined in the portal_config.yml file"
)

@mock.patch("tethys_cli.proxyapps_commands.write_error")
@mock.patch("tethys_cli.proxyapps_commands.read_settings")
def test_get_engine_connection_error(self, mock_settings, mock_write_error):
# database settings is empty here, so it will cause an error
mock_settings.return_value = {
"DATABASES": {"default": {"ENGINE": "django.db.backends.postgresql"}}
}
get_engine()
mock_write_error.assert_called_with("Error when connecting to the database")

@mock.patch("tethys_cli.proxyapps_commands.create_engine")
@mock.patch("tethys_cli.proxyapps_commands.read_settings")
def test_get_engine_sqlite(self, mock_settings, mock_create_engine):
mock_settings.return_value = {
"DATABASES": {
"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": "db_name"}
}
}
mock_engine = mock_create_engine.return_value
mock_engine.connect.return_value = "Mocked SQLite Engine"

# Call the function that uses create_engine
get_engine()

# Assertions
mock_create_engine.assert_called_once()
mock_create_engine.assert_called_with("sqlite:///db_name", pool_pre_ping=True)

@mock.patch("tethys_cli.proxyapps_commands.write_info")
@mock.patch("tethys_cli.proxyapps_commands.print")
def test_list_proxy_apps(self, mock_print, mock_write_info):
list_proxyapps()
mock_args = mock.Mock()
list_proxyapps(mock_args)
rts_call_args = mock_print.call_args_list
check_list = []

Expand All @@ -97,82 +49,125 @@ def test_list_proxy_apps(self, mock_print, mock_write_info):
self.assertIn(" My_Proxy_App_for_Testing", check_list)

@mock.patch("tethys_cli.proxyapps_commands.write_error")
def test_update_proxy_apps_no_app_name(self, mock_write_error):
mock_args = []
update_proxyapp(mock_args)
mock_write_error.assert_called_with("proxy_app_name cannot be empty")

@mock.patch("tethys_cli.proxyapps_commands.write_error")
def test_update_proxy_apps_no_app_key_name(self, mock_write_error):
mock_args = [self.app_name]
update_proxyapp(mock_args)
mock_write_error.assert_called_with("proxy_app_key cannot be empty")

@mock.patch("tethys_cli.proxyapps_commands.write_error")
def test_update_proxy_apps_no_app_value_name(self, mock_write_error):
mock_args = [self.app_name, "logo_url"]
update_proxyapp(mock_args)
mock_write_error.assert_called_with("proxy_app_value cannot be empty")
@mock.patch("tethys_cli.proxyapps_commands.exit", side_effect=SystemExit)
def test_update_proxy_apps_no_app(self, mock_exit, mock_write_error):
mock_args = mock.Mock()
mock_args.proxy_app_name = "non_existing_proxy_app"
mock_args.proxy_app_key = "non_existing_key"
mock_args.proxy_app_key_value = "https://fake.com"

self.assertRaises(
SystemExit,
update_proxyapp,
mock_args,
)

@mock.patch("tethys_cli.proxyapps_commands.write_error")
def test_update_proxy_apps_no_app(self, mock_write_error):
mock_args = ["My_Proxy_App_for_Testing2", "logo_url", "https://fake.com"]
update_proxyapp(mock_args)
mock_write_error.assert_called_with(
"Proxy app My_Proxy_App_for_Testing2 does not exits"
"Proxy app non_existing_proxy_app does not exits"
)
mock_exit.assert_called_with(1)

@mock.patch("tethys_cli.proxyapps_commands.write_error")
def test_update_proxy_apps_no_correct_key(self, mock_write_error):
mock_args = [
self.app_name,
"non_existing_key",
"https://fake.com",
]
update_proxyapp(mock_args)
@mock.patch("tethys_cli.proxyapps_commands.exit", side_effect=SystemExit)
def test_update_proxy_apps_no_correct_key(self, mock_exit, mock_write_error):
mock_args = mock.Mock()
mock_args.proxy_app_name = self.app_name
mock_args.proxy_app_key = "non_existing_key"
mock_args.proxy_app_key_value = "https://fake.com"

self.assertRaises(
SystemExit,
update_proxyapp,
mock_args,
)

mock_write_error.assert_called_with(
f"Attribute non_existing_key does not exists in Proxy app {self.app_name}"
)
mock_exit.assert_called_with(1)

@mock.patch("tethys_cli.proxyapps_commands.write_success")
def test_update_proxy_apps(self, mock_write_success):
mock_args = [
self.app_name,
"logo_url",
"https://fake.com",
]
update_proxyapp(mock_args)
@mock.patch("tethys_cli.proxyapps_commands.exit", side_effect=SystemExit)
def test_update_proxy_apps(self, mock_exit, mock_write_success):
mock_args = mock.Mock()
mock_args.proxy_app_name = self.app_name
mock_args.proxy_app_key = "logo_url"
mock_args.proxy_app_key_value = "https://fake.com"
self.assertRaises(
SystemExit,
update_proxyapp,
mock_args,
)
mock_write_success.assert_called_with(f"Proxy app {self.app_name} was updated")
mock_exit.assert_called_with(0)

@mock.patch("tethys_cli.proxyapps_commands.write_error")
def test_add_proxy_apps_no_app_name(self, mock_write_error):
mock_args = []
add_proxyapp(mock_args)
mock_write_error.assert_called_with("proxy_app_name argument cannot be empty")

@mock.patch("tethys_cli.proxyapps_commands.write_error")
def test_add_proxy_apps_no_endpoint(self, mock_write_error):
mock_args = ["new_proxy_app"]
add_proxyapp(mock_args)
@mock.patch("tethys_cli.proxyapps_commands.exit", side_effect=SystemExit)
def test_add_proxy_apps_with_existing_proxy_app(self, mock_exit, mock_write_error):
mock_args = mock.Mock()
mock_args.proxy_app_name = self.app_name
mock_args.proxy_app_endpoint = "http://foo.example.com/my-proxy-app"

self.assertRaises(
SystemExit,
add_proxyapp,
mock_args,
)
mock_write_error.assert_called_with(
"proxy_app_endpoint argument cannot be empty"
f"There is already a proxy app with that name: {self.app_name}"
)
mock_exit.assert_called_with(1)

@mock.patch("tethys_cli.proxyapps_commands.write_error")
def test_add_proxy_apps_with_existing_proxy_app(self, mock_write_error):
mock_args = ["My_Proxy_App_for_Testing", "http://foo.example.com/my-proxy-app"]
breakpoint()
add_proxyapp(mock_args)
@mock.patch("tethys_cli.proxyapps_commands.exit", side_effect=SystemExit)
def test_add_proxyapp_integrity_error(self, mock_exit, mock_write_error):
app_name_mock = "My_Proxy_App_for_Testing_2"
mock_args = mock.Mock()
mock_args.proxy_app_name = app_name_mock
mock_args.proxy_app_endpoint = "http://foo.example.com/my-proxy-app"
mock_args.proxy_app_description = None
mock_args.proxy_app_logo_url = None
mock_args.proxy_app_tags = None
mock_args.proxy_app_enabled = None
mock_args.proxy_app_show_in_apps_library = None
mock_args.proxy_app_back_url = None
mock_args.proxy_app_open_new_tab = None
mock_args.proxy_app_display_external_icon = None
mock_args.proxy_app_order = None

self.assertRaises(
SystemExit,
add_proxyapp,
mock_args,
)
mock_write_error.assert_called_with(
f"There is already a proxy app with that name: {self.app_name}"
f"Not possible to add the proxy app: {app_name_mock}"
)
mock_exit.assert_called_with(1)

@mock.patch("tethys_cli.proxyapps_commands.write_success")
def test_add_proxyapp_only_two_arguments(self, mock_write_success):
@mock.patch("tethys_cli.proxyapps_commands.exit", side_effect=SystemExit)
def test_add_proxyapp_success(self, mock_exit, mock_write_success):
romer8 marked this conversation as resolved.
Show resolved Hide resolved
app_name_mock = "My_Proxy_App_for_Testing_2"
endpoint_mock = "http://foo.example.com/my-proxy-app"
mock_args = [app_name_mock, endpoint_mock]
add_proxyapp(mock_args)
mock_args = mock.Mock()
mock_args.proxy_app_name = app_name_mock
mock_args.proxy_app_endpoint = "http://foo.example.com/my-proxy-app"
mock_args.proxy_app_description = ""
mock_args.proxy_app_logo_url = ""
mock_args.proxy_app_tags = ""
mock_args.proxy_app_enabled = True
mock_args.proxy_app_show_in_apps_library = True
mock_args.proxy_app_back_url = ""
mock_args.proxy_app_open_new_tab = True
mock_args.proxy_app_display_external_icon = False
mock_args.proxy_app_order = 0

self.assertRaises(
SystemExit,
add_proxyapp,
mock_args,
)
new_proxy_app = ProxyApp.objects.get(name=app_name_mock)
new_proxy_app.delete()
mock_write_success.assert_called_with(f"Proxy app {app_name_mock} added")
mock_exit.assert_called_with(0)
Loading
Loading