# Path: vitrageclient/tests/cli/base.py
# class CliTestCase(base.BaseTestCase):
# 
#     """Test case base class for all unit tests."""
# 
#     # original error method of argparse uses exit
#     # I just want to raise an exception and get the error message
#     # that exit outputs
#     @staticmethod
#     def _my_parser_error_func(message):
#         raise argparse.ArgumentTypeError(message)
# 
# Path: vitrageclient/v1/cli/event.py
# class EventPost(command.Command):
#     """Post an event to Vitrage"""
# 
#     @staticmethod
#     def iso8601(argument_value):
#         try:
#             if argument_value:
#                 iso8601.parse_date(argument_value)
#         except ParseError:
#             msg = "%s must be an iso8601 date" % argument_value
#             raise argparse.ArgumentTypeError(msg)
# 
#     def get_parser(self, prog_name):
#         parser = super(EventPost, self).get_parser(prog_name)
# 
#         parser.add_argument('--type',
#                             required=True,
#                             help='The type of the event')
# 
#         parser.add_argument('--time',
#                             default='',
#                             type=self.iso8601,
#                             help='The timestamp of the event in ISO 8601 '
#                                  'format: YYYY-MM-DDTHH:MM:SS.mmmmmm. '
#                                  'If not specified, the current time is used')
# 
#         parser.add_argument('--details',
#                             default='{}',
#                             help='A json string with the event details')
# 
#         return parser
# 
#     def take_action(self, parsed_args):
# 
#         if parsed_args.time:
#             event_time = parsed_args.time
#         else:
#             event_time = datetime.now().isoformat()
# 
#         event_type = parsed_args.type
#         details = parsed_args.details
# 
#         self.app.client.event.post(event_time=event_time,
#                                    event_type=event_type,
#                                    details=json.loads(details))


from argparse import ArgumentParser
from argparse import ArgumentTypeError
from unittest import mock
from datetime import datetime
from testtools import ExpectedException
from vitrageclient.tests.cli.base import CliTestCase
from vitrageclient.v1.cli.event import EventPost
        self.assertRaises(ArgumentTypeError, self.event_post.iso8601,
                          '2014/12/13 12:44:21')

    @mock.patch.object(ArgumentParser, "error")
    def test_parser_event_post_with_not_a_date_string(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,
                               'argument --time: -5 must be an iso8601 date'):
            parser.parse_args(args=['--type', 'bla',
                                    '--time', '-5',
                                    '--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'])

    @mock.patch('vitrageclient.v1.cli.event.datetime')
    def test_parser_event_post_without_time_uses_time_now(self, dt_mock):
        current_time = datetime.now()
        dt_mock.now.return_value = current_time
        iso_time = current_time.isoformat()
        parser = self.event_post.get_parser('vitrage event post')

# Path: vitrageclient/tests/cli/base.py
# class CliTestCase(base.BaseTestCase):
# 
#     """Test case base class for all unit tests."""
# 
#     # original error method of argparse uses exit
#     # I just want to raise an exception and get the error message
#     # that exit outputs
#     @staticmethod
#     def _my_parser_error_func(message):
#         raise argparse.ArgumentTypeError(message)
# 
# Path: vitrageclient/v1/cli/event.py
# class EventPost(command.Command):
#     """Post an event to Vitrage"""
# 
#     @staticmethod
#     def iso8601(argument_value):
#         try:
#             if argument_value:
#                 iso8601.parse_date(argument_value)
#         except ParseError:
#             msg = "%s must be an iso8601 date" % argument_value
#             raise argparse.ArgumentTypeError(msg)
# 
#     def get_parser(self, prog_name):
#         parser = super(EventPost, self).get_parser(prog_name)
# 
#         parser.add_argument('--type',
#                             required=True,
#                             help='The type of the event')
# 
#         parser.add_argument('--time',
#                             default='',
#                             type=self.iso8601,
#                             help='The timestamp of the event in ISO 8601 '
#                                  'format: YYYY-MM-DDTHH:MM:SS.mmmmmm. '
#                                  'If not specified, the current time is used')
# 
#         parser.add_argument('--details',
#                             default='{}',
#                             help='A json string with the event details')
# 
#         return parser
# 
#     def take_action(self, parsed_args):
# 
#         if parsed_args.time:
#             event_time = parsed_args.time
#         else:
#             event_time = datetime.now().isoformat()
# 
#         event_type = parsed_args.type
#         details = parsed_args.details
# 
#         self.app.client.event.post(event_time=event_time,
#                                    event_type=event_type,
#                                    details=json.loads(details))


# Path: vitrageclient/tests/cli/test_event_post.py
from argparse import ArgumentParser
from argparse import ArgumentTypeError
from unittest import mock
from datetime import datetime
from testtools import ExpectedException
from vitrageclient.tests.cli.base import CliTestCase
from vitrageclient.v1.cli.event import EventPost

        self.assertRaises(ArgumentTypeError, self.event_post.iso8601,
                          '2014/12/13 12:44:21')

    @mock.patch.object(ArgumentParser, "error")
    def test_parser_event_post_with_not_a_date_string(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,
                               'argument --time: -5 must be an iso8601 date'):
            parser.parse_args(args=['--type', 'bla',
                                    '--time', '-5',
                                    '--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'])

    @mock.patch('vitrageclient.v1.cli.event.datetime')
    def test_parser_event_post_without_time_uses_time_now(self, dt_mock):
        current_time = datetime.now()
        dt_mock.now.return_value = current_time
        iso_time = current_time.isoformat()
        parser = self.event_post.get_parser('vitrage event post')

        args = parser.parse_args(args=['--type', 'bla',