Skip to content

Commit

Permalink
Jkmarx/update management command argparse (#2607)
Browse files Browse the repository at this point in the history
* Update management command, argparse.

* Move for consistency.

* Add tests for `create_workflow_engine`

* Add tests for `create_galaxy_instance`
  • Loading branch information
jkmarx authored and scottx611x committed Feb 20, 2018
1 parent 24fa9f4 commit 6842262
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 28 deletions.
17 changes: 11 additions & 6 deletions refinery/core/management/commands/create_workflowengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,34 @@


class Command(BaseCommand):
args = "<Galaxy instance ID> <Group name>"
help = "Creates a %s workflow engine with the specified Galaxy instance " \
"and group." % Site.objects.get_current().name
"""
Name: handle
Description:
main program; run the command
"""
def add_arguments(self, parser):
parser.add_argument('galaxy_instance_id', type=int)
parser.add_argument('group_name')

def handle(self, *args, **options):
"""This function creates a workflow engine and assigns it to the
specified group
"""
try:
instance = Instance.objects.get(id=args[0])
except IndexError:
instance = Instance.objects.get(id=options["galaxy_instance_id"])
except KeyError:
raise CommandError("Please provide a Galaxy instance ID")
except Instance.DoesNotExist:
raise CommandError(
"Unable to retrieve Galaxy instance with id '%d'" % args[0])
"Unable to retrieve Galaxy instance with id '%s'" %
options["galaxy_instance_id"]
)
# get *manager* group for indicated group
try:
group_name = args[1]
except IndexError:
group_name = options["group_name"]
except KeyError:
raise CommandError("Please provide a group name")
try:
manager_group = ExtendedGroup.objects.get(
Expand Down
7 changes: 3 additions & 4 deletions refinery/core/management/commands/set_up_site_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Command(BaseCommand):
Description:
main program; run the command
"""
def add_arguments(self, parser):
parser.add_argument('refinery_instance_name')
parser.add_argument('refinery_base_url')

def handle(self, *args, **options):
try:
Expand All @@ -26,10 +29,6 @@ def handle(self, *args, **options):
else:
set_up_site_name(refinery_instance_name, refinery_base_url)

def add_arguments(self, parser):
parser.add_argument('refinery_instance_name')
parser.add_argument('refinery_base_url')


def set_up_site_name(refinery_instance_name, refinery_base_url):
"""Set up the site name
Expand Down
28 changes: 28 additions & 0 deletions refinery/core/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2267,3 +2267,31 @@ def test_import_annotations(self):
neo4j interactions """
with mock.patch.object(ImportAnnotationsCommand, "handle"):
call_command("import_annotations", "-c")

def test_create_workflow_engine(self):
galaxy_instance = GalaxyInstanceFactory()
call_command(
"create_workflowengine",
str(galaxy_instance.id),
ExtendedGroup.objects.public_group().name
)
self.assertIsNotNone(
WorkflowEngine.objects.get(instance=galaxy_instance)
)

def test_create_workflow_engine_bad_galaxy_instance(self):
with self.assertRaises(CommandError):
call_command(
"create_workflowengine",
str(123),
ExtendedGroup.objects.public_group().name
)

def test_create_workflow_engine_bad_group_name(self):
galaxy_instance = GalaxyInstanceFactory()
with self.assertRaises(CommandError):
call_command(
"create_workflowengine",
str(galaxy_instance.id),
"non-existent group name"
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@


class Command(BaseCommand):
args = "<base_url> <api_key>"
help = "Creates a new Galaxy instance."

def add_arguments(self, parser):
parser.add_argument(
'--file_name',
action='store'
)
parser.add_argument('base_url')
parser.add_argument('api_key')
parser.add_argument(
'--description',
action='store',
Expand All @@ -42,31 +39,40 @@ def add_arguments(self, parser):
"""
def handle(self, *args, **options):
try:
base_url = args[0]
base_url = options['base_url']
except IndexError:
raise CommandError("Please provide a base URL for Galaxy instance")
try:
api_key = args[1]
api_key = options['api_key']
except IndexError:
raise CommandError("Please provide an API key")
instance_count = Instance.objects.filter(
base_url__exact=base_url).count()
base_url__exact=base_url
).count()

if instance_count > 0:
self.stdout.write("Instance with URL '%s' already exists" %
base_url)
logger.error("Instance with URL '%s' already exists", base_url)
aready_exists_message = "Instance with URL '{}' already " \
"exists".format(base_url)
self.stdout.write(aready_exists_message)
logger.info(aready_exists_message)
return

instance = Instance.objects.create(base_url=base_url,
api_key=api_key,
data_url=options['data_url'],
api_url=options['api_url'],
description=options['description'])
if instance is not None:
self.stdout.write("Instance '%s -- %s' created" %
base_url, api_key)
logger.info("Instance '%s -- %s' created", base_url, api_key)
creation_message = "Instance '{} -- {}' created".format(
base_url,
api_key
)
self.stdout.write(creation_message)
logger.info(creation_message)
else:
self.stdout.write("Unable to create instance '%s -- %s'" %
base_url, api_key)
logger.error("Unable to create instance '%s -- %s'",
base_url, api_key)
error_message = "Unable to create instance '{} -- {}'".format(
base_url,
api_key
)
self.stdout.write(error_message)
logger.error(error_message)
20 changes: 20 additions & 0 deletions refinery/galaxy_connector/tests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import uuid

from django.core.management import call_command
from django.test import TestCase

from bioblend import galaxy
import mock

from factory_boy.django_model_factories import GalaxyInstanceFactory
from galaxy_connector.models import Instance


class GalaxyInstanceTests(TestCase):
Expand Down Expand Up @@ -100,3 +102,21 @@ def test_no_history_files_if_history_content_entry_isnt_a_file(self):
self.GALAXY_HISTORY_ID
)
self.assertEqual(len(history_file_list), 0)


class TestManagementCommands(TestCase):
def test_create_galaxy_instance(self):
fake_api_key = str(uuid.uuid4())
fake_galaxy_url = "http://www.example.com/galaxy",
call_command(
"create_galaxy_instance",
fake_galaxy_url,
fake_api_key
)

self.assertIsNotNone(
Instance.objects.get(
base_url=fake_galaxy_url,
api_key=fake_api_key
)
)

0 comments on commit 6842262

Please sign in to comment.