def test_wrong_dateformat(self):
     '''Tests database error wrong date_needed_by format'''
     data = widget_app.map_data_to_obj(WidgetOrder,
                                       type='Widget',
                                       color='red',
                                       quantity=1,
                                       date_needed_by='2019-02-19dfafa')
     with self.assertRaises(GenericDatabaseException):
         widget_app.create_widget_order(HANDLER, data)
 def test_create_widget_order(self):
     '''Tests creating an order record'''
     data = widget_app.map_data_to_obj(WidgetOrder,
                                       type='Widget',
                                       color='red',
                                       quantity=1,
                                       date_needed_by='2019-02-19')
     results = widget_app.create_widget_order(HANDLER, data)
     self.assertIsInstance(results[1], str)
     self.assertEqual(results[0], 0)
 def test_None_date(self):
     '''Tests order with `None` widget date_needed_by'''
     data = widget_app.map_data_to_obj(WidgetOrder,
                                       type='Widget',
                                       color='red',
                                       quantity=1,
                                       date_needed_by=None)
     results = widget_app.create_widget_order(HANDLER, data)
     self.assertEqual(results[1], '[ERROR] DATE NOT VALID')
     self.assertEqual(results[0], 4004)
 def test_wrong_widget_quantity(self):
     '''Tests order with wrong widget quantity'''
     data = widget_app.map_data_to_obj(WidgetOrder,
                                       type='Widget',
                                       color='red1',
                                       quantity=0,
                                       date_needed_by='2019-02-19')
     results = widget_app.create_widget_order(HANDLER, data)
     self.assertEqual(
         results[1], '[ERROR] QUANTITY NOT VALID (Must be greater than 0)')
     self.assertEqual(results[0], 4003)
 def test_None_widget_color(self):
     '''Tests order with `None` widget color'''
     data = widget_app.map_data_to_obj(WidgetOrder,
                                       type='Widget',
                                       color=None,
                                       quantity=1,
                                       date_needed_by='2019-02-19')
     results = widget_app.create_widget_order(HANDLER, data)
     self.assertEqual(
         results[1],
         '[ERROR] COLOR IS NOT VALID (Must be either red, blue or yellow)')
     self.assertEqual(results[0], 4002)
 def test_None_widget_type(self):
     '''Tests order with `None` widget type'''
     data = widget_app.map_data_to_obj(WidgetOrder,
                                       type=None,
                                       color='red',
                                       quantity=1,
                                       date_needed_by='2019-02-19')
     results = widget_app.create_widget_order(HANDLER, data)
     self.assertEqual(
         results[1],
         '[ERROR] TYPE IS NOT VALID (Must be either Widget, Widget Pro or Widget Xtreme)'
     )
     self.assertEqual(results[0], 4001)
Пример #7
0
def create_widget_order():
    """ Create Widget Order.
    GET:
        Summary: create widget order endpoint.
        Description: save a widget order.
        Parameters:
        -----------
            - name: type
              in: json
              description: type of widget
              type: string
              required: true

            - name: color
              in: json
              description: color of widget
              type: string
              required: true

            - name: quantity
              in: json
              description: quantity of widgets
              type: int
              required: true

            - name: date_needed_by
                in: json
                description: date order should be delivered by
                type: string
                required: true
        Responses:
        ----------
            200:
                description: order saved, order number to be returned.
            404:
                order not saved, data is wrong.
    """

    try:
        type = _get_param(request, 'type', None)
        color = _get_param(request, 'color', None)
        quantity = _get_param(request, 'quantity', None)
        date_needed_by = _get_param(request, 'date_needed_by', None)

        widget_order = widget_app.map_data_to_obj(
            WidgetOrder,
            type=type,
            color=color,
            quantity=quantity,
            date_needed_by=date_needed_by)
        results = widget_app.create_widget_order(HANDLER, widget_order)
        return widget_app.response(HANDLER, results, request.method,
                                   request.url)
    except GenericDatabaseException as err:
        LOGGER.exception(err)
        return widget_app.exception(HANDLER, GenericDatabaseException,
                                    request.method, 'widget')
    except Exception as err:
        LOGGER.exception(err)
        return widget_app.exception(HANDLER, GenericDatabaseException,
                                    request.method, 'widget')