From e50d0f0dc2ae6577f34414d1291710eb0bd4b9ea Mon Sep 17 00:00:00 2001 From: Eyal Date: Mon, 3 Jul 2017 15:18:47 +0300 Subject: [PATCH] post event paramters fix * make the default value for time work * use the argparse option to make the type argument mandatory * add tests Change-Id: Id3186fa90300f34fe3530109f11bd655a39d8363 --- vitrageclient/tests/cli/test_event_post.py | 16 ++++++++++++++++ vitrageclient/v1/cli/event.py | 9 +++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/vitrageclient/tests/cli/test_event_post.py b/vitrageclient/tests/cli/test_event_post.py index a1f2824..1f6289a 100644 --- a/vitrageclient/tests/cli/test_event_post.py +++ b/vitrageclient/tests/cli/test_event_post.py @@ -51,3 +51,19 @@ class EventPostTest(CliTestCase): parser.parse_args(args=['--type', 'bla', '--time', '-5', '--details', 'blabla']) + + @mock.patch.object(ArgumentParser, "error") + def test_parser_event_post_without_time(self, mock_parser): + mock_parser.side_effect = self._my_parser_error_func + parser = self.event_post.get_parser('vitrage event post') + + parser.parse_args(args=['--type', 'bla', '--details', 'blabla']) + + @mock.patch.object(ArgumentParser, "error") + def test_parser_event_post_type_required(self, mock_parser): + mock_parser.side_effect = self._my_parser_error_func + parser = self.event_post.get_parser('vitrage event post') + + # noinspection PyCallByClass + with ExpectedException(ArgumentTypeError, r'.*--type'): + parser.parse_args(args=['--details', 'blabla']) diff --git a/vitrageclient/v1/cli/event.py b/vitrageclient/v1/cli/event.py index eb2c529..d1934bd 100644 --- a/vitrageclient/v1/cli/event.py +++ b/vitrageclient/v1/cli/event.py @@ -20,8 +20,6 @@ from datetime import datetime from iso8601 import iso8601 from iso8601 import ParseError -from vitrageclient.common import exc - # noinspection PyAbstractClass class EventPost(command.Command): @@ -30,7 +28,8 @@ class EventPost(command.Command): @staticmethod def iso8601(argument_value): try: - iso8601.parse_date(argument_value) + if argument_value: + iso8601.parse_date(argument_value) except ParseError: msg = "%s must be an iso8601 date" % argument_value raise argparse.ArgumentTypeError(msg) @@ -39,6 +38,7 @@ class EventPost(command.Command): parser = super(EventPost, self).get_parser(prog_name) parser.add_argument('--type', + required=True, help='The type of the event') parser.add_argument('--time', @@ -55,9 +55,6 @@ class EventPost(command.Command): return parser def take_action(self, parsed_args): - if not parsed_args.type: - raise exc.CommandException( - message='Missing event type, please add --type') if parsed_args.time: event_time = parsed_args.time