Skip to content

Commit

Permalink
Fix for Spatial Dataset Service created w/ CLI does not work until ma…
Browse files Browse the repository at this point in the history
…nually saved in admin pages #763
  • Loading branch information
swainn committed Feb 2, 2022
1 parent 6266f2c commit 5db986f
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 15 deletions.
113 changes: 103 additions & 10 deletions tests/unit_tests/test_tethys_cli/test_services_commands.py
Expand Up @@ -259,6 +259,7 @@ def test_services_create_spatial_command_IndexError(self, mock_service, mock_pre
"""
mock_args = mock.MagicMock()
mock_args.connection = 'IndexError:9876@IndexError' # No 'http' or '://'
mock_args.type = 'GeoServer'

services_create_spatial_command(mock_args)

Expand All @@ -282,6 +283,7 @@ def test_services_create_spatial_command_FormatError(self, mock_service, mock_pr
mock_args = mock.MagicMock()
mock_args.connection = 'foo:pass@http:://foo:1234'
mock_args.public_endpoint = 'foo@foo:foo' # No 'http' or '://'
mock_args.type = 'GeoServer'

services_create_spatial_command(mock_args)

Expand All @@ -305,6 +307,7 @@ def test_services_create_spatial_command_IntegrityError(self, mock_service, mock
mock_args = mock.MagicMock()
mock_args.connection = 'foo:pass@http:://foo:1234'
mock_args.public_endpoint = 'http://foo:1234'
mock_args.type = 'GeoServer'
mock_service.side_effect = IntegrityError

services_create_spatial_command(mock_args)
Expand All @@ -318,18 +321,56 @@ def test_services_create_spatial_command_IntegrityError(self, mock_service, mock

@mock.patch('tethys_cli.services_commands.pretty_output')
@mock.patch('tethys_services.models.SpatialDatasetService')
def test_services_create_spatial_command(self, mock_service, mock_pretty_output):
def test_services_create_spatial_command_geoserver(self, mock_service, mock_pretty_output):
"""
Test for services_create_spatial_command
For going through the function and saving
:param mock_service: mock for SpatialDatasetService
:param mock_pretty_output: mock for pretty_output text
:return:
"""
mock_args = mock.MagicMock()
mock_args.connection = 'foo:pass@http:://foo:1234'
mock_args.public_endpoint = 'http://foo:1234'
mock_service.return_value = mock.MagicMock()
mock_args = mock.MagicMock(
connection='foo:pass@http://localhost:8181/geoserver/rest/',
public_endpoint='https://www.example.com:443/geoserver/rest/',
apikey='apikey123',
type='GeoServer'
)
mock_args.name = 'test_geoserver'

services_create_spatial_command(mock_args)

mock_service.assert_called()

po_call_args = mock_pretty_output().__enter__().write.call_args_list
self.assertEqual(1, len(po_call_args))
self.assertEqual('Successfully created new Spatial Dataset Service!', po_call_args[0][0][0])
mock_service.assert_called_with(
name='test_geoserver',
endpoint='http://localhost:8181/geoserver/rest/',
public_endpoint='https://www.example.com:443/geoserver/rest/',
apikey='apikey123',
username='foo',
password='pass',
engine=mock_service.GEOSERVER
)

@mock.patch('tethys_cli.services_commands.pretty_output')
@mock.patch('tethys_services.models.SpatialDatasetService')
def test_services_create_spatial_command_thredds(self, mock_service, mock_pretty_output):
"""
Test for services_create_spatial_command
For going through the function and saving
:param mock_service: mock for SpatialDatasetService
:param mock_pretty_output: mock for pretty_output text
:return:
"""
mock_args = mock.MagicMock(
connection='foo:pass@http://localhost:8181/thredds/catalog.xml',
public_endpoint='https://www.example.com:443/thredds/catalog.xml',
apikey='apikey123',
type='THREDDS'
)
mock_args.name = 'test_thredds'

services_create_spatial_command(mock_args)

Expand All @@ -338,6 +379,15 @@ def test_services_create_spatial_command(self, mock_service, mock_pretty_output)
po_call_args = mock_pretty_output().__enter__().write.call_args_list
self.assertEqual(1, len(po_call_args))
self.assertEqual('Successfully created new Spatial Dataset Service!', po_call_args[0][0][0])
mock_service.assert_called_with(
name='test_thredds',
endpoint='http://localhost:8181/thredds/catalog.xml',
public_endpoint='https://www.example.com:443/thredds/catalog.xml',
apikey='apikey123',
username='foo',
password='pass',
engine=mock_service.THREDDS
)

@mock.patch('tethys_cli.services_commands.pretty_output')
@mock.patch('tethys_cli.services_commands.exit')
Expand Down Expand Up @@ -724,6 +774,7 @@ def test_services_create_dataset_command_IndexError(self, mock_service, mock_pre

mock_args = mock.MagicMock()
mock_args.connection = 'IndexError:9876@IndexError' # No 'http' or '://'
mock_args.type = 'HydroShare'

services_create_dataset_command(mock_args)

Expand All @@ -741,6 +792,7 @@ def test_services_create_dataset_command_FormatError(self, mock_service, mock_pr
mock_args = mock.MagicMock()
mock_args.connection = 'foo:pass@http:://foo:1234'
mock_args.public_endpoint = 'foo@foo:foo' # No 'http' or '://'
mock_args.type = 'HydroShare'

services_create_dataset_command(mock_args)

Expand All @@ -758,6 +810,7 @@ def test_services_create_dataset_command_IntegrityError(self, mock_service, mock
mock_args = mock.MagicMock()
mock_args.connection = 'foo:pass@http:://foo:1234'
mock_args.public_endpoint = 'http://foo:1234'
mock_args.type = 'HydroShare'
mock_service.side_effect = IntegrityError

services_create_dataset_command(mock_args)
Expand All @@ -771,11 +824,42 @@ def test_services_create_dataset_command_IntegrityError(self, mock_service, mock

@mock.patch('tethys_cli.services_commands.pretty_output')
@mock.patch('tethys_services.models.DatasetService')
def test_services_create_dataset_command(self, mock_service, mock_pretty_output):
mock_args = mock.MagicMock()
mock_args.connection = 'foo:pass@http:://foo:1234'
mock_args.public_endpoint = 'http://foo:1234'
mock_service.return_value = mock.MagicMock()
def test_services_create_dataset_command_hydroshare(self, mock_service, mock_pretty_output):
mock_args = mock.MagicMock(
connection='foo:pass@http://localhost:80',
public_endpoint='http://www.example.com:80',
apikey='apikey123',
type='HydroShare'
)
mock_args.name = 'test_hydroshare'

services_create_dataset_command(mock_args)

mock_service.assert_called()

po_call_args = mock_pretty_output().__enter__().write.call_args_list
self.assertEqual(1, len(po_call_args))
self.assertEqual('Successfully created new Dataset Service!', po_call_args[0][0][0])
mock_service.assert_called_with(
name='test_hydroshare',
endpoint='http://localhost:80',
public_endpoint='http://www.example.com:80',
apikey='apikey123',
username='foo',
password='pass',
engine=mock_service.HYDROSHARE
)

@mock.patch('tethys_cli.services_commands.pretty_output')
@mock.patch('tethys_services.models.DatasetService')
def test_services_create_dataset_command_ckan(self, mock_service, mock_pretty_output):
mock_args = mock.MagicMock(
connection='foo:pass@http://localhost:80',
public_endpoint='http://www.example.com:80',
apikey='apikey123',
type='CKAN'
)
mock_args.name = 'test_ckan'

services_create_dataset_command(mock_args)

Expand All @@ -784,6 +868,15 @@ def test_services_create_dataset_command(self, mock_service, mock_pretty_output)
po_call_args = mock_pretty_output().__enter__().write.call_args_list
self.assertEqual(1, len(po_call_args))
self.assertEqual('Successfully created new Dataset Service!', po_call_args[0][0][0])
mock_service.assert_called_with(
name='test_ckan',
endpoint='http://localhost:80',
public_endpoint='http://www.example.com:80',
apikey='apikey123',
username='foo',
password='pass',
engine=mock_service.CKAN
)

@mock.patch('tethys_cli.services_commands.input')
@mock.patch('tethys_cli.services_commands.pretty_output')
Expand Down
21 changes: 16 additions & 5 deletions tethys_cli/services_commands.py
Expand Up @@ -183,18 +183,24 @@ def services_create_spatial_command(args):
apikey = args.apikey or ''
service_type = args.type

engines = {
'GeoServer': SpatialDatasetService.GEOSERVER,
'THREDDS': SpatialDatasetService.THREDDS
}

if 'http' not in endpoint or '://' not in endpoint:
raise IndexError()
if public_endpoint and 'http' not in public_endpoint or '://' not in public_endpoint:
raise FormatError()

endpoint = add_geoserver_rest_to_endpoint(endpoint)
if public_endpoint:
public_endpoint = add_geoserver_rest_to_endpoint(public_endpoint)
if service_type == 'GeoServer':
endpoint = add_geoserver_rest_to_endpoint(endpoint)
if public_endpoint:
public_endpoint = add_geoserver_rest_to_endpoint(public_endpoint)

new_persistent_service = SpatialDatasetService(name=name, endpoint=endpoint, public_endpoint=public_endpoint,
apikey=apikey, username=service_username,
password=service_password, engine=service_type)
password=service_password, engine=engines[service_type])
new_persistent_service.save()

with pretty_output(FG_GREEN) as p:
Expand Down Expand Up @@ -230,6 +236,11 @@ def services_create_dataset_command(args):
apikey = args.apikey or ''
service_type = args.type

engines = {
'CKAN': DatasetService.CKAN,
'HydroShare': DatasetService.HYDROSHARE
}

if 'http' not in endpoint or '://' not in endpoint:
raise IndexError()

Expand All @@ -239,7 +250,7 @@ def services_create_dataset_command(args):

new_persistent_service = DatasetService(name=name, endpoint=endpoint, public_endpoint=public_endpoint,
apikey=apikey, username=service_username,
password=service_password, engine=service_type)
password=service_password, engine=engines[service_type])
new_persistent_service.save()

with pretty_output(FG_GREEN) as p:
Expand Down

0 comments on commit 5db986f

Please sign in to comment.